Skip to content

Commit

Permalink
Merge pull request #40 from shoriwe/Logs-Features
Browse files Browse the repository at this point in the history
Logs features
  • Loading branch information
shoriwe authored Nov 23, 2020
2 parents 982aa6d + 616c925 commit 2710384
Show file tree
Hide file tree
Showing 29 changed files with 300 additions and 185 deletions.
1 change: 1 addition & 0 deletions internal/ProxiesSetup/ForwardSocks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func SetupForwardSocks5(
proxyProtocol := new(ForwardToSocks5.ForwardToSocks5)
proxyProtocol.TargetHost = *targetHost
proxyProtocol.TargetPort = *targetPort
proxyProtocol.SetLoggingMethod(log.Print)
proxyAuth := new(proxy.Auth)
if len(*username) > 0 && len(*password) > 0 {
proxyAuth.User = *username
Expand Down
2 changes: 2 additions & 0 deletions internal/ProxiesSetup/HTTP.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"github.com/shoriwe/FullProxy/internal/SetupControllers"
"github.com/shoriwe/FullProxy/pkg/Proxies/HTTP"
"gopkg.in/elazarl/goproxy.v1"
"log"
)

func SetupHTTP(host *string, port *string, slave *bool, tls *bool, username []byte, password []byte) {
proxy := new(HTTP.HTTP)
proxyController := goproxy.NewProxyHttpServer()
proxy.ProxyController = proxyController
proxy.SetLoggingMethod(log.Print)
if len(username) > 0 && len(password) > 0 {
proxy.SetAuthenticationMethod(BasicAuthentication(username, password))
}
Expand Down
2 changes: 2 additions & 0 deletions internal/ProxiesSetup/LocalForward.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package ProxiesSetup
import (
"github.com/shoriwe/FullProxy/internal/SetupControllers"
"github.com/shoriwe/FullProxy/pkg/Proxies/PortForward"
"log"
)

func SetupLocalForward(host *string, port *string, masterHost *string, masterPort *string) {
proxy := new(PortForward.LocalForward)
proxy.TargetHost = *host
proxy.TargetPort = *port
proxy.SetLoggingMethod(log.Print)
SetupControllers.GeneralSlave(masterHost, masterPort, proxy)
}
1 change: 1 addition & 0 deletions internal/ProxiesSetup/RemoteForward.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ func SetupRemoteForward(host *string, port *string, masterHost *string, masterPo
proxy.TLSConfiguration = tlsConfiguration
proxy.MasterHost = *masterHost
proxy.MasterPort = *masterPort
proxy.SetLoggingMethod(log.Print)
SetupControllers.RemotePortForwardSlave(masterHost, masterPort, host, port)
}
2 changes: 2 additions & 0 deletions internal/ProxiesSetup/Socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package ProxiesSetup
import (
"github.com/shoriwe/FullProxy/internal/SetupControllers"
"github.com/shoriwe/FullProxy/pkg/Proxies/SOCKS5"
"log"
)

func SetupSocks5(host *string, port *string, slave *bool, username []byte, password []byte) {
Expand All @@ -14,6 +15,7 @@ func SetupSocks5(host *string, port *string, slave *bool, username []byte, passw
proxy.WantedAuthMethod = SOCKS5.NoAuthRequired
proxy.SetAuthenticationMethod(NoAuthentication)
}
proxy.SetLoggingMethod(log.Print)
if *slave {
SetupControllers.GeneralSlave(host, port, proxy)
} else {
Expand Down
3 changes: 2 additions & 1 deletion internal/SetupControllers/Bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ func Bind(host *string, port *string, proxy ConnectionControllers.ProxyProtocol)
if bindError != nil {
log.Fatal("Could not bind to wanted address")
}
log.Print("Bind successfully")
log.Print("Bind successfully in: ", *host, ":", *port)
controller := new(ConnectionControllers.Bind)
controller.SetLoggingMethod(log.Print)
controller.Server = server
controller.ProxyProtocol = proxy
log.Fatal(controller.Serve())
Expand Down
1 change: 1 addition & 0 deletions internal/SetupControllers/GeneralSlave.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ func GeneralSlave(host *string, port *string, proxy ConnectionControllers.ProxyP
controller.MasterHost = *host
controller.MasterPort = *port
controller.ProxyProtocol = proxy
controller.SetLoggingMethod(log.Print)
log.Fatal(controller.Serve())
}
1 change: 1 addition & 0 deletions internal/SetupControllers/Master.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ func MasterGeneral(host *string, port *string) {
controller.MasterConnectionWriter = masterConnectionWriter
controller.TLSConfiguration = tlsConfiguration
controller.MasterHost = *host
controller.SetLoggingMethod(log.Print)
log.Fatal(controller.Serve())
}
1 change: 1 addition & 0 deletions internal/SetupControllers/RemotePortForwardSlave.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@ func RemotePortForwardSlave(
controller.MasterConnection = masterConnection
controller.MasterConnectionReader = masterConnectionReader
controller.MasterConnectionWriter = masterConnectionWriter
controller.SetLoggingMethod(log.Print)
log.Fatal(controller.Serve())
}
8 changes: 8 additions & 0 deletions pkg/ConnectionControllers/Bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
type Bind struct {
Server net.Listener
ProxyProtocol ProxyProtocol
LoggingMethod LoggingMethod
}

func (bind *Bind) SetLoggingMethod(loggingMethod LoggingMethod) error {
bind.LoggingMethod = loggingMethod
return nil
}

func (bind *Bind) Serve() error {
for {
clientConnection, connectionError := bind.Server.Accept()
LogData(bind.LoggingMethod, "Client connection received from: ", clientConnection.RemoteAddr().String())
if connectionError != nil {
LogData(bind.LoggingMethod, connectionError)
return connectionError
}
clientConnectionReader, clientConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(clientConnection)
Expand Down
18 changes: 14 additions & 4 deletions pkg/ConnectionControllers/Constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package ConnectionControllers

import (
"bufio"
"github.com/shoriwe/FullProxy/pkg/Proxies/Basic"
"github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy"
"github.com/shoriwe/FullProxy/pkg/Sockets"
"net"
)
Expand All @@ -14,13 +14,23 @@ var (
UnknownOperation = []byte{4}
)

type ConnectionController interface {
Serve() error
func LogData(loggingMethod LoggingMethod, arguments ...interface{}) {
if loggingMethod != nil {
loggingMethod(arguments...)
}
}

type AuthenticationMethod func(username []byte, password []byte) bool

type LoggingMethod func(args ...interface{})

type ConnectionController interface {
SetLoggingMethod(LoggingMethod)
Serve() error
}

type ProxyProtocol interface {
SetLoggingMethod(LoggingMethod) error
SetAuthenticationMethod(AuthenticationMethod) error
Handle(net.Conn, *bufio.Reader, *bufio.Writer) error
}
Expand All @@ -29,7 +39,7 @@ func StartGeneralProxying(clientConnection net.Conn, targetConnection net.Conn)
clientConnectionReader, clientConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(clientConnection)
targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection)
if targetConnectionReader != nil && targetConnectionWriter != nil {
portProxy := Basic.PortProxy{
portProxy := PortProxy.PortProxy{
TargetConnection: targetConnection,
TargetConnectionReader: targetConnectionReader,
TargetConnectionWriter: targetConnectionWriter,
Expand Down
12 changes: 12 additions & 0 deletions pkg/ConnectionControllers/Master/GeneralMaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ type General struct {
MasterHost string
TLSConfiguration *tls.Config
Server net.Listener
LoggingMethod ConnectionControllers.LoggingMethod
}

func (general *General) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error {
general.LoggingMethod = loggingMethod
return nil
}

func (general *General) Serve() error {
Expand All @@ -26,6 +32,7 @@ func (general *General) Serve() error {
finalError = connectionError
break
}
ConnectionControllers.LogData(general.LoggingMethod, "Client connection received from: ", clientConnection.RemoteAddr().String())
_, connectionError = Sockets.Send(general.MasterConnectionWriter, &ConnectionControllers.NewConnection)
if connectionError != nil {
finalError = connectionError
Expand All @@ -34,13 +41,18 @@ func (general *General) Serve() error {

targetConnection, connectionError := general.Server.Accept()
if connectionError != nil {
ConnectionControllers.LogData(general.LoggingMethod, connectionError)
continue
}
// Verify that the new connection is also from the slave
if strings.Split(targetConnection.RemoteAddr().String(), ":")[0] == general.MasterHost {
ConnectionControllers.LogData(general.LoggingMethod, "Target connection received from: ", targetConnection.RemoteAddr().String())
targetConnection = Sockets.UpgradeServerToTLS(targetConnection, general.TLSConfiguration)
go ConnectionControllers.StartGeneralProxying(clientConnection, targetConnection)
} else {
ConnectionControllers.LogData(general.LoggingMethod, "(Ignoring) Connection received from a non slave client: ", targetConnection.RemoteAddr().String())
}
}
ConnectionControllers.LogData(general.LoggingMethod, finalError)
return finalError
}
11 changes: 11 additions & 0 deletions pkg/ConnectionControllers/Master/RemotePortForwardMaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type RemotePortForward struct {
TLSConfiguration *tls.Config
RemoteHost string
RemotePort string
LoggingMethod ConnectionControllers.LoggingMethod
}

func (remotePortForward *RemotePortForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error {
remotePortForward.LoggingMethod = loggingMethod
return nil
}

func (remotePortForward *RemotePortForward) Serve() error {
Expand Down Expand Up @@ -55,11 +61,16 @@ func (remotePortForward *RemotePortForward) Serve() error {

if connectionError != nil {
// Do Something to notify the client that connection was not possible
ConnectionControllers.LogData(remotePortForward.LoggingMethod, connectionError)
continue
}
if strings.Split(clientConnection.RemoteAddr().String(), ":")[0] == remotePortForward.RemoteHost {
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "Client connection received from: ", clientConnection.RemoteAddr().String())
go ConnectionControllers.StartGeneralProxying(clientConnection, targetConnection)
} else {
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "(Ignoring) Connection received from a non slave client: ", clientConnection.RemoteAddr().String())
}
}
ConnectionControllers.LogData(remotePortForward.LoggingMethod, finalError)
return finalError
}
14 changes: 8 additions & 6 deletions pkg/ConnectionControllers/Slave/GeneralSlave.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"github.com/shoriwe/FullProxy/pkg/ConnectionControllers"
"github.com/shoriwe/FullProxy/pkg/Sockets"
"log"
"net"
"time"
)
Expand All @@ -18,15 +17,18 @@ type General struct {
MasterPort string
TLSConfiguration *tls.Config
ProxyProtocol ConnectionControllers.ProxyProtocol
LoggingMethod ConnectionControllers.LoggingMethod
}

func (general *General) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error {
general.LoggingMethod = loggingMethod
return nil
}

func (general *General) Serve() error {
var finalError error
for {
timeoutSetError := general.MasterConnection.SetReadDeadline(time.Now().Add(20 * time.Second))
if timeoutSetError != nil {
log.Fatal(timeoutSetError)
}
_ = general.MasterConnection.SetReadDeadline(time.Now().Add(20 * time.Second))
NumberOfReceivedBytes, buffer, connectionError := Sockets.Receive(general.MasterConnectionReader, 1024)
if connectionError != nil {
if parsedConnectionError, ok := connectionError.(net.Error); !(ok && parsedConnectionError.Timeout()) {
Expand All @@ -41,7 +43,7 @@ func (general *General) Serve() error {
continue
}
clientConnection, connectionError := Sockets.TLSConnect(&general.MasterHost, &general.MasterPort, general.TLSConfiguration)

ConnectionControllers.LogData(general.LoggingMethod, "Client connection received from: ", clientConnection.RemoteAddr().String())
if connectionError != nil {
finalError = connectionError
break
Expand Down
21 changes: 15 additions & 6 deletions pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package Slave
import (
"bufio"
"crypto/tls"
"errors"
"github.com/shoriwe/FullProxy/pkg/ConnectionControllers"
"github.com/shoriwe/FullProxy/pkg/Sockets"
"log"
"net"
"time"
)
Expand All @@ -18,6 +18,12 @@ type RemotePortForward struct {
MasterHost string
MasterPort string
TLSConfiguration *tls.Config
LoggingMethod ConnectionControllers.LoggingMethod
}

func (remotePortForward *RemotePortForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error {
remotePortForward.LoggingMethod = loggingMethod
return nil
}

func (remotePortForward *RemotePortForward) Serve() error {
Expand All @@ -28,6 +34,7 @@ func (remotePortForward *RemotePortForward) Serve() error {
finalError = connectionError
break
}
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "Client connection received from: ", clientConnection.RemoteAddr().String())
_, connectionError = Sockets.Send(remotePortForward.MasterConnectionWriter, &ConnectionControllers.NewConnection)
if connectionError != nil {
if parsedConnectionError, ok := connectionError.(net.Error); !(ok && parsedConnectionError.Timeout()) {
Expand All @@ -38,6 +45,7 @@ func (remotePortForward *RemotePortForward) Serve() error {
_ = remotePortForward.MasterConnection.SetReadDeadline(time.Now().Add(3 * time.Second))
numberOfBytesReceived, response, connectionError := Sockets.Receive(remotePortForward.MasterConnectionReader, 1)
if connectionError != nil {
ConnectionControllers.LogData(remotePortForward.LoggingMethod, connectionError)
continue
}
if numberOfBytesReceived != 1 {
Expand All @@ -53,16 +61,17 @@ func (remotePortForward *RemotePortForward) Serve() error {
go ConnectionControllers.StartGeneralProxying(clientConnection, targetConnection)
} else {
_ = clientConnection.Close()
log.Fatal("Connectivity issues with master server")
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "Connectivity issues with master server")
return connectionError
}
case ConnectionControllers.FailToConnectToTarget[0]:
_ = clientConnection.Close()
log.Print("Something goes wrong when master connected to target")
break
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "Something goes wrong when master connected to target")
return errors.New("Something goes wrong when master connected to target")
case ConnectionControllers.UnknownOperation[0]:
_ = clientConnection.Close()
log.Print("The master did not understood the message")
break
ConnectionControllers.LogData(remotePortForward.LoggingMethod, "The master did not understood the message")
return errors.New("The master did not understood the message")
}
}
return finalError
Expand Down
10 changes: 10 additions & 0 deletions pkg/Proxies/Constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package Proxies

import "github.com/shoriwe/FullProxy/pkg/ConnectionControllers"

func Authenticate(authenticationMethod ConnectionControllers.AuthenticationMethod, username []byte, password []byte) bool {
if authenticationMethod != nil {
return authenticationMethod(username, password)
}
return true
}
Loading

0 comments on commit 2710384

Please sign in to comment.