-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
152 lines (137 loc) · 4.84 KB
/
Copy pathcore.js
File metadata and controls
152 lines (137 loc) · 4.84 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const fs = require('fs');
const color = require('cli-color');
const tmp = require('tmp');
const spawn = require('child_process').spawn;
const beautify = require('js-beautify').js_beautify;
function proc(program, configFile) {
var saveToFile = true === program.save
? 'config.json' : program.save || false;
var v2rayPath = program.v2ray
? program.v2ray : 'v2ray';
if (fs.existsSync(configFile.base) === false) {
errorText("Base config not found!");
console.log("(v2cust -h for help.)");
process.exit(1);
}
if (fs.existsSync(configFile.override) === false) {
errorText("Override config not found!");
process.exit(1);
}
if (fs.accessSync(configFile.base, fs.constants.R_OK) === false) {
errorText("Base config is not readable!");
process.exit(1);
}
if (fs.accessSync(configFile.override, fs.constants.R_OK) === false) {
errorText("Override config is not readable!");
process.exit(1);
}
try {
var baseConfig = fs.readFileSync(configFile.base, encoding = 'utf-8');
var overrideConfig = fs.readFileSync(configFile.override, encoding = 'utf-8');
} catch (err) {
console.error(err);
process.exit(1);
}
try {
var baseConfigJSON = JSON.parse(baseConfig);
var overrideConfigJSON = JSON.parse(overrideConfig);
} catch (err) {
errorText("Fail to parse JSON. Detailed message:");
console.error(err);
process.exit(1);
}
try {
let mergeConfig = determine(program);
// console.log(mergeConfig);
if (mergeConfig.log) baseConfigJSON.log = overrideConfigJSON.log;
if (mergeConfig.inbound) {
baseConfigJSON.inbound = overrideConfigJSON.inbound;
baseConfigJSON.inbounds = overrideConfigJSON.inbounds;
}
if (mergeConfig.outbound) {
baseConfigJSON.outbound = overrideConfigJSON.outbound;
baseConfigJSON.outbounds = overrideConfigJSON.outbounds;
}
if (mergeConfig.api) baseConfigJSON.api = overrideConfigJSON.api;
if (mergeConfig.dns) baseConfigJSON.dns = overrideConfigJSON.dns;
if (mergeConfig.stats) baseConfigJSON.stats = overrideConfigJSON.stats;
if (mergeConfig.policy) baseConfigJSON.policy = overrideConfigJSON.policy;
if (mergeConfig.transport) baseConfigJSON.transport = overrideConfigJSON.transport;
if (mergeConfig.reverse) baseConfigJSON.reverse = overrideConfigJSON.reverse;
if (mergeConfig.routing) {
if (!overrideConfigJSON.routing) {
baseConfigJSON.routing = baseConfigJSON.inboundDetour = baseConfigJSON.outboundDetour = undefined;
} else {
baseConfigJSON.routing = overrideConfigJSON.routing;
baseConfigJSON.inboundDetour = overrideConfigJSON.inboundDetour;
baseConfigJSON.outboundDetour = overrideConfigJSON.outboundDetour;
}
}
} catch (err) {
errorText("Fail to merge file. Detailed message:");
console.error(err);
process.exit(1);
}
var content = beautify(JSON.stringify(baseConfigJSON));
if (program.show) {
console.log(color.blue(color.bold("[Info] ") + "merged config:"));
console.log(content);
process.exit();
}
if (saveToFile) {
saveConfig(saveToFile, content, program.overwrite);
process.exit();
} else {
runV2ray(v2rayPath, content);
}
}
function errorText(text) {
console.error(color.red(color.bold('[Error] ') + text));
}
function saveConfig(filename, text, overwrite) {
console.log(color.blue(color.bold("[Info] ") + "saving to " + filename));
if (fs.existsSync(filename)) {
if (!overwrite) {
console.warn(color.yellow.bold("[WARNING] ") + "File exists, overwrite?");
console.warn("Use --overwrite to overwrite.");
process.exit(1);
} else {
console.warn(color.yellow.bold("[Warning] ") + "Overwriting " + filename);
}
}
try {
fs.writeFileSync(filename, text);
} catch (err) {
errorText("Fail to save file. Detailed message:");
console.error(err);
process.exit(1);
}
}
function runV2ray(exePath, content) {
var tmpobj = tmp.fileSync();
fs.writeFileSync(tmpobj.fd, content);
let v2rayProc = spawn(exePath, ['-config', tmpobj.name]);
v2rayProc.stdout.on('data', (data) => { console.log(`${data}`); });
v2rayProc.stderr.on('data', (data) => { console.log(`${data}`); });
v2rayProc.on('exit', function (code, signal) {
console.log('v2ray has exited. Exit code: ' + code);
process.exit();
});
}
function determine(program) {
var dtm = {};
dtm.log = program.log ? true : false;
dtm.inbound = program.inbound ? true : false;
dtm.outbound = program.outbound ? true : false;
dtm.api = program.api ? true : false;
dtm.dns = program.dns ? true : false;
dtm.stats = program.stats ? true : false;
dtm.routing = program.routing ? true : false;
dtm.policy = program.policy ? true : false;
dtm.transport = program.transport ? true : false;
dtm.reverse = program.reverse ? true : false;
return dtm;
}
module.exports = {
process: proc
}