Skip to content

Commit

Permalink
feat: VLESS URI 输入兼容 Shadowrocket 导出格式
Browse files Browse the repository at this point in the history
  • Loading branch information
xream committed Jan 15, 2024
1 parent f4c4cdb commit de98bc3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 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.162",
"version": "2.14.164",
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
"main": "src/main.js",
"scripts": {
Expand Down
53 changes: 46 additions & 7 deletions backend/src/core/proxy-utils/parsers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,27 @@ function URI_VLESS() {
};
const parse = (line) => {
line = line.split('vless://')[1];
let isShadowrocket;
let parsed = /^(.*?)@(.*?):(\d+)\/?(\?(.*?))?(?:#(.*?))?$/.exec(line);
if (!parsed) {
// eslint-disable-next-line no-unused-vars
let [_, base64, other] = /^(.*?)(\?.*?$)/.exec(line);
line = `${Base64.decode(base64)}${other}`;
parsed = /^(.*?)@(.*?):(\d+)\/?(\?(.*?))?(?:#(.*?))?$/.exec(line);
isShadowrocket = true;
}
// eslint-disable-next-line no-unused-vars
let [__, uuid, server, port, ___, addons = '', name] =
/^(.*?)@(.*?):(\d+)\/?(\?(.*?))?(?:#(.*?))?$/.exec(line);
let [__, uuid, server, port, ___, addons = '', name] = parsed;
if (isShadowrocket) {
uuid = uuid.replace(/^.*?:/g, '');
}

port = parseInt(`${port}`, 10);
uuid = decodeURIComponent(uuid);
if (name != null) {
name = decodeURIComponent(name);
}
name = name ?? `VLESS ${server}:${port}`;

const proxy = {
type: 'vless',
name,
Expand All @@ -355,9 +367,29 @@ function URI_VLESS() {
params[key] = value;
}

proxy.name = name ?? params.remarks ?? `VLESS ${server}:${port}`;

proxy.tls = params.security && params.security !== 'none';
proxy.sni = params.sni;
if (
isShadowrocket &&
!proxy.tls &&
params.tls &&
!/0|none|false/.test(params.tls)
) {
proxy.tls = true;
params.security = params.security ?? 'reality';
}
proxy.sni = params.sni ?? params.peer;
proxy.flow = params.flow;
if (!proxy.flow && isShadowrocket && params.xtls) {
// "none" is undefined
const flow = [undefined, 'xtls-rprx-direct', 'xtls-rprx-vision'][
params.xtls
];
if (flow) {
proxy.flow = flow;
}
}
proxy['client-fingerprint'] = params.fp;
proxy.alpn = params.alpn ? params.alpn.split(',') : undefined;
proxy['skip-cert-verify'] = /(TRUE)|1/i.test(params.allowInsecure);
Expand All @@ -371,21 +403,28 @@ function URI_VLESS() {
opts['short-id'] = params.sid;
}
if (Object.keys(opts).length > 0) {
// proxy[`${params.security}-opts`] = opts;
proxy[`${params.security}-opts`] = opts;
}
}

proxy.network = params.type;
if (!proxy.network && isShadowrocket && params.obfs) {
proxy.network = params.obfs;
}
if (proxy.network && !['tcp', 'none'].includes(proxy.network)) {
const opts = {};
if (params.path) {
opts.path = params.path;
}
if (params.host) {
opts.headers = { Host: params.host };
}
if (params.serviceName) {
opts[`${proxy.network}-service-name`] = params.serviceName;
} else if (isShadowrocket && params.path) {
opts[`${proxy.network}-service-name`] = params.path;
delete params.path;
}
if (params.path) {
opts.path = params.path;
}
// https://github.com/XTLS/Xray-core/issues/91
if (['grpc'].includes(proxy.network)) {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/utils/flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function getFlowHeaders(url, ua, timeout) {
const cached = headersResourceCache.get(url);
let flowInfo;
if (!$arguments?.noCache && cached) {
$.info(`使用缓存的流量信息: ${url}`);
// $.info(`使用缓存的流量信息: ${url}`);
flowInfo = cached;
} else {
const { defaultFlowUserAgent, defaultTimeout } = $.read(SETTINGS_KEY);
Expand All @@ -42,7 +42,7 @@ export async function getFlowHeaders(url, ua, timeout) {
const requestTimeout = timeout || defaultTimeout;
const http = HTTP();
try {
$.info(`使用 HEAD 方法获取流量信息: ${url}`);
// $.info(`使用 HEAD 方法获取流量信息: ${url}`);
const { headers } = await http.head({
url: url
.split(/[\r\n]+/)
Expand Down

0 comments on commit de98bc3

Please sign in to comment.