forked from localtunnel/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
218 lines (171 loc) · 5.91 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import Koa from 'koa';
import tldjs from 'tldjs';
import Debug from 'debug';
import http from 'http';
const jwt = require('jsonwebtoken');
import { hri } from 'human-readable-ids';
import Router from 'koa-router';
import ClientManager from './lib/ClientManager';
const debug = Debug('localtunnel:server');
const secret = process.env.JWT_SECRET || 'v2cloud-vnc';
const authenticated = async (ctx, next) => {
const token = ctx.request.headers['vnc-token'];
debug("TOKEN: ", token);
if (!token) ctx.throw(403, 'No token.');
try {
debug('authenticated jwt', jwt.verify(token, secret));
} catch (err) {
ctx.throw(err.status || 403, err.text);
}
await next();
};
export default function(opt) {
opt = opt || {};
const validHosts = (opt.domain) ? [opt.domain] : undefined;
const myTldjs = tldjs.fromUserSettings({ validHosts });
const landingPage = opt.landing || 'https://v2cloud.com';
function GetClientIdFromHostname(hostname) {
return myTldjs.getSubdomain(hostname);
}
const manager = new ClientManager(opt);
const schema = opt.secure ? 'https' : 'http';
const app = new Koa();
const router = new Router();
app.use(authenticated);
router.get('/api/status', async (ctx, next) => {
ctx.body = {
tunnelsCount: manager.stats.tunnels,
tunnels: manager.getClients(),
mem: process.memoryUsage(),
};
});
router.get('/api/tunnels/:id/status', async (ctx, next) => {
const clientId = ctx.params.id;
const client = manager.getClient(clientId);
if (!client) {
ctx.throw(404);
return;
}
const stats = client.stats();
ctx.body = {
connected_sockets: stats.connectedSockets,
available_sockets: stats.availableSockets,
client_address: stats.clientAddress,
waiting_connections: stats.waitingConnections,
no_more_socket_events: stats.noMoreSocketEvents,
is_closed: stats.isClosed,
};
});
router.post('/api/tunnels/:id/delete', async (ctx, next) => {
const clientId = ctx.params.id;
const client = manager.getClient(clientId);
if (!client) {
ctx.throw(404);
return;
}
manager.removeClient(clientId);
ctx.body = {
delete_status: "success",
};
});
app.use(router.routes());
app.use(router.allowedMethods());
// root endpoint
app.use(async (ctx, next) => {
const path = ctx.request.path;
// skip anything not on the root path
if (path !== '/') {
await next();
return;
}
const isNewClientRequest = ctx.query['new'] !== undefined;
if (isNewClientRequest) {
const reqId = hri.random();
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
ctx.body = info;
return;
}
// no new client request, send to landing page
ctx.redirect(landingPage);
});
// anything after the / path is a request for a specific client name
// This is a backwards compat feature
app.use(async (ctx, next) => {
const parts = ctx.request.path.split('/');
// any request with several layers of paths is not allowed
// rejects /foo/bar
// allow /foo
if (parts.length !== 2) {
await next();
return;
}
const reqId = parts[1];
// limit requested hostnames to 63 characters
if (! /^(?:[a-z0-9][a-z0-9\-]{4,63}[a-z0-9]|[a-z0-9]{4,63})$/.test(reqId)) {
const msg = 'Invalid subdomain. Subdomains must be lowercase and between 4 and 63 alphanumeric characters.';
ctx.status = 403;
ctx.body = {
message: msg,
};
return;
}
debug('making new client with id %s', reqId);
const info = await manager.newClient(reqId);
const url = schema + '://' + info.id + '.' + ctx.request.host;
info.url = url;
ctx.body = info;
return;
});
const server = http.createServer();
const appCallback = app.callback();
server.on('request', (req, res) => {
debug("ON REQUEST URL:", req.url);
// 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;
}
// get clientId from route /connect/$clientId
const clientId = req.url.substr(0, "/connect".length) === "/connect"
? req.url.substr("/connect/".length)
: GetClientIdFromHostname(hostname);
if (!clientId) {
appCallback(req, res);
return;
}
const client = manager.getClient(clientId);
if (!client) {
res.statusCode = 404;
res.end(`Can't find active tunnel ${clientId}...`);
return;
}
client.handleRequest(req, res);
});
server.on('upgrade', (req, socket, head) => {
debug("ON UPGRADE URL:", req.url);
const hostname = req.headers.host;
if (!hostname) {
socket.destroy();
return;
}
const clientId = req.url.substr(0, "/connect".length) === "/connect"
? req.url.substr("/connect/".length)
: GetClientIdFromHostname(hostname);
if (!clientId) {
socket.destroy();
return;
}
const client = manager.getClient(clientId);
if (!client) {
socket.destroy();
return;
}
client.handleUpgrade(req, socket);
});
return server;
};