shadowsocks.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. "github.com/sagernet/sing-box/common/mux"
  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-shadowsocks"
  12. "github.com/sagernet/sing-shadowsocks/shadowimpl"
  13. "github.com/sagernet/sing/common"
  14. "github.com/sagernet/sing/common/bufio"
  15. E "github.com/sagernet/sing/common/exceptions"
  16. M "github.com/sagernet/sing/common/metadata"
  17. N "github.com/sagernet/sing/common/network"
  18. "github.com/sagernet/sing/common/uot"
  19. )
  20. var _ adapter.Outbound = (*Shadowsocks)(nil)
  21. type Shadowsocks struct {
  22. myOutboundAdapter
  23. dialer N.Dialer
  24. method shadowsocks.Method
  25. serverAddr M.Socksaddr
  26. uot bool
  27. multiplexDialer N.Dialer
  28. }
  29. func NewShadowsocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
  30. method, err := shadowimpl.FetchMethod(options.Method, options.Password)
  31. if err != nil {
  32. return nil, err
  33. }
  34. outbound := &Shadowsocks{
  35. myOutboundAdapter: myOutboundAdapter{
  36. protocol: C.TypeShadowsocks,
  37. network: options.Network.Build(),
  38. router: router,
  39. logger: logger,
  40. tag: tag,
  41. },
  42. dialer: dialer.NewOutbound(router, options.OutboundDialerOptions),
  43. method: method,
  44. serverAddr: options.ServerOptions.Build(),
  45. uot: options.UoT,
  46. }
  47. if !options.UoT {
  48. outbound.multiplexDialer, err = mux.NewClientWithOptions(ctx, (*shadowsocksDialer)(outbound), common.PtrValueOrDefault(options.MultiplexOptions))
  49. if err != nil {
  50. return nil, err
  51. }
  52. }
  53. return outbound, nil
  54. }
  55. func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  56. ctx, metadata := adapter.AppendContext(ctx)
  57. metadata.Outbound = h.tag
  58. metadata.Destination = destination
  59. if h.multiplexDialer == nil {
  60. switch N.NetworkName(network) {
  61. case N.NetworkTCP:
  62. h.logger.InfoContext(ctx, "outbound connection to ", destination)
  63. case N.NetworkUDP:
  64. if h.uot {
  65. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  66. tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, M.Socksaddr{
  67. Fqdn: uot.UOTMagicAddress,
  68. Port: destination.Port,
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. return uot.NewClientConn(tcpConn), nil
  74. }
  75. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  76. }
  77. return (*shadowsocksDialer)(h).DialContext(ctx, network, destination)
  78. } else {
  79. switch N.NetworkName(network) {
  80. case N.NetworkTCP:
  81. h.logger.InfoContext(ctx, "outbound multiplex connection to ", destination)
  82. case N.NetworkUDP:
  83. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  84. }
  85. return h.multiplexDialer.DialContext(ctx, network, destination)
  86. }
  87. }
  88. func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  89. ctx, metadata := adapter.AppendContext(ctx)
  90. metadata.Outbound = h.tag
  91. metadata.Destination = destination
  92. if h.multiplexDialer == nil {
  93. if h.uot {
  94. h.logger.InfoContext(ctx, "outbound UoT packet connection to ", destination)
  95. tcpConn, err := (*shadowsocksDialer)(h).DialContext(ctx, N.NetworkTCP, M.Socksaddr{
  96. Fqdn: uot.UOTMagicAddress,
  97. Port: destination.Port,
  98. })
  99. if err != nil {
  100. return nil, err
  101. }
  102. return uot.NewClientConn(tcpConn), nil
  103. }
  104. h.logger.InfoContext(ctx, "outbound packet connection to ", destination)
  105. return (*shadowsocksDialer)(h).ListenPacket(ctx, destination)
  106. } else {
  107. h.logger.InfoContext(ctx, "outbound multiplex packet connection to ", destination)
  108. return h.multiplexDialer.ListenPacket(ctx, destination)
  109. }
  110. }
  111. func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  112. return NewEarlyConnection(ctx, h, conn, metadata)
  113. }
  114. func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  115. return NewPacketConnection(ctx, h, conn, metadata)
  116. }
  117. func (h *Shadowsocks) Close() error {
  118. return common.Close(h.multiplexDialer)
  119. }
  120. var _ N.Dialer = (*shadowsocksDialer)(nil)
  121. type shadowsocksDialer Shadowsocks
  122. func (h *shadowsocksDialer) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  123. switch N.NetworkName(network) {
  124. case N.NetworkTCP:
  125. outConn, err := h.dialer.DialContext(ctx, N.NetworkTCP, h.serverAddr)
  126. if err != nil {
  127. return nil, err
  128. }
  129. return h.method.DialEarlyConn(outConn, destination), nil
  130. case N.NetworkUDP:
  131. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return &bufio.BindPacketConn{PacketConn: h.method.DialPacketConn(outConn), Addr: destination}, nil
  136. default:
  137. return nil, E.Extend(N.ErrUnknownNetwork, network)
  138. }
  139. }
  140. func (h *shadowsocksDialer) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  141. outConn, err := h.dialer.DialContext(ctx, N.NetworkUDP, h.serverAddr)
  142. if err != nil {
  143. return nil, err
  144. }
  145. return h.method.DialPacketConn(outConn), nil
  146. }