shadowsocks.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. "github.com/sagernet/sing-shadowsocks"
  11. "github.com/sagernet/sing-shadowsocks/shadowimpl"
  12. "github.com/sagernet/sing/common/bufio"
  13. M "github.com/sagernet/sing/common/metadata"
  14. N "github.com/sagernet/sing/common/network"
  15. )
  16. var _ adapter.Outbound = (*Shadowsocks)(nil)
  17. type Shadowsocks struct {
  18. myOutboundAdapter
  19. dialer N.Dialer
  20. method shadowsocks.Method
  21. serverAddr M.Socksaddr
  22. }
  23. func NewShadowsocks(router adapter.Router, logger log.Logger, tag string, options option.ShadowsocksOutboundOptions) (*Shadowsocks, error) {
  24. method, err := shadowimpl.FetchMethod(options.Method, options.Password)
  25. if err != nil {
  26. return nil, err
  27. }
  28. return &Shadowsocks{
  29. myOutboundAdapter{
  30. protocol: C.TypeDirect,
  31. logger: logger,
  32. tag: tag,
  33. network: options.Network.Build(),
  34. },
  35. dialer.NewOutbound(router, options.OutboundDialerOptions),
  36. method,
  37. options.ServerOptions.Build(),
  38. }, nil
  39. }
  40. func (h *Shadowsocks) DialContext(ctx context.Context, network string, destination M.Socksaddr) (net.Conn, error) {
  41. ctx, metadata := adapter.AppendContext(ctx)
  42. metadata.Outbound = h.tag
  43. metadata.Destination = destination
  44. switch network {
  45. case C.NetworkTCP:
  46. h.logger.WithContext(ctx).Info("outbound connection to ", destination)
  47. outConn, err := h.dialer.DialContext(ctx, C.NetworkTCP, h.serverAddr)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return h.method.DialEarlyConn(outConn, destination), nil
  52. case C.NetworkUDP:
  53. h.logger.WithContext(ctx).Info("outbound packet connection to ", destination)
  54. outConn, err := h.dialer.DialContext(ctx, C.NetworkUDP, h.serverAddr)
  55. if err != nil {
  56. return nil, err
  57. }
  58. return &bufio.BindPacketConn{PacketConn: h.method.DialPacketConn(outConn), Addr: destination}, nil
  59. default:
  60. panic("unknown network " + network)
  61. }
  62. }
  63. func (h *Shadowsocks) ListenPacket(ctx context.Context, destination M.Socksaddr) (net.PacketConn, error) {
  64. ctx, metadata := adapter.AppendContext(ctx)
  65. metadata.Outbound = h.tag
  66. metadata.Destination = destination
  67. h.logger.WithContext(ctx).Info("outbound packet connection to ", h.serverAddr)
  68. outConn, err := h.dialer.DialContext(ctx, "udp", h.serverAddr)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return h.method.DialPacketConn(outConn), nil
  73. }
  74. func (h *Shadowsocks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  75. return NewEarlyConnection(ctx, h, conn, metadata)
  76. }
  77. func (h *Shadowsocks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  78. return NewPacketConnection(ctx, h, conn, metadata)
  79. }