Inbound rule support
This commit is contained in:
27
adapter/outbound/builder.go
Normal file
27
adapter/outbound/builder.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package outbound
|
||||
|
||||
import (
|
||||
"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"
|
||||
F "github.com/sagernet/sing/common/format"
|
||||
)
|
||||
|
||||
func New(router adapter.Router, logger log.Logger, index int, options option.Outbound) (adapter.Outbound, error) {
|
||||
var tag string
|
||||
if options.Tag != "" {
|
||||
tag = options.Tag
|
||||
} else {
|
||||
tag = F.ToString(index)
|
||||
}
|
||||
outboundLogger := logger.WithPrefix(F.ToString("outbound/", options.Type, "[", tag, "]: "))
|
||||
switch options.Type {
|
||||
case C.TypeDirect:
|
||||
return NewDirect(router, outboundLogger, options.Tag, options.DirectOptions), nil
|
||||
case C.TypeShadowsocks:
|
||||
return NewShadowsocks(router, outboundLogger, options.Tag, options.ShadowsocksOptions)
|
||||
default:
|
||||
panic(F.ToString("unknown outbound type: ", options.Type))
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,8 @@ import (
|
||||
|
||||
"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-box/option"
|
||||
"github.com/sagernet/sing/common"
|
||||
"github.com/sagernet/sing/common/buf"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
@@ -48,7 +48,7 @@ func (d *defaultDialer) ListenPacket(ctx context.Context) (net.PacketConn, error
|
||||
return d.ListenConfig.ListenPacket(ctx, "udp", "")
|
||||
}
|
||||
|
||||
func newDialer(options config.DialerOptions) N.Dialer {
|
||||
func newDialer(options option.DialerOptions) N.Dialer {
|
||||
var dialer net.Dialer
|
||||
var listener net.ListenConfig
|
||||
if options.BindInterface != "" {
|
||||
@@ -70,13 +70,13 @@ func newDialer(options config.DialerOptions) N.Dialer {
|
||||
|
||||
type lazyDialer struct {
|
||||
router adapter.Router
|
||||
options config.DialerOptions
|
||||
options option.DialerOptions
|
||||
dialer N.Dialer
|
||||
initOnce sync.Once
|
||||
initErr error
|
||||
}
|
||||
|
||||
func NewDialer(router adapter.Router, options config.DialerOptions) N.Dialer {
|
||||
func NewDialer(router adapter.Router, options option.DialerOptions) N.Dialer {
|
||||
if options.Detour == "" {
|
||||
return newDialer(options)
|
||||
}
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"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-box/option"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
M "github.com/sagernet/sing/common/metadata"
|
||||
N "github.com/sagernet/sing/common/network"
|
||||
@@ -21,7 +21,7 @@ type Direct struct {
|
||||
overrideDestination M.Socksaddr
|
||||
}
|
||||
|
||||
func NewDirect(router adapter.Router, logger log.Logger, tag string, options *config.DirectOutboundOptions) *Direct {
|
||||
func NewDirect(router adapter.Router, logger log.Logger, tag string, options *option.DirectOutboundOptions) *Direct {
|
||||
outbound := &Direct{
|
||||
myOutboundAdapter: myOutboundAdapter{
|
||||
protocol: C.TypeDirect,
|
||||
@@ -45,26 +45,26 @@ func NewDirect(router adapter.Router, logger log.Logger, tag string, options *co
|
||||
|
||||
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:
|
||||
destination = d.overrideDestination
|
||||
case 2:
|
||||
newDestination := d.overrideDestination
|
||||
newDestination.Port = destination.Port
|
||||
destination = newDestination
|
||||
case 2:
|
||||
case 3:
|
||||
destination.Port = d.overrideDestination.Port
|
||||
}
|
||||
switch network {
|
||||
case C.NetworkTCP:
|
||||
d.logger.WithContext(ctx).Debug("outbound connection to ", destination)
|
||||
d.logger.WithContext(ctx).Info("outbound connection to ", destination)
|
||||
case C.NetworkUDP:
|
||||
d.logger.WithContext(ctx).Debug("outbound packet connection to ", destination)
|
||||
d.logger.WithContext(ctx).Info("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")
|
||||
d.logger.WithContext(ctx).Info("outbound packet connection")
|
||||
return d.dialer.ListenPacket(ctx)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,9 +5,9 @@ import (
|
||||
"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-box/option"
|
||||
"github.com/sagernet/sing-shadowsocks"
|
||||
"github.com/sagernet/sing-shadowsocks/shadowimpl"
|
||||
"github.com/sagernet/sing/common/bufio"
|
||||
@@ -24,7 +24,7 @@ type Shadowsocks struct {
|
||||
serverAddr M.Socksaddr
|
||||
}
|
||||
|
||||
func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, options *config.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
|
||||
func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, options *option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
|
||||
outbound := &Shadowsocks{
|
||||
myOutboundAdapter: myOutboundAdapter{
|
||||
protocol: C.TypeDirect,
|
||||
@@ -66,14 +66,14 @@ func (o *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn
|
||||
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)
|
||||
o.logger.WithContext(ctx).Info("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)
|
||||
o.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
|
||||
outConn, err := o.dialer.DialContext(ctx, "udp", o.serverAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -85,7 +85,7 @@ func (o *Shadowsocks) DialContext(ctx context.Context, network string, destinati
|
||||
}
|
||||
|
||||
func (o *Shadowsocks) ListenPacket(ctx context.Context) (net.PacketConn, error) {
|
||||
o.logger.WithContext(ctx).Debug("outbound packet connection to ", o.serverAddr)
|
||||
o.logger.WithContext(ctx).Info("outbound packet connection to ", o.serverAddr)
|
||||
outConn, err := o.dialer.ListenPacket(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in New Issue
Block a user