sing-box/inbound/socks.go

53 lines
1.6 KiB
Go
Raw Normal View History

2022-07-01 11:34:02 +00:00
package inbound
import (
2024-10-21 15:38:34 +00:00
std_bufio "bufio"
2022-07-01 11:34:02 +00:00
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/common/uot"
2022-07-01 11:34:02 +00:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 06:07:50 +00:00
"github.com/sagernet/sing-box/option"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/common/auth"
2024-10-21 15:38:34 +00:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-29 16:29:22 +00:00
N "github.com/sagernet/sing/common/network"
2022-07-08 15:03:57 +00:00
"github.com/sagernet/sing/protocol/socks"
2022-07-01 11:34:02 +00:00
)
2022-08-29 11:43:13 +00:00
var (
2024-10-21 15:38:34 +00:00
_ adapter.Inbound = (*Socks)(nil)
_ adapter.TCPInjectableInbound = (*Socks)(nil)
2022-08-29 11:43:13 +00:00
)
2022-07-01 11:34:02 +00:00
type Socks struct {
myInboundAdapter
authenticator *auth.Authenticator
2022-07-01 11:34:02 +00:00
}
func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
2022-07-01 11:34:02 +00:00
inbound := &Socks{
myInboundAdapter{
2023-08-04 09:13:46 +00:00
protocol: C.TypeSOCKS,
2022-07-29 16:29:22 +00:00
network: []string{N.NetworkTCP},
2022-07-01 11:34:02 +00:00
ctx: ctx,
router: uot.NewRouter(router, logger),
2022-07-01 11:34:02 +00:00
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
auth.NewAuthenticator(options.Users),
}
inbound.connHandler = inbound
return inbound
}
2024-10-21 15:38:34 +00:00
func (h *Socks) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
err := socks.HandleConnectionEx(ctx, conn, std_bufio.NewReader(conn), h.authenticator, nil, h.upstreamUserHandlerEx(metadata), metadata.Source, metadata.Destination, onClose)
N.CloseOnHandshakeFailure(conn, onClose, err)
if err != nil {
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
}
2022-08-29 11:43:13 +00:00
}