Add socks4/4a support

This commit is contained in:
世界 2021-09-16 14:42:12 +08:00
parent 238bd5d050
commit 77d0419aca
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 154 additions and 33 deletions

View file

@ -15,6 +15,7 @@ import (
"github.com/xtls/xray-core/common/signal"
"github.com/xtls/xray-core/common/task"
"github.com/xtls/xray-core/core"
"github.com/xtls/xray-core/features/dns"
"github.com/xtls/xray-core/features/policy"
"github.com/xtls/xray-core/transport"
"github.com/xtls/xray-core/transport/internet"
@ -24,6 +25,8 @@ import (
type Client struct {
serverPicker protocol.ServerPicker
policyManager policy.Manager
version Version
dns dns.Client
}
// NewClient create a new Socks5 client based on the given config.
@ -41,10 +44,16 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
}
v := core.MustFromContext(ctx)
return &Client{
c := &Client{
serverPicker: protocol.NewRoundRobinServerPicker(serverList),
policyManager: v.GetFeature(policy.ManagerType()).(policy.Manager),
}, nil
version: config.Version,
}
if config.Version == Version_SOCKS4 {
c.dns = v.GetFeature(dns.ClientType()).(dns.Client)
}
return c, nil
}
// Process implements proxy.Outbound.Process.
@ -91,6 +100,31 @@ func (c *Client) Process(ctx context.Context, link *transport.Link, dialer inter
Address: destination.Address,
Port: destination.Port,
}
switch c.version {
case Version_SOCKS4:
if request.Address.Family().IsDomain() {
ips, err := c.dns.LookupIP(request.Address.Domain(), dns.IPOption{
IPv4Enable: true,
})
if err != nil {
return err
} else if len(ips) == 0 {
return dns.ErrEmptyResponse
}
request.Address = net.IPAddress(ips[0])
}
fallthrough
case Version_SOCKS4A:
request.Version = socks4Version
if destination.Network == net.Network_UDP {
return newError("udp is not supported in socks4")
} else if destination.Address.Family().IsIPv6() {
return newError("ipv6 is not supported in socks4")
}
}
if destination.Network == net.Network_UDP {
request.Command = protocol.RequestCommandUDP
}