-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.js
93 lines (88 loc) · 2.87 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const crypto = require('crypto');
const url = require('url');
class RuleProviderStruct {
constructor(behavior, url, group, prepend, name) {
this.Behavior = behavior;
this.Url = url;
this.Group = group;
this.Prepend = prepend;
this.name = name;
}
}
class RuleStruct {
constructor(rule, prepend) {
this.Rule = rule;
this.Prepend = prepend;
}
}
function validateQuery(query) {
if ((query.sub === undefined || query.sub === '') && (query.proxy === undefined || query.proxy === '')) {
throw new Error('参数错误: sub 和 proxy 不能同时为空');
}
if (query.sub) {
query.subs = query.sub.split(',');
query.subs.forEach(sub => {
if (!sub.startsWith('http')) {
throw new Error('参数错误: sub 格式错误');
}
new url.URL(sub); // Will throw if invalid
});
} else {
query.subs = null;
}
if (query.proxy) {
query.proxies = query.proxy.split(',');
} else {
query.proxies = null;
}
// Template URL validation
if (query.template) {
new url.URL(query.template); // Will throw if invalid
}
// RuleProvider parsing and validation
if (query.ruleProvider) {
const reg = /\[(.*?)\]/g;
let ruleProviders = [...query.ruleProvider.matchAll(reg)];
query.RuleProviders = ruleProviders.map(rp => {
const parts = rp[1].split(',');
if (parts.length < 4) {
throw new Error('参数错误: ruleProvider 格式错误');
}
const uri = new url.URL(parts[1]);
let name = parts[4];
if (!name) {
const hash = crypto.createHash('sha224').update(uri.toString()).digest('hex');
name = hash;
}
return new RuleProviderStruct(parts[0], uri.toString(), parts[2], parts[3] === 'true', name);
});
} else {
query.RuleProviders = null;
}
// Rule parsing and validation
if (query.rule) {
const reg = /\[(.*?)\]/g;
let rules = [...query.rule.matchAll(reg)];
query.Rules = rules.map(r => {
const parts = r[1].split(',');
return new RuleStruct(parts[0], parts[1] === 'true');
});
} else {
query.Rules = null;
}
// Replace parsing and validation
if (query.replace && query.replace.trim() !== "") {
const reg = /\[<(.*?)>,<(.*?)>\]/g;
let replaces = [...query.replace.matchAll(reg)];
query.ReplaceKeys = replaces.map(r => r[1]);
query.ReplaceTo = replaces.map(r => r[2]);
}
query.refresh = query.refresh === 'true';
query.lazy = query.lazy === 'true';
query.nodeList = query.nodeList === 'true';
query.autoTest = query.autoTest === 'true';
return query;
}
module.exports = {
validateQuery
}