|
| 1 | +package transport |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "net" |
| 8 | + "net/http" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/gorilla/websocket" |
| 13 | + "github.com/musix/backhaul/internal/config" |
| 14 | + "github.com/musix/backhaul/internal/utils" |
| 15 | + "github.com/musix/backhaul/internal/web" |
| 16 | + |
| 17 | + "github.com/sirupsen/logrus" |
| 18 | + "github.com/xtaci/smux" |
| 19 | +) |
| 20 | + |
| 21 | +type WsMuxTransport struct { |
| 22 | + config *WsMuxConfig |
| 23 | + ctx context.Context |
| 24 | + cancel context.CancelFunc |
| 25 | + logger *logrus.Logger |
| 26 | + smuxSession []*smux.Session |
| 27 | + restartMutex sync.Mutex |
| 28 | + timeout time.Duration |
| 29 | + usageMonitor *web.Usage |
| 30 | +} |
| 31 | + |
| 32 | +type WsMuxConfig struct { |
| 33 | + RemoteAddr string |
| 34 | + Nodelay bool |
| 35 | + KeepAlive time.Duration |
| 36 | + RetryInterval time.Duration |
| 37 | + Token string |
| 38 | + MuxSession int |
| 39 | + Forwarder map[int]string |
| 40 | + MuxVersion int |
| 41 | + MaxFrameSize int |
| 42 | + MaxReceiveBuffer int |
| 43 | + MaxStreamBuffer int |
| 44 | + Sniffer bool |
| 45 | + WebPort int |
| 46 | + SnifferLog string |
| 47 | + TunnelStatus string |
| 48 | + Mode config.TransportType |
| 49 | +} |
| 50 | + |
| 51 | +func NewWSMuxClient(parentCtx context.Context, config *WsMuxConfig, logger *logrus.Logger) *WsMuxTransport { |
| 52 | + // Create a derived context from the parent context |
| 53 | + ctx, cancel := context.WithCancel(parentCtx) |
| 54 | + |
| 55 | + // Initialize the TcpTransport struct |
| 56 | + client := &WsMuxTransport{ |
| 57 | + config: config, |
| 58 | + ctx: ctx, |
| 59 | + cancel: cancel, |
| 60 | + logger: logger, |
| 61 | + smuxSession: make([]*smux.Session, config.MuxSession), |
| 62 | + timeout: 10 * time.Second, // Default timeout |
| 63 | + usageMonitor: web.NewDataStore(fmt.Sprintf(":%v", config.WebPort), ctx, config.SnifferLog, config.Sniffer, &config.TunnelStatus, logger), |
| 64 | + } |
| 65 | + |
| 66 | + return client |
| 67 | +} |
| 68 | + |
| 69 | +func (c *WsMuxTransport) Restart() { |
| 70 | + if !c.restartMutex.TryLock() { |
| 71 | + c.logger.Warn("client is already restarting") |
| 72 | + return |
| 73 | + } |
| 74 | + defer c.restartMutex.Unlock() |
| 75 | + |
| 76 | + c.logger.Info("restarting client...") |
| 77 | + if c.cancel != nil { |
| 78 | + c.cancel() |
| 79 | + } |
| 80 | + |
| 81 | + time.Sleep(2 * time.Second) |
| 82 | + |
| 83 | + ctx, cancel := context.WithCancel(context.Background()) |
| 84 | + c.ctx = ctx |
| 85 | + c.cancel = cancel |
| 86 | + |
| 87 | + // Re-initialize variables |
| 88 | + c.smuxSession = make([]*smux.Session, c.config.MuxSession) |
| 89 | + c.usageMonitor = web.NewDataStore(fmt.Sprintf(":%v", c.config.WebPort), ctx, c.config.SnifferLog, c.config.Sniffer, &c.config.TunnelStatus, c.logger) |
| 90 | + c.config.TunnelStatus = "" |
| 91 | + |
| 92 | + go c.MuxDialer() |
| 93 | + |
| 94 | +} |
| 95 | + |
| 96 | +func (c *WsMuxTransport) MuxDialer() { |
| 97 | + // for webui |
| 98 | + if c.config.WebPort > 0 { |
| 99 | + go c.usageMonitor.Monitor() |
| 100 | + } |
| 101 | + |
| 102 | + c.config.TunnelStatus = "Disconnected (WSMux)" |
| 103 | + |
| 104 | + for id := 0; id < c.config.MuxSession; id++ { |
| 105 | + innerloop: |
| 106 | + for { |
| 107 | + select { |
| 108 | + case <-c.ctx.Done(): |
| 109 | + return |
| 110 | + default: |
| 111 | + c.logger.Debugf("initiating new mux session to address %s (session ID: %d)", c.config.RemoteAddr, id) |
| 112 | + // Dial to the tunnel server |
| 113 | + tunnelTCPConn, err := c.wsDialer(c.config.RemoteAddr, "/channel") |
| 114 | + if err != nil { |
| 115 | + c.logger.Errorf("failed to dial tunnel server at %s: %v", c.config.RemoteAddr, err) |
| 116 | + time.Sleep(c.config.RetryInterval) |
| 117 | + continue |
| 118 | + } |
| 119 | + |
| 120 | + // config fot smux |
| 121 | + config := smux.Config{ |
| 122 | + Version: c.config.MuxVersion, // Smux protocol version |
| 123 | + KeepAliveInterval: 10 * time.Second, // Shorter keep-alive interval to quickly detect dead peers |
| 124 | + KeepAliveTimeout: 30 * time.Second, // Aggressive timeout to handle unresponsive connections |
| 125 | + MaxFrameSize: c.config.MaxFrameSize, |
| 126 | + MaxReceiveBuffer: c.config.MaxReceiveBuffer, |
| 127 | + MaxStreamBuffer: c.config.MaxStreamBuffer, |
| 128 | + } |
| 129 | + |
| 130 | + // SMUX server |
| 131 | + session, err := smux.Server(tunnelTCPConn.UnderlyingConn(), &config) |
| 132 | + if err != nil { |
| 133 | + c.logger.Errorf("failed to create mux session: %v", err) |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + c.smuxSession[id] = session |
| 138 | + c.logger.Infof("mux session established successfully (session ID: %d)", id) |
| 139 | + go c.handleMUXStreams(id) |
| 140 | + break innerloop |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + c.config.TunnelStatus = "Connected (WSMux)" |
| 145 | +} |
| 146 | + |
| 147 | +func (c *WsMuxTransport) handleMUXStreams(id int) { |
| 148 | + for { |
| 149 | + select { |
| 150 | + case <-c.ctx.Done(): |
| 151 | + return |
| 152 | + default: |
| 153 | + stream, err := c.smuxSession[id].AcceptStream() |
| 154 | + if err != nil { |
| 155 | + c.logger.Errorf("failed to accept mux stream for session ID %d: %v", id, err) |
| 156 | + c.logger.Info("attempting to restart client...") |
| 157 | + go c.Restart() |
| 158 | + return |
| 159 | + |
| 160 | + } |
| 161 | + go c.handleTCPSession(stream) |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func (c *WsMuxTransport) handleTCPSession(tcpsession net.Conn) { |
| 167 | + select { |
| 168 | + case <-c.ctx.Done(): |
| 169 | + return |
| 170 | + default: |
| 171 | + port, err := utils.ReceiveBinaryInt(tcpsession) |
| 172 | + |
| 173 | + if err != nil { |
| 174 | + c.logger.Tracef("unable to get the port from the %s connection: %v", tcpsession.RemoteAddr().String(), err) |
| 175 | + tcpsession.Close() |
| 176 | + return |
| 177 | + } |
| 178 | + go c.localDialer(tcpsession, port) |
| 179 | + |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +func (c *WsMuxTransport) localDialer(tunnelConnection net.Conn, port uint16) { |
| 184 | + select { |
| 185 | + case <-c.ctx.Done(): |
| 186 | + return |
| 187 | + default: |
| 188 | + localAddress, ok := c.config.Forwarder[int(port)] |
| 189 | + if !ok { |
| 190 | + localAddress = fmt.Sprintf("127.0.0.1:%d", port) |
| 191 | + } |
| 192 | + |
| 193 | + localConnection, err := c.tcpDialer(localAddress, c.config.Nodelay) |
| 194 | + if err != nil { |
| 195 | + c.logger.Errorf("failed to connect to local address %s: %v", localAddress, err) |
| 196 | + tunnelConnection.Close() |
| 197 | + return |
| 198 | + } |
| 199 | + c.logger.Debugf("connected to local address %s successfully", localAddress) |
| 200 | + go utils.ConnectionHandler(localConnection, tunnelConnection, c.logger, c.usageMonitor, int(port), c.config.Sniffer) |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +func (c *WsMuxTransport) tcpDialer(address string, tcpnodelay bool) (*net.TCPConn, error) { |
| 205 | + // Resolve the address to a TCP address |
| 206 | + tcpAddr, err := net.ResolveTCPAddr("tcp", address) |
| 207 | + if err != nil { |
| 208 | + return nil, err |
| 209 | + } |
| 210 | + |
| 211 | + // options |
| 212 | + dialer := &net.Dialer{ |
| 213 | + Timeout: c.timeout, // Set the connection timeout |
| 214 | + KeepAlive: c.config.KeepAlive, // Set the keep-alive duration |
| 215 | + } |
| 216 | + |
| 217 | + // Dial the TCP connection with a timeout |
| 218 | + conn, err := dialer.Dial("tcp", tcpAddr.String()) |
| 219 | + if err != nil { |
| 220 | + return nil, err |
| 221 | + } |
| 222 | + |
| 223 | + // Type assert the net.Conn to *net.TCPConn |
| 224 | + tcpConn, ok := conn.(*net.TCPConn) |
| 225 | + if !ok { |
| 226 | + conn.Close() |
| 227 | + return nil, fmt.Errorf("failed to convert net.Conn to *net.TCPConn") |
| 228 | + } |
| 229 | + |
| 230 | + if tcpnodelay { |
| 231 | + // Enable TCP_NODELAY |
| 232 | + err = tcpConn.SetNoDelay(true) |
| 233 | + if err != nil { |
| 234 | + tcpConn.Close() |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + return tcpConn, nil |
| 240 | +} |
| 241 | + |
| 242 | +func (c *WsMuxTransport) wsDialer(addr string, path string) (*websocket.Conn, error) { |
| 243 | + // Create a TLS configuration that allows insecure connections |
| 244 | + tlsConfig := &tls.Config{ |
| 245 | + InsecureSkipVerify: true, // Skip server certificate verification |
| 246 | + } |
| 247 | + |
| 248 | + // Setup headers with authorization |
| 249 | + headers := http.Header{} |
| 250 | + headers.Add("Authorization", fmt.Sprintf("Bearer %v", c.config.Token)) |
| 251 | + |
| 252 | + var wsURL string |
| 253 | + dialer := websocket.Dialer{} |
| 254 | + if c.config.Mode == config.WSMUX { |
| 255 | + wsURL = fmt.Sprintf("ws://%s%s", addr, path) |
| 256 | + dialer = websocket.Dialer{ |
| 257 | + HandshakeTimeout: c.timeout, // Set handshake timeout |
| 258 | + NetDial: func(_, addr string) (net.Conn, error) { |
| 259 | + conn, err := net.DialTimeout("tcp", addr, c.timeout) |
| 260 | + if err != nil { |
| 261 | + return nil, err |
| 262 | + } |
| 263 | + tcpConn := conn.(*net.TCPConn) |
| 264 | + tcpConn.SetKeepAlive(true) // Enable TCP keepalive |
| 265 | + tcpConn.SetKeepAlivePeriod(c.config.KeepAlive) // Set keepalive period |
| 266 | + return tcpConn, nil |
| 267 | + }, |
| 268 | + } |
| 269 | + } else { |
| 270 | + wsURL = fmt.Sprintf("wss://%s%s", addr, path) |
| 271 | + dialer = websocket.Dialer{ |
| 272 | + TLSClientConfig: tlsConfig, // Pass the insecure TLS config here |
| 273 | + HandshakeTimeout: c.timeout, // Set handshake timeout |
| 274 | + NetDial: func(_, addr string) (net.Conn, error) { |
| 275 | + conn, err := net.DialTimeout("tcp", addr, c.timeout) |
| 276 | + if err != nil { |
| 277 | + return nil, err |
| 278 | + } |
| 279 | + tcpConn := conn.(*net.TCPConn) |
| 280 | + tcpConn.SetKeepAlive(true) // Enable TCP keepalive |
| 281 | + tcpConn.SetKeepAlivePeriod(c.config.KeepAlive) // Set keepalive period |
| 282 | + return tcpConn, nil |
| 283 | + }, |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + // Dial to the WebSocket server |
| 288 | + tunnelWSConn, _, err := dialer.Dial(wsURL, headers) |
| 289 | + if err != nil { |
| 290 | + return nil, err |
| 291 | + } |
| 292 | + |
| 293 | + return tunnelWSConn, nil |
| 294 | +} |
0 commit comments