sing-box/inbound/vmess.go

167 lines
4.8 KiB
Go
Raw Normal View History

2022-07-18 12:32:31 +08:00
package inbound
import (
"context"
2022-07-25 08:14:09 +08:00
"crypto/tls"
2022-07-18 12:32:31 +08:00
"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"
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"
"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"
N "github.com/sagernet/sing/common/network"
)
var _ adapter.Inbound = (*VMess)(nil)
type VMess struct {
myInboundAdapter
2022-08-22 18:53:47 +08:00
ctx context.Context
2022-07-25 08:14:09 +08:00
service *vmess.Service[int]
users []option.VMessUser
2022-07-30 22:00:04 +08:00
tlsConfig *TLSConfig
2022-08-22 18:53:47 +08:00
transport adapter.V2RayServerTransport
2022-07-18 12:32:31 +08:00
}
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,
2022-07-30 00:29:22 +08:00
network: []string{N.NetworkTCP},
2022-07-18 12:32:31 +08:00
ctx: ctx,
router: router,
logger: logger,
tag: tag,
listenOptions: options.ListenOptions,
},
2022-08-22 18:53:47 +08:00
ctx: ctx,
2022-07-18 12:32:31 +08:00
users: options.Users,
}
service := vmess.NewService[int](adapter.NewUpstreamContextHandler(inbound.newConnection, inbound.newPacketConnection, inbound))
2022-08-22 18:53:47 +08:00
inbound.service = service
2022-08-01 12:23:34 +08:00
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 {
2022-08-22 18:53:47 +08:00
inbound.tlsConfig, err = NewTLSConfig(ctx, logger, common.PtrValueOrDefault(options.TLS))
if err != nil {
return nil, err
}
}
if options.Transport != nil {
2022-08-22 22:19:25 +08:00
var tlsConfig *tls.Config
if inbound.tlsConfig != nil {
tlsConfig = inbound.tlsConfig.Config()
}
inbound.transport, err = v2ray.NewServerTransport(ctx, common.PtrValueOrDefault(options.Transport), tlsConfig, adapter.NewUpstreamHandler(adapter.InboundContext{}, inbound.newTransportConnection, nil, nil), 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
}
}
2022-07-18 12:32:31 +08:00
inbound.connHandler = inbound
return inbound, nil
}
2022-07-30 22:00:04 +08:00
func (h *VMess) Start() error {
2022-08-22 18:53:47 +08:00
err := common.Start(
2022-08-01 12:23:34 +08:00
h.service,
2022-08-22 22:19:25 +08:00
common.PtrOrNil(h.tlsConfig),
2022-08-01 12:23:34 +08:00
)
2022-08-22 18:53:47 +08:00
if err != nil {
return err
}
if h.transport == nil {
return h.myInboundAdapter.Start()
}
2022-08-22 21:20:05 +08:00
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)
}
}()
2022-08-22 18:53:47 +08:00
}
2022-08-22 21:20:05 +08:00
if common.Contains(h.transport.Network(), N.NetworkUDP) {
udpConn, err := h.myInboundAdapter.ListenUDP()
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
}
func (h *VMess) Close() error {
return common.Close(
2022-08-01 12:23:34 +08:00
h.service,
2022-07-30 22:00:04 +08:00
&h.myInboundAdapter,
common.PtrOrNil(h.tlsConfig),
2022-08-22 18:53:47 +08:00
h.transport,
2022-07-30 22:00:04 +08:00
)
}
2022-07-18 12:32:31 +08:00
func (h *VMess) NewConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-07-25 08:14:09 +08:00
if h.tlsConfig != nil {
2022-07-30 22:00:04 +08:00
conn = tls.Server(conn, h.tlsConfig.Config())
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
}
func (h *VMess) newTransportConnection(ctx context.Context, conn net.Conn, metadata adapter.InboundContext) error {
2022-08-23 19:44:40 +08:00
metadata = h.createMetadata(conn, metadata)
2022-08-22 18:53:47 +08:00
return h.service.NewConnection(adapter.WithContext(log.ContextWithNewID(ctx), &metadata), conn, adapter.UpstreamMetadata(metadata))
2022-07-18 12:32:31 +08:00
}
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)
}