Skip to content

Commit

Permalink
refactor: refactor client and server
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdieidi committed Sep 10, 2023
1 parent eccc304 commit bd92909
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 56 deletions.
19 changes: 3 additions & 16 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"crypto/tls"
"flag"
"fmt"
"io"
"net"
"net/http"
"os"
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions server/tcp.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
31 changes: 0 additions & 31 deletions server/tls.go

This file was deleted.

28 changes: 28 additions & 0 deletions server/utls.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions server/ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit bd92909

Please sign in to comment.