2024-11-02 00:39:02 +08:00
|
|
|
package vless
|
2023-02-25 16:24:08 +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"
|
2023-11-08 12:09:22 +08:00
|
|
|
"github.com/sagernet/sing-box/common/mux"
|
2023-02-25 16:24:08 +08:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2023-11-08 12:09:22 +08:00
|
|
|
"github.com/sagernet/sing-box/common/uot"
|
2023-02-25 16:24:08 +08:00
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
"github.com/sagernet/sing-box/log"
|
|
|
|
"github.com/sagernet/sing-box/option"
|
|
|
|
"github.com/sagernet/sing-box/transport/v2ray"
|
|
|
|
"github.com/sagernet/sing-vmess"
|
|
|
|
"github.com/sagernet/sing-vmess/packetaddr"
|
2024-07-02 14:01:35 +08:00
|
|
|
"github.com/sagernet/sing-vmess/vless"
|
2023-02-25 16:24:08 +08:00
|
|
|
"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"
|
2024-11-02 00:39:02 +08:00
|
|
|
"github.com/sagernet/sing/common/logger"
|
2023-02-25 16:24:08 +08:00
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func RegisterInbound(registry *inbound.Registry) {
|
|
|
|
inbound.Register[option.VLESSInboundOptions](registry, C.TypeVLESS, NewInbound)
|
|
|
|
}
|
2023-02-25 16:24:08 +08:00
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
|
|
|
|
|
|
|
|
type Inbound struct {
|
|
|
|
inbound.Adapter
|
2023-02-25 16:24:08 +08:00
|
|
|
ctx context.Context
|
2024-11-02 00:39:02 +08:00
|
|
|
router adapter.ConnectionRouterEx
|
|
|
|
logger logger.ContextLogger
|
|
|
|
listener *listener.Listener
|
2023-02-25 16:24:08 +08:00
|
|
|
users []option.VLESSUser
|
|
|
|
service *vless.Service[int]
|
|
|
|
tlsConfig tls.ServerConfig
|
|
|
|
transport adapter.V2RayServerTransport
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VLESSInboundOptions) (adapter.Inbound, error) {
|
|
|
|
inbound := &Inbound{
|
|
|
|
Adapter: inbound.NewAdapter(C.TypeVLESS, tag),
|
|
|
|
ctx: ctx,
|
|
|
|
router: uot.NewRouter(router, logger),
|
|
|
|
logger: logger,
|
|
|
|
users: options.Users,
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
2023-11-08 12:09:22 +08:00
|
|
|
var err error
|
|
|
|
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-11-23 22:34:02 +08:00
|
|
|
service := vless.NewService[int](logger, adapter.NewUpstreamContextHandlerEx(inbound.newConnectionEx, inbound.newPacketConnectionEx))
|
2023-02-25 16:24:08 +08:00
|
|
|
service.UpdateUsers(common.MapIndexed(inbound.users, func(index int, _ option.VLESSUser) int {
|
|
|
|
return index
|
|
|
|
}), common.Map(inbound.users, func(it option.VLESSUser) string {
|
|
|
|
return it.UUID
|
2023-03-01 20:28:40 +08:00
|
|
|
}), common.Map(inbound.users, func(it option.VLESSUser) string {
|
|
|
|
return it.Flow
|
2023-02-25 16:24:08 +08:00
|
|
|
}))
|
|
|
|
inbound.service = service
|
|
|
|
if options.TLS != nil {
|
2023-08-29 13:43:42 +08:00
|
|
|
inbound.tlsConfig, err = tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
2023-02-25 16:24:08 +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))
|
2023-02-25 16:24:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "create server transport: ", options.Transport.Type)
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
})
|
2023-02-25 16:24:08 +08:00
|
|
|
return inbound, nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 18:10:41 +08:00
|
|
|
func (h *Inbound) Start(stage adapter.StartStage) error {
|
|
|
|
if stage != adapter.StartStateStart {
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-24 09:49:15 +08:00
|
|
|
if h.tlsConfig != nil {
|
|
|
|
err := h.tlsConfig.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
if h.transport == nil {
|
2024-11-02 00:39:02 +08:00
|
|
|
return h.listener.Start()
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
if common.Contains(h.transport.Network(), N.NetworkTCP) {
|
2024-11-02 00:39:02 +08:00
|
|
|
tcpListener, err := h.listener.ListenTCP()
|
2023-02-25 16:24:08 +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)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
if common.Contains(h.transport.Network(), N.NetworkUDP) {
|
2024-11-02 00:39:02 +08:00
|
|
|
udpConn, err := h.listener.ListenUDP()
|
2023-02-25 16:24:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
sErr := h.transport.ServePacket(udpConn)
|
|
|
|
if sErr != nil && !E.IsClosed(sErr) {
|
|
|
|
h.logger.Error("transport serve error: ", sErr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
func (h *Inbound) Close() error {
|
2023-02-25 16:24:08 +08:00
|
|
|
return common.Close(
|
|
|
|
h.service,
|
2024-11-09 21:16:11 +08:00
|
|
|
h.listener,
|
2023-02-25 16:24:08 +08:00
|
|
|
h.tlsConfig,
|
|
|
|
h.transport,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
2023-02-25 16:24:08 +08:00
|
|
|
if h.tlsConfig != nil && h.transport == nil {
|
2024-11-23 22:34:02 +08:00
|
|
|
tlsConn, err := tls.ServerHandshake(ctx, conn, h.tlsConfig)
|
2023-02-25 16:24:08 +08:00
|
|
|
if err != nil {
|
2024-11-23 22:34:02 +08:00
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
|
|
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source, ": TLS handshake"))
|
|
|
|
return
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
2024-11-23 22:34:02 +08:00
|
|
|
conn = tlsConn
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
2024-11-23 22:34:02 +08:00
|
|
|
err := h.service.NewConnection(adapter.WithContext(ctx, &metadata), conn, metadata.Source, onClose)
|
2024-10-21 23:38:34 +08:00
|
|
|
if err != nil {
|
2024-11-23 22:34:02 +08:00
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, err)
|
|
|
|
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
2024-10-21 23:38:34 +08:00
|
|
|
}
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
func (h *Inbound) newConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2023-02-25 16:24:08 +08:00
|
|
|
userIndex, loaded := auth.UserFromContext[int](ctx)
|
|
|
|
if !loaded {
|
2024-11-23 22:34:02 +08:00
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
|
|
|
return
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
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)
|
2024-11-23 22:34:02 +08:00
|
|
|
h.router.RouteConnectionEx(ctx, conn, metadata, onClose)
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
|
2024-11-23 22:34:02 +08:00
|
|
|
func (h *Inbound) newPacketConnectionEx(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
2024-11-09 21:16:11 +08:00
|
|
|
metadata.Inbound = h.Tag()
|
|
|
|
metadata.InboundType = h.Type()
|
2023-02-25 16:24:08 +08:00
|
|
|
userIndex, loaded := auth.UserFromContext[int](ctx)
|
|
|
|
if !loaded {
|
2024-11-23 22:34:02 +08:00
|
|
|
N.CloseOnHandshakeFailure(conn, onClose, os.ErrInvalid)
|
|
|
|
return
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
user := h.users[userIndex].Name
|
|
|
|
if user == "" {
|
|
|
|
user = F.ToString(userIndex)
|
|
|
|
} else {
|
|
|
|
metadata.User = user
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2024-11-23 22:34:02 +08:00
|
|
|
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
2023-02-25 16:24:08 +08:00
|
|
|
}
|
|
|
|
|
2024-11-02 00:39:02 +08:00
|
|
|
var _ adapter.V2RayServerTransportHandler = (*inboundTransportHandler)(nil)
|
|
|
|
|
|
|
|
type inboundTransportHandler Inbound
|
2023-02-25 16:24:08 +08:00
|
|
|
|
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.Source = source
|
|
|
|
metadata.Destination = destination
|
|
|
|
h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
|
|
|
(*Inbound)(h).NewConnectionEx(ctx, conn, metadata, onClose)
|
|
|
|
}
|