forked from localtunnel/server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
88 lines (71 loc) · 2.22 KB
/
server.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
import Koa from 'koa';
import tldjs from 'tldjs';
import http from 'http';
import Router from 'koa-router';
import ClientManager from './lib/ClientManager';
import { Address, createAddress } from './lib/Address';
function setupAPIs(clientManager) {
const app = new Koa();
const router = new Router();
router.get('/api/status', async (ctx, next) => {
const stats = clientManager.stats;
ctx.body = {
tunnels: stats.tunnels,
mem: process.memoryUsage(),
};
});
router.get('/api/tunnels/:id/status', async (ctx, next) => {
const clientId = ctx.params.id;
const client = clientManager.getClient(clientId);
if (!client) {
ctx.throw(404);
return;
}
const stats = client.stats();
ctx.body = {
connected_sockets: stats.connectedSockets,
};
});
app.use(router.routes());
app.use(router.allowedMethods());
return app;
}
function startServer(opt, clientManager, appCallback) {
const validHosts = (opt.domain) ? [opt.domain] : undefined;
const myTldjs = tldjs.fromUserSettings({ validHosts });
const server = http.createServer();
server.on('request', (req, res) => {
// without a hostname, we won't know who the request is for
const hostname = req.headers.host;
if (!hostname) {
res.statusCode = 400;
res.end('Host header is required');
return;
}
const clientId = myTldjs.getSubdomain(hostname);
if (!clientId) {
appCallback(req, res);
return;
}
const address = createAddress(clientId);
if (address == null) {
appCallback(req, res);
return;
}
const client = clientManager.getClient(address.get());
if (!client) {
res.statusCode = 404;
res.end('404');
return;
}
client.handleRequest(req, res);
});
return server;
}
export default function(opt) {
opt = opt || {};
const manager = new ClientManager(opt);
const app = setupAPIs(manager);
const appCallback = app.callback();
return startServer(opt, manager, appCallback);
};