This commit is contained in:
Evan Reichard 2025-09-19 17:22:36 -04:00
parent 0223d35b34
commit 07151857c5
4 changed files with 30 additions and 16 deletions

View File

@ -2,11 +2,11 @@ package cmd
import ( import (
"fmt" "fmt"
"log"
"net" "net"
"sync" "sync"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -57,7 +57,7 @@ func startTCPTunnel(vhost, hostPort string) error {
var msg TunnelMessage var msg TunnelMessage
err := conn.ReadJSON(&msg) err := conn.ReadJSON(&msg)
if err != nil { if err != nil {
log.Printf("Error reading from tunnel: %v", err) log.Errorf("Error reading from tunnel: %v", err)
break break
} }
@ -71,7 +71,7 @@ func startTCPTunnel(vhost, hostPort string) error {
// New connection // New connection
localConn, err = net.Dial("tcp", hostPort) localConn, err = net.Dial("tcp", hostPort)
if err != nil { if err != nil {
log.Printf("Failed to connect to %s: %v", hostPort, err) log.Errorf("Failed to connect to %s: %v", hostPort, err)
continue continue
} }
@ -110,7 +110,7 @@ func startTCPTunnel(vhost, hostPort string) error {
// Write data to local connection // Write data to local connection
if _, err := localConn.Write(msg.Data); err != nil { 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() localConn.Close()
connMutex.Lock() connMutex.Lock()
delete(connections, msg.StreamID) delete(connections, msg.StreamID)

2
go.mod
View File

@ -5,6 +5,8 @@ go 1.24.4
require ( require (
github.com/gorilla/websocket v1.5.3 // indirect github.com/gorilla/websocket v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // 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/cobra v1.10.1 // indirect
github.com/spf13/pflag v1.0.9 // indirect github.com/spf13/pflag v1.0.9 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
) )

10
go.sum
View File

@ -1,12 +1,22 @@
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= 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 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= 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 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= 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/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 h1:lJeBwCfmrnXthfAupyUTzJ/J4Nc1RsHC/mSRU2dll/s=
github.com/spf13/cobra v1.10.1/go.mod h1:7SmJGaTHFVBY0jW4NXGluQoLvhqFQM+6XSKD+P4XaB0= 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 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= 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/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= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -4,7 +4,6 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"fmt" "fmt"
"log"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -12,6 +11,7 @@ import (
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
log "github.com/sirupsen/logrus"
) )
type TunnelConnection struct { type TunnelConnection struct {
@ -51,7 +51,7 @@ func (s *Server) Start(addr string) error {
} }
defer listener.Close() defer listener.Close()
log.Printf("Conduit server listening on %s", addr) log.Infof("Conduit server listening on %s", addr)
for { for {
conn, err := listener.Accept() conn, err := listener.Accept()
@ -120,7 +120,7 @@ func (s *Server) proxyRawConnection(clientConn net.Conn, tunnelConn *TunnelConne
} }
if err := tunnelConn.WriteJSON(msg); err != nil { if err := tunnelConn.WriteJSON(msg); err != nil {
log.Printf("Error sending initial data: %v", err) log.Errorf("Error sending initial data: %v", err)
return return
} }
@ -200,7 +200,7 @@ func (s *Server) handleRawConnection(conn net.Conn) {
s.mu.RUnlock() s.mu.RUnlock()
if exists { if exists {
log.Printf("Relaying %s to tunnel", subdomain) log.Infof("Relaying %s to tunnel", subdomain)
s.proxyRawConnection(conn, tunnelConn, buffer[:n]) s.proxyRawConnection(conn, tunnelConn, buffer[:n])
return return
} }
@ -219,12 +219,14 @@ func (s *Server) handleAsHTTP(conn net.Conn, initialData []byte) {
return return
} }
// Handle control endpoints // Handle Control Endpoints
if req.URL.Path == "/_conduit/tunnel" { switch req.URL.Path {
case "/_conduit/tunnel":
s.handleTunnelUpgrade(conn, req) s.handleTunnelUpgrade(conn, req)
} else if req.URL.Path == "/_conduit/status" { return
case "/_conduit/status":
s.handleStatus(conn) s.handleStatus(conn)
} else { default:
conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n")) conn.Write([]byte("HTTP/1.1 404 Not Found\r\n\r\n"))
} }
} }
@ -244,7 +246,7 @@ func (s *Server) handleTunnelMessages(tunnel *TunnelConnection) {
select { select {
case streamChan <- msg.Data: case streamChan <- msg.Data:
case <-time.After(time.Second): 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() s.mu.RUnlock()
@ -264,7 +266,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) {
// Use the upgrader // Use the upgrader
wsConn, err := s.upgrader.Upgrade(fakeWriter, req, nil) wsConn, err := s.upgrader.Upgrade(fakeWriter, req, nil)
if err != nil { if err != nil {
log.Printf("WebSocket upgrade failed: %v", err) log.Errorf("WebSocket upgrade failed: %v", err)
return return
} }
@ -279,7 +281,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) {
s.tunnels[vhost] = tunnel s.tunnels[vhost] = tunnel
s.mu.Unlock() s.mu.Unlock()
log.Printf("Tunnel established: %s", vhost) log.Infof("Tunnel established: %s", vhost)
// Keep connection alive and handle cleanup // Keep connection alive and handle cleanup
defer func() { defer func() {
@ -287,7 +289,7 @@ func (s *Server) handleTunnelUpgrade(conn net.Conn, req *http.Request) {
delete(s.tunnels, vhost) delete(s.tunnels, vhost)
s.mu.Unlock() s.mu.Unlock()
wsConn.Close() wsConn.Close()
log.Printf("Tunnel closed: %s", vhost) log.Infof("Tunnel closed: %s", vhost)
}() }()
// Handle tunnel messages // Handle tunnel messages