sing-box/inbound/redirect.go

46 lines
1.3 KiB
Go
Raw Normal View History

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"
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
}
2024-10-21 15:38:34 +00:00
func (r *Redirect) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
2022-07-15 00:42:02 +00:00
destination, err := redir.GetOriginalDestination(conn)
if err != nil {
2024-10-21 15:38:34 +00:00
conn.Close()
r.logger.ErrorContext(ctx, "process connection from ", conn.RemoteAddr(), ": get redirect destination: ", err)
return
2022-07-15 00:42:02 +00:00
}
metadata.Destination = M.SocksaddrFromNetIP(destination)
2024-10-21 15:38:34 +00:00
r.newConnectionEx(ctx, conn, metadata, onClose)
2022-07-15 00:42:02 +00:00
}