sing-box/outbound/direct.go

92 lines
2.9 KiB
Go
Raw Normal View History

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