@@ -27,6 +27,7 @@ func handleSocks5Proxy(conn net.Conn, keyingMaterial [32]byte, config *AppS5SCon
2727 ServerIP : config .ServerIP ,
2828 Localbind : config .Localbind ,
2929 AccessCtrl : config .AccessCtrl ,
30+ Outbound : config .UpstreamClient ,
3031 }
3132 s5auth := & Socks5AuthConfig {
3233 AuthenticateUser : nil ,
@@ -57,6 +58,12 @@ func handleSocks5Proxy(conn net.Conn, keyingMaterial [32]byte, config *AppS5SCon
5758
5859 conn .SetReadDeadline (time.Time {})
5960
61+ if config .UpstreamClient != nil && req .Command != "CONNECT" {
62+ config .Logger .Printf ("SOCKS5 %s rejected for %s->%s: -x upstream mode supports CONNECT only" , req .Command , conn .RemoteAddr (), reqTarget )
63+ sendSocks5Response (conn , REP_COMMAND_NOT_SUPPORTED , "0.0.0.0" , 0 )
64+ return
65+ }
66+
6067 if req .Command == "CONNECT" && config .EnableConnect {
6168 err = handleDirectTCPConnect (& s5config , conn , req .Host , req .Port )
6269 if err != nil {
@@ -97,6 +104,12 @@ func handleHTTPProxy(conn *netx.BufferedConn, config *AppS5SConfig) {
97104 }
98105
99106 // 普通 HTTP
107+ if config .UpstreamClient != nil {
108+ config .Logger .Printf ("HTTP %s rejected for %s->%s: -x upstream mode supports CONNECT only" , req .Method , conn .RemoteAddr (), req .Host )
109+ _ , _ = conn .Write ([]byte ("HTTP/1.1 501 Not Implemented\r \n Connection: close\r \n \r \n " ))
110+ return
111+ }
112+
100113 err = handleHTTPForwardSimple (conn , req , config )
101114 if err != nil {
102115 config .Logger .Printf ("HTTP CONNECT failed for %s->%s: %v" , conn .RemoteAddr (), req .Host , err )
@@ -273,6 +286,18 @@ func handleHTTPConnect(clientConn net.Conn, req *http.Request, config *AppS5SCon
273286 config .Logger .Printf ("HTTP-CONNECT: %s->%s connecting..." , clientConn .RemoteAddr ().String (), req .Host )
274287 defer config .Logger .Printf ("HTTP: %s client disconnected." , clientConn .RemoteAddr ().String ())
275288
289+ if config .UpstreamClient != nil {
290+ targetConn , err := config .UpstreamClient .DialTimeout ("tcp" , req .Host , 25 * time .Second )
291+ if err != nil {
292+ clientConn .Write ([]byte ("HTTP/1.1 502 Bad Gateway\r \n \r \n " ))
293+ return fmt .Errorf ("upstream HTTP CONNECT failed: %w" , err )
294+ }
295+ defer targetConn .Close ()
296+ clientConn .Write ([]byte ("HTTP/1.1 200 Connection Established\r \n \r \n " ))
297+ bidirectionalCopy (clientConn , targetConn )
298+ return nil
299+ }
300+
276301 dialer := & net.Dialer {}
277302 ctx , cancel := context .WithTimeout (context .Background (), 25 * time .Second )
278303 defer cancel ()
0 commit comments