sing-box/adapter/inbound/socks.go

43 lines
1.1 KiB
Go
Raw Normal View History

2022-07-01 19:34:02 +08:00
package inbound
import (
"context"
"net"
"github.com/sagernet/sing-box/adapter"
"github.com/sagernet/sing-box/config"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing/common/auth"
M "github.com/sagernet/sing/common/metadata"
"github.com/sagernet/sing/protocol/socks"
)
var _ adapter.Inbound = (*Socks)(nil)
type Socks struct {
myInboundAdapter
authenticator auth.Authenticator
}
func NewSocks(ctx context.Context, router adapter.Router, logger log.Logger, tag string, options *config.SimpleInboundOptions) *Socks {
inbound := &Socks{
myInboundAdapter{
protocol: C.TypeSocks,
network: []string{C.NetworkTCP},
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
auth.NewAuthenticator(options.Users),
}
inbound.connHandler = inbound
return inbound
}
func (h *Socks) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamHandler(metadata), M.Metadata{})
}