-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.js
146 lines (128 loc) · 3.73 KB
/
http.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
const { createServer } = require('http');
const { createProxyServer } = require('http-proxy');
const { readFile, readdir } = require('fs/promises');
const { join } = require('path');
function createHttpProxyServer(configDir, domain) {
const proxy = createProxyServer({ ws: true });
const server = createServer((req, res) => {
getHostConfigIfProxyable(configDir, domain, req)
.then((config) => {
if (config) {
proxy.web(
req,
res,
{ target: `http://127.0.0.1:${config.port}` },
(err) => {
if (err.code === 'ECONNREFUSED') {
sendHtml(
res,
`<h1>Service not started</h1>
<p>${config.domain} expects a listener on port ${config.port}</p>
<a href="http://localhost">Available services</a>`,
503,
);
} else {
sendHtml(res, `<h1>Error</h1><pre>${err.stack}</pre>`, 500);
}
},
);
} else {
return getServiceListPage(configDir).then((page) => {
sendHtml(res, page);
});
}
})
.catch((err) => {
sendHtml(res, `<h1>Error</h1><pre>${err.stack}</pre>`, 500);
});
});
server.on('upgrade', (req, socket, head) => {
getHostConfigIfProxyable(configDir, domain, req)
.then((config) => {
if (!config) return;
proxy.ws(
req,
socket,
head,
{ target: `http://127.0.0.1:${config.port}` },
(err) => {
if (err) {
console.error(err);
}
},
);
})
.catch((err) => {
console.error(err);
});
});
return server;
}
async function getHostConfigIfProxyable(configDir, domain, req) {
// make sure we've got all the required info
if (
!req.headers.host ||
!req.headers.host.endsWith(domain) ||
!req.socket.remoteAddress
) {
return null;
}
// make sure we can get the config
const config = await readHostConfig(configDir, req.headers.host);
if (!config) return null;
return config;
}
async function readHostConfig(configDir, host) {
try {
const domains = (await getAvailableDomains(configDir)).sort(
(a, b) => b.length - a.length,
);
const domain = domains.find((x) => host === x || host.endsWith('.' + x));
if (!domain) return null;
const config = await readFile(join(configDir, domain), 'utf8');
const port = parseInt(config);
return isNaN(port) ? null : { domain, port };
} catch {
return null;
}
}
async function getAvailableDomains(configDir) {
try {
const files = await readdir(configDir);
return files.filter((x) => !x.startsWith('.'));
} catch (err) {
if (err.code === 'ENOENT') {
return [];
} else throw err;
}
}
async function getServiceListPage(configDir) {
const domains = await getAvailableDomains(configDir);
const domainList = domains.map(
(domain) => `<li><a href="http://${domain}">${domain}</a></li>`,
);
return domainList.length
? `<h1>Available services</h1><ul>${domainList.join('')}</ul>`
: `<h1>No available services</h1><p>Define some services in <code>${configDir}</code> to get started.</p>`;
}
function sendHtml(res, contents, status = 200) {
if (!res.headersSent) {
res.writeHead(status, { 'Content-Type': 'text/html' });
}
res.end(getHtmlDoc(contents));
}
function getHtmlDoc(contents) {
return `<!DOCTYPE html>
<html>
<head>
<title>Proxy</title>
<style>
body { font-family: sans-serif; }
</style>
<body>
${contents}
</body>
</html>`;
}
module.exports.createHttpProxyServer = createHttpProxyServer;
module.exports.getAvailableDomains = getAvailableDomains;