sing-box/protocol/vmess/inbound.go

236 lines
7.3 KiB
Go
Raw Normal View History

2024-11-02 00:39:02 +08:00
package vmess
2022-07-18 12:32:31 +08:00
import (
"context"
"net"
"os"
"github.com/sagernet/sing-box/adapter"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing-box/adapter/inbound"
"github.com/sagernet/sing-box/common/listener"
"github.com/sagernet/sing-box/common/mux"
2022-09-09 18:45:10 +08:00
"github.com/sagernet/sing-box/common/tls"
"github.com/sagernet/sing-box/common/uot"
2022-07-18 12:32:31 +08:00
C "github.com/sagernet/sing-box/constant"
"github.com/sagernet/sing-box/log"
"github.com/sagernet/sing-box/option"
2022-08-22 18:53:47 +08:00
"github.com/sagernet/sing-box/transport/v2ray"
2022-07-18 12:32:31 +08:00
"github.com/sagernet/sing-vmess"
2022-08-27 11:28:01 +08:00
"github.com/sagernet/sing-vmess/packetaddr"
2022-07-18 12:32:31 +08:00
"github.com/sagernet/sing/common"
"github.com/sagernet/sing/common/auth"
2022-07-30 22:00:04 +08:00
E "github.com/sagernet/sing/common/exceptions"
2022-07-18 12:32:31 +08:00
F "github.com/sagernet/sing/common/format"
2024-11-02 00:39:02 +08:00
"github.com/sagernet/sing/common/logger"
2022-08-27 11:28:01 +08:00
M "github.com/sagernet/sing/common/metadata"
2022-07-18 12:32:31 +08:00
N "github.com/sagernet/sing/common/network"
2023-08-31 14:00:47 +08:00
"github.com/sagernet/sing/common/ntp"
2022-07-18 12:32:31 +08:00
)
2024-11-02 00:39:02 +08:00
func RegisterInbound(registry *inbound.Registry) {
inbound.Register[option.VMessInboundOptions](registry, C.TypeVMess, NewInbound)
}
2022-07-18 12:32:31 +08:00
2024-11-02 00:39:02 +08:00
var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
type Inbound struct {
inbound.Adapter
2022-08-22 18:53:47 +08:00
ctx context.Context
2024-11-02 00:39:02 +08:00
router adapter.ConnectionRouterEx
logger logger.ContextLogger
listener *listener.Listener
2022-07-25 08:14:09 +08:00
service *vmess.Service[int]
users []option.VMessUser
2022-09-09 18:45:10 +08:00
tlsConfig tls.ServerConfig
2022-08-22 18:53:47 +08:00
transport adapter.V2RayServerTransport
2022-07-18 12:32:31 +08:00
}
2024-11-02 00:39:02 +08:00
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VMessInboundOptions) (adapter.Inbound, error) {
inbound := &Inbound{
Adapter: inbound.NewAdapter(C.TypeVMess, tag),
ctx: ctx,
router: uot.NewRouter(router, logger),
logger: logger,
users: options.Users,
2022-07-18 12:32:31 +08:00
}
var err error
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
if err != nil {
return nil, err
}
var serviceOptions []vmess.ServiceOption
2023-08-31 14:00:47 +08:00
if timeFunc := ntp.TimeFuncFromContext(ctx); timeFunc != nil {
2023-02-21 14:53:00 +08:00
serviceOptions = append(serviceOptions, vmess.ServiceWithTimeFunc(timeFunc))
}
if options.Transport != nil && options.Transport.Type != "" {
serviceOptions = append(serviceOptions, vmess.ServiceWithDisableHeaderProtection())
}
service := vmess.NewService[int](adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound), serviceOptions...)
2022-08-22 18:53:47 +08:00
inbound.service = service
err = service.UpdateUsers(common.MapIndexed(options.Users, func(index int, it option.VMessUser) int {
2022-07-18 12:32:31 +08:00
return index
2022-08-01 12:23:34 +08:00
}), common.Map(options.Users, func(it option.VMessUser) string {
return it.UUID
}), common.Map(options.Users, func(it option.VMessUser) int {
return it.AlterId
2022-07-18 12:32:31 +08:00
}))
if err != nil {
return nil, err
}
2022-07-25 08:14:09 +08:00
if options.TLS != nil {
2023-08-29 13:43:42 +08:00
inbound.tlsConfig, err = tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
2022-08-22 18:53:47 +08:00
if err != nil {
return nil, err
}
}
if options.Transport != nil {
2024-11-02 00:39:02 +08:00
inbound.transport, err = v2ray.NewServerTransport(ctx, logger, common.PtrValueOrDefault(options.Transport), inbound.tlsConfig, (*inboundTransportHandler)(inbound))
2022-07-25 08:14:09 +08:00
if err != nil {
2022-08-22 22:19:25 +08:00
return nil, E.Cause(err, "create server transport: ", options.Transport.Type)
2022-07-25 08:14:09 +08:00
}
}
2024-11-02 00:39:02 +08:00
inbound.listener = listener.New(listener.Options{
Context: ctx,
Logger: logger,
Network: []string{N.NetworkTCP},
Listen: options.ListenOptions,
ConnectionHandler: inbound,
})
2022-07-18 12:32:31 +08:00
return inbound, nil
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) Start() error {
2024-06-24 09:49:15 +08:00
err := h.service.Start()
2022-08-22 18:53:47 +08:00
if err != nil {
return err
}
2024-06-24 09:49:15 +08:00
if h.tlsConfig != nil {
err = h.tlsConfig.Start()
if err != nil {
return err
}
}
2022-08-22 18:53:47 +08:00
if h.transport == nil {
2024-11-02 00:39:02 +08:00
return h.listener.Start()
2022-08-22 18:53:47 +08:00
}
2022-08-22 21:20:05 +08:00
if common.Contains(h.transport.Network(), N.NetworkTCP) {
2024-11-02 00:39:02 +08:00
tcpListener, err := h.listener.ListenTCP()
2022-08-22 21:20:05 +08:00
if err != nil {
return err
}
go func() {
sErr := h.transport.Serve(tcpListener)
if sErr != nil && !E.IsClosed(sErr) {
h.logger.Error("transport serve error: ", sErr)
}
}()
2022-08-22 18:53:47 +08:00
}
2022-08-22 21:20:05 +08:00
if common.Contains(h.transport.Network(), N.NetworkUDP) {
2024-11-02 00:39:02 +08:00
udpConn, err := h.listener.ListenUDP()
2022-08-22 21:20:05 +08:00
if err != nil {
return err
2022-08-22 18:53:47 +08:00
}
2022-08-22 21:20:05 +08:00
go func() {
sErr := h.transport.ServePacket(udpConn)
if sErr != nil && !E.IsClosed(sErr) {
h.logger.Error("transport serve error: ", sErr)
}
}()
}
2022-08-22 18:53:47 +08:00
return nil
2022-07-30 22:00:04 +08:00
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) Close() error {
2022-07-30 22:00:04 +08:00
return common.Close(
2022-08-01 12:23:34 +08:00
h.service,
2024-11-02 00:39:02 +08:00
&h.listener,
2022-09-09 18:45:10 +08:00
h.tlsConfig,
2022-08-22 18:53:47 +08:00
h.transport,
2022-07-30 22:00:04 +08:00
)
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-09-30 11:27:18 +08:00
var err error
2022-08-29 10:10:41 +08:00
if h.tlsConfig != nil && h.transport == nil {
2022-09-30 11:27:18 +08:00
conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
if err != nil {
return err
}
2022-07-25 08:14:09 +08:00
}
2022-07-18 12:32:31 +08:00
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
2022-08-22 18:53:47 +08:00
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
2024-10-21 23:38:34 +08:00
err := h.NewConnection(ctx, conn, metadata)
N.CloseOnHandshakeFailure(conn, onClose, err)
if err != nil {
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
}
2022-08-29 19:43:13 +08:00
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) newConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-18 12:32:31 +08:00
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)
}
2024-11-02 00:39:02 +08:00
func (h *Inbound) newPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
2022-07-18 12:32:31 +08:00
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
}
2022-08-27 11:28:01 +08:00
if metadata.Destination.Fqdn == packetaddr.SeqPacketMagicAddress {
metadata.Destination = M.Socksaddr{}
conn = packetaddr.NewConn(conn.(vmess.PacketConn), metadata.Destination)
h.logger.InfoContext(ctx, "[", user, "] inbound packet addr connection")
} else {
h.logger.InfoContext(ctx, "[", user, "] inbound packet connection to ", metadata.Destination)
}
2022-07-18 12:32:31 +08:00
return h.router.RoutePacketConnection(ctx, conn, metadata)
}
2024-11-02 00:39:02 +08:00
var _ adapter.V2RayServerTransportHandler = (*inboundTransportHandler)(nil)
type inboundTransportHandler Inbound
2024-11-02 00:39:02 +08:00
func (h *inboundTransportHandler) NewConnectionEx(ctx context.Context, conn net.Conn, source M.Socksaddr, destination M.Socksaddr, onClose N.CloseHandlerFunc) {
var metadata adapter.InboundContext
metadata.Inbound = h.Tag()
metadata.InboundType = h.Type()
metadata.InboundDetour = h.listener.ListenOptions().Detour
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
metadata.Source = source
metadata.Destination = destination
h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
(*Inbound)(h).NewConnectionEx(ctx, conn, metadata, onClose)
}
func (h *Inbound) NewError(ctx context.Context, err error) {
NewError(h.logger, ctx, err)
}
2024-11-02 00:39:02 +08:00
// Deprecated: remove
func NewError(logger logger.ContextLogger, ctx context.Context, err error) {
common.Close(err)
if E.IsClosedOrCanceled(err) {
logger.DebugContext(ctx, "connection closed: ", err)
return
}
logger.ErrorContext(ctx, err)
}