11package socks
22
33import (
4+ "bytes"
45 "context"
56 "io"
67 "time"
@@ -19,6 +20,7 @@ import (
1920 "github.com/xtls/xray-core/features"
2021 "github.com/xtls/xray-core/features/policy"
2122 "github.com/xtls/xray-core/features/routing"
23+ "github.com/xtls/xray-core/proxy/http"
2224 "github.com/xtls/xray-core/transport/internet/stat"
2325 "github.com/xtls/xray-core/transport/internet/udp"
2426)
@@ -29,6 +31,7 @@ type Server struct {
2931 policyManager policy.Manager
3032 cone bool
3133 udpFilter * UDPFilter
34+ httpServer * http.Server
3235}
3336
3437// NewServer creates a new Server object.
@@ -39,9 +42,14 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
3942 policyManager : v .GetFeature (policy .ManagerType ()).(policy.Manager ),
4043 cone : ctx .Value ("cone" ).(bool ),
4144 }
45+ httpConfig := & http.ServerConfig {
46+ UserLevel : config .UserLevel ,
47+ }
4248 if config .AuthType == AuthType_PASSWORD {
49+ httpConfig .Accounts = config .Accounts
4350 s .udpFilter = new (UDPFilter ) // We only use this when auth is enabled
4451 }
52+ s .httpServer , _ = http .NewServer (ctx , httpConfig )
4553 return s , nil
4654}
4755
@@ -77,15 +85,21 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
7785
7886 switch network {
7987 case net .Network_TCP :
80- return s .processTCP (ctx , conn , dispatcher )
88+ firstbyte := make ([]byte , 1 )
89+ conn .Read (firstbyte )
90+ if firstbyte [0 ] != 5 && firstbyte [0 ] != 4 { // Check if it is Socks5/4/4a
91+ errors .LogDebug (ctx , "Not Socks request, try to parse as HTTP request" )
92+ return s .httpServer .ProcessWithFirstbyte (ctx , network , conn , dispatcher , firstbyte ... )
93+ }
94+ return s .processTCP (ctx , conn , dispatcher , firstbyte )
8195 case net .Network_UDP :
8296 return s .handleUDPPayload (ctx , conn , dispatcher )
8397 default :
8498 return errors .New ("unknown network: " , network )
8599 }
86100}
87101
88- func (s * Server ) processTCP (ctx context.Context , conn stat.Connection , dispatcher routing.Dispatcher ) error {
102+ func (s * Server ) processTCP (ctx context.Context , conn stat.Connection , dispatcher routing.Dispatcher , firstbyte [] byte ) error {
89103 plcy := s .policy ()
90104 if err := conn .SetReadDeadline (time .Now ().Add (plcy .Timeouts .Handshake )); err != nil {
91105 errors .LogInfoInner (ctx , err , "failed to set deadline" )
@@ -103,7 +117,11 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche
103117 localAddress : net .IPAddress (conn .LocalAddr ().(* net.TCPAddr ).IP ),
104118 }
105119
106- reader := & buf.BufferedReader {Reader : buf .NewReader (conn )}
120+ // Firstbyte is for forwarded conn from SOCKS inbound
121+ // Because it needs first byte to choose protocol
122+ // We need to add it back
123+ readerWithoutFirstbyte := & buf.BufferedReader {Reader : buf .NewReader (conn )}
124+ reader := io .MultiReader (bytes .NewReader (firstbyte ), readerWithoutFirstbyte )
107125 request , err := svrSession .Handshake (reader , conn )
108126 if err != nil {
109127 if inbound .Source .IsValid () {
0 commit comments