sing-box/adapter/outbound/direct.go

86 lines
2.6 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package outbound
2022-06-30 21:27:56 +08:00
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
2022-06-30 21:27:56 +08:00
"github.com/sagernet/sing/common/bufio"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
)
2022-07-01 19:34:02 +08:00
var _ adapter.Outbound = (*Direct)(nil)
2022-06-30 21:27:56 +08:00
2022-07-01 19:34:02 +08:00
type Direct struct {
myOutboundAdapter
2022-06-30 21:27:56 +08:00
overrideOption int
overrideDestination M.Socksaddr
}
2022-07-02 14:07:50 +08:00
func NewDirect(router adapter.Router, logger log.Logger, tag string, options *option.DirectOutboundOptions) *Direct {
2022-07-01 19:34:02 +08:00
outbound := &Direct{
myOutboundAdapter: myOutboundAdapter{
protocol: C.TypeDirect,
logger: logger,
tag: tag,
dialer: NewDialer(router, options.DialerOptions),
},
2022-06-30 21:27:56 +08:00
}
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}
}
2022-07-01 19:34:02 +08:00
return outbound
2022-06-30 21:27:56 +08:00
}
2022-07-01 19:34:02 +08:00
func (d *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
2022-06-30 21:27:56 +08:00
switch d.overrideOption {
case 1:
2022-07-02 14:07:50 +08:00
destination = d.overrideDestination
case 2:
2022-06-30 21:27:56 +08:00
newDestination := d.overrideDestination
newDestination.Port = destination.Port
destination = newDestination
2022-07-02 14:07:50 +08:00
case 3:
2022-06-30 21:27:56 +08:00
destination.Port = d.overrideDestination.Port
}
switch network {
case C.NetworkTCP:
2022-07-02 14:07:50 +08:00
d.logger.WithContext(ctx).Info("outbound connection to ", destination)
2022-06-30 21:27:56 +08:00
case C.NetworkUDP:
2022-07-02 14:07:50 +08:00
d.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
2022-06-30 21:27:56 +08:00
}
return d.dialer.DialContext(ctx, network, destination)
}
2022-07-01 19:34:02 +08:00
func (d *Direct) ListenPacket(ctx context.Context) (net.PacketConn, error) {
2022-07-02 14:07:50 +08:00
d.logger.WithContext(ctx).Info("outbound packet connection")
2022-06-30 21:27:56 +08:00
return d.dialer.ListenPacket(ctx)
}
2022-07-01 19:34:02 +08:00
func (d *Direct) NewConnection(ctx context.Context, conn net.Conn, destination M.Socksaddr) error {
2022-06-30 21:27:56 +08:00
outConn, err := d.DialContext(ctx, "tcp", destination)
if err != nil {
return err
}
return bufio.CopyConn(ctx, conn, outConn)
}
2022-07-01 19:34:02 +08:00
func (d *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, destination M.Socksaddr) error {
2022-06-30 21:27:56 +08:00
outConn, err := d.ListenPacket(ctx)
if err != nil {
return err
}
return bufio.CopyPacketConn(ctx, conn, bufio.NewPacketConn(outConn))
}