Skip to content

Commit 98506ff

Browse files
committed
feat: 清理依赖, 更新正式版
1 parent 5c7c95b commit 98506ff

3 files changed

Lines changed: 94 additions & 52 deletions

File tree

Rewrite-Parser.js

Lines changed: 91 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,12 @@ let fheaddMethod = '%APPEND%'
262262
let skipaddMethod = '%APPEND%'
263263
let realaddMethod = '%APPEND%'
264264

265+
let hn2 = false //surge模块中带有禁用MITM参数时无法捕捉hostname,此变量用以判断有无此类参数,以便后续解析
266+
let hn2name = 'hostname'
267+
265268
//待输出
266269
let modInfo = [] //模块简介
270+
let loonArg = [] //[Argument]
267271
let httpFrame = '' //Stash的http:父框架
268272
let tiles = [] //磁贴覆写
269273
let General = []
@@ -513,18 +517,20 @@ if (binaryInfo != null && binaryInfo.length > 0) {
513517
if (/^#!.+?=\s*$/.test(x)) {
514518
} else if (isLooniOS && /^#!(?:select|input)\s*=\s*.+/.test(x)) {
515519
getInputInfo(x, modInputBox)
516-
} else if (/^#!.+?=.+/.test(x) && !/^#!(?:select|input)\s*=\s*.+/.test(x)) {
520+
} else if (/^#!.+?=.+/.test(x) && !/^#!(?:select|input|arguments)\s*=\s*.+/.test(x)) {
517521
getModInfo(x)
518522
}
519523

520524
//#!arguments参数
521-
if (!isSurgeiOS && /^#!arguments\s*=\s*.+/.test(x)) {
525+
if (/^#!arguments\s*=\s*.+/.test(x) || /^[^#]=\s*(input|select|switch)\s*,/.test(x)) {
522526
parseArguments(x)
523527
}
524528

525529
//hostname
526530
if (/^hostname\s*=.+/.test(x)) hnaddMethod = getHn(x, hnBox, hnaddMethod)
527531

532+
if (hn2 == true && x.match(hn2name)) hnaddMethod = getHn(x, hnBox, hnaddMethod)
533+
528534
if (/^force-http-engine-hosts\s*=.+/.test(x)) fheaddMethod = getHn(x, fheBox, fheaddMethod)
529535

530536
if (/^skip-proxy\s*=.+/.test(x)) skipaddMethod = getHn(x, skipBox, skipaddMethod)
@@ -805,6 +811,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
805811
? getJsInfo(x, /[=,\s]\s*cronexpr?\s*=\s*/)
806812
: /cron\s+"/.test(x)
807813
? x.split('"')[1]
814+
: /cron\s+[^\s]+?\s+/
815+
? x.split(/\s/)[1]
808816
: ''
809817
ability = getJsInfo(x, /[=,\s]\s*ability\s*=\s*/)
810818
engine = getJsInfo(x, /[=,\s]\s*engine\s*=\s*/)
@@ -1046,6 +1054,17 @@ if (binaryInfo != null && binaryInfo.length > 0) {
10461054
realBox = pieceHn(realBox)
10471055
if (synMitm) fheBox = hnBox
10481056

1057+
if (isSurgeiOS && sgArg.length > 0) {
1058+
let sgargArr = []
1059+
for (let i = 0; i < sgArg.length; i++) {
1060+
let key = sgArg[i].key
1061+
let value = sgArg[i].value.split(',')[0].trim()
1062+
let a = key + ':' + value
1063+
sgargArr.push(a)
1064+
}
1065+
modInfoObj['arguments'] = (sgargArr[0] || '') && `${sgargArr.join(',')}`
1066+
}
1067+
10491068
//模块信息输出
10501069
switch (targetApp) {
10511070
case 'surge-module':
@@ -1085,6 +1104,18 @@ if (binaryInfo != null && binaryInfo.length > 0) {
10851104
break
10861105
} //模块信息输出结束
10871106

1107+
//[Argument]输出
1108+
if (isLooniOS && sgArg.length > 0) {
1109+
for (let i = 0; i < sgArg.length; i++) {
1110+
let key = sgArg[i].key
1111+
let type = sgArg[i].type
1112+
let value = sgArg[i].value
1113+
if (type == 'switch') value = /^true/.test(value) ? 'true,false' : 'false,true'
1114+
let tag = sgArg[i].tag
1115+
loonArg.push(key + '=' + type + ',' + value + ',' + tag)
1116+
}
1117+
}
1118+
10881119
//rule输出 switch不适合
10891120
for (let i = 0; i < ruleBox.length; i++) {
10901121
noteK = ruleBox[i].noteK ? '#' : ''
@@ -1403,6 +1434,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
14031434

14041435
cronexp = reJsValue(nCron || 'null', ncronexp, jsname, ori, cronexp)
14051436

1437+
cronexp = /,/.test(cronexp) ? '"' + cronexp + '"' : cronexp
1438+
14061439
jsname = reJsValue(njsnametarget || 'null', njsname, jsname, ori, jsname)
14071440

14081441
timeout = reJsValue(timeoutt || 'null', timeoutv, jsname, ori, timeout)
@@ -1485,9 +1518,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
14851518
jsname +
14861519
' = type=' +
14871520
jstype +
1488-
', cronexp="' +
1521+
', cronexp=' +
14891522
cronexp +
1490-
'"' +
14911523
', script-path=' +
14921524
jsurl +
14931525
updatetime +
@@ -1501,9 +1533,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
15011533
mark +
15021534
noteK +
15031535
jstype +
1504-
' "' +
1536+
' ' +
15051537
cronexp +
1506-
'"' +
15071538
' script-path=' +
15081539
jsurl +
15091540
timeout +
@@ -1628,7 +1659,7 @@ if (binaryInfo != null && binaryInfo.length > 0) {
16281659
providers.push(`${noteK2}"` + jsname + '":' + `${noteKn4}url: ` + jsurl + `${noteKn4}interval: 86400`)
16291660
}
16301661
if (jstype == 'cron') {
1631-
cron.push(mark + `${noteK4}- name: "` + jsname + `"${noteKn6}cron: "` + cronexp + `"${timeout}` + jsarg)
1662+
cron.push(mark + `${noteK4}- name: "` + jsname + `"${noteKn6}cron: ` + cronexp + `${timeout}` + jsarg)
16321663
providers.push(`${noteK2}"` + jsname + '":' + `${noteKn4}url: ` + jsurl + `${noteKn4}interval: 86400`)
16331664
}
16341665
if (jstype == 'generic') {
@@ -1653,6 +1684,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
16531684
case 'loon-plugin':
16541685
modInfo = (modInfo[0] || '') && `${modInfo.join('\n')}`
16551686

1687+
loonArg = (loonArg[0] || '') && `[Argument]\n${loonArg.join('\n')}`
1688+
16561689
rules = (rules[0] || '') && `[Rule]\n${rules.join('\n')}`
16571690

16581691
Panel = (Panel[0] || '') && `[Panel]\n${Panel.join('\n\n')}`
@@ -1670,15 +1703,19 @@ if (binaryInfo != null && binaryInfo.length > 0) {
16701703
script = (script[0] || '') && `[Script]\n${script.join('\n\n')}`
16711704

16721705
if (isLooniOS) {
1673-
MITM = hnBox.length > 0 ? '[MITM]\nhostname = ' + hnBox : ''
1706+
MITM = hnBox.length > 0 ? `[MITM]\n${hn2name} = ` + hnBox : ''
16741707
fheBox.length > 0 && General.push('force-http-engine-hosts = ' + fheBox)
16751708
skipBox.length > 0 && General.push('skip-proxy = ' + skipBox)
16761709
realBox.length > 0 && General.push('real-ip = ' + realBox)
16771710
General = (General[0] || '') && `[General]\n${General.join('\n\n')}`
16781711
}
16791712

16801713
if (isSurgeiOS || isShadowrocket) {
1681-
MITM = hnBox.length > 0 ? `[MITM]\nhostname = ${hnaddMethod} ` + hnBox : ''
1714+
if (isSurgeiOS) {
1715+
MITM = hnBox.length > 0 ? `[MITM]\n${hn2name} = ${hnaddMethod} ` + hnBox : ''
1716+
} else {
1717+
MITM = hnBox.length > 0 ? `[MITM]\nhostname = ${hnaddMethod} ` + hnBox : ''
1718+
}
16821719
fheBox.length > 0 && General.push(`force-http-engine-hosts = ${fheaddMethod} ` + fheBox)
16831720
skipBox.length > 0 && General.push(`skip-proxy = ${skipaddMethod} ` + skipBox)
16841721
realBox.length > 0 && General.push(`always-real-ip = ${realaddMethod} ` + realBox)
@@ -1687,6 +1724,8 @@ if (binaryInfo != null && binaryInfo.length > 0) {
16871724

16881725
body = `${modInfo}
16891726
1727+
${loonArg}
1728+
16901729
${General}
16911730
16921731
${rules}
@@ -1788,12 +1827,22 @@ ${providers}
17881827
break
17891828
} //输出内容结束
17901829
body = body.replace(/\n{2,}/g, '\n\n')
1791-
if (sgArg.length > 0) {
1830+
if (!isSurgeiOS && !isLooniOS && sgArg.length > 0) {
1831+
body = body.replaceAll('{{{', '{').replaceAll('}}}', '}')
1832+
for (let i = 0; i < sgArg.length; i++) {
1833+
let e = '{' + sgArg[i].key + '}'
1834+
let r = sgArg[i].value.split(',')[0]
1835+
body = body.replaceAll(e, r)
1836+
} //for
1837+
} else if (isSurgeiOS) {
1838+
body = body.replaceAll('{{{', '{').replaceAll('}}}', '}')
17921839
for (let i = 0; i < sgArg.length; i++) {
1793-
let e = '{{{' + sgArg[i].key + '}}}'
1794-
let r = sgArg[i].value
1840+
let e = '{' + sgArg[i].key + '}'
1841+
let r = '{{{' + sgArg[i].key + '}}}'
17951842
body = body.replaceAll(e, r)
17961843
} //for
1844+
} else if (isLooniOS) {
1845+
body = body.replaceAll('{{{', '{').replaceAll('}}}', '}')
17971846
}
17981847

17991848
eval(evJsmodi)
@@ -2264,14 +2313,37 @@ function getPolicy(str) {
22642313
}
22652314

22662315
function parseArguments(str) {
2267-
const queryString = str.split(/#!arguments\s*=\s*/)[1] //获取查询字符串部分
2268-
const regex = /([^:,]+):(\s*".+?"|[^,]*)/g //匹配键值对的正则表达式
2269-
let match
2316+
if (/#!arguments/.test(str)) {
2317+
const queryString = str.split(/#!arguments\s*=\s*/)[1] //获取查询字符串部分
2318+
const regex = /([^:,]+):(\s*".+?"|[^,]*)/g //匹配键值对的正则表达式
2319+
let match
2320+
2321+
while ((match = regex.exec(queryString))) {
2322+
const key = match[1].trim().replace(/^"(.+)"$/, '$1') //去除头尾空白符和引号
2323+
const value = match[2].trim().replace(/^"(.+)"$/, '$1') //去除头尾空白符和引号
2324+
const type = /^(true|false)$/.test(value) ? 'switch' : 'input'
2325+
const tag = `tag=${key}, desc=${key}`
2326+
2327+
sgArg.push({ key, value, type, tag }) //将键值对添加到对象中
2328+
2329+
if (value == 'hostname') {
2330+
hn2 = true
2331+
hn2name = '{{{' + key + '}}}'
2332+
}
2333+
}
2334+
} else {
2335+
const regex = /(^.*?)\s*=\s*(.*?)\s*,(.*?),\s*([^,]*\s*=.+)/ //获取信息
2336+
const key = str.match(regex)[1]
2337+
const type = str.match(regex)[2]
2338+
const value = str.match(regex)[3]
2339+
const tag = str.match(regex)[4]
22702340

2271-
while ((match = regex.exec(queryString))) {
2272-
const key = match[1].trim().replace(/^"(.+)"$/, '$1') //去除头尾空白符和引号
2273-
const value = match[2].trim().replace(/^"(.+)"$/, '$1') //去除头尾空白符和引号
2274-
sgArg.push({ key, value }) //将键值对添加到对象中
2341+
sgArg.push({ key, value, type, tag })
2342+
2343+
if (value == 'hostname') {
2344+
hn2 = true
2345+
hn2name = '{{{' + key + '}}}'
2346+
}
22752347
}
22762348
}
22772349

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
"http-server": "^0.12.3",
1717
"iconv-lite": "0.6.3",
1818
"koa": "^2.14.2",
19+
"lodash": "^4.17.21",
1920
"nodemon": "^3.0.0",
2021
"script-hub": "file:",
21-
"tough-cookie": "^4.0.0",
22-
"vm2": "^3.9.19"
22+
"tough-cookie": "^4.0.0"
2323
},
2424
"packageManager": "pnpm@9.1.0"
25-
}
25+
}

service.js

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,36 +12,6 @@ const HOST = process.env.HOST || '0.0.0.0'
1212
const BASE_URL = process.env.BASE_URL || `http://127.0.0.1:${PORT}`
1313
const BETA_BASE_URL = process.env.BETA_BASE_URL || `http://127.0.0.1:${BETA_PORT}`
1414

15-
const { NodeVM } = require('vm2')
16-
const vmconfigs = {
17-
console: 'inherit',
18-
require: {
19-
builtin: ['fs'],
20-
mock: {
21-
fs: {
22-
readFileSync(path) {
23-
try {
24-
const data = fs.readFileSync(path.join(workspace, path))
25-
return data
26-
} catch (err) {
27-
console.error(err)
28-
}
29-
},
30-
writeFileSync(path, data) {
31-
try {
32-
fs.writeFileSync(path.join(workspace, path), data)
33-
} catch (err) {
34-
console.error(err)
35-
}
36-
},
37-
existsSync(path) {
38-
return fs.existsSync(path.join(workspace, path))
39-
},
40-
},
41-
},
42-
},
43-
}
44-
4515
const evalFn = async ({ $request, scriptFilePath }) => {
4616
let content = fs.readFileSync(scriptFilePath, { encoding: 'utf8' })
4717
content = content.replace(/\$\.?done\(/g, '$eval_env.resolve(')

0 commit comments

Comments
 (0)