@@ -297,7 +297,7 @@ func (c *Common) initConfig(parsedURL *url.URL) {
297297 c .getProxyProtocol (parsedURL )
298298}
299299
300- // sendProxyV1Header 发送 Proxy Protocol v1 header
300+ // sendProxyV1Header 发送PROXY v1
301301func (c * Common ) sendProxyV1Header (ip string , conn net.Conn ) error {
302302 if c .proxyProtocol != "1" {
303303 return nil
@@ -322,15 +322,12 @@ func (c *Common) sendProxyV1Header(ip string, conn net.Conn) error {
322322 return fmt .Errorf ("unsupported IP protocol for PROXY v1" )
323323 }
324324
325- header : = fmt .Sprintf ( "PROXY %s %s %s %d %d\r \n " ,
325+ if _ , err = fmt .Fprintf ( conn , "PROXY %s %s %s %d %d\r \n " ,
326326 protocol ,
327- clientAddr .IP .String (), // 用户真实地址
328- remoteAddr .IP .String (), // 目标服务地址
329- clientAddr .Port , // 用户真实端口
330- remoteAddr .Port ) // 目标服务端口
331-
332- // 发送 header
333- if _ , err = conn .Write ([]byte (header )); err != nil {
327+ clientAddr .IP .String (),
328+ remoteAddr .IP .String (),
329+ clientAddr .Port ,
330+ remoteAddr .Port ); err != nil {
334331 return err
335332 }
336333
@@ -697,7 +694,8 @@ func (c *Common) commonTCPLoop() {
697694
698695 // 构建并发送启动URL到客户端
699696 launchURL := & url.URL {
700- Host : id ,
697+ Host : targetConn .RemoteAddr ().String (),
698+ Path : id ,
701699 Fragment : "1" , // TCP模式
702700 }
703701
@@ -905,7 +903,7 @@ func (c *Common) commonOnce() error {
905903 c .logger .Debug ("Tunnel pool reset: %v active connections" , c .tunnelPool .Active ())
906904 }()
907905 case "1" : // TCP
908- go c .commonTCPOnce (signalURL . Host )
906+ go c .commonTCPOnce (signalURL )
909907 case "2" : // UDP
910908 go c .commonUDPOnce (signalURL )
911909 case "i" : // PING
@@ -930,7 +928,8 @@ func (c *Common) commonOnce() error {
930928}
931929
932930// commonTCPOnce 共用处理单个TCP请求
933- func (c * Common ) commonTCPOnce (id string ) {
931+ func (c * Common ) commonTCPOnce (signalURL * url.URL ) {
932+ id := strings .TrimPrefix (signalURL .Path , "/" )
934933 c .logger .Debug ("TCP launch signal: cid %v <- %v" , id , c .tunnelTCPConn .RemoteAddr ())
935934
936935 // 尝试获取TCP连接槽位
@@ -973,9 +972,15 @@ func (c *Common) commonTCPOnce(id string) {
973972 c .targetTCPConn = targetConn .(* net.TCPConn )
974973 targetConn = & conn.StatConn {Conn : targetConn , RX : & c .tcpRX , TX : & c .tcpTX , Rate : c .rateLimiter }
975974 c .logger .Debug ("Target connection: %v <-> %v" , targetConn .LocalAddr (), targetConn .RemoteAddr ())
976- c .logger .Debug ("Starting exchange: %v <-> %v" , remoteConn .LocalAddr (), targetConn .LocalAddr ())
975+
976+ // 发送PROXY v1
977+ if err := c .sendProxyV1Header (signalURL .Host , targetConn ); err != nil {
978+ c .logger .Error ("sendProxyV1Header failed: %v" , err )
979+ return
980+ }
977981
978982 // 交换数据
983+ c .logger .Debug ("Starting exchange: %v <-> %v" , remoteConn .LocalAddr (), targetConn .LocalAddr ())
979984 _ , _ , err = conn .DataExchange (remoteConn , targetConn , c .readTimeout )
980985
981986 // 交换完成
@@ -1243,9 +1248,15 @@ func (c *Common) singleTCPLoop() error {
12431248
12441249 c .targetTCPConn = targetConn .(* net.TCPConn )
12451250 c .logger .Debug ("Target connection: %v <-> %v" , targetConn .LocalAddr (), targetConn .RemoteAddr ())
1246- c .logger .Debug ("Starting exchange: %v <-> %v" , tunnelConn .LocalAddr (), targetConn .LocalAddr ())
1251+
1252+ // 发送PROXY v1
1253+ if err := c .sendProxyV1Header (tunnelConn .RemoteAddr ().String (), targetConn ); err != nil {
1254+ c .logger .Error ("sendProxyV1Header failed: %v" , err )
1255+ return
1256+ }
12471257
12481258 // 交换数据
1259+ c .logger .Debug ("Starting exchange: %v <-> %v" , tunnelConn .LocalAddr (), targetConn .LocalAddr ())
12491260 _ , _ , err = conn .DataExchange (tunnelConn , targetConn , c .readTimeout )
12501261
12511262 // 交换完成
0 commit comments