shadowsocks.go 5.5 KB

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