socks.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. C "github.com/sagernet/sing-box/constant"
  7. "github.com/sagernet/sing-box/log"
  8. "github.com/sagernet/sing-box/option"
  9. "github.com/sagernet/sing/common/auth"
  10. N "github.com/sagernet/sing/common/network"
  11. "github.com/sagernet/sing/protocol/socks"
  12. )
  13. var _ adapter.Inbound = (*Socks)(nil)
  14. type Socks struct {
  15. myInboundAdapter
  16. authenticator auth.Authenticator
  17. }
  18. func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
  19. inbound := &Socks{
  20. myInboundAdapter{
  21. protocol: C.TypeSocks,
  22. network: []string{N.NetworkTCP},
  23. ctx: ctx,
  24. router: router,
  25. logger: logger,
  26. tag: tag,
  27. listenOptions: options.ListenOptions,
  28. },
  29. auth.NewAuthenticator(options.Users),
  30. }
  31. inbound.connHandler = inbound
  32. return inbound
  33. }
  34. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  35. return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
  36. }