Skip to content

Commit abb654d

Browse files
FanglidingRPRX
andauthored
Socks inbound: Support HTTP inbound (#3682)
Co-authored-by: RPRX <[email protected]>
1 parent 030c9ef commit abb654d

File tree

3 files changed

+120
-5
lines changed

3 files changed

+120
-5
lines changed

proxy/http/server.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package http
22

33
import (
44
"bufio"
5+
"bytes"
56
"context"
67
"encoding/base64"
78
"io"
@@ -83,14 +84,28 @@ type readerOnly struct {
8384
}
8485

8586
func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
87+
return s.ProcessWithFirstbyte(ctx, network, conn, dispatcher)
88+
}
89+
90+
// Firstbyte is for forwarded conn from SOCKS inbound
91+
// Because it needs first byte to choose protocol
92+
// We need to add it back
93+
// Other parts are the same as the process function
94+
func (s *Server) ProcessWithFirstbyte(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher, firstbyte ...byte) error {
8695
inbound := session.InboundFromContext(ctx)
8796
inbound.Name = "http"
8897
inbound.CanSpliceCopy = 2
8998
inbound.User = &protocol.MemoryUser{
9099
Level: s.config.UserLevel,
91100
}
92-
93-
reader := bufio.NewReaderSize(readerOnly{conn}, buf.Size)
101+
var reader *bufio.Reader
102+
if len(firstbyte) > 0 {
103+
readerWithoutFirstbyte := bufio.NewReaderSize(readerOnly{conn}, buf.Size)
104+
multiReader := io.MultiReader(bytes.NewReader(firstbyte), readerWithoutFirstbyte)
105+
reader = bufio.NewReaderSize(multiReader, buf.Size)
106+
} else {
107+
reader = bufio.NewReaderSize(readerOnly{conn}, buf.Size)
108+
}
94109

95110
Start:
96111
if err := conn.SetReadDeadline(time.Now().Add(s.policy().Timeouts.Handshake)); err != nil {

proxy/socks/server.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package socks
22

33
import (
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() {

testing/scenarios/socks_test.go

+82
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/xtls/xray-core/proxy/blackhole"
1515
"github.com/xtls/xray-core/proxy/dokodemo"
1616
"github.com/xtls/xray-core/proxy/freedom"
17+
"github.com/xtls/xray-core/proxy/http"
1718
"github.com/xtls/xray-core/proxy/socks"
1819
"github.com/xtls/xray-core/testing/servers/tcp"
1920
"github.com/xtls/xray-core/testing/servers/udp"
@@ -102,6 +103,87 @@ func TestSocksBridgeTCP(t *testing.T) {
102103
}
103104
}
104105

106+
func TestSocksWithHttpRequest(t *testing.T) {
107+
tcpServer := tcp.Server{
108+
MsgProcessor: xor,
109+
}
110+
dest, err := tcpServer.Start()
111+
common.Must(err)
112+
defer tcpServer.Close()
113+
114+
serverPort := tcp.PickPort()
115+
serverConfig := &core.Config{
116+
Inbound: []*core.InboundHandlerConfig{
117+
{
118+
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
119+
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
120+
Listen: net.NewIPOrDomain(net.LocalHostIP),
121+
}),
122+
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
123+
AuthType: socks.AuthType_PASSWORD,
124+
Accounts: map[string]string{
125+
"Test Account": "Test Password",
126+
},
127+
Address: net.NewIPOrDomain(net.LocalHostIP),
128+
UdpEnabled: false,
129+
}),
130+
},
131+
},
132+
Outbound: []*core.OutboundHandlerConfig{
133+
{
134+
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
135+
},
136+
},
137+
}
138+
139+
clientPort := tcp.PickPort()
140+
clientConfig := &core.Config{
141+
Inbound: []*core.InboundHandlerConfig{
142+
{
143+
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
144+
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
145+
Listen: net.NewIPOrDomain(net.LocalHostIP),
146+
}),
147+
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
148+
Address: net.NewIPOrDomain(dest.Address),
149+
Port: uint32(dest.Port),
150+
NetworkList: &net.NetworkList{
151+
Network: []net.Network{net.Network_TCP},
152+
},
153+
}),
154+
},
155+
},
156+
Outbound: []*core.OutboundHandlerConfig{
157+
{
158+
ProxySettings: serial.ToTypedMessage(&http.ClientConfig{
159+
Server: []*protocol.ServerEndpoint{
160+
{
161+
Address: net.NewIPOrDomain(net.LocalHostIP),
162+
Port: uint32(serverPort),
163+
User: []*protocol.User{
164+
{
165+
Account: serial.ToTypedMessage(&http.Account{
166+
Username: "Test Account",
167+
Password: "Test Password",
168+
}),
169+
},
170+
},
171+
},
172+
},
173+
}),
174+
},
175+
},
176+
}
177+
178+
servers, err := InitializeServerConfigs(serverConfig, clientConfig)
179+
common.Must(err)
180+
defer CloseAllServers(servers)
181+
182+
if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
183+
t.Error(err)
184+
}
185+
}
186+
105187
func TestSocksBridageUDP(t *testing.T) {
106188
udpServer := udp.Server{
107189
MsgProcessor: xor,

0 commit comments

Comments
 (0)