1
0

mixed.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package inbound
  2. import (
  3. std_bufio "bufio"
  4. "context"
  5. "net"
  6. "os"
  7. "github.com/sagernet/sing-box/adapter"
  8. "github.com/sagernet/sing-box/common/uot"
  9. C "github.com/sagernet/sing-box/constant"
  10. "github.com/sagernet/sing-box/log"
  11. "github.com/sagernet/sing-box/option"
  12. "github.com/sagernet/sing/common/auth"
  13. N "github.com/sagernet/sing/common/network"
  14. "github.com/sagernet/sing/protocol/http"
  15. "github.com/sagernet/sing/protocol/socks"
  16. "github.com/sagernet/sing/protocol/socks/socks4"
  17. "github.com/sagernet/sing/protocol/socks/socks5"
  18. )
  19. var (
  20. _ adapter.Inbound = (*Mixed)(nil)
  21. _ adapter.InjectableInbound = (*Mixed)(nil)
  22. )
  23. type Mixed struct {
  24. myInboundAdapter
  25. authenticator *auth.Authenticator
  26. }
  27. func NewMixed(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.HTTPMixedInboundOptions) *Mixed {
  28. inbound := &Mixed{
  29. myInboundAdapter{
  30. protocol: C.TypeMixed,
  31. network: []string{N.NetworkTCP},
  32. ctx: ctx,
  33. router: uot.NewRouter(router, logger),
  34. logger: logger,
  35. tag: tag,
  36. listenOptions: options.ListenOptions,
  37. setSystemProxy: options.SetSystemProxy,
  38. },
  39. auth.NewAuthenticator(options.Users),
  40. }
  41. inbound.connHandler = inbound
  42. return inbound
  43. }
  44. func (h *Mixed) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  45. reader := std_bufio.NewReader(conn)
  46. headerBytes, err := reader.Peek(1)
  47. if err != nil {
  48. return err
  49. }
  50. switch headerBytes[0] {
  51. case socks4.Version, socks5.Version:
  52. return socks.HandleConnection0(ctx, conn, reader, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
  53. default:
  54. return http.HandleConnection(ctx, conn, reader, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
  55. }
  56. }
  57. func (h *Mixed) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
  58. return os.ErrInvalid
  59. }