2022-07-15 00:42:02 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
"github.com/sagernet/sing-box/common/redir"
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
2022-07-29 16:29:22 +00:00
|
|
|
N "github.com/sagernet/sing/common/network"
|
2022-07-15 00:42:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Redirect struct {
|
|
|
|
myInboundAdapter
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRedirect(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.RedirectInboundOptions) *Redirect {
|
|
|
|
redirect := &Redirect{
|
|
|
|
myInboundAdapter{
|
|
|
|
protocol: C.TypeRedirect,
|
2022-07-29 16:29:22 +00:00
|
|
|
network: []string{N.NetworkTCP},
|
2022-07-15 00:42:02 +00:00
|
|
|
ctx: ctx,
|
|
|
|
router: router,
|
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
listenOptions: options.ListenOptions,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
redirect.connHandler = redirect
|
|
|
|
return redirect
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Redirect) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
destination, err := redir.GetOriginalDestination(conn)
|
|
|
|
if err != nil {
|
|
|
|
return E.Cause(err, "get redirect destination")
|
|
|
|
}
|
|
|
|
metadata.Destination = M.SocksaddrFromNetIP(destination)
|
|
|
|
return r.newConnection(ctx, conn, metadata)
|
|
|
|
}
|