Refactor adapter
This commit is contained in:
138
adapter/outbound/default.go
Normal file
138
adapter/outbound/default.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"runtime"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/database64128/tfo-go"
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/config"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
"github.com/sagernet/sing/common/control"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
type myOutboundAdapter struct {
|
||||
protocol string
|
||||
logger log.Logger
|
||||
tag string
|
||||
dialer N.Dialer
|
||||
}
|
||||
|
||||
func (a *myOutboundAdapter) Type() string {
|
||||
return a.protocol
|
||||
}
|
||||
|
||||
func (a *myOutboundAdapter) Tag() string {
|
||||
return a.tag
|
||||
}
|
||||
|
||||
type defaultDialer struct {
|
||||
tfo.Dialer
|
||||
net.ListenConfig
|
||||
}
|
||||
|
||||
func (d *defaultDialer) DialContext(ctx context.Context, network string, address M.Socksaddr) (net.Conn, error) {
|
||||
return d.Dialer.DialContext(ctx, network, address.String())
|
||||
}
|
||||
|
||||
func (d *defaultDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
return d.ListenConfig.ListenPacket(ctx, "udp", "")
|
||||
}
|
||||
|
||||
func newDialer(options config.DialerOptions) N.Dialer {
|
||||
var dialer net.Dialer
|
||||
var listener net.ListenConfig
|
||||
if options.BindInterface != "" {
|
||||
dialer.Control = control.Append(dialer.Control, control.BindToInterface(options.BindInterface))
|
||||
listener.Control = control.Append(listener.Control, control.BindToInterface(options.BindInterface))
|
||||
}
|
||||
if options.RoutingMark != 0 {
|
||||
dialer.Control = control.Append(dialer.Control, control.RoutingMark(options.RoutingMark))
|
||||
listener.Control = control.Append(listener.Control, control.RoutingMark(options.RoutingMark))
|
||||
}
|
||||
if options.ReuseAddr {
|
||||
listener.Control = control.Append(listener.Control, control.ReuseAddr())
|
||||
}
|
||||
if options.ConnectTimeout != 0 {
|
||||
dialer.Timeout = time.Duration(options.ConnectTimeout) * time.Second
|
||||
}
|
||||
return &defaultDialer{tfo.Dialer{Dialer: dialer, DisableTFO: !options.TCPFastOpen}, listener}
|
||||
}
|
||||
|
||||
type lazyDialer struct {
|
||||
router adapter.Router
|
||||
options config.DialerOptions
|
||||
dialer N.Dialer
|
||||
initOnce sync.Once
|
||||
initErr error
|
||||
}
|
||||
|
||||
func NewDialer(router adapter.Router, options config.DialerOptions) N.Dialer {
|
||||
if options.Detour == "" {
|
||||
return newDialer(options)
|
||||
}
|
||||
return &lazyDialer{
|
||||
router: router,
|
||||
options: options,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *lazyDialer) Dialer() (N.Dialer, error) {
|
||||
d.initOnce.Do(func() {
|
||||
var loaded bool
|
||||
d.dialer, loaded = d.router.Outbound(d.options.Detour)
|
||||
if !loaded {
|
||||
d.initErr = E.New("outbound detour not found: ", d.options.Detour)
|
||||
}
|
||||
})
|
||||
return d.dialer, d.initErr
|
||||
}
|
||||
|
||||
func (d *lazyDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
dialer, err := d.Dialer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dialer.DialContext(ctx, network, destination)
|
||||
}
|
||||
|
||||
func (d *lazyDialer) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
dialer, err := d.Dialer()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return dialer.ListenPacket(ctx)
|
||||
}
|
||||
|
||||
func CopyEarlyConn(ctx context.Context, conn net.Conn, serverConn net.Conn) error {
|
||||
_payload := buf.StackNew()
|
||||
payload := common.Dup(_payload)
|
||||
err := conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = payload.ReadFrom(conn)
|
||||
if err != nil && !E.IsTimeout(err) {
|
||||
return E.Cause(err, "read payload")
|
||||
}
|
||||
err = conn.SetReadDeadline(time.Time{})
|
||||
if err != nil {
|
||||
payload.Release()
|
||||
return err
|
||||
}
|
||||
_, err = serverConn.Write(payload.Bytes())
|
||||
if err != nil {
|
||||
return E.Cause(err, "client handshake")
|
||||
}
|
||||
runtime.KeepAlive(_payload)
|
||||
return bufio.CopyConn(ctx, conn, serverConn)
|
||||
}
|
||||
85
adapter/outbound/direct.go
Normal file
85
adapter/outbound/direct.go
Normal file
@@ -0,0 +1,85 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/config"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
var _ adapter.Outbound = (*Direct)(nil)
|
||||
|
||||
type Direct struct {
|
||||
myOutboundAdapter
|
||||
overrideOption int
|
||||
overrideDestination M.Socksaddr
|
||||
}
|
||||
|
||||
func NewDirect(router adapter.Router, logger log.Logger, tag string, options *config.DirectOutboundOptions) *Direct {
|
||||
outbound := &Direct{
|
||||
myOutboundAdapter: myOutboundAdapter{
|
||||
protocol: C.TypeDirect,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
dialer: NewDialer(router, options.DialerOptions),
|
||||
},
|
||||
}
|
||||
if options.OverrideAddress != "" && options.OverridePort != 0 {
|
||||
outbound.overrideOption = 1
|
||||
outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
|
||||
} else if options.OverrideAddress != "" {
|
||||
outbound.overrideOption = 2
|
||||
outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
|
||||
} else if options.OverridePort != 0 {
|
||||
outbound.overrideOption = 3
|
||||
outbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
|
||||
}
|
||||
return outbound
|
||||
}
|
||||
|
||||
func (d *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
|
||||
switch d.overrideOption {
|
||||
case 0:
|
||||
destination = d.overrideDestination
|
||||
case 1:
|
||||
newDestination := d.overrideDestination
|
||||
newDestination.Port = destination.Port
|
||||
destination = newDestination
|
||||
case 2:
|
||||
destination.Port = d.overrideDestination.Port
|
||||
}
|
||||
switch network {
|
||||
case C.NetworkTCP:
|
||||
d.logger.WithContext(ctx).Debug("outbound connection to ", destination)
|
||||
case C.NetworkUDP:
|
||||
d.logger.WithContext(ctx).Debug("outbound packet connection to ", destination)
|
||||
}
|
||||
return d.dialer.DialContext(ctx, network, destination)
|
||||
}
|
||||
|
||||
func (d *Direct) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
d.logger.WithContext(ctx).Debug("outbound packet connection")
|
||||
return d.dialer.ListenPacket(ctx)
|
||||
}
|
||||
|
||||
func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
|
||||
outConn, err := d.DialContext(ctx, "tcp", destination)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bufio.CopyConn(ctx, conn, outConn)
|
||||
}
|
||||
|
||||
func (d *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
|
||||
outConn, err := d.ListenPacket(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
|
||||
}
|
||||
94
adapter/outbound/shadowsocks.go
Normal file
94
adapter/outbound/shadowsocks.go
Normal file
@@ -0,0 +1,94 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/config"
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-shadowsocks"
|
||||
"github.com/sagernet/sing-shadowsocks/shadowimpl"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
E "github.com/sagernet/sing/common/exceptions"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
)
|
||||
|
||||
var _ adapter.Outbound = (*Shadowsocks)(nil)
|
||||
|
||||
type Shadowsocks struct {
|
||||
myOutboundAdapter
|
||||
method shadowsocks.Method
|
||||
serverAddr M.Socksaddr
|
||||
}
|
||||
|
||||
func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, options *config.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
|
||||
outbound := &Shadowsocks{
|
||||
myOutboundAdapter: myOutboundAdapter{
|
||||
protocol: C.TypeDirect,
|
||||
logger: logger,
|
||||
tag: tag,
|
||||
dialer: NewDialer(router, options.DialerOptions),
|
||||
},
|
||||
}
|
||||
var err error
|
||||
outbound.method, err = shadowimpl.FetchMethod(options.Method, options.Password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if options.Server == "" {
|
||||
return nil, E.New("missing server address")
|
||||
} else if options.ServerPort == 0 {
|
||||
return nil, E.New("missing server port")
|
||||
}
|
||||
outbound.serverAddr = M.ParseSocksaddrHostPort(options.Server, options.ServerPort)
|
||||
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:
|
||||
o.logger.WithContext(ctx).Debug("outbound connection to ", destination)
|
||||
outConn, err := o.dialer.DialContext(ctx, "tcp", o.serverAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return o.method.DialEarlyConn(outConn, destination), nil
|
||||
case C.NetworkUDP:
|
||||
o.logger.WithContext(ctx).Debug("outbound packet connection to ", destination)
|
||||
outConn, err := o.dialer.DialContext(ctx, "udp", o.serverAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &bufio.BindPacketConn{PacketConn: o.method.DialPacketConn(outConn), Addr: destination}, nil
|
||||
default:
|
||||
panic("unknown network " + network)
|
||||
}
|
||||
}
|
||||
|
||||
func (o *Shadowsocks) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
o.logger.WithContext(ctx).Debug("outbound packet connection to ", o.serverAddr)
|
||||
outConn, err := o.dialer.ListenPacket(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return o.method.DialPacketConn(&bufio.BindPacketConn{PacketConn: outConn, Addr: o.serverAddr.UDPAddr()}), nil
|
||||
}
|
||||
Reference in New Issue
Block a user