Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Socks inbound: Support HTTP inbound by default #3682

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions proxy/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"bufio"
"bytes"
"context"
"encoding/base64"
"io"
Expand Down Expand Up @@ -83,14 +84,28 @@ type readerOnly struct {
}

func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
return s.ProcessWithFirstbyte(ctx, network, conn, dispatcher)
}

// Firstbyte is for forwarded conn from SOCKS inbound
// Because it needs first byte to choose protocol
// We need to add it back
// Other parts are the same as the process function
func (s *Server) ProcessWithFirstbyte(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher, firstbyte ...byte) error {
inbound := session.InboundFromContext(ctx)
inbound.Name = "http"
inbound.CanSpliceCopy = 2
inbound.User = &protocol.MemoryUser{
Level: s.config.UserLevel,
}

reader := bufio.NewReaderSize(readerOnly{conn}, buf.Size)
var reader *bufio.Reader
if len(firstbyte) > 0 {
readerWithoutFirstbyte := bufio.NewReaderSize(readerOnly{conn}, buf.Size)
multiReader := io.MultiReader(bytes.NewReader(firstbyte), readerWithoutFirstbyte)
reader = bufio.NewReaderSize(multiReader, buf.Size)
} else {
reader = bufio.NewReaderSize(readerOnly{conn}, buf.Size)
}

Start:
if err := conn.SetReadDeadline(time.Now().Add(s.policy().Timeouts.Handshake)); err != nil {
Expand Down
33 changes: 26 additions & 7 deletions proxy/socks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/xtls/xray-core/features"
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/features/routing"
"github.com/xtls/xray-core/proxy/http"
"github.com/xtls/xray-core/transport/internet/stat"
"github.com/xtls/xray-core/transport/internet/udp"
)
Expand All @@ -29,6 +30,7 @@ type Server struct {
policyManager policy.Manager
cone bool
udpFilter *UDPFilter
httpServer *http.Server
}

// NewServer creates a new Server object.
Expand All @@ -39,9 +41,14 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
cone: ctx.Value("cone").(bool),
}
httpConfig := &http.ServerConfig{
UserLevel: config.UserLevel,
}
if config.AuthType == AuthType_PASSWORD {
httpConfig.Accounts = config.Accounts
s.udpFilter = new(UDPFilter) // We only use this when auth is enabled
}
s.httpServer, _ = http.NewServer(ctx, httpConfig)
return s, nil
}

Expand Down Expand Up @@ -77,15 +84,21 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con

switch network {
case net.Network_TCP:
return s.processTCP(ctx, conn, dispatcher)
firstbyte := make([]byte, 1)
conn.Read(firstbyte)
if firstbyte[0] != 5 && firstbyte[0] != 4 { // Check if it is Socks5/4/4a
errors.LogDebug(ctx, "Not Socks request, try to parse as HTTP request")
return s.httpServer.ProcessWithFirstbyte(ctx, network, conn, dispatcher, firstbyte...)
}
return s.processTCP(ctx, conn, dispatcher, firstbyte)
case net.Network_UDP:
return s.handleUDPPayload(ctx, conn, dispatcher)
default:
return errors.New("unknown network: ", network)
}
}

func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher) error {
func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatcher routing.Dispatcher, firstbyte []byte) error {
plcy := s.policy()
if err := conn.SetReadDeadline(time.Now().Add(plcy.Timeouts.Handshake)); err != nil {
errors.LogInfoInner(ctx, err, "failed to set deadline")
Expand All @@ -103,7 +116,13 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche
localAddress: net.IPAddress(conn.LocalAddr().(*net.TCPAddr).IP),
}

reader := &buf.BufferedReader{Reader: buf.NewReader(conn)}
// Firstbyte is for forwarded conn from SOCKS inbound
// Because it needs first byte to choose protocol
// We need to add it back
reader := &buf.BufferedReader{
Reader: buf.NewReader(conn),
Buffer: buf.MultiBuffer{buf.FromBytes(firstbyte)},
}
request, err := svrSession.Handshake(reader, conn)
if err != nil {
if inbound.Source.IsValid() {
Expand Down Expand Up @@ -136,7 +155,7 @@ func (s *Server) processTCP(ctx context.Context, conn stat.Connection, dispatche
})
}

return s.transport(ctx, reader, conn, dest, dispatcher, inbound)
return s.transport(ctx, conn, dest, dispatcher, inbound)
}

