Repository navigation
Expand file tree
/
Copy pathtestserver.js
More file actions
68 lines (59 loc) · 1.77 KB
/
Copy pathtestserver.js
File metadata and controls
68 lines (59 loc) · 1.77 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
import https from 'https'
import http from 'http'
import pem from 'pem'
import { createProxy } from 'proxy'
export function startServer (PORT) {
return new Promise((resolve, reject) => {
pem.createCertificate({ days: 1, selfSigned: true }, function (err, keys) {
if (err) {
reject(err)
}
const server = https.createServer({ key: keys.serviceKey, cert: keys.certificate }, function (req, res) {
res.end(':' + req.url + ':')
}).listen(PORT)
resolve(server)
})
})
}
function startProxy (PORT, auth) {
const server = http.createServer()
const p = createProxy(server)
if (auth) {
p.authenticate = function (req) {
return req.headers['proxy-authorization'] === 'Basic Ym9iOmFsaWNl' // user bob password alice
}
}
// noinspection JSUnresolvedFunction
server.listen(PORT)
return server
}
const servers = []
export async function start () {
servers.push(await startServer(8443))
servers.push(await startServer(8444))
servers.push(startProxy(3128))
servers.push(startProxy(3129, true))
}
export async function stop () {
servers[0].close()
servers[1].close()
servers[2].close()
servers[3].close()
}
/*
if (require.main === module) {
start()
console.log(`
Server1 listening on https://localhost:8443
Server2 listening on https://localhost:8444
Proxy1 listening on http://localhost:3128
Proxy2 listening on http://localhost:3129 Authentication "bob:alice"
Get https://localhost:8443/quit to exit process
curl -k https://localhost:8443
curl -k https://localhost:8444
curl -k --proxy localhost:3128 https://localhost:8443
curl -k --proxy-basic --proxy-user bob:alice --proxy localhost:3129 https://localhost:8443
curl -k --proxy-basic --proxy-user WRONG:PASSWORD --proxy localhost:3129 https://localhost:8443
`)
}
*/