mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-14 04:43:17 +00:00
123 lines
3.3 KiB
Go
123 lines
3.3 KiB
Go
package inbound
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"net"
|
|
"os"
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
|
C "github.com/sagernet/sing-box/constant"
|
|
"github.com/sagernet/sing-box/log"
|
|
"github.com/sagernet/sing-box/option"
|
|
"github.com/sagernet/sing-vmess"
|
|
"github.com/sagernet/sing/common"
|
|
"github.com/sagernet/sing/common/auth"
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
F "github.com/sagernet/sing/common/format"
|
|
N "github.com/sagernet/sing/common/network"
|
|
)
|
|
|
|
var _ adapter.Inbound = (*VMess)(nil)
|
|
|
|
type VMess struct {
|
|
myInboundAdapter
|
|
service *vmess.Service[int]
|
|
users []option.VMessUser
|
|
tlsConfig *TLSConfig
|
|
}
|
|
|
|
func NewVMess(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VMessInboundOptions) (*VMess, error) {
|
|
inbound := &VMess{
|
|
myInboundAdapter: myInboundAdapter{
|
|
protocol: C.TypeVMess,
|
|
network: []string{N.NetworkTCP},
|
|
ctx: ctx,
|
|
router: router,
|
|
logger: logger,
|
|
tag: tag,
|
|
listenOptions: options.ListenOptions,
|
|
},
|
|
users: options.Users,
|
|
}
|
|
service := vmess.NewService[int](adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound))
|
|
err := service.UpdateUsers(common.MapIndexed(options.Users, func(index int, it option.VMessUser) int {
|
|
return index
|
|
}), common.Map(options.Users, func(it option.VMessUser) string {
|
|
return it.UUID
|
|
}), common.Map(options.Users, func(it option.VMessUser) int {
|
|
return it.AlterId
|
|
}))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if options.TLS != nil {
|
|
tlsConfig, err := NewTLSConfig(logger, common.PtrValueOrDefault(options.TLS))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
inbound.tlsConfig = tlsConfig
|
|
}
|
|
inbound.service = service
|
|
inbound.connHandler = inbound
|
|
return inbound, nil
|
|
}
|
|
|
|
func (h *VMess) Start() error {
|
|
if h.tlsConfig != nil {
|
|
err := h.tlsConfig.Start()
|
|
if err != nil {
|
|
return E.Cause(err, "create TLS config")
|
|
}
|
|
}
|
|
return common.Start(
|
|
h.service,
|
|
&h.myInboundAdapter,
|
|
)
|
|
}
|
|
|
|
func (h *VMess) Close() error {
|
|
return common.Close(
|
|
h.service,
|
|
&h.myInboundAdapter,
|
|
common.PtrOrNil(h.tlsConfig),
|
|
)
|
|
}
|
|
|
|
func (h *VMess) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
if h.tlsConfig != nil {
|
|
conn = tls.Server(conn, h.tlsConfig.Config())
|
|
}
|
|
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
|
|
}
|
|
|
|
func (h *VMess) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
userIndex, loaded := auth.UserFromContext[int](ctx)
|
|
if !loaded {
|
|
return os.ErrInvalid
|
|
}
|
|
user := h.users[userIndex].Name
|
|
if user == "" {
|
|
user = F.ToString(userIndex)
|
|
} else {
|
|
metadata.User = user
|
|
}
|
|
h.logger.InfoContext(ctx, "[", user, "] inbound connection to ", metadata.Destination)
|
|
return h.router.RouteConnection(ctx, conn, metadata)
|
|
}
|
|
|
|
func (h *VMess) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
userIndex, loaded := auth.UserFromContext[int](ctx)
|
|
if !loaded {
|
|
return os.ErrInvalid
|
|
}
|
|
user := h.users[userIndex].Name
|
|
if user == "" {
|
|
user = F.ToString(userIndex)
|
|
} else {
|
|
metadata.User = user
|
|
}
|
|
h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
|
|
return h.router.RoutePacketConnection(ctx, conn, metadata)
|
|
}
|