-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcli.js
executable file
·146 lines (121 loc) · 3.82 KB
/
cli.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
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
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cluster_1 = require("cluster");
const makeWork = () => {
const worker = cluster_1.fork();
worker.once('exit', (code) => {
if ( code === 3) {
logger (`worker exit with code 3, STOP running!`)
process.exit()
}
logger(`Cluster Worker exit! create Work again!`);
return makeWork();
});
setTimeout (() => {
worker.exit(0)
}, 1000*60*60*12)
};
const util_1 = require ( "util" );
const { logger } = require("./dist/GateWay/log");
const [,,...args] = process.argv;
const setup = require ('./package.json')
let debug = false
const printUsage = () => {
logger (`qtgate server version ${ setup.version }\nGateway usage: qtgate-server -g password port\nProxy server usage: qtgate-server -p gatewayFileName.json proxyPort [listenPORT] [listenPath] \n` );
process.exit (0);
}
const checkIptables = () => {
if ( process.platform !== 'linux' ) {
return false;
}
return true;
};
args.forEach(n => {
if (/\-d/.test(n)) {
debug = true;
};
});
const checkIPTables = ( CallBack ) => {
if ( !checkIptables()) {
return CallBack ( new Error ('Linux only!'))
}
const child_process_1 = require("child_process");
return child_process_1.exec('which iptables', ( err, stdout, stderr ) => {
if (err) {
return CallBack(err);
}
if ( stdout ) {
return CallBack ( null, true );
}
return CallBack(stderr);
});
};
if ( !args[0] ) {
printUsage ();
}
if ( args[0] === '-g' ) {
if (cluster_1.isPrimary) {
makeWork();
} else {
const server = require('./dist/GateWay/qtGate_httpServer');
if ( !args [1] ) {
const { request } = require('https');
const options = {
hostname: 'tq-proxy.13b995b1f.ca',
port: 443,
path: '/',
method: 'POST',
headers: {
'Content-Type': 'application/json',
}
};
let data = ''
const req = request(options, (res) => {
res.on('data', (d) => {
data += d.toString();
});
res.once('end', () => {
if (!data) {
logger (`can't setup password`);
process.exit (3);
}
logger (`start gateway at port ${80} password ${ data }`);
return new server.ssModeV3 ( 80, data, debug, true);
})
})
req.on('error', (e) => {
console.error(e);
process.exit (3);
});
return req.end();
}
new server.ssModeV3 ( args [2], args [1], debug, false);
}
}
if ( args[0] === '-p' ) {
let gateway = null;
const filename = process.cwd() + '/'+ args[1]
try {
gateway = require ( filename );
} catch (ex) {
console.log (`JSON Error! have no ${ filename }`);
return printUsage()
}
if ( !gateway ) {
console.log (`gateway Error! have no ./gateway.json`)
printUsage()
}
if ( args[3] && args[4]) {
checkIPTables (( err, data ) => {
if ( err ) {
console.log ( 'Your system have not support iptables!', err );
return process.exit (1);
}
const ipFilter = require ('./dist/ProxyServer/clientIpfliter');
new ipFilter.default ( args[4], args[3], args[2]);
})
}
const proxyServer = require ('./dist/ProxyServer/client');
const server = new proxyServer.proxyServer ( args[2], gateway, debug );
}