forked from alibaba/anyproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsServerMgr.js
More file actions
197 lines (177 loc) · 5.31 KB
/
httpsServerMgr.js
File metadata and controls
197 lines (177 loc) · 5.31 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
'use strict'
//manage https servers
const async = require('async'),
https = require('https'),
tls = require('tls'),
crypto = require('crypto'),
color = require('colorful'),
certMgr = require('./certMgr'),
logUtil = require('./log'),
util = require('./util'),
wsServerMgr = require('./wsServerMgr'),
co = require('co'),
constants = require('constants'),
asyncTask = require('async-task-mgr');
const createSecureContext = tls.createSecureContext || crypto.createSecureContext;
//using sni to avoid multiple ports
function SNIPrepareCert(serverName, SNICallback) {
let keyContent,
crtContent,
ctx;
async.series([
(callback) => {
certMgr.getCertificate(serverName, (err, key, crt) => {
if (err) {
callback(err);
} else {
keyContent = key;
crtContent = crt;
callback();
}
});
},
(callback) => {
try {
ctx = createSecureContext({
key: keyContent,
cert: crtContent
});
callback();
} catch (e) {
callback(e);
}
}
], (err) => {
if (!err) {
const tipText = 'proxy server for __NAME established'.replace('__NAME', serverName);
logUtil.printLog(color.yellow(color.bold('[internal https]')) + color.yellow(tipText));
SNICallback(null, ctx);
} else {
logUtil.printLog('err occurred when prepare certs for SNI - ' + err, logUtil.T_ERR);
logUtil.printLog('err occurred when prepare certs for SNI - ' + err.stack, logUtil.T_ERR);
}
});
}
//config.port - port to start https server
//config.handler - request handler
/**
* Create an https server
*
* @param {object} config
* @param {number} config.port
* @param {function} config.handler
*/
function createHttpsServer(config) {
if (!config || !config.port || !config.handler) {
throw (new Error('please assign a port'));
}
return new Promise((resolve) => {
certMgr.getCertificate('anyproxy_internal_https_server', (err, keyContent, crtContent) => {
const server = https.createServer({
secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1,
SNICallback: SNIPrepareCert,
key: keyContent,
cert: crtContent
}, config.handler).listen(config.port);
resolve(server);
});
});
}
/**
* create an https server that serving on IP address
* @param @required {object} config
* @param @required {string} config.ip the IP address of the server
* @param @required {number} config.port the port to listen on
* @param @required {function} handler the handler of each connect
*/
function createIPHttpsServer(config) {
if (!config || !config.port || !config.handler) {
throw (new Error('please assign a port'));
}
if (!config.ip) {
throw (new Error('please assign an IP to create the https server'));
}
return new Promise((resolve) => {
certMgr.getCertificate(config.ip, (err, keyContent, crtContent) => {
const server = https.createServer({
secureOptions: constants.SSL_OP_NO_SSLv3 || constants.SSL_OP_NO_TLSv1,
key: keyContent,
cert: crtContent
}, config.handler).listen(config.port);
resolve(server);
});
});
}
/**
*
*
* @class httpsServerMgr
* @param {object} config
* @param {function} config.handler handler to deal https request
*
*/
class httpsServerMgr {
constructor(config) {
if (!config || !config.handler) {
throw new Error('handler is required');
}
this.instanceDefaultHost = '127.0.0.1';
this.httpsAsyncTask = new asyncTask();
this.handler = config.handler;
this.wsHandler = config.wsHandler
}
getSharedHttpsServer(hostname) {
// ip address will have a unique name
const finalHost = util.isIpDomain(hostname) ? hostname : this.instanceDefaultHost;
const self = this;
function prepareServer(callback) {
let instancePort;
co(util.getFreePort)
.then(co.wrap(function *(port) {
instancePort = port;
let httpsServer = null;
// if ip address passed in, will create an IP http server
if (util.isIpDomain(hostname)) {
httpsServer = yield createIPHttpsServer({
ip: hostname,
port,
handler: self.handler
});
} else {
httpsServer = yield createHttpsServer({
port,
handler: self.handler
});
}
wsServerMgr.getWsServer({
server: httpsServer,
connHandler: self.wsHandler
});
httpsServer.on('upgrade', (req, cltSocket, head) => {
logUtil.debug('will let WebSocket server to handle the upgrade event');
});
const result = {
host: finalHost,
port: instancePort,
};
callback(null, result);
return result;
}))
.catch(e => {
callback(e);
});
}
return new Promise((resolve, reject) => {
// each ip address will gain a unit task name,
// while the domain address will share a common task name
self.httpsAsyncTask.addTask(`createHttpsServer-${finalHost}`, prepareServer, (error, serverInfo) => {
if (error) {
reject(error);
} else {
resolve(serverInfo);
}
});
});
}
}
module.exports = httpsServerMgr;