if request.Command == protocol.RequestCommandUDP {
Expand All @@ -155,7 +174,7 @@ func (*Server) handleUDP(c io.Reader) error {
return common.Error2(io.Copy(buf.DiscardBytes, c))
}

func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writer, dest net.Destination, dispatcher routing.Dispatcher, inbound *session.Inbound) error {
func (s *Server) transport(ctx context.Context, conn stat.Connection, dest net.Destination, dispatcher routing.Dispatcher, inbound *session.Inbound) error {
ctx, cancel := context.WithCancel(ctx)
timer := signal.CancelAfterInactivity(ctx, cancel, s.policy().Timeouts.ConnectionIdle)

Expand All @@ -172,7 +191,7 @@ func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ

requestDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.DownlinkOnly)
if err := buf.Copy(buf.NewReader(reader), link.Writer, buf.UpdateActivity(timer)); err != nil {
if err := buf.Copy(buf.NewReader(conn), link.Writer, buf.UpdateActivity(timer)); err != nil {
return errors.New("failed to transport all TCP request").Base(err)
}

Expand All @@ -182,7 +201,7 @@ func (s *Server) transport(ctx context.Context, reader io.Reader, writer io.Writ
responseDone := func() error {
defer timer.SetTimeout(plcy.Timeouts.UplinkOnly)

v2writer := buf.NewWriter(writer)
v2writer := buf.NewWriter(conn)
if err := buf.Copy(link.Reader, v2writer, buf.UpdateActivity(timer)); err != nil {
return errors.New("failed to transport all TCP response").Base(err)
}
Expand Down
82 changes: 82 additions & 0 deletions testing/scenarios/socks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/xtls/xray-core/proxy/blackhole"
"github.com/xtls/xray-core/proxy/dokodemo"
"github.com/xtls/xray-core/proxy/freedom"
"github.com/xtls/xray-core/proxy/http"
"github.com/xtls/xray-core/proxy/socks"
"github.com/xtls/xray-core/testing/servers/tcp"
"github.com/xtls/xray-core/testing/servers/udp"
Expand Down Expand Up @@ -102,6 +103,87 @@ func TestSocksBridgeTCP(t *testing.T) {
}
}

func TestSocksWithHttpRequest(t *testing.T) {
tcpServer := tcp.Server{
MsgProcessor: xor,
}
dest, err := tcpServer.Start()
common.Must(err)
defer tcpServer.Close()

serverPort := tcp.PickPort()
serverConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(serverPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&socks.ServerConfig{
AuthType: socks.AuthType_PASSWORD,
Accounts: map[string]string{
"Test Account": "Test Password",
},
Address: net.NewIPOrDomain(net.LocalHostIP),
UdpEnabled: false,
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&freedom.Config{}),
},
},
}

clientPort := tcp.PickPort()
clientConfig := &core.Config{
Inbound: []*core.InboundHandlerConfig{
{
ReceiverSettings: serial.ToTypedMessage(&proxyman.ReceiverConfig{
PortList: &net.PortList{Range: []*net.PortRange{net.SinglePortRange(clientPort)}},
Listen: net.NewIPOrDomain(net.LocalHostIP),
}),
ProxySettings: serial.ToTypedMessage(&dokodemo.Config{
Address: net.NewIPOrDomain(dest.Address),
Port: uint32(dest.Port),
NetworkList: &net.NetworkList{
Network: []net.Network{net.Network_TCP},
},
}),
},
},
Outbound: []*core.OutboundHandlerConfig{
{
ProxySettings: serial.ToTypedMessage(&http.ClientConfig{
Server: []*protocol.ServerEndpoint{
{
Address: net.NewIPOrDomain(net.LocalHostIP),
Port: uint32(serverPort),
User: []*protocol.User{
{
Account: serial.ToTypedMessage(&http.Account{
Username: "Test Account",
Password: "Test Password",
}),
},
},
},
},
}),
},
},
}

servers, err := InitializeServerConfigs(serverConfig, clientConfig)
common.Must(err)
defer CloseAllServers(servers)

if err := testTCPConn(clientPort, 1024, time.Second*2)(); err != nil {
t.Error(err)
}
}

func TestSocksBridageUDP(t *testing.T) {
udpServer := udp.Server{
MsgProcessor: xor,
Expand Down