mixed.go 2.0 KB

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