diff --git a/cmd/link.go b/cmd/link.go index bd00dbb..64de815 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -2,11 +2,11 @@ package cmd import ( "fmt" - "log" "net" "sync" "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -57,7 +57,7 @@ func startTCPTunnel(vhost, hostPort string) error { var msg TunnelMessage err := conn.ReadJSON(&msg) if err != nil { - log.Printf("Error reading from tunnel: %v", err) + log.Errorf("Error reading from tunnel: %v", err) break } @@ -71,7 +71,7 @@ func startTCPTunnel(vhost, hostPort string) error { // New connection localConn, err = net.Dial("tcp", hostPort) if err != nil { - log.Printf("Failed to connect to %s: %v", hostPort, err) + log.Errorf("Failed to connect to %s: %v", hostPort, err) continue } @@ -110,7 +110,7 @@ func startTCPTunnel(vhost, hostPort string) error { // Write data to local connection if _, err := localConn.Write(msg.Data); err != nil { - log.Printf("Error writing to local connection: %v", err) + log.Errorf("Error writing to local connection: %v", err) localConn.Close() connMutex.Lock() delete(connections, msg.StreamID) diff --git a/go.mod b/go.mod index 51ef378..29ae61b 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,8 @@ go 1.24.4 require ( github.com/gorilla/websocket v1.5.3 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/sirupsen/logrus v1.9.3 // indirect github.com/spf13/cobra v1.10.1 // indirect github.com/spf13/pflag v1.0.9 // indirect + golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect ) diff --git a/go.sum b/go.sum index a3d862c..7cb5bd1 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,22 @@ github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= github.com/spf13/cobra v1.10.1 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s= github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY= github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/server/server.go b/server/server.go index 8dfa19b..698bc3d 100644 --- a/server/server.go +++ b/server/server.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - "log" "net" "net/http" "strings" @@ -12,6 +11,7 @@ import ( "time" "github.com/gorilla/websocket" + log "github.com/sirupsen/logrus" ) type TunnelConnection struct { @@ -51,7 +51,7 @@ func (s *Server) Start(addr string) error { } defer listener.Close() - log.Printf("Conduit server listening on %s", addr) + log.Infof("Conduit server listening on %s", addr) for { conn, err := listener.Accept() @@ -120,7 +120,7 @@ func (s *Server) proxyRawConnection(clientConn net.Conn, tunnelConn *TunnelConne } if err := tunnelConn.WriteJSON(msg); err != nil { - log.Printf("Error sending initial data: %v", err) + log.Errorf("Error sending initial data: %v", err) return } @@ -200,7 +200,7 @@ func (s *Server) handleRawConnection(conn net.Conn) { s.mu.RUnlock() if exists { - log.Printf("Relaying %s to tunnel", subdomain) + log.Infof("Relaying %s to tunnel", subdomain) s.proxyRawConnection(conn, tunnelConn, buffer[:n]) return } @@ -219,12 +219,14 @@ func (s *Server) handleAsHTTP(conn net.Conn, initialData []byte) { return } - // Handle control endpoints - if req.URL.Path == "/_conduit/tunnel" { + // Handle Control Endpoints + switch req.URL.Path { + case "/_conduit/tunnel": s.handleTunnelUpgrade(conn, req) - } else if req.URL.Path == "/_conduit/status" { + return + case "/_conduit/status": s.handleStatus(conn) - } else { + default: conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n")) } } @@ -244,7 +246,7 @@ func (s *Server) handleTunnelMessages(tunnel *TunnelConnection) { select { case streamChan <- msg.Data: case <-time.After(time.Second): - log.Printf("Stream %s channel full, dropping data", msg.StreamID) + log.Infof("Stream %s channel full, dropping data", msg.StreamID) } } s.mu.RUnlock() @@ -264,7 +266,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) { // Use the upgrader wsConn, err := s.upgrader.Upgrade(fakeWriter, req, nil) if err != nil { - log.Printf("WebSocket upgrade failed: %v", err) + log.Errorf("WebSocket upgrade failed: %v", err) return } @@ -279,7 +281,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) { s.tunnels[vhost] = tunnel s.mu.Unlock() - log.Printf("Tunnel established: %s", vhost) + log.Infof("Tunnel established: %s", vhost) // Keep connection alive and handle cleanup defer func() { @@ -287,7 +289,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) { delete(s.tunnels, vhost) s.mu.Unlock() wsConn.Close() - log.Printf("Tunnel closed: %s", vhost) + log.Infof("Tunnel closed: %s", vhost) }() // Handle tunnel messages