From 2ee530a17b142cce7255fd096c2f85a221051f20 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 02:40:45 -0500 Subject: [PATCH 01/10] Update Protocol.go --- pkg/Proxies/{Basic => PortProxy}/Protocol.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename pkg/Proxies/{Basic => PortProxy}/Protocol.go (99%) diff --git a/pkg/Proxies/Basic/Protocol.go b/pkg/Proxies/PortProxy/Protocol.go similarity index 99% rename from pkg/Proxies/Basic/Protocol.go rename to pkg/Proxies/PortProxy/Protocol.go index 0392b99..2de3e62 100644 --- a/pkg/Proxies/Basic/Protocol.go +++ b/pkg/Proxies/PortProxy/Protocol.go @@ -1,4 +1,4 @@ -package Basic +package PortProxy import ( "bufio" From 587fb91ed4ed711a34f31c8d4cad8345a016233a Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 02:41:09 -0500 Subject: [PATCH 02/10] Log features interfaces --- pkg/ConnectionControllers/Constants.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/pkg/ConnectionControllers/Constants.go b/pkg/ConnectionControllers/Constants.go index 4cca51e..b551ff4 100644 --- a/pkg/ConnectionControllers/Constants.go +++ b/pkg/ConnectionControllers/Constants.go @@ -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" ) @@ -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 } @@ -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, From 83419637008720b56fda3f16e496261648ab8209 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 02:41:23 -0500 Subject: [PATCH 03/10] Controllers log features --- pkg/ConnectionControllers/Bind.go | 8 +++++++ .../Master/GeneralMaster.go | 12 +++++++++++ .../Master/RemotePortForwardMaster.go | 11 ++++++++++ .../Slave/GeneralSlave.go | 14 +++++++------ .../Slave/RemotePortForwardSlave.go | 21 +++++++++++++------ 5 files changed, 54 insertions(+), 12 deletions(-) diff --git a/pkg/ConnectionControllers/Bind.go b/pkg/ConnectionControllers/Bind.go index 417ae0b..20c9f11 100644 --- a/pkg/ConnectionControllers/Bind.go +++ b/pkg/ConnectionControllers/Bind.go @@ -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) diff --git a/pkg/ConnectionControllers/Master/GeneralMaster.go b/pkg/ConnectionControllers/Master/GeneralMaster.go index af909df..7b1a254 100644 --- a/pkg/ConnectionControllers/Master/GeneralMaster.go +++ b/pkg/ConnectionControllers/Master/GeneralMaster.go @@ -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 { @@ -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 @@ -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 } diff --git a/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go b/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go index c843b73..0ebbd79 100644 --- a/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go +++ b/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go @@ -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 { @@ -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 } diff --git a/pkg/ConnectionControllers/Slave/GeneralSlave.go b/pkg/ConnectionControllers/Slave/GeneralSlave.go index 1e02cb3..fc3d5f4 100644 --- a/pkg/ConnectionControllers/Slave/GeneralSlave.go +++ b/pkg/ConnectionControllers/Slave/GeneralSlave.go @@ -5,7 +5,6 @@ import ( "crypto/tls" "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" "github.com/shoriwe/FullProxy/pkg/Sockets" - "log" "net" "time" ) @@ -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()) { @@ -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 diff --git a/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go b/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go index 0c5f85c..d7e198b 100644 --- a/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go +++ b/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go @@ -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" ) @@ -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 { @@ -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()) { @@ -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 { @@ -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 From 8b38b884e185f10403fb955b6133f23515413bcc Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 02:41:41 -0500 Subject: [PATCH 04/10] Refactoring --- pkg/Proxies/HTTP/Protocol.go | 2 +- pkg/Proxies/PortForward/Local.go | 4 ++-- pkg/Proxies/PortForward/Remote.go | 4 ++-- pkg/Proxies/SOCKS5/ConnectCommand.go | 4 ++-- pkg/Proxies/Translation/ForwardToSocks5/Protocol.go | 4 ++-- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/Proxies/HTTP/Protocol.go b/pkg/Proxies/HTTP/Protocol.go index 246cb1f..c7a1366 100644 --- a/pkg/Proxies/HTTP/Protocol.go +++ b/pkg/Proxies/HTTP/Protocol.go @@ -49,7 +49,7 @@ func (httpProtocol *HTTP) SetAuthenticationMethod(authenticationMethod Connectio proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) { authentication := strings.Split(request.Header.Get("Proxy-Authorization"), " ") if len(authentication) == 2 { - if authentication[0] == "Basic" { + if authentication[0] == "PortProxy" { rawCredentials, decodingError := base64.StdEncoding.DecodeString(authentication[1]) if decodingError == nil { credentials := bytes.Split(rawCredentials, []byte(":")) diff --git a/pkg/Proxies/PortForward/Local.go b/pkg/Proxies/PortForward/Local.go index 02d0d7b..bbe5e13 100644 --- a/pkg/Proxies/PortForward/Local.go +++ b/pkg/Proxies/PortForward/Local.go @@ -3,7 +3,7 @@ package PortForward import ( "bufio" "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" - "github.com/shoriwe/FullProxy/pkg/Proxies/Basic" + "github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy" "github.com/shoriwe/FullProxy/pkg/Sockets" "net" ) @@ -24,7 +24,7 @@ func (localForward *LocalForward) Handle( targetConnection, connectionError := Sockets.Connect(&localForward.TargetHost, &localForward.TargetPort) if connectionError == nil { targetReader, targetWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := Basic.PortProxy{ + portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, TargetConnectionReader: targetReader, TargetConnectionWriter: targetWriter, diff --git a/pkg/Proxies/PortForward/Remote.go b/pkg/Proxies/PortForward/Remote.go index c4f8693..1901538 100644 --- a/pkg/Proxies/PortForward/Remote.go +++ b/pkg/Proxies/PortForward/Remote.go @@ -4,7 +4,7 @@ import ( "bufio" "crypto/tls" "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" - "github.com/shoriwe/FullProxy/pkg/Proxies/Basic" + "github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy" "github.com/shoriwe/FullProxy/pkg/Sockets" "net" ) @@ -25,7 +25,7 @@ func (remoteForward *RemoteForward) Handle( (*remoteForward).TLSConfiguration) if connectionError == nil { targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := Basic.PortProxy{ + portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, TargetConnectionReader: targetConnectionReader, TargetConnectionWriter: targetConnectionWriter, diff --git a/pkg/Proxies/SOCKS5/ConnectCommand.go b/pkg/Proxies/SOCKS5/ConnectCommand.go index d1aea4b..3a0d11c 100644 --- a/pkg/Proxies/SOCKS5/ConnectCommand.go +++ b/pkg/Proxies/SOCKS5/ConnectCommand.go @@ -3,7 +3,7 @@ package SOCKS5 import ( "bufio" "encoding/binary" - "github.com/shoriwe/FullProxy/pkg/Proxies/Basic" + "github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy" "github.com/shoriwe/FullProxy/pkg/Sockets" "net" ) @@ -26,7 +26,7 @@ func (socks5 *Socks5) PrepareConnect( _, connectionError = Sockets.Send(clientConnectionWriter, &response) if connectionError == nil { targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := Basic.PortProxy{ + portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, TargetConnectionReader: targetConnectionReader, TargetConnectionWriter: targetConnectionWriter, diff --git a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go index d364216..fb07376 100644 --- a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go +++ b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go @@ -3,7 +3,7 @@ package ForwardToSocks5 import ( "bufio" "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" - "github.com/shoriwe/FullProxy/pkg/Proxies/Basic" + "github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy" "github.com/shoriwe/FullProxy/pkg/Sockets" "golang.org/x/net/proxy" "net" @@ -26,7 +26,7 @@ func (forwardToSocks5 *ForwardToSocks5) Handle( targetConnection, connectionError := forwardToSocks5.Socks5Dialer.Dial("tcp", forwardToSocks5.TargetHost+":"+forwardToSocks5.TargetPort) if connectionError == nil { targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := Basic.PortProxy{ + portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, TargetConnectionReader: targetConnectionReader, TargetConnectionWriter: targetConnectionWriter, From b4ec407b3d9d30cf1130dcaada1bb9ae7765f899 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:27:37 -0500 Subject: [PATCH 05/10] Better file names --- pkg/Proxies/SOCKS5/Authentication.go | 69 +++++++++++++++++++ .../SOCKS5/AuthenticationController.go | 50 -------------- ...CommandController.go => ExecuteCommand.go} | 2 +- 3 files changed, 70 insertions(+), 51 deletions(-) create mode 100644 pkg/Proxies/SOCKS5/Authentication.go delete mode 100644 pkg/Proxies/SOCKS5/AuthenticationController.go rename pkg/Proxies/SOCKS5/{CommandController.go => ExecuteCommand.go} (94%) diff --git a/pkg/Proxies/SOCKS5/Authentication.go b/pkg/Proxies/SOCKS5/Authentication.go new file mode 100644 index 0000000..f2bb06f --- /dev/null +++ b/pkg/Proxies/SOCKS5/Authentication.go @@ -0,0 +1,69 @@ +package SOCKS5 + +import ( + "bufio" + "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" + "github.com/shoriwe/FullProxy/pkg/Sockets" + "net" +) + +func (socks5 *Socks5) UsernamePasswordAuthentication(clientConnectionReader *bufio.Reader) (bool, byte) { + numberOfReceivedBytes, credentials, connectionError := Sockets.Receive(clientConnectionReader, 1024) + if connectionError != nil { + return false, 0 + } + if numberOfReceivedBytes < 4 { + return false, 0 + } + if credentials[0] != BasicNegotiation { + return false, 0 + } + receivedUsernameLength := int(credentials[1]) + if receivedUsernameLength+3 >= numberOfReceivedBytes { + return false, 0 + } + receivedUsername := credentials[2 : 2+receivedUsernameLength] + rawReceivedUsernamePassword := credentials[2+receivedUsernameLength+1 : numberOfReceivedBytes] + if socks5.AuthenticationMethod(receivedUsername, rawReceivedUsernamePassword) { + return true, UsernamePassword + } + return false, 0 +} + +func (socks5 *Socks5) AuthenticateClient(clientConnection net.Conn, clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer) bool { + + var foundMethod = InvalidMethod + numberOfReceivedBytes, clientImplementedMethods, _ := Sockets.Receive(clientConnectionReader, 1024) + if clientImplementedMethods == nil { + _, _ = Sockets.Send(clientConnectionWriter, &NoSupportedMethods) + return false + } else if numberOfReceivedBytes >= 3 { + if clientImplementedMethods[0] == Version && int(clientImplementedMethods[1]) == numberOfReceivedBytes-2 { + for index := 2; index < numberOfReceivedBytes; index++ { + if clientImplementedMethods[index] == socks5.WantedAuthMethod { + foundMethod = socks5.WantedAuthMethod + break + } + } + } + } + + switch foundMethod { + case UsernamePassword: + _, connectionError := Sockets.Send(clientConnectionWriter, &UsernamePasswordSupported) + if connectionError == nil { + if success, authenticationProtocol := socks5.UsernamePasswordAuthentication(clientConnectionReader); success && authenticationProtocol == UsernamePassword { + _, connectionError = Sockets.Send(clientConnectionWriter, &UsernamePasswordSucceededResponse) + return connectionError == nil + } + _, _ = Sockets.Send(clientConnectionWriter, &AuthenticationFailed) + } + case NoAuthRequired: + _, connectionError := Sockets.Send(clientConnectionWriter, &NoAuthRequiredSupported) + return connectionError == nil + default: + ConnectionControllers.LogData(socks5.LoggingMethod, "Client doesn't support authentication methods: ", clientConnection.RemoteAddr().String()) + _, _ = Sockets.Send(clientConnectionWriter, &NoSupportedMethods) + } + return false +} diff --git a/pkg/Proxies/SOCKS5/AuthenticationController.go b/pkg/Proxies/SOCKS5/AuthenticationController.go deleted file mode 100644 index 338e3cb..0000000 --- a/pkg/Proxies/SOCKS5/AuthenticationController.go +++ /dev/null @@ -1,50 +0,0 @@ -package SOCKS5 - -import ( - "bufio" - "github.com/shoriwe/FullProxy/pkg/Sockets" -) - -func (socks5 *Socks5) GetClientAuthenticationImplementedMethods(clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer) bool { - - var foundMethod = InvalidMethod - numberOfReceivedBytes, clientImplementedMethods, _ := Sockets.Receive(clientConnectionReader, 1024) - if clientImplementedMethods == nil { - _, _ = Sockets.Send(clientConnectionWriter, &NoSupportedMethods) - return false - } else if numberOfReceivedBytes >= 3 { - if clientImplementedMethods[0] == Version && int(clientImplementedMethods[1]) == numberOfReceivedBytes-2 { - for index := 2; index < numberOfReceivedBytes; index++ { - if clientImplementedMethods[index] == socks5.WantedAuthMethod { - foundMethod = socks5.WantedAuthMethod - break - } - } - } - } - - switch foundMethod { - case UsernamePassword: - _, connectionError := Sockets.Send(clientConnectionWriter, &UsernamePasswordSupported) - if connectionError == nil { - if success, authenticationProtocol := socks5.HandleUsernamePasswordAuthentication(clientConnectionReader); success && authenticationProtocol == UsernamePassword { - _, connectionError = Sockets.Send(clientConnectionWriter, &UsernamePasswordSucceededResponse) - authResult := connectionError == nil - /* - if connectionError == nil { - log.Print("Login failed with invalid credentials from: ", clientHost) - } - log.Print("Login succeeded from: ", clientHost) - */ - return authResult - } - _, _ = Sockets.Send(clientConnectionWriter, &AuthenticationFailed) - } - case NoAuthRequired: - _, connectionError := Sockets.Send(clientConnectionWriter, &NoAuthRequiredSupported) - return connectionError == nil - default: - _, _ = Sockets.Send(clientConnectionWriter, &NoSupportedMethods) - } - return false -} diff --git a/pkg/Proxies/SOCKS5/CommandController.go b/pkg/Proxies/SOCKS5/ExecuteCommand.go similarity index 94% rename from pkg/Proxies/SOCKS5/CommandController.go rename to pkg/Proxies/SOCKS5/ExecuteCommand.go index 91d3629..1154f9e 100644 --- a/pkg/Proxies/SOCKS5/CommandController.go +++ b/pkg/Proxies/SOCKS5/ExecuteCommand.go @@ -6,7 +6,7 @@ import ( "net" ) -func (socks5 *Socks5) HandleCommandExecution( +func (socks5 *Socks5) ExecuteCommand( clientConnection net.Conn, clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer, targetRequestedCommand *byte, targetHostType *byte, From 2eac7f8071c1215030081ba59883ff0c8bca6788 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:27:44 -0500 Subject: [PATCH 06/10] Delete UsernamePasswordAuthentication.go --- .../SOCKS5/UsernamePasswordAuthentication.go | 29 ------------------- 1 file changed, 29 deletions(-) delete mode 100644 pkg/Proxies/SOCKS5/UsernamePasswordAuthentication.go diff --git a/pkg/Proxies/SOCKS5/UsernamePasswordAuthentication.go b/pkg/Proxies/SOCKS5/UsernamePasswordAuthentication.go deleted file mode 100644 index 9f507c7..0000000 --- a/pkg/Proxies/SOCKS5/UsernamePasswordAuthentication.go +++ /dev/null @@ -1,29 +0,0 @@ -package SOCKS5 - -import ( - "bufio" - "github.com/shoriwe/FullProxy/pkg/Sockets" -) - -func (socks5 *Socks5) HandleUsernamePasswordAuthentication(clientConnectionReader *bufio.Reader) (bool, byte) { - numberOfReceivedBytes, credentials, connectionError := Sockets.Receive(clientConnectionReader, 1024) - if connectionError != nil { - return false, 0 - } - if numberOfReceivedBytes < 4 { - return false, 0 - } - if credentials[0] != BasicNegotiation { - return false, 0 - } - receivedUsernameLength := int(credentials[1]) - if receivedUsernameLength+3 >= numberOfReceivedBytes { - return false, 0 - } - receivedUsername := credentials[2 : 2+receivedUsernameLength] - rawReceivedUsernamePassword := credentials[2+receivedUsernameLength+1 : numberOfReceivedBytes] - if socks5.AuthenticationMethod(receivedUsername, rawReceivedUsernamePassword) { - return true, UsernamePassword - } - return false, 0 -} From 91bdbf6554266fbc6106acc62ae6087aaa5bb633 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:27:47 -0500 Subject: [PATCH 07/10] Create Constants.go --- pkg/Proxies/Constants.go | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 pkg/Proxies/Constants.go diff --git a/pkg/Proxies/Constants.go b/pkg/Proxies/Constants.go new file mode 100644 index 0000000..9e83aaf --- /dev/null +++ b/pkg/Proxies/Constants.go @@ -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 +} From 2c12ea73cc59cfb3c845f31ddc851318b5c19b82 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:27:57 -0500 Subject: [PATCH 08/10] Added support for the log function --- pkg/Proxies/HTTP/Protocol.go | 20 ++++-- pkg/Proxies/PortForward/Local.go | 10 ++- pkg/Proxies/PortForward/Remote.go | 10 ++- pkg/Proxies/SOCKS5/BindCommand.go | 4 +- pkg/Proxies/SOCKS5/ConnectCommand.go | 59 ++++++++-------- pkg/Proxies/SOCKS5/Protocol.go | 68 +++++++++++-------- pkg/Proxies/SOCKS5/UDPAssociateCommand.go | 4 +- .../Translation/ForwardToSocks5/Protocol.go | 26 ++++--- 8 files changed, 126 insertions(+), 75 deletions(-) diff --git a/pkg/Proxies/HTTP/Protocol.go b/pkg/Proxies/HTTP/Protocol.go index c7a1366..9567910 100644 --- a/pkg/Proxies/HTTP/Protocol.go +++ b/pkg/Proxies/HTTP/Protocol.go @@ -5,8 +5,8 @@ import ( "bytes" "encoding/base64" "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" + "github.com/shoriwe/FullProxy/pkg/Proxies" "gopkg.in/elazarl/goproxy.v1" - "log" "net" "net/http" "net/http/httptest" @@ -41,9 +41,18 @@ func CreateCustomResponseWriter( type HTTP struct { AuthenticationMethod ConnectionControllers.AuthenticationMethod ProxyController *goproxy.ProxyHttpServer + LoggingMethod ConnectionControllers.LoggingMethod +} + +func (httpProtocol *HTTP) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { + httpProtocol.LoggingMethod = loggingMethod + return nil } func (httpProtocol *HTTP) SetAuthenticationMethod(authenticationMethod ConnectionControllers.AuthenticationMethod) error { + if httpProtocol.ProxyController == nil { + panic("No HTTP proxy controller was set") + } httpProtocol.ProxyController.OnRequest().DoFunc(func( request *http.Request, proxyCtx *goproxy.ProxyCtx) (*http.Request, *http.Response) { @@ -54,14 +63,15 @@ func (httpProtocol *HTTP) SetAuthenticationMethod(authenticationMethod Connectio if decodingError == nil { credentials := bytes.Split(rawCredentials, []byte(":")) if len(credentials) == 2 { - if authenticationMethod(credentials[0], credentials[1]) { + if Proxies.Authenticate(authenticationMethod, credentials[0], credentials[1]) { + ConnectionControllers.LogData(httpProtocol.LoggingMethod, "Login successful with: ", request.RemoteAddr) return request, nil } } } } } - log.Print("Login failed with invalid credentials from: ", request.RemoteAddr) + ConnectionControllers.LogData(httpProtocol.LoggingMethod, "Login failed with invalid credentials from: ", request.RemoteAddr) return request, goproxy.NewResponse(request, goproxy.ContentTypeText, http.StatusProxyAuthRequired, "Don't waste your time!") }) return nil @@ -73,7 +83,9 @@ func (httpProtocol *HTTP) Handle( clientConnectionWriter *bufio.Writer) error { request, parsingError := http.ReadRequest(clientConnectionReader) - if parsingError == nil { + if parsingError != nil { + ConnectionControllers.LogData(httpProtocol.LoggingMethod, parsingError) + } else { request.RemoteAddr = clientConnection.RemoteAddr().String() responseWriter := CreateCustomResponseWriter(clientConnection, clientConnectionReader, clientConnectionWriter) httpProtocol.ProxyController.ServeHTTP(responseWriter, request) diff --git a/pkg/Proxies/PortForward/Local.go b/pkg/Proxies/PortForward/Local.go index bbe5e13..6057d1c 100644 --- a/pkg/Proxies/PortForward/Local.go +++ b/pkg/Proxies/PortForward/Local.go @@ -11,18 +11,26 @@ import ( type LocalForward struct { TargetHost string TargetPort string + LoggingMethod ConnectionControllers.LoggingMethod } func (localForward *LocalForward) SetAuthenticationMethod(_ ConnectionControllers.AuthenticationMethod) error { return nil } +func (localForward *LocalForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { + localForward.LoggingMethod = loggingMethod + return nil +} + func (localForward *LocalForward) Handle( clientConnection net.Conn, clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer) error { targetConnection, connectionError := Sockets.Connect(&localForward.TargetHost, &localForward.TargetPort) - if connectionError == nil { + if connectionError != nil { + ConnectionControllers.LogData(localForward.LoggingMethod, connectionError) + } else { targetReader, targetWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, diff --git a/pkg/Proxies/PortForward/Remote.go b/pkg/Proxies/PortForward/Remote.go index 1901538..03e9c87 100644 --- a/pkg/Proxies/PortForward/Remote.go +++ b/pkg/Proxies/PortForward/Remote.go @@ -13,6 +13,12 @@ type RemoteForward struct { MasterHost string MasterPort string TLSConfiguration *tls.Config + LoggingMethod ConnectionControllers.LoggingMethod +} + +func (remoteForward *RemoteForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { + remoteForward.LoggingMethod = loggingMethod + return nil } func (remoteForward *RemoteForward) Handle( @@ -23,7 +29,9 @@ func (remoteForward *RemoteForward) Handle( &remoteForward.MasterHost, &remoteForward.MasterPort, (*remoteForward).TLSConfiguration) - if connectionError == nil { + if connectionError != nil { + ConnectionControllers.LogData(remoteForward.LoggingMethod, connectionError) + } else { targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) portProxy := PortProxy.PortProxy{ TargetConnection: targetConnection, diff --git a/pkg/Proxies/SOCKS5/BindCommand.go b/pkg/Proxies/SOCKS5/BindCommand.go index 50f9d68..a54e548 100644 --- a/pkg/Proxies/SOCKS5/BindCommand.go +++ b/pkg/Proxies/SOCKS5/BindCommand.go @@ -3,6 +3,7 @@ package SOCKS5 import ( "bufio" "errors" + "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" "net" ) @@ -11,5 +12,6 @@ func (socks5 *Socks5) PrepareBind( clientConnectionWriter *bufio.Writer, targetHost *string, targetPort *string, targetHostType *byte) error { _ = clientConnection.Close() - return errors.New("method not implemented yet") + ConnectionControllers.LogData(socks5.LoggingMethod, "Bind method not implemented yet") + return errors.New("Bind method not implemented yet") } diff --git a/pkg/Proxies/SOCKS5/ConnectCommand.go b/pkg/Proxies/SOCKS5/ConnectCommand.go index 3a0d11c..2fb45c0 100644 --- a/pkg/Proxies/SOCKS5/ConnectCommand.go +++ b/pkg/Proxies/SOCKS5/ConnectCommand.go @@ -3,6 +3,7 @@ package SOCKS5 import ( "bufio" "encoding/binary" + "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" "github.com/shoriwe/FullProxy/pkg/Proxies/PortProxy" "github.com/shoriwe/FullProxy/pkg/Sockets" "net" @@ -16,33 +17,35 @@ func (socks5 *Socks5) PrepareConnect( targetHostType *byte) error { targetConnection, connectionError := Sockets.Connect(targetHost, targetPort) // new(big.Int).SetBytes(rawTargetPort).String()) - if connectionError == nil { - localHostPort := targetConnection.LocalAddr().(*net.TCPAddr) - localPort := make([]byte, 2) - binary.BigEndian.PutUint16(localPort, uint16(localHostPort.Port)) - response := []byte{Version, Succeeded, 0, *targetHostType} - response = append(response[:], localHostPort.IP[:]...) - response = append(response[:], localPort[:]...) - _, connectionError = Sockets.Send(clientConnectionWriter, &response) - if connectionError == nil { - targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := PortProxy.PortProxy{ - TargetConnection: targetConnection, - TargetConnectionReader: targetConnectionReader, - TargetConnectionWriter: targetConnectionWriter, - } - return portProxy.Handle( - clientConnection, - clientConnectionReader, - clientConnectionWriter) - } else { - _ = clientConnection.Close() - _ = targetConnection.Close() - return connectionError - } + if connectionError != nil { + ConnectionControllers.LogData(socks5.LoggingMethod, connectionError) + failResponse := []byte{Version, ConnectionRefused, 0, *targetHostType, 0, 0} + _, _ = Sockets.Send(clientConnectionWriter, &failResponse) + _ = clientConnection.Close() + return connectionError } - failResponse := []byte{Version, ConnectionRefused, 0, *targetHostType, 0, 0} - _, _ = Sockets.Send(clientConnectionWriter, &failResponse) - _ = clientConnection.Close() - return connectionError + localAddress := targetConnection.LocalAddr().(*net.TCPAddr) + localPort := make([]byte, 2) + binary.BigEndian.PutUint16(localPort, uint16(localAddress.Port)) + response := []byte{Version, Succeeded, 0, *targetHostType} + response = append(response[:], localAddress.IP[:]...) + response = append(response[:], localPort[:]...) + _, connectionError = Sockets.Send(clientConnectionWriter, &response) + if connectionError != nil { + _ = clientConnection.Close() + _ = targetConnection.Close() + ConnectionControllers.LogData(socks5.LoggingMethod, connectionError) + return connectionError + } + ConnectionControllers.LogData(socks5.LoggingMethod, "Client: ", clientConnection.RemoteAddr().String(), " -> Target: ", targetConnection.RemoteAddr().String()) + targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) + portProxy := PortProxy.PortProxy{ + TargetConnection: targetConnection, + TargetConnectionReader: targetConnectionReader, + TargetConnectionWriter: targetConnectionWriter, + } + return portProxy.Handle( + clientConnection, + clientConnectionReader, + clientConnectionWriter) } diff --git a/pkg/Proxies/SOCKS5/Protocol.go b/pkg/Proxies/SOCKS5/Protocol.go index cd7ae5e..a69a3fb 100644 --- a/pkg/Proxies/SOCKS5/Protocol.go +++ b/pkg/Proxies/SOCKS5/Protocol.go @@ -12,20 +12,24 @@ import ( type Socks5 struct { AuthenticationMethod ConnectionControllers.AuthenticationMethod WantedAuthMethod byte + LoggingMethod ConnectionControllers.LoggingMethod } func ReceiveTargetRequest(clientConnectionReader *bufio.Reader) (byte, byte, []byte, []byte) { numberOfBytesReceived, targetRequest, ConnectionError := Sockets.Receive(clientConnectionReader, 1024) - if ConnectionError == nil { - if targetRequest[0] == Version { - if targetRequest[1] == Connect || targetRequest[1] == Bind || targetRequest[1] == UDPAssociate { - if targetRequest[3] == IPv4 || targetRequest[3] == IPv6 || targetRequest[3] == DomainName { - return targetRequest[1], targetRequest[3], targetRequest[4 : numberOfBytesReceived-2], targetRequest[numberOfBytesReceived-2 : numberOfBytesReceived] - } - } - } + if ConnectionError != nil { + return 0, 0, nil, nil + } + if targetRequest[0] != Version { + return 0, 0, nil, nil + } + if !(targetRequest[1] == Connect || targetRequest[1] == Bind || targetRequest[1] == UDPAssociate) { + return 0, 0, nil, nil } - return 0, 0, nil, nil + if !(targetRequest[3] == IPv4 || targetRequest[3] == IPv6 || targetRequest[3] == DomainName) { + return 0, 0, nil, nil + } + return targetRequest[1], targetRequest[3], targetRequest[4 : numberOfBytesReceived-2], targetRequest[numberOfBytesReceived-2 : numberOfBytesReceived] } func GetTargetHostPort(targetRequestedCommand *byte, targetHostType *byte, rawTargetHost []byte, rawTargetPort []byte) (byte, string, string) { @@ -42,6 +46,11 @@ func GetTargetHostPort(targetRequestedCommand *byte, targetHostType *byte, rawTa return ConnectionRefused, "", "" } +func (socks5 *Socks5) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { + socks5.LoggingMethod = loggingMethod + return nil +} + func (socks5 *Socks5) SetAuthenticationMethod(authenticationMethod ConnectionControllers.AuthenticationMethod) error { socks5.AuthenticationMethod = authenticationMethod return nil @@ -53,28 +62,29 @@ func (socks5 *Socks5) Handle( clientConnectionWriter *bufio.Writer) error { var targetRequestedCommand byte - + var finalError string // Receive connection - clientHasCompatibleAuthMethods := socks5.GetClientAuthenticationImplementedMethods(clientConnectionReader, clientConnectionWriter) - if clientHasCompatibleAuthMethods { - var targetHost string - var targetPort string - rawTargetRequestedCommand, targetHostType, rawTargetHost, rawTargetPort := ReceiveTargetRequest( - clientConnectionReader) - targetRequestedCommand, targetHost, targetPort = GetTargetHostPort( - &rawTargetRequestedCommand, &targetHostType, - rawTargetHost, rawTargetPort) - if targetRequestedCommand != ConnectionRefused { - return socks5.HandleCommandExecution(clientConnection, clientConnectionReader, clientConnectionWriter, - &targetRequestedCommand, &targetHostType, &targetHost, &targetPort) - } + authenticationSuccessful := socks5.AuthenticateClient(clientConnection, clientConnectionReader, clientConnectionWriter) + if !authenticationSuccessful { + finalError = "Authentication Failed with: " + clientConnection.RemoteAddr().String() + _ = clientConnection.Close() + ConnectionControllers.LogData(socks5.LoggingMethod, finalError) + return errors.New(finalError) } - var finalError string - if !clientHasCompatibleAuthMethods { - finalError = "No compatible auth methods found" + ConnectionControllers.LogData(socks5.LoggingMethod, "Login succeeded from: ", clientConnection.RemoteAddr().String()) + var targetHost string + var targetPort string + rawTargetRequestedCommand, targetHostType, rawTargetHost, rawTargetPort := ReceiveTargetRequest( + clientConnectionReader) + targetRequestedCommand, targetHost, targetPort = GetTargetHostPort( + &rawTargetRequestedCommand, &targetHostType, + rawTargetHost, rawTargetPort) + if targetRequestedCommand == ConnectionRefused { + finalError = "Target connection refused: " + targetHost + ":" + targetPort _ = clientConnection.Close() - } else if targetRequestedCommand == ConnectionRefused { - finalError = "connection refused to target host" + ConnectionControllers.LogData(socks5.LoggingMethod, finalError) + return errors.New(finalError) } - return errors.New(finalError) + return socks5.ExecuteCommand(clientConnection, clientConnectionReader, clientConnectionWriter, + &targetRequestedCommand, &targetHostType, &targetHost, &targetPort) } diff --git a/pkg/Proxies/SOCKS5/UDPAssociateCommand.go b/pkg/Proxies/SOCKS5/UDPAssociateCommand.go index 610ed91..57dbde1 100644 --- a/pkg/Proxies/SOCKS5/UDPAssociateCommand.go +++ b/pkg/Proxies/SOCKS5/UDPAssociateCommand.go @@ -3,11 +3,13 @@ package SOCKS5 import ( "bufio" "errors" + "github.com/shoriwe/FullProxy/pkg/ConnectionControllers" "net" ) func (socks5 *Socks5) PrepareUDPAssociate(clientConnection net.Conn, clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer, targetHost *string, targetPort *string, targetHostType *byte) error { _ = clientConnection.Close() - return errors.New("method not implemented yet") + ConnectionControllers.LogData(socks5.LoggingMethod, "UDP-Associate method not implemented yet") + return errors.New("UDP-Associate method not implemented yet") } diff --git a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go index fb07376..93810c9 100644 --- a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go +++ b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go @@ -13,8 +13,13 @@ type ForwardToSocks5 struct { TargetHost string TargetPort string Socks5Dialer proxy.Dialer + LoggingMethod ConnectionControllers.LoggingMethod } +func (forwardToSocks5 *ForwardToSocks5)SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { + forwardToSocks5.LoggingMethod = loggingMethod + return nil +} func (forwardToSocks5 *ForwardToSocks5) SetAuthenticationMethod(authenticationMethod ConnectionControllers.AuthenticationMethod) error { return nil } @@ -24,15 +29,16 @@ func (forwardToSocks5 *ForwardToSocks5) Handle( clientConnectionReader *bufio.Reader, clientConnectionWriter *bufio.Writer) error { targetConnection, connectionError := forwardToSocks5.Socks5Dialer.Dial("tcp", forwardToSocks5.TargetHost+":"+forwardToSocks5.TargetPort) - if connectionError == nil { - targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) - portProxy := PortProxy.PortProxy{ - TargetConnection: targetConnection, - TargetConnectionReader: targetConnectionReader, - TargetConnectionWriter: targetConnectionWriter, - } - return portProxy.Handle(clientConnection, clientConnectionReader, clientConnectionWriter) + if connectionError != nil { + ConnectionControllers.LogData(forwardToSocks5.LoggingMethod, connectionError) + _ = clientConnection.Close() + return connectionError + } + targetConnectionReader, targetConnectionWriter := Sockets.CreateSocketConnectionReaderWriter(targetConnection) + portProxy := PortProxy.PortProxy{ + TargetConnection: targetConnection, + TargetConnectionReader: targetConnectionReader, + TargetConnectionWriter: targetConnectionWriter, } - _ = clientConnection.Close() - return connectionError + return portProxy.Handle(clientConnection, clientConnectionReader, clientConnectionWriter) } From 6406839b7a85eea302c461bbf373a8d33210f5df Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:28:29 -0500 Subject: [PATCH 09/10] Code reformat --- pkg/ConnectionControllers/Constants.go | 4 ++-- pkg/ConnectionControllers/Master/GeneralMaster.go | 2 +- .../Master/RemotePortForwardMaster.go | 4 ++-- pkg/ConnectionControllers/Slave/GeneralSlave.go | 4 ++-- pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go | 2 +- pkg/Proxies/HTTP/Protocol.go | 2 +- pkg/Proxies/PortForward/Local.go | 4 ++-- pkg/Proxies/PortForward/Remote.go | 2 +- pkg/Proxies/SOCKS5/Protocol.go | 4 ++-- pkg/Proxies/Translation/ForwardToSocks5/Protocol.go | 8 ++++---- 10 files changed, 18 insertions(+), 18 deletions(-) diff --git a/pkg/ConnectionControllers/Constants.go b/pkg/ConnectionControllers/Constants.go index b551ff4..5a5744b 100644 --- a/pkg/ConnectionControllers/Constants.go +++ b/pkg/ConnectionControllers/Constants.go @@ -14,7 +14,7 @@ var ( UnknownOperation = []byte{4} ) -func LogData(loggingMethod LoggingMethod, arguments...interface{}) { +func LogData(loggingMethod LoggingMethod, arguments ...interface{}) { if loggingMethod != nil { loggingMethod(arguments...) } @@ -22,7 +22,7 @@ func LogData(loggingMethod LoggingMethod, arguments...interface{}) { type AuthenticationMethod func(username []byte, password []byte) bool -type LoggingMethod func(args...interface{}) +type LoggingMethod func(args ...interface{}) type ConnectionController interface { SetLoggingMethod(LoggingMethod) diff --git a/pkg/ConnectionControllers/Master/GeneralMaster.go b/pkg/ConnectionControllers/Master/GeneralMaster.go index 7b1a254..d352aae 100644 --- a/pkg/ConnectionControllers/Master/GeneralMaster.go +++ b/pkg/ConnectionControllers/Master/GeneralMaster.go @@ -16,7 +16,7 @@ type General struct { MasterHost string TLSConfiguration *tls.Config Server net.Listener - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } func (general *General) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { diff --git a/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go b/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go index 0ebbd79..4dad8f0 100644 --- a/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go +++ b/pkg/ConnectionControllers/Master/RemotePortForwardMaster.go @@ -18,10 +18,10 @@ type RemotePortForward struct { TLSConfiguration *tls.Config RemoteHost string RemotePort string - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } -func (remotePortForward *RemotePortForward)SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { +func (remotePortForward *RemotePortForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { remotePortForward.LoggingMethod = loggingMethod return nil } diff --git a/pkg/ConnectionControllers/Slave/GeneralSlave.go b/pkg/ConnectionControllers/Slave/GeneralSlave.go index fc3d5f4..1a56ee3 100644 --- a/pkg/ConnectionControllers/Slave/GeneralSlave.go +++ b/pkg/ConnectionControllers/Slave/GeneralSlave.go @@ -17,10 +17,10 @@ type General struct { MasterPort string TLSConfiguration *tls.Config ProxyProtocol ConnectionControllers.ProxyProtocol - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } -func (general *General)SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { +func (general *General) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { general.LoggingMethod = loggingMethod return nil } diff --git a/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go b/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go index d7e198b..022f9e4 100644 --- a/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go +++ b/pkg/ConnectionControllers/Slave/RemotePortForwardSlave.go @@ -18,7 +18,7 @@ type RemotePortForward struct { MasterHost string MasterPort string TLSConfiguration *tls.Config - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } func (remotePortForward *RemotePortForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { diff --git a/pkg/Proxies/HTTP/Protocol.go b/pkg/Proxies/HTTP/Protocol.go index 9567910..4821921 100644 --- a/pkg/Proxies/HTTP/Protocol.go +++ b/pkg/Proxies/HTTP/Protocol.go @@ -41,7 +41,7 @@ func CreateCustomResponseWriter( type HTTP struct { AuthenticationMethod ConnectionControllers.AuthenticationMethod ProxyController *goproxy.ProxyHttpServer - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } func (httpProtocol *HTTP) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { diff --git a/pkg/Proxies/PortForward/Local.go b/pkg/Proxies/PortForward/Local.go index 6057d1c..3c53c2a 100644 --- a/pkg/Proxies/PortForward/Local.go +++ b/pkg/Proxies/PortForward/Local.go @@ -9,8 +9,8 @@ import ( ) type LocalForward struct { - TargetHost string - TargetPort string + TargetHost string + TargetPort string LoggingMethod ConnectionControllers.LoggingMethod } diff --git a/pkg/Proxies/PortForward/Remote.go b/pkg/Proxies/PortForward/Remote.go index 03e9c87..e107f7b 100644 --- a/pkg/Proxies/PortForward/Remote.go +++ b/pkg/Proxies/PortForward/Remote.go @@ -13,7 +13,7 @@ type RemoteForward struct { MasterHost string MasterPort string TLSConfiguration *tls.Config - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } func (remoteForward *RemoteForward) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { diff --git a/pkg/Proxies/SOCKS5/Protocol.go b/pkg/Proxies/SOCKS5/Protocol.go index a69a3fb..3862f0a 100644 --- a/pkg/Proxies/SOCKS5/Protocol.go +++ b/pkg/Proxies/SOCKS5/Protocol.go @@ -12,7 +12,7 @@ import ( type Socks5 struct { AuthenticationMethod ConnectionControllers.AuthenticationMethod WantedAuthMethod byte - LoggingMethod ConnectionControllers.LoggingMethod + LoggingMethod ConnectionControllers.LoggingMethod } func ReceiveTargetRequest(clientConnectionReader *bufio.Reader) (byte, byte, []byte, []byte) { @@ -86,5 +86,5 @@ func (socks5 *Socks5) Handle( return errors.New(finalError) } return socks5.ExecuteCommand(clientConnection, clientConnectionReader, clientConnectionWriter, - &targetRequestedCommand, &targetHostType, &targetHost, &targetPort) + &targetRequestedCommand, &targetHostType, &targetHost, &targetPort) } diff --git a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go index 93810c9..6406968 100644 --- a/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go +++ b/pkg/Proxies/Translation/ForwardToSocks5/Protocol.go @@ -10,13 +10,13 @@ import ( ) type ForwardToSocks5 struct { - TargetHost string - TargetPort string - Socks5Dialer proxy.Dialer + TargetHost string + TargetPort string + Socks5Dialer proxy.Dialer LoggingMethod ConnectionControllers.LoggingMethod } -func (forwardToSocks5 *ForwardToSocks5)SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { +func (forwardToSocks5 *ForwardToSocks5) SetLoggingMethod(loggingMethod ConnectionControllers.LoggingMethod) error { forwardToSocks5.LoggingMethod = loggingMethod return nil } From 616c925bf7d73e0c67d74b7a6c7777c45d57f7c3 Mon Sep 17 00:00:00 2001 From: Antonio Jose Donis Hung Date: Mon, 23 Nov 2020 03:34:51 -0500 Subject: [PATCH 10/10] Final touchs for the support of logs --- internal/ProxiesSetup/ForwardSocks5.go | 1 + internal/ProxiesSetup/HTTP.go | 2 ++ internal/ProxiesSetup/LocalForward.go | 2 ++ internal/ProxiesSetup/RemoteForward.go | 1 + internal/ProxiesSetup/Socks5.go | 2 ++ internal/SetupControllers/Bind.go | 3 ++- internal/SetupControllers/GeneralSlave.go | 1 + internal/SetupControllers/Master.go | 1 + internal/SetupControllers/RemotePortForwardSlave.go | 1 + 9 files changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/ProxiesSetup/ForwardSocks5.go b/internal/ProxiesSetup/ForwardSocks5.go index a8e3379..69fc19f 100644 --- a/internal/ProxiesSetup/ForwardSocks5.go +++ b/internal/ProxiesSetup/ForwardSocks5.go @@ -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 diff --git a/internal/ProxiesSetup/HTTP.go b/internal/ProxiesSetup/HTTP.go index e3eb2e6..1c2e8c3 100644 --- a/internal/ProxiesSetup/HTTP.go +++ b/internal/ProxiesSetup/HTTP.go @@ -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)) } diff --git a/internal/ProxiesSetup/LocalForward.go b/internal/ProxiesSetup/LocalForward.go index 96ae209..2cea490 100644 --- a/internal/ProxiesSetup/LocalForward.go +++ b/internal/ProxiesSetup/LocalForward.go @@ -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) } diff --git a/internal/ProxiesSetup/RemoteForward.go b/internal/ProxiesSetup/RemoteForward.go index 633d54c..0736593 100644 --- a/internal/ProxiesSetup/RemoteForward.go +++ b/internal/ProxiesSetup/RemoteForward.go @@ -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) } diff --git a/internal/ProxiesSetup/Socks5.go b/internal/ProxiesSetup/Socks5.go index fef436a..774fbbc 100644 --- a/internal/ProxiesSetup/Socks5.go +++ b/internal/ProxiesSetup/Socks5.go @@ -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) { @@ -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 { diff --git a/internal/SetupControllers/Bind.go b/internal/SetupControllers/Bind.go index dd5836b..3cb85a6 100644 --- a/internal/SetupControllers/Bind.go +++ b/internal/SetupControllers/Bind.go @@ -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()) diff --git a/internal/SetupControllers/GeneralSlave.go b/internal/SetupControllers/GeneralSlave.go index 0d1068c..3e07d88 100644 --- a/internal/SetupControllers/GeneralSlave.go +++ b/internal/SetupControllers/GeneralSlave.go @@ -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()) } diff --git a/internal/SetupControllers/Master.go b/internal/SetupControllers/Master.go index 2edc7d4..f7fa0e1 100644 --- a/internal/SetupControllers/Master.go +++ b/internal/SetupControllers/Master.go @@ -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()) } diff --git a/internal/SetupControllers/RemotePortForwardSlave.go b/internal/SetupControllers/RemotePortForwardSlave.go index c7467fe..226d5f5 100644 --- a/internal/SetupControllers/RemotePortForwardSlave.go +++ b/internal/SetupControllers/RemotePortForwardSlave.go @@ -31,5 +31,6 @@ func RemotePortForwardSlave( controller.MasterConnection = masterConnection controller.MasterConnectionReader = masterConnectionReader controller.MasterConnectionWriter = masterConnectionWriter + controller.SetLoggingMethod(log.Print) log.Fatal(controller.Serve()) }