From bd929095f1b4d87388b6bcf76323530d6dee1da7 Mon Sep 17 00:00:00 2001 From: Mehdi Eidi Date: Sun, 10 Sep 2023 23:41:54 +0330 Subject: [PATCH] refactor: refactor client and server --- client/main.go | 19 +++---------------- server/main.go | 14 +++++++------- server/tcp.go | 28 ++++++++++++++++++++++++++++ server/tls.go | 31 ------------------------------- server/utls.go | 28 ++++++++++++++++++++++++++++ server/ws.go | 4 ++-- 6 files changed, 68 insertions(+), 56 deletions(-) create mode 100644 server/tcp.go delete mode 100644 server/tls.go create mode 100644 server/utls.go diff --git a/client/main.go b/client/main.go index b7093c9..0b3d0e2 100644 --- a/client/main.go +++ b/client/main.go @@ -10,7 +10,7 @@ import ( "os/signal" "syscall" - utls "github.com/refraction-networking/utls" + tls "github.com/refraction-networking/utls" "nhooyr.io/websocket" "tunelo/pkg/logger/plain" @@ -74,27 +74,14 @@ func main() { switch protocol { case "utls": - // certPEM, err := os.ReadFile("cert.pem") - // if err != nil { - // log.Error(fmt.Errorf("reading cert file: %v", err), nil) - // os.Exit(1) - // } - - // rootCAs := x509.NewCertPool() - // if ok := rootCAs.AppendCertsFromPEM(certPEM); !ok { - // log.Error(fmt.Errorf("appending cert to root CAs: %v", err), nil) - // os.Exit(1) - // } - if serverDomain == "" { log.Error(fmt.Errorf("server domain cannot be empty"), nil) os.Exit(1) } - tlsConfig := &utls.Config{ + tlsConfig := &tls.Config{ ServerName: serverDomain, InsecureSkipVerify: true, - // RootCAs: rootCAs, } tcpConn, err := net.Dial("tcp", serverAddr) @@ -104,7 +91,7 @@ func main() { } defer tcpConn.Close() - tlsConn := utls.UClient(tcpConn, tlsConfig, utls.HelloChrome_102) + tlsConn := tls.UClient(tcpConn, tlsConfig, tls.HelloChrome_102) if err := tlsConn.Handshake(); err != nil { log.Error(fmt.Errorf("tls handshake: %v", err), nil) os.Exit(1) diff --git a/server/main.go b/server/main.go index 1a83905..3dfdfcd 100644 --- a/server/main.go +++ b/server/main.go @@ -4,7 +4,6 @@ import ( "crypto/tls" "flag" "fmt" - "io" "net" "net/http" "os" @@ -69,7 +68,7 @@ func main() { log.Info(fmt.Sprintf("TLS server listening on %s", serverAddr), nil) - tls := TLS{log: log, vpnConn: vpnConn} + utlsTransport := utlsTransport{log: log, vpnConn: vpnConn} for { conn, err := tlsListener.Accept() @@ -80,7 +79,7 @@ func main() { log.Info("tls connection accepted. Proxy started...", nil) - go tls.handle(conn) + go utlsTransport.handle(conn) } case "tcp": tcpListener, err := net.Listen("tcp", serverAddr) @@ -92,6 +91,8 @@ func main() { log.Info(fmt.Sprintf("TCP server listening on %s", serverAddr), nil) + tcpTransport := tcpTransport{log: log, vpnConn: vpnConn} + for { tcpConn, err := tcpListener.Accept() if err != nil { @@ -102,13 +103,12 @@ func main() { log.Info("tcp connection accepted. Proxy started...", nil) - go io.Copy(vpnConn, tcpConn) - go io.Copy(tcpConn, vpnConn) + go tcpTransport.handle(tcpConn) } default: - ws := ws{vpnConn: vpnConn, log: log} + wsTransport := wsTransport{vpnConn: vpnConn, log: log} - http.HandleFunc("/ws", ws.handler) + http.HandleFunc("/ws", wsTransport.handler) log.Info(fmt.Sprintf("WebSocket server listening on %s", serverAddr), nil) if err := http.ListenAndServe(serverAddr, nil); err != nil { diff --git a/server/tcp.go b/server/tcp.go new file mode 100644 index 0000000..f7a7614 --- /dev/null +++ b/server/tcp.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "io" + "net" + + "tunelo/pkg/logger" +) + +type tcpTransport struct { + vpnConn *net.UDPConn + log logger.Logger +} + +func (t *tcpTransport) handle(conn net.Conn) { + defer conn.Close() + + go func() { + if _, err := io.Copy(t.vpnConn, conn); err != nil { + t.log.Error(fmt.Errorf("copying from tcp conn to vpn: %v", err), nil) + } + }() + + if _, err := io.Copy(conn, t.vpnConn); err != nil { + t.log.Error(fmt.Errorf("copying from vpn to tcp conn: %v", err), nil) + } +} diff --git a/server/tls.go b/server/tls.go deleted file mode 100644 index d627437..0000000 --- a/server/tls.go +++ /dev/null @@ -1,31 +0,0 @@ -package main - -import ( - "fmt" - "io" - "net" - - "tunelo/pkg/logger" -) - -type TLS struct { - vpnConn *net.UDPConn - log logger.Logger -} - -func (t *TLS) handle(conn net.Conn) { - // TODO: figure out how to handle connection closure. - // defer conn.Close() - - go func() { - if _, err := io.Copy(t.vpnConn, conn); err != nil { - t.log.Error(fmt.Errorf("copying from tls conn to vpn: %v", err), nil) - } - }() - - go func() { - if _, err := io.Copy(conn, t.vpnConn); err != nil { - t.log.Error(fmt.Errorf("copying from vpn to tls conn: %v", err), nil) - } - }() -} diff --git a/server/utls.go b/server/utls.go new file mode 100644 index 0000000..545cb38 --- /dev/null +++ b/server/utls.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "io" + "net" + + "tunelo/pkg/logger" +) + +type utlsTransport struct { + vpnConn *net.UDPConn + log logger.Logger +} + +func (t *utlsTransport) handle(conn net.Conn) { + defer conn.Close() + + go func() { + if _, err := io.Copy(t.vpnConn, conn); err != nil { + t.log.Error(fmt.Errorf("copying from tls conn to vpn: %v", err), nil) + } + }() + + if _, err := io.Copy(conn, t.vpnConn); err != nil { + t.log.Error(fmt.Errorf("copying from vpn to tls conn: %v", err), nil) + } +} diff --git a/server/ws.go b/server/ws.go index ba70aca..457d9f1 100644 --- a/server/ws.go +++ b/server/ws.go @@ -11,12 +11,12 @@ import ( "tunelo/pkg/logger" ) -type ws struct { +type wsTransport struct { vpnConn *net.UDPConn log logger.Logger } -func (s *ws) handler(w http.ResponseWriter, r *http.Request) { +func (s *wsTransport) handler(w http.ResponseWriter, r *http.Request) { conn, err := websocket.Accept(w, r, nil) if err != nil { s.log.Error(fmt.Errorf("accepting ws conn: %v", err), nil)