socks.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "os"
  6. "github.com/sagernet/sing-box/adapter"
  7. "github.com/sagernet/sing-box/common/uot"
  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/common/auth"
  12. N "github.com/sagernet/sing/common/network"
  13. "github.com/sagernet/sing/protocol/socks"
  14. )
  15. var (
  16. _ adapter.Inbound = (*Socks)(nil)
  17. _ adapter.InjectableInbound = (*Socks)(nil)
  18. )
  19. type Socks struct {
  20. myInboundAdapter
  21. authenticator *auth.Authenticator
  22. }
  23. func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
  24. inbound := &Socks{
  25. myInboundAdapter{
  26. protocol: C.TypeSOCKS,
  27. network: []string{N.NetworkTCP},
  28. ctx: ctx,
  29. router: uot.NewRouter(router, logger),
  30. logger: logger,
  31. tag: tag,
  32. listenOptions: options.ListenOptions,
  33. },
  34. auth.NewAuthenticator(options.Users),
  35. }
  36. inbound.connHandler = inbound
  37. return inbound
  38. }
  39. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  40. return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
  41. }
  42. func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  43. return os.ErrInvalid
  44. }