feat(nodes): per-node mux/XUDP + sockopt (TCP FastOpen/keepalive, mark) [T1]
Adds optional per-node multiplexing and socket options, applied after
ParseShareLink at all three outbound build sites (group members, standalone
node targets, chain hops) and BEFORE withMark so the loop-guard escape mark
(255) is always re-asserted last and wins — a per-node mark that is neither 0
nor 255 is refused (would break the nft escape rule → routing loop).
- model.go: Node.{Mux,MuxConcurrency,XUDPConcurrency,XUDPProxyUDP443,Mark,
TCPFastOpen,TCPKeepAliveIdle}; uci.go parses them. All zero-value = off, so
existing nodes emit byte-identical outbounds.
- generate.go: applyNodeOpts() emits outbound-level `mux` (XUDP keys only for
vless/vmess) + streamSettings.sockopt tcp knobs; skips mux under XTLS Vision.
- nodes.js: modalonly mux/XUDP/sockopt fields (depends on mux).
- tests: node_opts_test.go (mux emitted/off, XUDP gating, vision skip,
loop-guard mark wins, tcp knobs).
Verified live on the 512M VM: mux node emits {enabled,concurrency:8,
xudpConcurrency:16,xudpProxyUDP443:reject}; sockopt.mark==255 preserved;
xray -test OK. r9.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
146ae252cc
commit
ee7df9f3a1
8 files changed
+275
-5
No files matched your search
@@ -65,6 +65,14 @@ config node
|
|||||||
option name 'reality-nl'
|
option name 'reality-nl'
|
||||||
option enabled '1'
|
option enabled '1'
|
||||||
option uri 'vless://...' # share-link, xrayctl парсит
|
option uri 'vless://...' # share-link, xrayctl парсит
|
||||||
|
# --- per-node мультиплексирование + sockopt (T1); все опциональны ---
|
||||||
|
option mux '0' # мультиплексирование вкл/выкл (игнор. для VLESS XTLS-Vision)
|
||||||
|
option mux_concurrency '8' # потоков на mux-соединение
|
||||||
|
option xudp_concurrency '16' # только vless/vmess
|
||||||
|
option xudp_udp443 'reject' # reject|allow|skip (vless/vmess)
|
||||||
|
option sockopt_mark '0' # 0 = loop-guard 255 (иное значение игнорируется)
|
||||||
|
option tcp_fast_open '' # ''|1|0
|
||||||
|
option tcp_keepalive_idle '0' # секунды; 0 = off
|
||||||
```
|
```
|
||||||
|
|
||||||
### group (0..N)
|
### group (0..N)
|
||||||
|
|||||||
@@ -254,6 +254,52 @@ return view.extend({
|
|||||||
o.placeholder = 'vless://...';
|
o.placeholder = 'vless://...';
|
||||||
o.monospace = true;
|
o.monospace = true;
|
||||||
|
|
||||||
|
// --- per-node multiplexing + sockopt (T1); modalonly so the grid stays clean ---
|
||||||
|
o = s.option(form.Flag, 'mux', _('Multiplexing (mux)'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.default = '0';
|
||||||
|
o.description = _('Enable connection multiplexing for this node. Ignored for VLESS XTLS-Vision.');
|
||||||
|
|
||||||
|
o = s.option(form.Value, 'mux_concurrency', _('Mux concurrency'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.datatype = 'range(1,1024)';
|
||||||
|
o.default = '8';
|
||||||
|
o.depends('mux', '1');
|
||||||
|
|
||||||
|
o = s.option(form.Value, 'xudp_concurrency', _('XUDP concurrency'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.datatype = 'uinteger';
|
||||||
|
o.default = '16';
|
||||||
|
o.depends('mux', '1');
|
||||||
|
o.description = _('VLESS/VMess only — ignored for other protocols.');
|
||||||
|
|
||||||
|
o = s.option(form.ListValue, 'xudp_udp443', _('XUDP UDP/443 policy'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.value('reject', _('reject (recommended)'));
|
||||||
|
o.value('allow', _('allow'));
|
||||||
|
o.value('skip', _('skip (direct)'));
|
||||||
|
o.default = 'reject';
|
||||||
|
o.depends('mux', '1');
|
||||||
|
|
||||||
|
o = s.option(form.ListValue, 'tcp_fast_open', _('TCP Fast Open'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.value('', _('inherit'));
|
||||||
|
o.value('1', _('on'));
|
||||||
|
o.value('0', _('off'));
|
||||||
|
o.default = '';
|
||||||
|
|
||||||
|
o = s.option(form.Value, 'tcp_keepalive_idle', _('TCP keepalive idle (s)'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.datatype = 'uinteger';
|
||||||
|
o.default = '0';
|
||||||
|
|
||||||
|
o = s.option(form.Value, 'sockopt_mark', _('Socket mark (advanced)'));
|
||||||
|
o.modalonly = true;
|
||||||
|
o.datatype = 'uinteger';
|
||||||
|
o.default = '0';
|
||||||
|
o.description = _('Leave 0. The loop-guard escape mark 255 is enforced regardless; ' +
|
||||||
|
'a different value is ignored to prevent routing loops.');
|
||||||
|
|
||||||
return m.render().then(function(mapEl) {
|
return m.render().then(function(mapEl) {
|
||||||
draw();
|
draw();
|
||||||
poll.add(function() {
|
poll.add(function() {
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ include $(TOPDIR)/rules.mk
|
|||||||
|
|
||||||
PKG_NAME:=shater-core
|
PKG_NAME:=shater-core
|
||||||
PKG_VERSION:=0.1.0
|
PKG_VERSION:=0.1.0
|
||||||
PKG_RELEASE:=8
|
PKG_RELEASE:=9
|
||||||
|
|
||||||
PKG_MAINTAINER:=Shater <maqrota@icloud.com>
|
PKG_MAINTAINER:=Shater <maqrota@icloud.com>
|
||||||
PKG_LICENSE:=GPL-2.0-or-later
|
PKG_LICENSE:=GPL-2.0-or-later
|
||||||
|
|||||||
+1
-1
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
|
|||||||
|
|
||||||
PKG_NAME:=xrayctl
|
PKG_NAME:=xrayctl
|
||||||
PKG_VERSION:=0.1.0
|
PKG_VERSION:=0.1.0
|
||||||
PKG_RELEASE:=8
|
PKG_RELEASE:=9
|
||||||
|
|
||||||
PKG_MAINTAINER:=shater
|
PKG_MAINTAINER:=shater
|
||||||
PKG_LICENSE:=MIT
|
PKG_LICENSE:=MIT
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -319,6 +320,7 @@ func (b *builder) emitGroup(g Group, prefix, balTag, proxyTag string) (string, e
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue // skip unparseable node, keep going
|
continue // skip unparseable node, keep going
|
||||||
}
|
}
|
||||||
|
b.applyNodeOpts(ob, nd)
|
||||||
withMark(ob)
|
withMark(ob)
|
||||||
if proxyTag != "" {
|
if proxyTag != "" {
|
||||||
ob["proxySettings"] = map[string]any{"tag": proxyTag}
|
ob["proxySettings"] = map[string]any{"tag": proxyTag}
|
||||||
@@ -410,6 +412,7 @@ func (b *builder) buildStandaloneNodes() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("node %q: %w", name, err)
|
return fmt.Errorf("node %q: %w", name, err)
|
||||||
}
|
}
|
||||||
|
b.applyNodeOpts(ob, nd)
|
||||||
withMark(ob)
|
withMark(ob)
|
||||||
b.outbounds = append(b.outbounds, ob)
|
b.outbounds = append(b.outbounds, ob)
|
||||||
b.emittedTag[tag] = true
|
b.emittedTag[tag] = true
|
||||||
@@ -508,6 +511,7 @@ func (b *builder) emitChainLayer(c Chain, i int, hop, proxyTag string) (string,
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", fmt.Errorf("chain %q hop %q: %w", c.Name, hop, err)
|
return "", fmt.Errorf("chain %q hop %q: %w", c.Name, hop, err)
|
||||||
}
|
}
|
||||||
|
b.applyNodeOpts(ob, *nd)
|
||||||
withMark(ob)
|
withMark(ob)
|
||||||
if proxyTag != "" {
|
if proxyTag != "" {
|
||||||
ob["proxySettings"] = map[string]any{"tag": proxyTag}
|
ob["proxySettings"] = map[string]any{"tag": proxyTag}
|
||||||
@@ -711,6 +715,79 @@ func (b *builder) setTargetTag(rule map[string]any, tag string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// withMark injects the loop-guard fwmark into an outbound's streamSettings.sockopt.
|
// withMark injects the loop-guard fwmark into an outbound's streamSettings.sockopt.
|
||||||
|
// outboundHasVisionFlow reports whether a vless outbound uses XTLS Vision, which
|
||||||
|
// is incompatible with mux (xray disables/ignores mux under vision).
|
||||||
|
func outboundHasVisionFlow(ob Outbound) bool {
|
||||||
|
st, _ := ob["settings"].(map[string]any)
|
||||||
|
if st == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
vnext, _ := st["vnext"].([]any)
|
||||||
|
if len(vnext) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
v0, _ := vnext[0].(map[string]any)
|
||||||
|
users, _ := v0["users"].([]any)
|
||||||
|
if len(users) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
u0, _ := users[0].(map[string]any)
|
||||||
|
flow, _ := u0["flow"].(string)
|
||||||
|
return strings.Contains(flow, "xtls-rprx-vision")
|
||||||
|
}
|
||||||
|
|
||||||
|
// applyNodeOpts attaches per-node mux/XUDP (outbound top-level) and non-mark
|
||||||
|
// sockopt (streamSettings.sockopt) to an already-parsed outbound. It MUST be
|
||||||
|
// called BEFORE withMark so the loop-guard mark (255) is re-asserted last and
|
||||||
|
// always wins — a per-node mark that is neither 0 (inherit) nor 255 is refused,
|
||||||
|
// otherwise that node's egress would no longer be recognised by the nft escape
|
||||||
|
// rule and would loop back into the tproxy.
|
||||||
|
func (b *builder) applyNodeOpts(ob Outbound, nd Node) {
|
||||||
|
if nd.Mux && !outboundHasVisionFlow(ob) {
|
||||||
|
conc := nd.MuxConcurrency
|
||||||
|
if conc <= 0 {
|
||||||
|
conc = 8
|
||||||
|
}
|
||||||
|
mux := map[string]any{"enabled": true, "concurrency": conc}
|
||||||
|
// XUDP (UDP-over-mux) is only meaningful for vless/vmess.
|
||||||
|
if p, _ := ob["protocol"].(string); p == "vless" || p == "vmess" {
|
||||||
|
if nd.XUDPConcurrency != 0 {
|
||||||
|
mux["xudpConcurrency"] = nd.XUDPConcurrency
|
||||||
|
}
|
||||||
|
if nd.XUDPProxyUDP443 != "" {
|
||||||
|
mux["xudpProxyUDP443"] = nd.XUDPProxyUDP443
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ob["mux"] = mux
|
||||||
|
}
|
||||||
|
if nd.TCPFastOpen != "" || nd.TCPKeepAliveIdle != 0 {
|
||||||
|
ss, _ := ob["streamSettings"].(map[string]any)
|
||||||
|
if ss == nil {
|
||||||
|
ss = map[string]any{}
|
||||||
|
ob["streamSettings"] = ss
|
||||||
|
}
|
||||||
|
sock, _ := ss["sockopt"].(map[string]any)
|
||||||
|
if sock == nil {
|
||||||
|
sock = map[string]any{}
|
||||||
|
ss["sockopt"] = sock
|
||||||
|
}
|
||||||
|
switch nd.TCPFastOpen {
|
||||||
|
case "1":
|
||||||
|
sock["tcpFastOpen"] = true
|
||||||
|
case "0":
|
||||||
|
sock["tcpFastOpen"] = false
|
||||||
|
}
|
||||||
|
if nd.TCPKeepAliveIdle != 0 {
|
||||||
|
sock["tcpKeepAliveIdle"] = nd.TCPKeepAliveIdle
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if nd.Mark != 0 && nd.Mark != loopMark {
|
||||||
|
fmt.Fprintf(os.Stderr,
|
||||||
|
"note: node %q sockopt_mark=%d ignored — loop-guard mark %d is authoritative\n",
|
||||||
|
nd.Name, nd.Mark, loopMark)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func withMark(ob Outbound) Outbound {
|
func withMark(ob Outbound) Outbound {
|
||||||
ss, _ := ob["streamSettings"].(map[string]any)
|
ss, _ := ob["streamSettings"].(map[string]any)
|
||||||
if ss == nil {
|
if ss == nil {
|
||||||
|
|||||||
@@ -85,6 +85,16 @@ type Node struct {
|
|||||||
FromSub string // "" = manual; else subscription name
|
FromSub string // "" = manual; else subscription name
|
||||||
Fingerprint string
|
Fingerprint string
|
||||||
Stale bool
|
Stale bool
|
||||||
|
|
||||||
|
// Per-node multiplexing + sockopt (T1). All optional; zero values = off, so
|
||||||
|
// existing nodes emit byte-identical outbounds. See applyNodeOpts in generate.go.
|
||||||
|
Mux bool // enable outbound mux
|
||||||
|
MuxConcurrency int // streams per mux connection (xray `concurrency`)
|
||||||
|
XUDPConcurrency int // vless/vmess only (UDP-over-mux)
|
||||||
|
XUDPProxyUDP443 string // reject|allow|skip (vless/vmess only)
|
||||||
|
Mark uint32 // sockopt mark; 0 = use loop-guard 255 (a different value is refused)
|
||||||
|
TCPFastOpen string // "" inherit | "1" on | "0" off
|
||||||
|
TCPKeepAliveIdle int // seconds; 0 = omit
|
||||||
}
|
}
|
||||||
|
|
||||||
// Group is a `config group`.
|
// Group is a `config group`.
|
||||||
|
|||||||
@@ -0,0 +1,122 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
// outboundByTagContains returns the first outbound whose tag contains sub.
|
||||||
|
func outboundByTagContains(cfg map[string]any, sub string) map[string]any {
|
||||||
|
for _, o := range cfg["outbounds"].([]any) {
|
||||||
|
ob := o.(map[string]any)
|
||||||
|
if tag, _ := ob["tag"].(string); tag != "" && contains(tag, sub) {
|
||||||
|
return ob
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func contains(s, sub string) bool {
|
||||||
|
for i := 0; i+len(sub) <= len(s); i++ {
|
||||||
|
if s[i:i+len(sub)] == sub {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func muxOf(ob map[string]any) map[string]any {
|
||||||
|
if ob == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
m, _ := ob["mux"].(map[string]any)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func sockoptOf(ob map[string]any) map[string]any {
|
||||||
|
if ob == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ss, _ := ob["streamSettings"].(map[string]any)
|
||||||
|
if ss == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
so, _ := ss["sockopt"].(map[string]any)
|
||||||
|
return so
|
||||||
|
}
|
||||||
|
|
||||||
|
func buildOneNode(t *testing.T, nd Node) map[string]any {
|
||||||
|
t.Helper()
|
||||||
|
m := &Model{Globals: defaultGlobals()}
|
||||||
|
nd.Enabled = true
|
||||||
|
m.Nodes = []Node{nd}
|
||||||
|
m.Rules = []Rule{{Name: "r", Enabled: true, Order: 10, Target: "node:" + nd.Name}}
|
||||||
|
cfg, err := BuildConfig(m)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("BuildConfig: %v", err)
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeMuxEmitted(t *testing.T) {
|
||||||
|
cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, Mux: true, MuxConcurrency: 4})
|
||||||
|
ob := outboundByTagContains(cfg, "node_n1")
|
||||||
|
mux := muxOf(ob)
|
||||||
|
if mux == nil || mux["enabled"] != true {
|
||||||
|
t.Fatalf("mux not enabled: %v", ob)
|
||||||
|
}
|
||||||
|
if mux["concurrency"] != 4 {
|
||||||
|
t.Fatalf("concurrency = %v, want 4", mux["concurrency"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeMuxDefaultOff(t *testing.T) {
|
||||||
|
cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality})
|
||||||
|
ob := outboundByTagContains(cfg, "node_n1")
|
||||||
|
if _, ok := ob["mux"]; ok {
|
||||||
|
t.Fatalf("mux key present on a mux-off node: %v", ob["mux"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeXUDPOnlyVlessVmess(t *testing.T) {
|
||||||
|
// vless: XUDP keys present.
|
||||||
|
cfg := buildOneNode(t, Node{Name: "nv", URI: "vless://11111111-1111-1111-1111-111111111111@a.example.com:443?type=tcp&security=reality&pbk=PBK&sid=aa&sni=www.microsoft.com#nv", Mux: true, XUDPConcurrency: 16, XUDPProxyUDP443: "reject"})
|
||||||
|
mux := muxOf(outboundByTagContains(cfg, "node_nv"))
|
||||||
|
if mux["xudpConcurrency"] != 16 || mux["xudpProxyUDP443"] != "reject" {
|
||||||
|
t.Fatalf("vless XUDP keys missing: %v", mux)
|
||||||
|
}
|
||||||
|
// trojan: mux present but no XUDP keys.
|
||||||
|
cfg = buildOneNode(t, Node{Name: "nt", URI: "trojan://pw@b.example.com:443?security=tls&sni=b.example.com#nt", Mux: true, XUDPConcurrency: 16, XUDPProxyUDP443: "reject"})
|
||||||
|
mux = muxOf(outboundByTagContains(cfg, "node_nt"))
|
||||||
|
if mux == nil || mux["enabled"] != true {
|
||||||
|
t.Fatalf("trojan mux not enabled: %v", mux)
|
||||||
|
}
|
||||||
|
if _, ok := mux["xudpConcurrency"]; ok {
|
||||||
|
t.Fatalf("trojan should not carry XUDP keys: %v", mux)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeMuxSkippedForVision(t *testing.T) {
|
||||||
|
uri := "vless://11111111-1111-1111-1111-111111111111@a.example.com:443?type=tcp&security=reality&pbk=PBK&sid=aa&sni=www.microsoft.com&flow=xtls-rprx-vision#nvis"
|
||||||
|
cfg := buildOneNode(t, Node{Name: "nvis", URI: uri, Mux: true})
|
||||||
|
ob := outboundByTagContains(cfg, "node_nvis")
|
||||||
|
if _, ok := ob["mux"]; ok {
|
||||||
|
t.Fatalf("mux must be skipped under XTLS Vision: %v", ob["mux"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeSockoptMarkLoopGuardWins(t *testing.T) {
|
||||||
|
cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, Mark: 1234})
|
||||||
|
so := sockoptOf(outboundByTagContains(cfg, "node_n1"))
|
||||||
|
if so == nil || so["mark"] != loopMark {
|
||||||
|
t.Fatalf("loop-guard mark not enforced, got %v", so)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeSockoptTCPKnobs(t *testing.T) {
|
||||||
|
cfg := buildOneNode(t, Node{Name: "n1", URI: vlessReality, TCPFastOpen: "1", TCPKeepAliveIdle: 30})
|
||||||
|
so := sockoptOf(outboundByTagContains(cfg, "node_n1"))
|
||||||
|
if so["tcpFastOpen"] != true || so["tcpKeepAliveIdle"] != 30 {
|
||||||
|
t.Fatalf("tcp knobs missing: %v", so)
|
||||||
|
}
|
||||||
|
if so["mark"] != loopMark {
|
||||||
|
t.Fatalf("loop mark lost alongside tcp knobs: %v", so)
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
-3
@@ -65,9 +65,16 @@ func ParseUCIExport(text string) (*Model, error) {
|
|||||||
})
|
})
|
||||||
case "node":
|
case "node":
|
||||||
m.Nodes = append(m.Nodes, Node{
|
m.Nodes = append(m.Nodes, Node{
|
||||||
Name: s.opt("name"),
|
Name: s.opt("name"),
|
||||||
Enabled: s.optBool("enabled", true),
|
Enabled: s.optBool("enabled", true),
|
||||||
URI: s.opt("uri"),
|
URI: s.opt("uri"),
|
||||||
|
Mux: s.optBool("mux", false),
|
||||||
|
MuxConcurrency: parseInt(s.opt("mux_concurrency"), 8),
|
||||||
|
XUDPConcurrency: parseInt(s.opt("xudp_concurrency"), 16),
|
||||||
|
XUDPProxyUDP443: s.optOr("xudp_udp443", "reject"),
|
||||||
|
Mark: parseUint32(s.opt("sockopt_mark"), 0),
|
||||||
|
TCPFastOpen: s.opt("tcp_fast_open"),
|
||||||
|
TCPKeepAliveIdle: parseInt(s.opt("tcp_keepalive_idle"), 0),
|
||||||
})
|
})
|
||||||
case "group":
|
case "group":
|
||||||
m.Groups = append(m.Groups, Group{
|
m.Groups = append(m.Groups, Group{
|
||||||
|
|||||||
Reference in new issue
Block a user