sing-box/inbound/socks.go

51 lines
1.4 KiB
Go
Raw Permalink Normal View History

2022-07-01 19:34:02 +08:00
package inbound
import (
"context"
"net"
2022-08-29 19:43:13 +08:00
"os"
2022-07-01 19:34:02 +08:00
"github.com/sagernet/sing-box/adapter"
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
2022-07-02 14:07:50 +08:00
"github.com/sagernet/sing-box/option"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/common/auth"
2022-07-30 00:29:22 +08:00
N "github.com/sagernet/sing/common/network"
2022-07-08 23:03:57 +08:00
"github.com/sagernet/sing/protocol/socks"
2022-07-01 19:34:02 +08:00
)
2022-08-29 19:43:13 +08:00
var (
_ adapter.Inbound = (*Socks)(nil)
_ adapter.InjectableInbound = (*Socks)(nil)
)
2022-07-01 19:34:02 +08:00
type Socks struct {
myInboundAdapter
authenticator auth.Authenticator
}
func NewSocks(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.SocksInboundOptions) *Socks {
2022-07-01 19:34:02 +08:00
inbound := &Socks{
myInboundAdapter{
protocol: C.TypeSocks,
2022-07-30 00:29:22 +08:00
network: []string{N.NetworkTCP},
2022-07-01 19:34:02 +08:00
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 {
2022-08-23 19:44:40 +08:00
return socks.HandleConnection(ctx, conn, h.authenticator, h.upstreamUserHandler(metadata), adapter.UpstreamMetadata(metadata))
2022-07-01 19:34:02 +08:00
}
2022-08-29 19:43:13 +08:00
func (h *Socks) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
return os.ErrInvalid
}