github.com/sagernet/sing-box@v1.9.0-rc.20/inbound/redirect.go (about)

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