Add socks outbound

This commit is contained in:
世界
2022-07-03 13:14:49 +08:00
parent ef5cfd59d4
commit 70c0812606
10 changed files with 122 additions and 28 deletions

View File

@@ -24,6 +24,8 @@ func New(router adapter.Router, logger log.Logger, index int, options option.Out
switch options.Type {
case C.TypeDirect:
return NewDirect(router, outboundLogger, options.Tag, options.DirectOptions), nil
case C.TypeSocks:
return NewSocks(router, outboundLogger, options.Tag, options.SocksOptions)
case C.TypeShadowsocks:
return NewShadowsocks(router, outboundLogger, options.Tag, options.ShadowsocksOptions)
default:

View File

@@ -44,7 +44,7 @@ func (d *defaultDialer) DialContext(ctx context.Context, network string, address
return d.Dialer.DialContext(ctx, network, address.String())
}
func (d *defaultDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) {
func (d *defaultDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
return d.ListenConfig.ListenPacket(ctx, "udp", "")
}
@@ -105,12 +105,12 @@ func (d *lazyDialer) DialContext(ctx context.Context, network string, destinatio
return dialer.DialContext(ctx, network, destination)
}
func (d *lazyDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) {
func (d *lazyDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
dialer, err := d.Dialer()
if err != nil {
return nil, err
}
return dialer.ListenPacket(ctx)
return dialer.ListenPacket(ctx, destination)
}
func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error {

View File

@@ -63,9 +63,9 @@ func (d *Direct) DialContext(ctx context.Context, network string, destination M.
return d.dialer.DialContext(ctx, network, destination)
}
func (d *Direct) ListenPacket(ctx context.Context) (net.PacketConn, error) {
func (d *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
d.logger.WithContext(ctx).Info("outbound packet connection")
return d.dialer.ListenPacket(ctx)
return d.dialer.ListenPacket(ctx, destination)
}
func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
@@ -77,7 +77,7 @@ func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M
}
func (d *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
outConn, err := d.ListenPacket(ctx)
outConn, err := d.ListenPacket(ctx, destination)
if err != nil {
return err
}

View File

@@ -47,22 +47,6 @@ func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, option
return outbound, nil
}
func (o *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
serverConn, err := o.DialContext(ctx, "tcp", destination)
if err != nil {
return err
}
return CopyEarlyConn(ctx, conn, serverConn)
}
func (o *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
serverConn, err := o.ListenPacket(ctx)
if err != nil {
return err
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(serverConn))
}
func (o *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
switch network {
case C.NetworkTCP:
@@ -84,11 +68,27 @@ func (o *Shadowsocks) DialContext(ctx context.Context, network string, destinati
}
}
func (o *Shadowsocks) ListenPacket(ctx context.Context) (net.PacketConn, error) {
func (o *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
o.logger.WithContext(ctx).Info("outbound packet connection to ", o.serverAddr)
outConn, err := o.dialer.ListenPacket(ctx)
outConn, err := o.dialer.ListenPacket(ctx, destination)
if err != nil {
return nil, err
}
return o.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: outConn, Addr: o.serverAddr.UDPAddr()}), nil
}
func (o *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
serverConn, err := o.DialContext(ctx, "tcp", destination)
if err != nil {
return err
}
return CopyEarlyConn(ctx, conn, serverConn)
}
func (o *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
serverConn, err := o.ListenPacket(ctx, destination)
if err != nil {
return err
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(serverConn))
}

78
adapter/outbound/socks.go Normal file
View File

@@ -0,0 +1,78 @@
package outbound
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/protocol/socks"
)
var _ adapter.Outbound = (*Socks)(nil)
type Socks struct {
myOutboundAdapter
client *socks.Client
}
func NewSocks(router adapter.Router, logger log.Logger, tag string, options option.SocksOutboundOptions) (*Socks, error) {
dialer := NewDialer(router, options.DialerOptions)
var version socks.Version
var err error
if options.Version != "" {
version, err = socks.ParseVersion(options.Version)
} else {
version = socks.Version5
}
if err != nil {
return nil, err
}
return &Socks{
myOutboundAdapter{
protocol: C.TypeSocks,
logger: logger,
tag: tag,
dialer: dialer,
},
socks.NewClient(dialer, M.ParseSocksaddrHostPort(options.Server, options.ServerPort), version, options.Username, options.Password),
}, nil
}
func (h *Socks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
switch network {
case C.NetworkTCP:
h.logger.WithContext(ctx).Info("outbound connection to ", destination)
case C.NetworkUDP:
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
default:
panic("unknown network " + network)
}
return h.client.DialContext(ctx, network, destination)
}
func (h *Socks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
return h.client.ListenPacket(ctx, destination)
}
func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
outConn, err := h.DialContext(ctx, "tcp", destination)
if err != nil {
return err
}
return bufio.CopyConn(ctx, conn, outConn)
}
func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
outConn, err := h.ListenPacket(ctx, destination)
if err != nil {
return err
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
}