redirect.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package inbound
  2. import (
  3. "context"
  4. "net"
  5. "github.com/sagernet/sing-box/adapter"
  6. "github.com/sagernet/sing-box/common/redir"
  7. C "github.com/sagernet/sing-box/constant"
  8. "github.com/sagernet/sing-box/log"
  9. "github.com/sagernet/sing-box/option"
  10. E "github.com/sagernet/sing/common/exceptions"
  11. M "github.com/sagernet/sing/common/metadata"
  12. N "github.com/sagernet/sing/common/network"
  13. )
  14. type Redirect struct {
  15. myInboundAdapter
  16. }
  17. func NewRedirect(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.RedirectInboundOptions) *Redirect {
  18. redirect := &Redirect{
  19. myInboundAdapter{
  20. protocol: C.TypeRedirect,
  21. network: []string{N.NetworkTCP},
  22. ctx: ctx,
  23. router: router,
  24. logger: logger,
  25. tag: tag,
  26. listenOptions: options.ListenOptions,
  27. },
  28. }
  29. redirect.connHandler = redirect
  30. return redirect
  31. }
  32. func (r *Redirect) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
  33. destination, err := redir.GetOriginalDestination(conn)
  34. if err != nil {
  35. return E.Cause(err, "get redirect destination")
  36. }
  37. metadata.Destination = M.SocksaddrFromNetIP(destination)
  38. return r.newConnection(ctx, conn, metadata)
  39. }