Skip to content

Commit

Permalink
feat: 支持 WireGuard URI 输入和输出
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Apr 16, 2024
1 parent 93a5ce6 commit e24de8d
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sub-store",
"version": "2.14.282",
"version": "2.14.283",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
84 changes: 84 additions & 0 deletions backend/src/core/proxy-utils/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,89 @@ function URI_TUIC() {
};
return { name, test, parse };
}
function URI_WireGuard() {
const name = 'URI WireGuard Parser';
const test = (line) => {
return /^(wireguard|wg):\/\//.test(line);
};
const parse = (line) => {
line = line.split(/(wireguard|wg):\/\//)[2];
/* eslint-disable no-unused-vars */
let [
__,
___,
privateKey,
server,
____,
port,
_____,
addons = '',
name,
] = /^((.*?)@)?(.*?)(:(\d+))?\/?(\?(.*?))?(?:#(.*?))?$/.exec(line);
/* eslint-enable no-unused-vars */

port = parseInt(`${port}`, 10);
if (isNaN(port)) {
port = 51820;
}
privateKey = decodeURIComponent(privateKey);
if (name != null) {
name = decodeURIComponent(name);
}
name = name ?? `WireGuard ${server}:${port}`;
const proxy = {
type: 'wireguard',
name,
server,
port,
'private-key': privateKey,
udp: true,
};
for (const addon of addons.split('&')) {
let [key, value] = addon.split('=');
key = key.replace(/_/, '-');
value = decodeURIComponent(value);
if (['reserved'].includes(key)) {
const parsed = value
.split(',')
.map((i) => parseInt(i.trim(), 10))
.filter((i) => Number.isInteger(i));
if (parsed.length === 3) {
proxy[key] = parsed;
}
} else if (['address', 'ip'].includes(key)) {
value.split(',').map((i) => {
const ip = i
.trim()
.replace(/\/\d+$/, '')
.replace(/^\[/, '')
.replace(/\]$/, '');
if (isIPv4(ip)) {
proxy.ip = ip;
} else if (isIPv6(ip)) {
proxy.ipv6 = ip;
}
});
} else if (['mtu'].includes(key)) {
const parsed = parseInt(value.trim(), 10);
if (Number.isInteger(parsed)) {
proxy[key] = parsed;
}
} else if (/publickey/i.test(key)) {
proxy['public-key'] = value;
} else if (/privatekey/i.test(key)) {
proxy['private-key'] = value;
} else if (['udp'].includes(key)) {
proxy[key] = /(TRUE)|1/i.test(value);
} else if (!['flag'].includes(key)) {
proxy[key] = value;
}
}

return proxy;
};
return { name, test, parse };
}

// Trojan URI format
function URI_Trojan() {
Expand Down Expand Up @@ -1196,6 +1279,7 @@ export default [
URI_VMess(),
URI_VLESS(),
URI_TUIC(),
URI_WireGuard(),
URI_Hysteria(),
URI_Hysteria2(),
URI_Trojan(),
Expand Down
3 changes: 3 additions & 0 deletions backend/src/core/proxy-utils/preprocessors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ function Base64Encoded() {
'aHR0c', // htt
'dmxlc3M=', // vless
'aHlzdGVyaWEy', // hysteria2
'd2lyZWd1YXJkOi8v', // wireguard://
'd2c6Ly8=', // wg://
'dHVpYzovLw==', // tuic://
];

const test = function (raw) {
Expand Down
45 changes: 44 additions & 1 deletion backend/src/core/proxy-utils/producers/uri.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,51 @@ export default function URI_Producer() {
}?${tuicParams.join('&')}#${encodeURIComponent(
proxy.name,
)}`;
break;
}
break;
case 'wireguard':
let wireguardParams = [];

Object.keys(proxy).forEach((key) => {
if (
![
'name',
'type',
'server',
'port',
'ip',
'ipv6',
'private-key',
].includes(key)
) {
if (['public-key'].includes(key)) {
wireguardParams.push(`publickey=${proxy[key]}`);
} else if (['udp'].includes(key)) {
if (proxy[key]) {
wireguardParams.push(`${key}=1`);
}
} else if (proxy[key]) {
wireguardParams.push(
`${key}=${encodeURIComponent(proxy[key])}`,
);
}
}
});
if (proxy.ip && proxy.ipv6) {
wireguardParams.push(
`address=${proxy.ip}/32,${proxy.ipv6}/128`,
);
} else if (proxy.ip) {
wireguardParams.push(`address=${proxy.ip}/32`);
} else if (proxy.ipv6) {
wireguardParams.push(`address=${proxy.ipv6}/128`);
}
result = `wireguard://${encodeURIComponent(
proxy['private-key'],
)}@${proxy.server}:${proxy.port}/?${wireguardParams.join(
'&',
)}#${encodeURIComponent(proxy.name)}`;
break;
}
return result;
};
Expand Down

0 comments on commit e24de8d

Please sign in to comment.