forked from Novage/wt-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun-uws-tracker.ts
More file actions
153 lines (132 loc) · 4.95 KB
/
run-uws-tracker.ts
File metadata and controls
153 lines (132 loc) · 4.95 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
/**
* Copyright 2019 Novage LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { UWebSocketsTracker } from "./uws-tracker";
import { FastTracker } from "./fast-tracker";
import { readFileSync } from "fs";
import { HttpResponse, HttpRequest } from "uWebSockets.js";
import { Tracker } from "./tracker";
import * as Debug from "debug";
const debugRequests = Debug("wt-tracker:uws-tracker-requests");
const debugRequestsEnabled = debugRequests.enabled;
// tslint:disable:no-console
async function main() {
let settingsFileData: any;
if (process.argv[2]) {
try {
settingsFileData = readFileSync(process.argv[2]);
} catch (e) {
console.error("failed to read configuration file:", e.toString());
return;
}
} else {
try {
settingsFileData = readFileSync("config.json");
} catch (e) {
if (e.code !== "ENOENT") {
console.error("failed to read configuration file:", e.toString());
return;
}
}
}
let settings: any;
try {
settings = (settingsFileData !== undefined) ? JSON.parse(settingsFileData.toString()) : {};
} catch (e) {
console.error("failed to parse JSON configuration file:", e.toString());
return;
}
const serversSettings = (settings.servers === undefined ? [{}] : settings.servers);
if (!(serversSettings instanceof Array)) {
console.error("failed to parse JSON configuration file: 'servers' property should be an array");
return;
}
const tracker = new FastTracker(settings.tracker);
try {
await runServers(serversSettings, tracker, settings.websocketsAccess);
} catch (e) {
console.error("failed to start the web server:", e.toString());
}
}
async function runServers(serversSettings: any[], tracker: Tracker, websocketsAccess: any) {
const servers: UWebSocketsTracker[] = [];
let indexHtml: Buffer | undefined;
try {
indexHtml = readFileSync("index.html");
} catch (e) {
if (e.code !== "ENOENT") {
throw e;
}
}
for (const serverSettings of serversSettings) {
serverSettings.access = websocketsAccess;
const server = new UWebSocketsTracker(tracker, serverSettings);
server.app
.get("/", (response: HttpResponse, request: HttpRequest) => {
debugRequest(server, request);
if (indexHtml !== undefined) {
response.end(indexHtml);
} else {
const status = "404 Not Found";
response.writeStatus(status).end(status);
}
})
.get("/stats.json", (response: HttpResponse, request: HttpRequest) => {
debugRequest(server, request);
const swarms = tracker.swarms;
let peersCount = 0;
for (const swarm of swarms.values()) {
peersCount += swarm.peers.length;
}
const serversStats = new Array<{ server: string, webSocketsCount: number }>();
for (const serverForStats of servers) {
const settings = serverForStats.settings;
serversStats.push({
server: `${settings.server.host}:${settings.server.port}`,
webSocketsCount: serverForStats.stats.webSocketsCount,
});
}
response.writeHeader("Content-Type", "application/json")
.end(JSON.stringify({
torrentsCount: swarms.size,
peersCount: peersCount,
servers: serversStats,
memory: process.memoryUsage(),
}));
})
.any("/*", (response: HttpResponse, request: HttpRequest) => {
debugRequest(server, request);
const status = "404 Not Found";
response.writeStatus(status).end(status);
});
servers.push(server);
await server.run();
console.info(`listening ${server.settings.server.host}:${server.settings.server.port}`);
}
}
function debugRequest(server: UWebSocketsTracker, request: HttpRequest) {
if (debugRequestsEnabled) {
debugRequests(server.settings.server.host, server.settings.server.port,
"request method:", request.getMethod(), "url:", request.getUrl(),
"query:", request.getQuery());
}
}
(async () => {
try {
await main();
} catch (e) {
console.error(e);
}
})();