outbound.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package socks
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/adapter/outbound"
  7. "github.com/sagernet/sing-box/common/dialer"
  8. C "github.com/sagernet/sing-box/constant"
  9. "github.com/sagernet/sing-box/log"
  10. "github.com/sagernet/sing-box/option"
  11. "github.com/sagernet/sing-dns"
  12. "github.com/sagernet/sing/common"
  13. E "github.com/sagernet/sing/common/exceptions"
  14. "github.com/sagernet/sing/common/logger"
  15. M "github.com/sagernet/sing/common/metadata"
  16. N "github.com/sagernet/sing/common/network"
  17. "github.com/sagernet/sing/common/uot"
  18. "github.com/sagernet/sing/protocol/socks"
  19. )
  20. func RegisterOutbound(registry *outbound.Registry) {
  21. outbound.Register[option.SOCKSOutboundOptions](registry, C.TypeSOCKS, NewOutbound)
  22. }
  23. var _ adapter.Outbound = (*Outbound)(nil)
  24. type Outbound struct {
  25. outbound.Adapter
  26. router adapter.Router
  27. logger logger.ContextLogger
  28. client *socks.Client
  29. resolve bool
  30. uotClient *uot.Client
  31. }
  32. func NewOutbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SOCKSOutboundOptions) (adapter.Outbound, error) {
  33. var version socks.Version
  34. var err error
  35. if options.Version != "" {
  36. version, err = socks.ParseVersion(options.Version)
  37. } else {
  38. version = socks.Version5
  39. }
  40. if err != nil {
  41. return nil, err
  42. }
  43. outboundDialer, err := dialer.New(router, options.DialerOptions)
  44. if err != nil {
  45. return nil, err
  46. }
  47. outbound := &Outbound{
  48. Adapter: outbound.NewAdapterWithDialerOptions(C.TypeSOCKS, options.Network.Build(), tag, options.DialerOptions),
  49. router: router,
  50. logger: logger,
  51. client: socks.NewClient(outboundDialer, options.ServerOptions.Build(), version, options.Username, options.Password),
  52. resolve: version == socks.Version4,
  53. }
  54. uotOptions := common.PtrValueOrDefault(options.UDPOverTCP)
  55. if uotOptions.Enabled {
  56. outbound.uotClient = &uot.Client{
  57. Dialer: outbound.client,
  58. Version: uotOptions.Version,
  59. }
  60. }
  61. return outbound, nil
  62. }
  63. func (h *Outbound) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  64. ctx, metadata := adapter.ExtendContext(ctx)
  65. metadata.Outbound = h.Tag()
  66. metadata.Destination = destination
  67. switch N.NetworkName(network) {
  68. case N.NetworkTCP:
  69. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  70. case N.NetworkUDP:
  71. if h.uotClient != nil {
  72. h.logger.InfoContext(ctx, "outbound UoT connect packet connection to ", destination)
  73. return h.uotClient.DialContext(ctx, network, destination)
  74. }
  75. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  76. default:
  77. return nil, E.Extend(N.ErrUnknownNetwork, network)
  78. }
  79. if h.resolve && destination.IsFqdn() {
  80. destinationAddresses, err := h.router.LookupDefault(ctx, destination.Fqdn)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return N.DialSerial(ctx, h.client, network, destination, destinationAddresses)
  85. }
  86. return h.client.DialContext(ctx, network, destination)
  87. }
  88. func (h *Outbound) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  89. ctx, metadata := adapter.ExtendContext(ctx)
  90. metadata.Outbound = h.Tag()
  91. metadata.Destination = destination
  92. if h.uotClient != nil {
  93. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  94. return h.uotClient.ListenPacket(ctx, destination)
  95. }
  96. if h.resolve && destination.IsFqdn() {
  97. destinationAddresses, err := h.router.LookupDefault(ctx, destination.Fqdn)
  98. if err != nil {
  99. return nil, err
  100. }
  101. packetConn, _, err := N.ListenSerial(ctx, h.client, destination, destinationAddresses)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return packetConn, nil
  106. }
  107. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  108. return h.client.ListenPacket(ctx, destination)
  109. }
  110. // TODO
  111. // Deprecated
  112. func (h *Outbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  113. if h.resolve {
  114. return outbound.NewDirectConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4)
  115. } else {
  116. return outbound.NewConnection(ctx, h, conn, metadata)
  117. }
  118. }
  119. // TODO
  120. // Deprecated
  121. func (h *Outbound) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  122. if h.resolve {
  123. return outbound.NewDirectPacketConnection(ctx, h.router, h, conn, metadata, dns.DomainStrategyUseIPv4)
  124. } else {
  125. return outbound.NewPacketConnection(ctx, h, conn, metadata)
  126. }
  127. }