Minor bug fixes

This commit is contained in:
Peng-YM 2022-06-17 20:00:21 +08:00
parent 1b526414cd
commit 0da236fbb1
10 changed files with 47 additions and 52 deletions

File diff suppressed because one or more lines are too long

View File

@ -30,32 +30,32 @@ function parse(raw) {
for (let line of lines) {
line = line.trim();
if (line.length === 0) continue; // skip empty line
let matched = lastParser && safeMatch(lastParser, line);
if (!matched) {
for (const parser of PROXY_PARSERS) {
if (safeMatch(parser, line)) {
lastParser = parser;
matched = true;
$.info(`Proxy parser: ${parser.name} is activated`);
break;
}
}
}
if (!matched) {
$.error(`Failed to find a rule to parse line: \n${line}\n`);
} else {
try {
const proxy = lastParser.parse(line);
if (!proxy) {
$.error(
`Parser ${lastParser.name} return nothing for \n${line}\n`,
);
}
let success = false;
// try to parse with last used parser
if (lastParser) {
const [proxy, error] = tryParse(lastParser, line);
if (!error) {
proxies.push(proxy);
} catch (err) {
$.error(`Failed to parse line: \n ${line}\n Reason: ${err}`);
success = true;
}
}
if (!success) {
// search for a new parser
for (const parser of PROXY_PARSERS) {
const [proxy, error] = tryParse(parser, line);
if (!error) {
proxies.push(proxy);
lastParser = parser;
success = true;
}
}
}
if (!success) {
$.error(`Failed to parse line: ${line}`);
}
}
return proxies;
@ -163,12 +163,20 @@ export const ProxyUtils = {
produce,
};
function safeMatch(p, line) {
let patternMatched;
function tryParse(parser, line) {
if (!safeMatch(parser, line)) return [null, new Error('Parser mismatch')];
try {
patternMatched = p.test(line);
const proxy = parser.parse(line);
return [proxy, null];
} catch (err) {
patternMatched = false;
return [null, err];
}
}
function safeMatch(parser, line) {
try {
return parser.test(line);
} catch (err) {
return false;
}
return patternMatched;
}

View File

@ -401,10 +401,7 @@ function Loon_VMess() {
function Loon_Trojan() {
const name = 'Loon Trojan Parser';
const test = (line) => {
return (
/^.*=\s*trojan/i.test(line.split(',')[0]) &&
line.indexOf('password') === -1
);
return /^.*=\s*trojan/i.test(line.split(',')[0]);
};
const parse = (line) => getLoonParser().parse(line);
@ -414,12 +411,7 @@ function Loon_Trojan() {
function Loon_Http() {
const name = 'Loon HTTP Parser';
const test = (line) => {
return (
/^.*=\s*http/i.test(line.split(',')[0]) &&
line.split(',').length === 5 &&
line.indexOf('username') === -1 &&
line.indexOf('password') === -1
);
return /^.*=\s*http/i.test(line.split(',')[0]);
};
const parse = (line) => getLoonParser().parse(line);
@ -450,10 +442,7 @@ function Surge_VMess() {
function Surge_Trojan() {
const name = 'Surge Trojan Parser';
const test = (line) => {
return (
/^.*=\s*trojan/.test(line.split(',')[0]) &&
line.indexOf('sni') !== -1
);
return /^.*=\s*trojan/.test(line.split(',')[0]);
};
const parse = (line) => getSurgeParser().parse(line);
return { name, test, parse };
@ -462,9 +451,7 @@ function Surge_Trojan() {
function Surge_Http() {
const name = 'Surge HTTP Parser';
const test = (line) => {
return (
/^.*=\s*https?/.test(line.split(',')[0]) && !Loon_Http().test(line)
);
return /^.*=\s*https?/.test(line.split(',')[0]);
};
const parse = (line) => getSurgeParser().parse(line);
return { name, test, parse };

View File

@ -56,7 +56,7 @@ shadowsocks = tag equals "shadowsocks"i address method password (obfs_ss/obfs_ho
}
vmess = tag equals "vmess"i address method uuid (transport/transport_host/transport_path/over_tls/tls_host/tls_verification/vmess_alterId/fast_open/udp_relay/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleTransport();
}
vless = tag equals "vless"i address uuid (transport/transport_host/transport_path/over_tls/tls_host/tls_verification/fast_open/udp_relay/others)* {

View File

@ -54,7 +54,7 @@ shadowsocks = tag equals "shadowsocks"i address method password (obfs_ss/obfs_ho
}
vmess = tag equals "vmess"i address method uuid (transport/transport_host/transport_path/over_tls/tls_host/tls_verification/vmess_alterId/fast_open/udp_relay/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleTransport();
}
vless = tag equals "vless"i address uuid (transport/transport_host/transport_path/over_tls/tls_host/tls_verification/fast_open/udp_relay/others)* {

View File

@ -82,7 +82,7 @@ shadowsocks = "shadowsocks" equals address
vmess = "vmess" equals address
(uuid/method/over_tls/tls_host/tls_verification/tag/obfs/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleObfs();
}

View File

@ -80,7 +80,7 @@ shadowsocks = "shadowsocks" equals address
vmess = "vmess" equals address
(uuid/method/over_tls/tls_host/tls_verification/tag/obfs/obfs_host/obfs_uri/udp_relay/udp_over_tcp/fast_open/aead/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleObfs();
}

View File

@ -45,7 +45,7 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/
}
vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/method/tls/sni/tls_verification/fast_open/udp_relay/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleWebsocket();
}
trojan = tag equals "trojan" address (passwordk/ws/ws_path/ws_headers/tls/sni/tls_verification/fast_open/udp_relay/others)* {

View File

@ -43,7 +43,7 @@ shadowsocks = tag equals "ss" address (method/passwordk/obfs/obfs_host/obfs_uri/
}
vmess = tag equals "vmess" address (vmess_uuid/vmess_aead/ws/ws_path/ws_headers/method/tls/sni/tls_verification/fast_open/udp_relay/others)* {
proxy.type = "vmess";
proxy.cipher = proxy.cipher || "none";
proxy.cipher = proxy.cipher || "auto";
handleWebsocket();
}
trojan = tag equals "trojan" address (passwordk/ws/ws_path/ws_headers/tls/sni/tls_verification/fast_open/udp_relay/others)* {

File diff suppressed because one or more lines are too long