direct.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package outbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/dialer"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. M "github.com/sagernet/sing/common/metadata"
  11. N "github.com/sagernet/sing/common/network"
  12. )
  13. var _ adapter.Outbound = (*Direct)(nil)
  14. type Direct struct {
  15. myOutboundAdapter
  16. dialer N.Dialer
  17. overrideOption int
  18. overrideDestination M.Socksaddr
  19. }
  20. func NewDirect(router adapter.Router, logger log.Logger, tag string, options option.DirectOutboundOptions) *Direct {
  21. outbound := &Direct{
  22. myOutboundAdapter: myOutboundAdapter{
  23. protocol: C.TypeDirect,
  24. logger: logger,
  25. tag: tag,
  26. network: []string{C.NetworkTCP, C.NetworkUDP},
  27. },
  28. dialer: dialer.NewOutbound(router, options.OutboundDialerOptions),
  29. }
  30. if options.OverrideAddress != "" && options.OverridePort != 0 {
  31. outbound.overrideOption = 1
  32. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  33. } else if options.OverrideAddress != "" {
  34. outbound.overrideOption = 2
  35. outbound.overrideDestination = M.ParseSocksaddrHostPort(options.OverrideAddress, options.OverridePort)
  36. } else if options.OverridePort != 0 {
  37. outbound.overrideOption = 3
  38. outbound.overrideDestination = M.Socksaddr{Port: options.OverridePort}
  39. }
  40. return outbound
  41. }
  42. func (h *Direct) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  43. ctx, metadata := adapter.AppendContext(ctx)
  44. metadata.Outbound = h.tag
  45. metadata.Destination = destination
  46. switch h.overrideOption {
  47. case 1:
  48. destination = h.overrideDestination
  49. case 2:
  50. newDestination := h.overrideDestination
  51. newDestination.Port = destination.Port
  52. destination = newDestination
  53. case 3:
  54. destination.Port = h.overrideDestination.Port
  55. }
  56. switch network {
  57. case C.NetworkTCP:
  58. h.logger.WithContext(ctx).Info("outbound connection to ", destination)
  59. case C.NetworkUDP:
  60. h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
  61. }
  62. return h.dialer.DialContext(ctx, network, destination)
  63. }
  64. func (h *Direct) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  65. ctx, metadata := adapter.AppendContext(ctx)
  66. metadata.Outbound = h.tag
  67. metadata.Destination = destination
  68. h.logger.WithContext(ctx).Info("outbound packet connection")
  69. return h.dialer.ListenPacket(ctx, destination)
  70. }
  71. func (h *Direct) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  72. return NewConnection(ctx, h, conn, metadata)
  73. }
  74. func (h *Direct) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  75. return NewPacketConnection(ctx, h, conn, metadata)
  76. }