fix(sub): carry finalmask in WireGuard share links and JSON outbounds (#7188)

The WireGuard link builders (Go and frontend) and the JSON subscription
outbound never read streamSettings.finalmask, so a WireGuard inbound with
a UDP mask produced a client config the server rejects.

The link now carries the mask as fm=<json>, the way vless, trojan and
shadowsocks already do, a Host's own mask is merged into its subscription
link, and the JSON outbound gets streamSettings.finalmask.

Closes #7185
This commit is contained in:
Nikan Zeyaei
2026-11-23 23:37:19 +03:30
committed by GitHub
parent f0d48afe6f
commit 5414381bf1
6 changed files with 94 additions and 5 deletions
+3
View File
@@ -1005,6 +1005,7 @@ export interface GenWireguardLinkInput {
port: number;
remark?: string;
peerIndex: number;
finalmask?: FinalMaskStreamSettings;
}
// Wireguard share link: wireguard://<peerPrivKey>@<host>:<port>
@@ -1029,6 +1030,7 @@ export function genWireguardLink(input: GenWireguardLinkInput): string {
if (typeof settings.mtu === 'number' && settings.mtu > 0) {
url.searchParams.set('mtu', String(settings.mtu));
}
applyFinalMaskToParams(input.finalmask, url.searchParams);
url.hash = encodeURIComponent(remark);
return url.toString();
@@ -1722,6 +1724,7 @@ function wireguardFanout(
port: e.port,
remark: tunnelPeerRemark(remark, e.remark, i, p),
peerIndex: i,
finalmask: inbound.streamSettings?.finalmask,
}),
),
);
@@ -88,3 +88,18 @@ describe('wireguard multi-client link/config fan-out', () => {
expect(links[0]).toContain('address=10.0.0.9%2F32');
});
});
describe('wireguard finalmask', () => {
it('puts the inbound finalmask into the link as fm', () => {
const base = wgInbound();
const mask = { tcp: [], udp: [{ type: 'salamander', settings: { password: 'pw' } }] };
const inbound = InboundSchema.parse({
...base,
streamSettings: { network: 'tcp', security: 'none', tcpSettings: {}, finalmask: mask },
});
const link = genWireguardLinks({ inbound, remark: 'wg', fallbackHostname: 'h.example' }).split(
'\r\n',
)[0];
expect(JSON.parse(new URL(link).searchParams.get('fm') ?? '')).toEqual(mask);
});
});
+5 -2
View File
@@ -740,7 +740,7 @@ func (s *SubJsonService) getConfig(subReq *SubService, inbound *model.Inbound, c
case "masque":
newOutbounds = append(newOutbounds, s.genMasque(inbound, newStream, client))
case "wireguard":
wgOutbound := s.genWireguard(subReq, inbound, client)
wgOutbound := s.genWireguard(subReq, inbound, newStream, client)
if wgOutbound == nil {
continue
}
@@ -1120,7 +1120,7 @@ func (s *SubJsonService) genMasque(inbound *model.Inbound, newStream map[string]
// mirroring genWireguardLink: the peer public key is derived from the inbound
// secretKey, the client owns the private key / tunnel address / pre-shared key,
// and the peer routes the full tunnel. Returns nil when the client has no key.
func (s *SubJsonService) genWireguard(subReq *SubService, inbound *model.Inbound, client model.Client) json_util.RawMessage {
func (s *SubJsonService) genWireguard(subReq *SubService, inbound *model.Inbound, stream map[string]any, client model.Client) json_util.RawMessage {
if client.PrivateKey == "" {
return nil
}
@@ -1160,6 +1160,9 @@ func (s *SubJsonService) genWireguard(subReq *SubService, inbound *model.Inbound
"tag": "proxy",
"settings": settings,
}
if finalmask, ok := stream["finalmask"].(map[string]any); ok && hasFinalMaskContent(finalmask) {
outbound["streamSettings"] = map[string]any{"finalmask": finalmask}
}
result, _ := json.MarshalIndent(outbound, "", " ")
return result
}
+24 -2
View File
@@ -533,7 +533,7 @@ func TestSubJsonServiceWireguard(t *testing.T) {
AllowedIPs: []string{"10.0.0.2/32", "fd00::2/128"},
}
raw := NewSubJsonService("", "", "", "", nil).genWireguard(&SubService{}, inbound, client)
raw := NewSubJsonService("", "", "", "", nil).genWireguard(&SubService{}, inbound, nil, client)
if raw == nil {
t.Fatal("genWireguard returned nil for a valid wireguard client")
}
@@ -577,7 +577,7 @@ func TestSubJsonServiceWireguardNoKey(t *testing.T) {
inbound := &model.Inbound{Listen: "203.0.113.9", Port: 51820, Protocol: model.WireGuard, Settings: `{}`}
client := model.Client{Email: "user"}
if raw := NewSubJsonService("", "", "", "", nil).genWireguard(&SubService{}, inbound, client); raw != nil {
if raw := NewSubJsonService("", "", "", "", nil).genWireguard(&SubService{}, inbound, nil, client); raw != nil {
t.Fatalf("genWireguard = %s, want nil for a keyless wireguard client", raw)
}
}
@@ -598,3 +598,25 @@ func TestSubJsonServiceSkipsTUIC(t *testing.T) {
t.Fatalf("getConfig emitted %d unsupported TUIC Xray config(s)", len(got))
}
}
func TestGenWireguardOutboundCarriesFinalMask(t *testing.T) {
serverPriv, _, _ := wgutil.GenerateWireguardKeypair()
clientPriv, _, _ := wgutil.GenerateWireguardKeypair()
inbound := &model.Inbound{Listen: "203.0.113.9", Port: 51820, Protocol: model.WireGuard, Settings: `{"secretKey":"` + serverPriv + `"}`}
client := model.Client{Email: "user", PrivateKey: clientPriv, AllowedIPs: []string{"10.0.0.2/32"}}
stream := map[string]any{"finalmask": map[string]any{"udp": []any{map[string]any{"type": "salamander"}}}}
raw := NewSubJsonService("", "", "", "", nil).genWireguard(&SubService{}, inbound, stream, client)
var out struct {
StreamSettings struct {
Finalmask struct {
UDP []struct{ Type string } `json:"udp"`
} `json:"finalmask"`
} `json:"streamSettings"`
}
if err := json.Unmarshal(raw, &out); err != nil {
t.Fatal(err)
}
if len(out.StreamSettings.Finalmask.UDP) != 1 || out.StreamSettings.Finalmask.UDP[0].Type != "salamander" {
t.Fatalf("outbound = %s, want salamander finalmask", raw)
}
}
+6 -1
View File
@@ -1133,11 +1133,16 @@ func (s *SubService) genWireguardLink(inbound *model.Inbound, email string) stri
if ka := client.KeepAliveSeconds(); ka > 0 {
params["keepalive"] = strconv.Itoa(ka)
}
if finalmask, ok := unmarshalStreamSettings(inbound.StreamSettings)["finalmask"].(map[string]any); ok {
applyFinalMaskParams(finalmask, params)
}
endpoints := s.advertisedEndpoints(inbound)
links := make([]string, 0, len(endpoints))
for _, e := range endpoints {
link := fmt.Sprintf("wireguard://%s@%s", encodeUserinfo(client.PrivateKey), joinHostPort(e.Address, e.Port))
links = append(links, buildLinkWithParams(link, params, s.endpointRemark(inbound, email, e.ep, "")))
epParams := cloneStringMap(params)
applyEndpointFinalMask(e, epParams)
links = append(links, buildLinkWithParams(link, epParams, s.endpointRemark(inbound, email, e.ep, "")))
}
return strings.Join(links, "\n")
}
+41
View File
@@ -388,3 +388,44 @@ func TestGetSubs_WireGuardEmptyOptionalTunnelFieldsDoNotInheritShared(t *testing
t.Fatalf("link inherited shared tunnel fields: %s", links[0])
}
}
func TestGenWireguardLinkCarriesFinalMask(t *testing.T) {
serverPriv, _, _ := wgutil.GenerateWireguardKeypair()
clientPriv, _, _ := wgutil.GenerateWireguardKeypair()
inbound := &model.Inbound{
Listen: "203.0.113.7",
Port: 51820,
Protocol: model.WireGuard,
Settings: `{"secretKey":"` + serverPriv + `","clients":[{"email":"user","privateKey":"` + clientPriv + `","allowedIPs":["10.0.0.2/32"]}]}`,
StreamSettings: `{"finalmask":{"udp":[{"type":"salamander","settings":{"password":"pw"}}]}}`,
}
link := (&SubService{}).genWireguardLink(inbound, "user")
u, err := url.Parse(link)
if err != nil {
t.Fatal(err)
}
fm := u.Query().Get("fm")
if !strings.Contains(fm, `"type":"salamander"`) || !strings.Contains(fm, `"password":"pw"`) {
t.Fatalf("fm = %q, want salamander mask", fm)
}
}
func TestGenWireguardLinkMergesHostFinalMask(t *testing.T) {
serverPriv, _, _ := wgutil.GenerateWireguardKeypair()
clientPriv, _, _ := wgutil.GenerateWireguardKeypair()
inbound := &model.Inbound{
Listen: "203.0.113.7",
Port: 51820,
Protocol: model.WireGuard,
Settings: `{"secretKey":"` + serverPriv + `","clients":[{"email":"user","privateKey":"` + clientPriv + `","allowedIPs":["10.0.0.2/32"]}]}`,
StreamSettings: `{"externalProxy":[{"dest":"edge.example.com","port":443,"forceTls":"same","finalMask":"{\"udp\":[{\"type\":\"salamander\",\"settings\":{\"password\":\"host\"}}]}"}]}`,
}
link := (&SubService{}).genWireguardLink(inbound, "user")
u, err := url.Parse(link)
if err != nil {
t.Fatal(err)
}
if u.Host != "edge.example.com:443" || !strings.Contains(u.Query().Get("fm"), `"password":"host"`) {
t.Fatalf("link = %s, want host endpoint with host finalmask", link)
}
}