2022-07-01 11:34:02 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
|
|
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"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
"github.com/sagernet/sing/protocol/socks"
|
2022-07-01 11:34:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ adapter.Inbound = (*Socks)(nil)
|
|
|
|
|
|
|
|
type Socks struct {
|
|
|
|
myInboundAdapter
|
|
|
|
authenticator auth.Authenticator
|
|
|
|
}
|
|
|
|
|
2022-07-14 06:04:57 +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{
|
|
|
|
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{})
|
|
|
|
}
|