mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-11 13:58:15 +03:00
Add vless encryption
This commit is contained in:
@@ -2,8 +2,11 @@ package vless
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/sagernet/sing-box/adapter"
|
||||
"github.com/sagernet/sing-box/adapter/inbound"
|
||||
@@ -14,6 +17,7 @@ import (
|
||||
C "github.com/sagernet/sing-box/constant"
|
||||
"github.com/sagernet/sing-box/log"
|
||||
"github.com/sagernet/sing-box/option"
|
||||
"github.com/sagernet/sing-box/protocol/vless/encryption"
|
||||
"github.com/sagernet/sing-box/transport/v2ray"
|
||||
"github.com/sagernet/sing-vmess/packetaddr"
|
||||
"github.com/sagernet/sing-vmess/vless"
|
||||
@@ -35,14 +39,15 @@ var _ adapter.TCPInjectableInbound = (*Inbound)(nil)
|
||||
|
||||
type Inbound struct {
|
||||
inbound.Adapter
|
||||
ctx context.Context
|
||||
router adapter.ConnectionRouterEx
|
||||
logger logger.ContextLogger
|
||||
listener *listener.Listener
|
||||
users []option.VLESSUser
|
||||
service *vless.Service[int]
|
||||
tlsConfig tls.ServerConfig
|
||||
transport adapter.V2RayServerTransport
|
||||
ctx context.Context
|
||||
router adapter.ConnectionRouterEx
|
||||
logger logger.ContextLogger
|
||||
listener *listener.Listener
|
||||
users []option.VLESSUser
|
||||
service *vless.Service[int]
|
||||
tlsConfig tls.ServerConfig
|
||||
transport adapter.V2RayServerTransport
|
||||
decryption *encryption.ServerInstance
|
||||
}
|
||||
|
||||
func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLogger, tag string, options option.VLESSInboundOptions) (adapter.Inbound, error) {
|
||||
@@ -79,6 +84,18 @@ func NewInbound(ctx context.Context, router adapter.Router, logger log.ContextLo
|
||||
return nil, E.Cause(err, "create server transport: ", options.Transport.Type)
|
||||
}
|
||||
}
|
||||
// Parse decryption configuration
|
||||
if options.Decryption != "" && options.Decryption != "none" {
|
||||
decryptionConfig, err := parseServerDecryption(options.Decryption)
|
||||
if err != nil {
|
||||
return nil, E.Cause(err, "parse decryption")
|
||||
}
|
||||
inbound.decryption = &encryption.ServerInstance{}
|
||||
if err := inbound.decryption.Init(decryptionConfig.keys, decryptionConfig.xorMode, decryptionConfig.secondsFrom, decryptionConfig.secondsTo, decryptionConfig.padding); err != nil {
|
||||
return nil, E.Cause(err, "initialize decryption")
|
||||
}
|
||||
logger.Debug("decryption initialized with ", len(decryptionConfig.keys), " keys xorMode=", decryptionConfig.xorMode, " secondsFrom=", decryptionConfig.secondsFrom, " secondsTo=", decryptionConfig.secondsTo, " padding=", decryptionConfig.padding)
|
||||
}
|
||||
inbound.listener = listener.New(listener.Options{
|
||||
Context: ctx,
|
||||
Logger: logger,
|
||||
@@ -130,6 +147,9 @@ func (h *Inbound) Start(stage adapter.StartStage) error {
|
||||
}
|
||||
|
||||
func (h *Inbound) Close() error {
|
||||
if h.decryption != nil {
|
||||
h.decryption.Close()
|
||||
}
|
||||
return common.Close(
|
||||
h.service,
|
||||
h.listener,
|
||||
@@ -139,6 +159,14 @@ func (h *Inbound) Close() error {
|
||||
}
|
||||
|
||||
func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc) {
|
||||
canSplice := h.transport == nil
|
||||
if canSplice && h.decryption != nil && h.decryption.IsFullRandomXorMode() {
|
||||
canSplice = false
|
||||
}
|
||||
h.newConnectionExInternal(ctx, conn, metadata, onClose, canSplice)
|
||||
}
|
||||
|
||||
func (h *Inbound) newConnectionExInternal(ctx context.Context, conn net.Conn, metadata adapter.InboundContext, onClose N.CloseHandlerFunc, canSplice bool) {
|
||||
if h.tlsConfig != nil && h.transport == nil {
|
||||
tlsConn, err := tls.ServerHandshake(ctx, conn, h.tlsConfig)
|
||||
if err != nil {
|
||||
@@ -148,7 +176,17 @@ func (h *Inbound) NewConnectionEx(ctx context.Context, conn net.Conn, metadata a
|
||||
}
|
||||
conn = tlsConn
|
||||
}
|
||||
err := h.service.NewConnection(adapter.WithContext(ctx, &metadata), conn, metadata.Source, onClose)
|
||||
// Apply decryption if configured
|
||||
if h.decryption != nil {
|
||||
encConn, err := h.decryption.Handshake(conn, nil)
|
||||
if err != nil {
|
||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source, ": encryption handshake"))
|
||||
return
|
||||
}
|
||||
conn = encConn
|
||||
}
|
||||
err := h.service.NewConnectionWithOptions(adapter.WithContext(ctx, &metadata), conn, metadata.Source, onClose, canSplice)
|
||||
if err != nil {
|
||||
N.CloseOnHandshakeFailure(conn, onClose, err)
|
||||
h.logger.ErrorContext(ctx, E.Cause(err, "process connection from ", metadata.Source))
|
||||
@@ -197,6 +235,90 @@ func (h *Inbound) newPacketConnectionEx(ctx context.Context, conn N.PacketConn,
|
||||
h.router.RoutePacketConnectionEx(ctx, conn, metadata, onClose)
|
||||
}
|
||||
|
||||
type serverDecryptionConfig struct {
|
||||
keys [][]byte
|
||||
xorMode uint32
|
||||
secondsFrom int64
|
||||
secondsTo int64
|
||||
padding string
|
||||
}
|
||||
|
||||
func parseServerDecryption(raw string) (serverDecryptionConfig, error) {
|
||||
var cfg serverDecryptionConfig
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return cfg, E.New("empty decryption string")
|
||||
}
|
||||
parts := strings.Split(raw, ".")
|
||||
if len(parts) < 4 {
|
||||
return cfg, E.New("invalid decryption string: missing components")
|
||||
}
|
||||
if parts[0] != "mlkem768x25519plus" {
|
||||
return cfg, E.New("unsupported decryption prefix: ", parts[0])
|
||||
}
|
||||
switch parts[1] {
|
||||
case "native":
|
||||
cfg.xorMode = 0
|
||||
case "xorpub":
|
||||
cfg.xorMode = 1
|
||||
case "random":
|
||||
cfg.xorMode = 2
|
||||
default:
|
||||
return cfg, E.New("unknown decryption mode: ", parts[1])
|
||||
}
|
||||
|
||||
secondsToken := strings.TrimSpace(parts[2])
|
||||
if secondsToken == "" {
|
||||
return cfg, E.New("invalid decryption seconds segment")
|
||||
}
|
||||
trimmed := strings.TrimSuffix(secondsToken, "s")
|
||||
if trimmed == "" {
|
||||
return cfg, E.New("invalid decryption seconds segment")
|
||||
}
|
||||
values := strings.SplitN(trimmed, "-", 2)
|
||||
secondsFrom, err := strconv.ParseInt(values[0], 10, 64)
|
||||
if err != nil {
|
||||
return cfg, E.Cause(err, "parse decryption seconds_from")
|
||||
}
|
||||
cfg.secondsFrom = secondsFrom
|
||||
if len(values) == 2 && values[1] != "" {
|
||||
secondsTo, err := strconv.ParseInt(values[1], 10, 64)
|
||||
if err != nil {
|
||||
return cfg, E.Cause(err, "parse decryption seconds_to")
|
||||
}
|
||||
cfg.secondsTo = secondsTo
|
||||
}
|
||||
|
||||
paddingPhase := true
|
||||
var paddingParts []string
|
||||
for _, segment := range parts[3:] {
|
||||
segment = strings.TrimSpace(segment)
|
||||
if segment == "" {
|
||||
return cfg, E.New("invalid empty segment in decryption string")
|
||||
}
|
||||
if data, err := base64.RawURLEncoding.DecodeString(segment); err == nil {
|
||||
if len(data) == 32 || len(data) == 64 {
|
||||
cfg.keys = append(cfg.keys, data)
|
||||
paddingPhase = false
|
||||
continue
|
||||
}
|
||||
return cfg, E.New("invalid decryption key length: ", len(data))
|
||||
}
|
||||
if paddingPhase {
|
||||
paddingParts = append(paddingParts, segment)
|
||||
continue
|
||||
}
|
||||
return cfg, E.New("invalid decryption key: ", segment)
|
||||
}
|
||||
if len(cfg.keys) == 0 {
|
||||
return cfg, E.New("no valid decryption keys found in decryption string")
|
||||
}
|
||||
if len(paddingParts) > 0 {
|
||||
cfg.padding = strings.Join(paddingParts, ".")
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
var _ adapter.V2RayServerTransportHandler = (*inboundTransportHandler)(nil)
|
||||
|
||||
type inboundTransportHandler Inbound
|
||||
@@ -210,5 +332,5 @@ func (h *inboundTransportHandler) NewConnectionEx(ctx context.Context, conn net.
|
||||
//nolint:staticcheck
|
||||
metadata.InboundOptions = h.listener.ListenOptions().InboundOptions
|
||||
h.logger.InfoContext(ctx, "inbound connection from ", metadata.Source)
|
||||
(*Inbound)(h).NewConnectionEx(ctx, conn, metadata, onClose)
|
||||
(*Inbound)(h).newConnectionExInternal(ctx, conn, metadata, onClose, false)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user