-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
177 lines (137 loc) · 5.69 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* papp browser for peer apps
* Copyright(c) 2015 Tom Zhou <iwebpp@gmail.com>
* MIT Licensed
*/
var exec = require('child_process').exec,
fork = require('child_process').fork,
child;
var http = require('http');
var fs = require('fs');
var socks = require('socks5');
var freeport = require('freeport');
var os = require('os');
var forwardProxy = require('forward-proxy');
var httppForward = require('httpp-forward');
// prompt user key
var readline = require('readline');
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('Please enter your user key:', function(answer) {
var userkey = answer && answer.trim();
console.log('You just typed userkey: '+userkey);
rl.question('\nPlease enter PAC server port:', function(answer) {
var pacport = answer && answer.trim(); pacport = parseInt(pacport, 10);
console.log('You just typed PAC port: ', pacport, '\n');
rl.close();
var prxySrv = new forwardProxy({
endpoints: [{ip: 'iwebpp.com', port: 51686}, {ip: 'iwebpp.com', port: 51868}],
turn: [{ip: 'iwebpp.com', agent: 51866, proxy: 51688}],
usrkey: userkey,
secmode: 'acl',
sslmode: 'srv', // only authenticate server
access_local: true // allow acces to local export
}, function(err, proxy){
if (err || !proxy) {
console.log(err+',create proxy failed');
return
}
// query export sevice once
prxySrv.queryExport(function(err, srv){
if (err || !srv) {
console.log('No available export service, please run export service in advance');
} else {
console.log('%d available export services', Object.keys(srv).length);
}
// turn on export service query timer
prxySrv.turnQuerytimer(true);
var importApp = proxy.importApp;
// 1.
// get free tcp port
freeport(function(err, prxyPort) {
if (err) throw new Error(err+', get proxy port failed');
// 2.
// start http proxy service
var pxySrv = http.createServer();
pxySrv.on('request', importApp.httpApp.proxy);
pxySrv.on('connect', importApp.httpApp.tunnel);
pxySrv.listen(prxyPort, function() {
console.log('Http proxy server listen on port '+prxyPort);
freeport(function(err, scksPort) {
if (err) throw new Error(err+', get socks port failed');
// 2.1
// start socks proxy service
var sockspxySrv = socks.createServer(importApp.socksApp);
sockspxySrv.listen(scksPort, function() {
sockspxySrv.on('error', function (e) {
console.error('SERVER ERROR: %j', e);
});
console.log('Socks proxy server listen on port '+scksPort);
// 3.
// start httpp-forward proxy server for httpp enabled sites
var httppPrxy = new httppForward(function(err, proxy1){
if (err || !proxy1) {
console.log(err+',create httpp forward proxy failed');
return
}
// 3.1
// start http forward proxy service
freeport(function(err, prxyPort1) {
if (err) throw new Error(err+', get httpp proxy port failed');
var pxySrv1 = http.createServer();
pxySrv1.on('request', proxy1.httpProxy);
pxySrv1.on('connect', proxy1.httpTunnel);
pxySrv1.listen(prxyPort1, function() {
console.log('Httpp-forward proxy server listen on port '+prxyPort1);
// 3.2
// start httpp socks forward proxy service
freeport(function(err, scksPort1) {
if (err) throw new Error(err+', get httpp socks port failed');
var sockspxySrv1 = socks.createServer(proxy1.socksProxy);
sockspxySrv1.listen(scksPort1, function() {
sockspxySrv1.on('error', function (e) {
console.error('httpp SERVER ERROR: %j', e);
});
console.log('Httpp-forward socks server listen on port '+scksPort1);
// 5.
// start pac server
///freeport(function(err, pacPort) {
///if (err) throw new Error(err+', get pac port failed');
var pacPort = pacport;
// pac server
var rawstr = fs.readFileSync(__dirname+'/auto.pac').toString('utf-8');
// fill http proxy server
var pacstr = rawstr.replace(/proxy_port/gi, ''+prxyPort);
pacstr = pacstr.replace(/socks_port/gi, ''+scksPort);
// fill httpp-forward server
pacstr = pacstr.replace(/proxy_httpp_port/gi, ''+prxyPort1);
pacstr = pacstr.replace(/socks_httpp_port/gi, ''+scksPort1);
///console.log('pacstr: '+pacstr);
var pacsrv = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'application/x-ns-proxy-autoconfig'});
res.end(pacstr);
});
pacsrv.listen(pacPort, function() {
console.log('pac server listening on '+pacPort);
console.log('\n\nPlease set Web Browser Proxy setting to PAC server on 127.0.0.1:'+pacPort);
/*var pac = fork('./pac.js', [pacPort, prxyPort, scksPort, prxyPort1, scksPort1]);
pac.on('exit', function(code){
console.log('pac server exited '+code);
// exit main program
process.exit(code);
});*/
});
});
});
});
});
});
});
});
});
});
});
});
});
});