Merge pull request #9 from Peng-YM/master

添加sfilter和srename
This commit is contained in:
Shawn
2020-07-15 16:21:35 +08:00
committed by GitHub

View File

@@ -76,7 +76,6 @@ PS. 隐藏参数 ntf=0/1, 用于关闭/打开资源解析器的提示通知
var content0 = $resource.content;
var link0 = $resource.link;
//debug
//const $notify=console.log
//const $resource={}
//const $done=function(snt){return snt}
//parameters
@@ -106,6 +105,7 @@ SubFlow() //流量通知
var type0 = Type_Check(content0); // 类型
var Pin0 = mark0 && para1.indexOf("in=") != -1 ? (para1.split("in=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null;
var Pout0 = mark0 && para1.indexOf("out=") != -1 ? (para1.split("out=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null;
var Psfilter = mark0 && para1.indexOf("sfilter=") != -1 ? Base64.decode(para1.split("sfilter=")[1].split("&")[0]) : null; // script filter
var Preg = mark0 && para1.indexOf("regex=") != -1 ? decodeURIComponent(para1.split("regex=")[1].split("&")[0]) : null; //server正则过滤参数
var Pregdel = mark0 && para1.indexOf("delreg=") != -1 ? decodeURIComponent(para1.split("delreg=")[1].split("&")[0]) : null; // 正则删除参数
var Phin0 = mark0 && para1.indexOf("inhn=") != -1 ? (para1.split("inhn=")[1].split("&")[0].split("+")).map(decodeURIComponent) : null; //hostname
@@ -115,6 +115,7 @@ var Pemoji=mark0 && para1.indexOf("emoji=")!=-1? para1.split("emoji=")[1].split(
var Pudp0 = mark0 && para1.indexOf("udp=") != -1 ? para1.split("udp=")[1].split("&")[0] : 0;
var Ptfo0 = mark0 && para1.indexOf("tfo=") != -1 ? para1.split("tfo=")[1].split("&")[0] : 0;
var Prname = mark0 && para1.indexOf("rename=") != -1 ? para1.split("rename=")[1].split("&")[0].split("+") : null;
var Psrename = mark0 && para1.indexOf("srename=") != -1 ? Base64.decode(para1.split("srename=")[1].split("&")[0]) : null; // script rename
var Prrname = mark0 && para1.indexOf("rrname=") != -1 ? para1.split("rrname=")[1].split("&")[0].split("+") : null;
var Ppolicy = mark0 && para1.indexOf("policy=") != -1 ? decodeURIComponent(para1.split("policy=")[1].split("&")[0]) : "Shawn";
var Pcert0 = mark0 && para1.indexOf("cert=") != -1 ? para1.split("cert=")[1].split("&")[0] : 1;
@@ -201,9 +202,11 @@ if(flag==3){ // rule 类型
$done({ content: total.join("\n") });
} else if (flag == 1) { //server 类型
if (Pinfo == 1 && ntf_flow == 0) { //假节点类型的流量通知
flowcheck(total)}
flowcheck(total)
}
if (Pin0 || Pout0) { total = Filter(total, Pin0, Pout0) }
if (Preg) { total = total.map(Regex).filter(Boolean) }
if (Psfilter) { total = FilterScript(total, Psfilter) }
if (Prrname) {
var Prn = Prrname;
total = total.map(Rename);
@@ -220,6 +223,7 @@ if(flag==3){ // rule 类型
if (Preplace) { // server 类型也可用 replace 参数进行重命名操作
total = ReplaceReg(total, Preplace)
}
if (Psrename) { total = RenameScript(total, Psrename) }
if (Psort0 == 1 || Psort0 == -1) {
total = QXSort(total, Psort0);
} else if (Psort0 == "x") {
@@ -410,7 +414,8 @@ function Rewrite_Filter(subs,Pin,Pout){
if (Pin0 && no1write != " 0⃣ ") { //有 in 参数就通知保留项目
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "☠️ 重写 rewrite 中保留以下" + no1write + "个匹配项:" + "\n ⨷ " + Nlist.join("\n ⨷ "), rwrite_link)
} else if (dwrite.length > 0) {
$notify("🤖 "+"重写引用 ➟ "+"⟦"+subtag+"⟧","⛔️ 筛选参数: "+pfi+pfo,"☠️ 重写 rewrite 中已禁用以下"+nowrite+"个匹配项:"+"\n ⨷ "+dwrite.join("\n ⨷ "),rwrite_link )}
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "☠️ 重写 rewrite 中已禁用以下" + nowrite + "个匹配项:" + "\n ⨷ " + dwrite.join("\n ⨷ "), rwrite_link)
}
}
if (Nlist.length == 0) { $notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfi + pfo, "⚠️ 筛选后剩余rewrite规则数为 0⃣ 条, 请检查参数及原始链接", nan_link) }
if (hostname != "") { Nlist.push(hostname) }
@@ -430,8 +435,10 @@ function HostNamecheck(content,parain,paraout){
if (parain && parain != "") {
if (parain.some(excludehn)) { //Pin 命中🎯️
nname.push(hname[i])
} else{$notify("..xx")
dname.push(hname[i])} //Pin 未命中🎯️的记录
} else {
$notify("..xx")
dname.push(hname[i])
} //Pin 未命中🎯️的记录
} else { nname.push(hname[i]) } //无in 参数
} else { dname.push(hname[i]) } //out 参数命中
} else if (parain && parain != "") { //不存在 out但有 in 参数时
@@ -450,7 +457,8 @@ function HostNamecheck(content,parain,paraout){
if (parain && no1name != " 0⃣ ") {
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfihn + pfohn, "☠️ 主机名 hostname 中已保留以下" + no1name + "个匹配项:" + "\n ⨷ " + nname.join(","), rwhost_link)
} else if (dname.length > 0) {
$notify("🤖 "+"重写引用 ➟ "+"⟦"+subtag+"⟧","⛔️ 筛选参数: "+pfihn+pfohn,"☠️ 主机名 hostname 中已删除以下"+noname+"个匹配项:"+"\n ⨷ "+dname.join(","),rwhost_link )}
$notify("🤖 " + "重写引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 筛选参数: " + pfihn + pfohn, "☠️ 主机名 hostname 中已删除以下" + noname + "个匹配项:" + "\n ⨷ " + dname.join(","), rwhost_link)
}
}
}
if (nname.length == 0) {
@@ -470,7 +478,8 @@ function Rcheck(content,param){
}
return flag
} else { //if param
return 2} //无参数
return 2
} //无参数
}
//分流规则转换及过滤,可用于 surge 及 quanx 的 rule-list
@@ -496,19 +505,23 @@ function Rule_Handle(subs,Pout,Pin){
if (Tin.some(include)) {
nlist.push(dd);
}
}else{nlist.push(dd);
} else {
nlist.push(dd);
}
} //else if cc
}//for cnt
var no = dlist.length <= 10 ? emojino[dlist.length] : dlist.length
if(dlist.length>0 ){ if(Pntf0!=0){$notify("🤖 "+"分流引用 ➟ "+"⟦"+subtag+"⟧","⛔️ 禁用: "+ Tout,"☠️ 已禁用以下"+no+"条匹配规则:"+"\n ⨷ "+dlist.join("\n ⨷ "),rule_link)}
if (dlist.length > 0) {
if (Pntf0 != 0) { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 禁用: " + Tout, "☠️ 已禁用以下" + no + "条匹配规则:" + "\n ⨷ " + dlist.join("\n ⨷ "), rule_link) }
} else { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "⛔️ 禁用: " + Tout, "⚠️ 未发现任何匹配项, 请检查参数或原始链接", nan_link) }
if (Tin != "" && Tin != null) { //有 in 跟 out 参数时
if (nlist.length > 0) {
var noin0 = nlist.length <= 10 ? emojino[nlist.length] : nlist.length
if (Pntf0 != 0) {
$notify("🤖 "+"分流引用 ➟ "+"⟦"+subtag+"⟧","✅ 保留:"+Tin,"🎯 已保留以下 "+noin0+"条匹配规则:"+"\n ⨁ "+nlist.join("\n ⨁ "),rule_link)}
} else{$notify("🤖 "+"分流引用 ➟ "+"⟦"+subtag+"⟧","✅ 保留:"+Tin+",⛔️ 禁用: "+Tout,"⚠️ 筛选后剩余规则数为 0⃣ 条, 请检查参数及原始链接",nan_link)
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "🎯 已保留以下 " + noin0 + "条匹配规则:" + "\n ⨁ " + nlist.join("\n ⨁ "), rule_link)
}
} else {
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin + ",⛔️ 禁用: " + Tout, "⚠️ 筛选后剩余规则数为 0⃣ 条, 请检查参数及原始链接", nan_link)
}
} else {// if Tin (No Tin)
if (nlist.length == 0) {
@@ -532,7 +545,8 @@ function Rule_Handle(subs,Pout,Pin){
if (nlist.length > 0) {
var noin = nlist.length <= 10 ? emojino[nlist.length] : nlist.length
if (Pntf0 != 0) {
$notify("🤖 "+"分流引用 ➟ "+"⟦"+subtag+"⟧","✅ 保留:"+Tin,"🎯 已保留以下 "+noin+"条匹配规则:"+"\n ⨁ "+nlist.join("\n ⨁ "),rule_link)}
$notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "🎯 已保留以下 " + noin + "条匹配规则:" + "\n ⨁ " + nlist.join("\n ⨁ "), rule_link)
}
} else { $notify("🤖 " + "分流引用 ➟ " + "⟦" + subtag + "⟧", "✅ 保留:" + Tin, "⚠️ 筛选后剩余规则数为 0⃣ 条, 请检查参数及原始链接", nan_link) }
return [...dlist, ...nlist];
} else { //if Tin
@@ -604,7 +618,8 @@ function SubsEd2QX(subs,Pudp,Ptfo,Pcert,Ptls13){
if (bnode.indexOf("over-tls=") == -1) { //v2rayN
node = V2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
} else { //quantumult 类型
node= VQ2QX(list0[i],Pudp,Ptfo,Pcert,Ptls13)}
node = VQ2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
}
} else if (type == "vmess" && list0[i].indexOf("remarks=") != -1) { //shadowrocket 类型
node = VR2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
} else if (type == "ssr") {
@@ -623,7 +638,8 @@ function SubsEd2QX(subs,Pudp,Ptfo,Pcert,Ptls13){
node = Loon2QX(list0[i])
}
if (node != "") {
QXlist.push(node)}
QXlist.push(node)
}
}
}
return QXlist
@@ -649,7 +665,8 @@ function Subs2QX(subs,Pudp,Ptfo,Pcert,Ptls13){
if (bnode.indexOf("over-tls=") == -1) { //v2rayN
node = V2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
} else { //quantumult 类型
node= VQ2QX(list0[i],Pudp,Ptfo,Pcert,Ptls13)}
node = VQ2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
}
} else if (type == "vmess" && list0[i].indexOf("remarks=") != -1) { //shadowrocket 类型
node = VR2QX(list0[i], Pudp, Ptfo, Pcert, Ptls13)
} else if (type == "ssr") {
@@ -713,10 +730,12 @@ function TagCheck_QX(content){
} // for
if (nulllist.length >= 1) {
no = nulllist.length <= 10 ? emojino[nulllist.length] : nulllist.length;
$notify("⚠️ 引用"+"⟦"+subtag+"⟧"+" 内有"+no+"个空节点名 ", "✅ 已将节点“类型+IP”设为节点名"," ⨁ "+nulllist.join("\n ⨁ "),nan_link)}
$notify("⚠️ 引用" + "⟦" + subtag + "⟧" + " 内有" + no + "个空节点名 ", "✅ 已将节点“类型+IP”设为节点名", " ⨁ " + nulllist.join("\n ⨁ "), nan_link)
}
if (duplist.length >= 1) {
no = duplist.length <= 10 ? emojino[duplist.length] : duplist.length;
$notify("⚠️ 引用"+"⟦"+subtag+"⟧"+" 内有"+no+"个重复节点名 ", "✅ 已添加⌘符号作为区分:", " ⨁ "+duplist.join("\n ⨁ "),nan_link)}
$notify("⚠️ 引用" + "⟦" + subtag + "⟧" + " 内有" + no + "个重复节点名 ", "✅ 已添加⌘符号作为区分:", " ⨁ " + duplist.join("\n ⨁ "), nan_link)
}
return Nlist
}
//http=example.com:443, username=name, password=pwd, over-tls=true, tls-host=example.com, tls-verification=true, tls13=true, fast-open=false, udp-relay=false, tag=http-tls-02
@@ -814,7 +833,8 @@ function V2QX(subs,Pudp,Ptfo,Pcert,Ptls13){
if (obfs == "" || obfs == undefined) {
nss.push(ip, mtd, pwd, tfo, udp, tag)
} else {
nss.push(ip,mtd,pwd,obfs,tfo,udp,tag);}
nss.push(ip, mtd, pwd, obfs, tfo, udp, tag);
}
QX = nss.join(", ");
}
return QX
@@ -882,7 +902,8 @@ function Scheck(content,param){
}//for
return flag
} else { //if param
return 2}
return 2
}
}
//节点过滤,使用+连接多个关键词(逻辑"或"):in 为保留out 为排除, "与"逻辑请用符号"."连接
@@ -905,10 +926,26 @@ function Filter(servers,Pin,Pout){
$notify("👥 引用" + "⟦" + subtag + "⟧" + " 开始节点筛选", "🕹 筛选关键字: " + pfi + pfo, "☠️ 已删除以下 " + no + "个节点\n" + Delist.join(", "), sub_link);
}
} else if (no1 == 0 || no1 == null) { //无剩余节点时强制通知
$notify("‼️ ⟦"+subtag+"⟧"+"筛选后节点数为0⃣","⚠️ 请自行检查原始链接以及筛选参数", link0, sub_link);}
$notify("‼️ ⟦" + subtag + "⟧" + "筛选后节点数为0⃣", "⚠️ 请自行检查原始链接以及筛选参数", link0, sub_link);
}
return Nlist
}
function FilterScript(servers, script) {
const $ = Tools();
eval(script);
// extract server tags
const nodes = {
names: servers.map(s => s.split("tag=")[1])
};
const IN = filter(nodes);
const res = servers.filter((_, i) => IN[i]);
if (res.length === 0) {
$notify("‼️ ⟦" + subtag + "⟧" + "筛选后节点数为0⃣", "⚠️ 请自行检查原始链接以及筛选参数", link0, sub_link);
}
return res;
}
//SSR 类型 URI 转换 quanx 格式
function SSR2QX(subs, Pudp, Ptfo) {
var nssr = []
@@ -1104,12 +1141,25 @@ function Rename(str){
} else if (oname == "" && nname == "") { //仅有@时,删除@符号
name = name.replace(/@/g, "")
} else {
name=name}
name = name
}
nserver = hd + "tag=" + name
}
} return nserver
}
function RenameScript(servers, script) {
const $ = Tools().rename;
// extract server tags
const nodes = {
names: servers.map(s => s.split("tag=")[1])
}
eval(script);
const newNames = rename(nodes);
// rename nodes
return servers.map((s, i) => s.split("tag=")[0] + "tag=" + newNames[i]);
}
//删除 emoji
function emoji_del(str) {
return unescape(escape(str).replace(/\%uD.{3}/g, ''));
@@ -1489,6 +1539,45 @@ function Base64Code(){
return _decode(
String(a).replace(/[-_]/g, function (m0) { return m0 == '-' ? '+' : '/' })
.replace(/[^A-Za-z0-9\+\/]/g, '')
);
).replace(/&gt;/g, ">").replace(/&lt;/g, "<");
};
}
/***********************************************************************************************/
function Tools() {
const filter = (src, ...regex) => {
const initial = [...Array(src.length).keys()].map(() => false);
return regex.reduce((a, expr) => OR(a, src.map(item => expr.test(item))), initial)
}
const rename = {
replace: (src, old, now) => {
return src.map(item => item.replace(old, now));
},
delete: (src, ...args) => {
return src.map(item => args.reduce((now, expr) => now.replace(expr, ''), item));
},
trim: (src) => {
return src.map(item => item.trim().replace(/[^\S\r\n]{2,}/g, ' '));
}
}
return {
filter, rename
}
}
function AND(...args) {
return args.reduce((a, b) => a.map((c, i) => b[i] && c));
}
function OR(...args) {
return args.reduce((a, b) => a.map((c, i) => b[i] || c))
}
function NOT(array) {
return array.map(c => !c);
}