Fixed Trojan issue

This commit is contained in:
Peng-YM 2020-09-14 10:12:53 +08:00
parent 8504738ee1
commit a0d8e79d3c
2 changed files with 3012 additions and 2980 deletions

View File

@ -59,7 +59,6 @@ $app.route("/api/sub/:name").get(getSub).patch(updateSub).delete(deleteSub);
$app.route("/api/sub").get(getAllSubs).post(newSub).delete(deleteAllSubs); $app.route("/api/sub").get(getAllSubs).post(newSub).delete(deleteAllSubs);
// refresh // refresh
$app.post("/api/refresh", refreshResource); $app.post("/api/refresh", refreshResource);
@ -85,7 +84,9 @@ $app.start();
async function IP_API(req, res) { async function IP_API(req, res) {
const server = decodeURIComponent(req.params.server); const server = decodeURIComponent(req.params.server);
const result = await $.http.get(`http://ip-api.com/json/${server}?lang=zh-CN`).then(resp => JSON.parse(resp.body)); const result = await $.http
.get(`http://ip-api.com/json/${server}?lang=zh-CN`)
.then((resp) => JSON.parse(resp.body));
res.json(result); res.json(result);
} }
@ -147,7 +148,7 @@ async function downloadSub(req, res) {
async function parseSub(sub, platform) { async function parseSub(sub, platform) {
let raw; let raw;
const key = new Base64Code().safeEncode(sub.url); const key = new Base64Code().safeEncode(sub.url);
if (platform === "Raw" || platform === 'URI') { if (platform === "Raw" || platform === "URI") {
const cache = $.read(`#${key}`); const cache = $.read(`#${key}`);
if (!cache) { if (!cache) {
raw = await downloadResource(sub.url); raw = await downloadResource(sub.url);
@ -239,12 +240,9 @@ async function parseSub(sub, platform) {
$filter.setFilter(filter(item.args)); $filter.setFilter(filter(item.args));
} }
try { try {
proxies = $filter.process(proxies); proxies = $filter.process(proxies);
} catch (err) { } catch (err) {
$.error( $.error(`Failed to apply filter "${item.type}"!\n REASON: ${err}`);
`Failed to apply filter "${item.type}"!\n REASON: ${err}`
);
} }
} }
} else if (item.type.indexOf("Operator") !== -1) { } else if (item.type.indexOf("Operator") !== -1) {
@ -263,7 +261,7 @@ async function parseSub(sub, platform) {
try { try {
proxies = $operator.process(proxies); proxies = $operator.process(proxies);
} catch (err) { } catch (err) {
`Failed to apply operator "${item.type}"!\n REASON: ${err}` `Failed to apply operator "${item.type}"!\n REASON: ${err}`;
} }
} }
} }
@ -275,7 +273,7 @@ async function parseSub(sub, platform) {
Loon_Producer, Loon_Producer,
Surge_Producer, Surge_Producer,
Raw_Producer, Raw_Producer,
URI_Producer URI_Producer,
]); ]);
return $parser.produce(proxies); return $parser.produce(proxies);
} }
@ -601,7 +599,8 @@ function ProxyParser(targetPlatform) {
} }
// skip unsupported proxies // skip unsupported proxies
// if proxy.supported is undefined, assume that all platforms are supported. // if proxy.supported is undefined, assume that all platforms are supported.
if (proxy.supported && proxy.supported[targetPlatform] === false) continue; if (proxy.supported && proxy.supported[targetPlatform] === false)
continue;
result.push(proxy); result.push(proxy);
break; break;
} catch (err) { } catch (err) {
@ -634,7 +633,7 @@ function ProxyParser(targetPlatform) {
return ""; return "";
} }
}) })
.filter(v => v.length > 0) // discard empty lines .filter((v) => v.length > 0) // discard empty lines
.join("\n"); .join("\n");
} }
} }
@ -658,15 +657,20 @@ function ProxyParser(targetPlatform) {
raw = raw raw = raw
.replace(/ - /g, " - ") .replace(/ - /g, " - ")
.replace(/:(?!\s)/g, ": ") .replace(/:(?!\s)/g, ": ")
.replace(/\,\"/g, ", \"") .replace(/\,\"/g, ', "')
.replace(/: {/g, ": {, ") .replace(/: {/g, ": {, ")
.replace(/, (host|path|tls|mux|skip)/g, ", $1") .replace(/, (host|path|tls|mux|skip)/g, ", $1")
.replace(/{name: /g, "{name: \"") .replace(/{name: /g, '{name: "')
.replace(/, server:/g, "\", server:") .replace(/, server:/g, '", server:')
.replace(/{|}/g, "") .replace(/{|}/g, "")
.replace(/,/g, "\n ") .replace(/,/g, "\n ");
} }
raw = raw.replace(/ -\n.*name/g, " - name").replace(/\$|\`/g, "").split("proxy-providers:")[0].split("proxy-groups:")[0].replace(/\"(name|type|server|port|cipher|password|)\"/g, "$1") raw = raw
.replace(/ -\n.*name/g, " - name")
.replace(/\$|\`/g, "")
.split("proxy-providers:")[0]
.split("proxy-groups:")[0]
.replace(/\"(name|type|server|port|cipher|password|)\"/g, "$1");
const proxies = YAML.eval(raw).proxies; const proxies = YAML.eval(raw).proxies;
output = proxies.map((p) => JSON.stringify(p)); output = proxies.map((p) => JSON.stringify(p));
} else if (raw.indexOf("ssd://") === 0) { } else if (raw.indexOf("ssd://") === 0) {
@ -693,8 +697,12 @@ function ProxyParser(targetPlatform) {
let hostname = server.server; let hostname = server.server;
port = server.port ? server.port : port; port = server.port ? server.port : port;
let tag = server.remarks ? server.remarks : i; let tag = server.remarks ? server.remarks : i;
let plugin = server.plugin_options ? "/?plugin=" + encodeURIComponent(server.plugin + ";" + server.plugin_options) : "" let plugin = server.plugin_options
output[i] = "ss://" + userinfo + "@" + hostname + ":" + port + plugin + "#" + tag ? "/?plugin=" +
encodeURIComponent(server.plugin + ";" + server.plugin_options)
: "";
output[i] =
"ss://" + userinfo + "@" + hostname + ":" + port + plugin + "#" + tag;
} }
} else { } else {
// check if content is based64 encoded // check if content is based64 encoded
@ -836,7 +844,7 @@ function URI_SS() {
mode: "websocket", mode: "websocket",
host: params["obfs-host"], host: params["obfs-host"],
path: params.path || "", path: params.path || "",
tls: params.tls tls: params.tls,
}; };
break; break;
default: default:
@ -1608,7 +1616,7 @@ function QX_Producer() {
case "trojan": case "trojan":
return `trojan=${proxy.server}:${proxy.port},password=${ return `trojan=${proxy.server}:${proxy.port},password=${
proxy.password proxy.password
},tls-host=${proxy.sni},tls-verification=${ }${proxy.sni ? ",tls-host=" + proxy.sni : ""},tls-verification=${
proxy.scert ? "false" : "true" proxy.scert ? "false" : "true"
}${proxy.tfo ? ",fast-open=true" : ",fast-open=false"}${ }${proxy.tfo ? ",fast-open=true" : ",fast-open=false"}${
proxy.udp ? ",udp-relay=true" : ",udp-relay=false" proxy.udp ? ",udp-relay=true" : ",udp-relay=false"
@ -1671,7 +1679,11 @@ function Loon_Producer() {
proxy.cipher === "auto" ? "none" : proxy.cipher proxy.cipher === "auto" ? "none" : proxy.cipher
},"${proxy.uuid}",over-tls:${proxy.tls}${obfs_opts}`; },"${proxy.uuid}",over-tls:${proxy.tls}${obfs_opts}`;
case "trojan": case "trojan":
return `${proxy.name}=trojan,${proxy.server},${proxy.port},"${proxy.password}",tls-name:${proxy.sni},skip-cert-verify:${proxy.scert}`; return `${proxy.name}=trojan,${proxy.server},${proxy.port},"${
proxy.password
}"${proxy.sni ? ",tls-name:" + proxy.sni : ""},skip-cert-verify:${
proxy.scert || "false"
}`;
case "http": case "http":
tls_opts = ""; tls_opts = "";
const base = `${proxy.name}=${proxy.tls ? "http" : "https"},${ const base = `${proxy.name}=${proxy.tls ? "http" : "https"},${
@ -1730,7 +1742,7 @@ function Surge_Producer() {
case "trojan": case "trojan":
return `${proxy.name}=trojan,${proxy.server},${proxy.port},password=${ return `${proxy.name}=trojan,${proxy.server},${proxy.port},password=${
proxy.password proxy.password
},sni=${proxy.sni},tfo=${proxy.tfo || "false"}`; }${proxy.sni ? ",sni=" + proxy.sni : ""},tfo=${proxy.tfo || "false"}`;
case "http": case "http":
tls_opts = ",tls=false"; tls_opts = ",tls=false";
if (proxy.tls) { if (proxy.tls) {
@ -1765,29 +1777,49 @@ function URI_Producer() {
switch (proxy.type) { switch (proxy.type) {
case "ss": case "ss":
const userinfo = `${proxy.cipher}:${proxy.password}`; const userinfo = `${proxy.cipher}:${proxy.password}`;
result = `ss://${Base64.safeEncode(userinfo)}@${proxy.server}:${proxy.port}/`; result = `ss://${Base64.safeEncode(userinfo)}@${proxy.server}:${
proxy.port
}/`;
if (proxy.plugin) { if (proxy.plugin) {
result += "?plugin="; result += "?plugin=";
const opts = proxy['plugin-opts']; const opts = proxy["plugin-opts"];
switch (proxy.plugin) { switch (proxy.plugin) {
case "obfs": case "obfs":
result += encodeURIComponent(`simple-obfs;obfs=${opts.mode}${opts.host ? ";obfs-host=" + opts.host : ""}`); result += encodeURIComponent(
break `simple-obfs;obfs=${opts.mode}${
opts.host ? ";obfs-host=" + opts.host : ""
}`
);
break;
case "v2ray-plugin": case "v2ray-plugin":
result += encodeURIComponent(`v2ray-plugin;obfs=${opts.mode}${opts.host ? ";obfs-host" + opts.host : ""}${opts.tls ? ";tls" : ""}`); result += encodeURIComponent(
break `v2ray-plugin;obfs=${opts.mode}${
opts.host ? ";obfs-host" + opts.host : ""
}${opts.tls ? ";tls" : ""}`
);
break;
default: default:
console.log(`FUCK`); console.log(`FUCK`);
throw new Error(`Unsupported plugin option: ${proxy.plugin}`); throw new Error(`Unsupported plugin option: ${proxy.plugin}`);
} }
} }
result += `#${encodeURIComponent(proxy.name)}`; result += `#${encodeURIComponent(proxy.name)}`;
break break;
case "ssr": case "ssr":
result = `${proxy.server}:${proxy.port}:${proxy.protocol}:${proxy.cipher}:${proxy.obfs}:${Base64.safeEncode(proxy.password)}/`; result = `${proxy.server}:${proxy.port}:${proxy.protocol}:${
result += `?remarks=${proxy.name}${proxy['obfs-param'] ? "&obfsparam=" + Base64.safeEncode(proxy['obfs-param']) : ""}${proxy['protocol-param'] ? "&protocolparam=" + Base64.safeEncode(proxy['protocol-param']) : ""}`; proxy.cipher
}:${proxy.obfs}:${Base64.safeEncode(proxy.password)}/`;
result += `?remarks=${proxy.name}${
proxy["obfs-param"]
? "&obfsparam=" + Base64.safeEncode(proxy["obfs-param"])
: ""
}${
proxy["protocol-param"]
? "&protocolparam=" + Base64.safeEncode(proxy["protocol-param"])
: ""
}`;
result = "vmess://" + Base64.safeEncode(result); result = "vmess://" + Base64.safeEncode(result);
break break;
case "vmess": case "vmess":
// V2RayN URI format // V2RayN URI format
result = { result = {
@ -1798,18 +1830,18 @@ function URI_Producer() {
type: "", type: "",
aid: 0, aid: 0,
net: proxy.network || "tcp", net: proxy.network || "tcp",
tls: proxy.tls ? "tls" : "" tls: proxy.tls ? "tls" : "",
} };
// obfs // obfs
if (proxy.network === 'ws') { if (proxy.network === "ws") {
result.path = proxy['ws-path'] || "/"; result.path = proxy["ws-path"] || "/";
result.host = proxy['ws-headers'].Host || proxy.server; result.host = proxy["ws-headers"].Host || proxy.server;
} }
result = Base64.safeEncode(JSON.stringify(result)); result = Base64.safeEncode(JSON.stringify(result));
break break;
case "trojan": case "trojan":
result = `trojan://${proxy.password}@${proxy.server}:${proxy.port}#${proxy.name}`; result = `trojan://${proxy.password}@${proxy.server}:${proxy.port}#${proxy.name}`;
break break;
default: default:
throw new Error(`Cannot handle proxy type: ${proxy.type}`); throw new Error(`Cannot handle proxy type: ${proxy.type}`);
} }
@ -2401,16 +2433,16 @@ function HTTP(defaultOptions = {}) {
function send(method, options) { function send(method, options) {
options = options.hasOwnProperty("url") ? options : { url: options }; options = options.hasOwnProperty("url") ? options : { url: options };
options.url = defaultOptions.baseURL ? defaultOptions.baseURL + options.url : options.url; options.url = defaultOptions.baseURL
? defaultOptions.baseURL + options.url
: options.url;
options = { ...defaultOptions, ...options }; options = { ...defaultOptions, ...options };
const timeout = options.timeout; const timeout = options.timeout;
const events = { const events = {
...{ ...{
onRequest: () => { onRequest: () => {},
},
onResponse: (resp) => resp, onResponse: (resp) => resp,
onTimeout: () => { onTimeout: () => {},
},
}, },
...options.events, ...options.events,
}; };
@ -2630,7 +2662,7 @@ function API(name = "untitled", debug = false) {
let opts = {}; let opts = {};
if (openURL) opts["openUrl"] = openURL; if (openURL) opts["openUrl"] = openURL;
if (mediaURL) opts["mediaUrl"] = mediaURL; if (mediaURL) opts["mediaUrl"] = mediaURL;
if (JSON.stringify(opts) === '{}') { if (JSON.stringify(opts) === "{}") {
$notification.post(title, subtitle, content); $notification.post(title, subtitle, content);
} else { } else {
$notification.post(title, subtitle, content, opts); $notification.post(title, subtitle, content, opts);

File diff suppressed because one or more lines are too long