2023-02-25 08:24:08 +00:00
|
|
|
package inbound
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/sagernet/sing-box/adapter"
|
2023-11-08 04:09:22 +00:00
|
|
|
"github.com/sagernet/sing-box/common/mux"
|
2023-02-25 08:24:08 +00:00
|
|
|
"github.com/sagernet/sing-box/common/tls"
|
2023-11-08 04:09:22 +00:00
|
|
|
"github.com/sagernet/sing-box/common/uot"
|
2023-02-25 08:24:08 +00: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 06:01:35 +00:00
|
|
|
"github.com/sagernet/sing-vmess/vless"
|
2023-02-25 08:24:08 +00: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"
|
|
|
|
M "github.com/sagernet/sing/common/metadata"
|
|
|
|
N "github.com/sagernet/sing/common/network"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ adapter.Inbound = (*VLESS)(nil)
|
|
|
|
_ adapter.InjectableInbound = (*VLESS)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type VLESS struct {
|
|
|
|
myInboundAdapter
|
|
|
|
ctx context.Context
|
|
|
|
users []option.VLESSUser
|
|
|
|
service *vless.Service[int]
|
|
|
|
tlsConfig tls.ServerConfig
|
|
|
|
transport adapter.V2RayServerTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewVLESS(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VLESSInboundOptions) (*VLESS, error) {
|
|
|
|
inbound := &VLESS{
|
|
|
|
myInboundAdapter: myInboundAdapter{
|
|
|
|
protocol: C.TypeVLESS,
|
|
|
|
network: []string{N.NetworkTCP},
|
|
|
|
ctx: ctx,
|
2023-11-08 04:09:22 +00:00
|
|
|
router: uot.NewRouter(router, logger),
|
2023-02-25 08:24:08 +00:00
|
|
|
logger: logger,
|
|
|
|
tag: tag,
|
|
|
|
listenOptions: options.ListenOptions,
|
|
|
|
},
|
|
|
|
ctx: ctx,
|
|
|
|
users: options.Users,
|
|
|
|
}
|
2023-11-08 04:09:22 +00:00
|
|
|
var err error
|
|
|
|
inbound.router, err = mux.NewRouterWithOptions(inbound.router, logger, common.PtrValueOrDefault(options.Multiplex))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-27 07:07:15 +00:00
|
|
|
service := vless.NewService[int](logger, adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound))
|
2023-02-25 08:24:08 +00: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 12:28:40 +00:00
|
|
|
}), common.Map(inbound.users, func(it option.VLESSUser) string {
|
|
|
|
return it.Flow
|
2023-02-25 08:24:08 +00:00
|
|
|
}))
|
|
|
|
inbound.service = service
|
|
|
|
if options.TLS != nil {
|
2023-08-29 05:43:42 +00:00
|
|
|
inbound.tlsConfig, err = tls.NewServer(ctx, logger, common.PtrValueOrDefault(options.TLS))
|
2023-02-25 08:24:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.Transport != nil {
|
|
|
|
inbound.transport, err = v2ray.NewServerTransport(ctx, common.PtrValueOrDefault(options.Transport), inbound.tlsConfig, (*vlessTransportHandler)(inbound))
|
|
|
|
if err != nil {
|
|
|
|
return nil, E.Cause(err, "create server transport: ", options.Transport.Type)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inbound.connHandler = inbound
|
|
|
|
return inbound, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) Start() error {
|
2024-06-24 01:49:15 +00:00
|
|
|
if h.tlsConfig != nil {
|
|
|
|
err := h.tlsConfig.Start()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-25 08:24:08 +00:00
|
|
|
}
|
|
|
|
if h.transport == nil {
|
|
|
|
return h.myInboundAdapter.Start()
|
|
|
|
}
|
|
|
|
if common.Contains(h.transport.Network(), N.NetworkTCP) {
|
|
|
|
tcpListener, err := h.myInboundAdapter.ListenTCP()
|
|
|
|
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) {
|
|
|
|
udpConn, err := h.myInboundAdapter.ListenUDP()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) Close() error {
|
|
|
|
return common.Close(
|
|
|
|
h.service,
|
|
|
|
&h.myInboundAdapter,
|
|
|
|
h.tlsConfig,
|
|
|
|
h.transport,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) newTransportConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
h.injectTCP(conn, metadata)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
|
|
|
|
var err error
|
|
|
|
if h.tlsConfig != nil && h.transport == nil {
|
|
|
|
conn, err = tls.ServerHandshake(ctx, conn, h.tlsConfig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) NewPacketConnection(ctx context.Context, conn N.PacketConn, metadata adapter.InboundContext) error {
|
|
|
|
return os.ErrInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *VLESS) 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 *VLESS) 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
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
return h.router.RoutePacketConnection(ctx, conn, metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ adapter.V2RayServerTransportHandler = (*vlessTransportHandler)(nil)
|
|
|
|
|
|
|
|
type vlessTransportHandler VLESS
|
|
|
|
|
|
|
|
func (t *vlessTransportHandler) NewConnection(ctx context.Context, conn net.Conn, metadata M.Metadata) error {
|
|
|
|
return (*VLESS)(t).newTransportConnection(ctx, conn, adapter.InboundContext{
|
|
|
|
Source: metadata.Source,
|
|
|
|
Destination: metadata.Destination,
|
|
|
|
})
|
|
|
|
}
|