socks.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. M "github.com/sagernet/sing/common/metadata"
  11. N "github.com/sagernet/sing/common/network"
  12. "github.com/sagernet/sing/protocol/socks"
  13. )
  14. var _ adapter.Inbound = (*Socks)(nil)
  15. type Socks struct {
  16. myInboundAdapter
  17. authenticator auth.Authenticator
  18. }
  19. func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
  20. inbound := &Socks{
  21. myInboundAdapter{
  22. protocol: C.TypeSocks,
  23. network: []string{N.NetworkTCP},
  24. ctx: ctx,
  25. router: router,
  26. logger: logger,
  27. tag: tag,
  28. listenOptions: options.ListenOptions,
  29. },
  30. auth.NewAuthenticator(options.Users),
  31. }
  32. inbound.connHandler = inbound
  33. return inbound
  34. }
  35. func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  36. return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamUserHandler(metadata), M.Metadata{})
  37. }