forked from phts/nodejs-torrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.ts
More file actions
48 lines (43 loc) · 1.24 KB
/
tracker.ts
File metadata and controls
48 lines (43 loc) · 1.24 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
/// <reference path="../typings/main.d.ts" />
import * as http from 'http';
import * as url from 'url';
import * as errors from './tracker-errors';
import AnnounceParamsParser from './announce-params-parser';
import TrackerService from './tracker-service';
export class Tracker {
private service: TrackerService;
private server: http.Server;
constructor () {
this.service = new TrackerService();
this.server = http.createServer();
this.server.on('request', this.onRequest.bind(this));
}
start (port: number) {
port = port || 8080;
this.server.listen(port);
}
close () {
this.server.close();
}
private onRequest(request: http.IncomingMessage, response: http.ServerResponse) {
var u = url.parse(request.url, true);
try {
if (request.method !== 'GET') {
throw new errors.NotFoundError();
}
if (u.pathname === '/announce') {
let params = new AnnounceParamsParser(u.query).parse();
this.service.announce(params);
} else {
throw new errors.NotFoundError();
}
} catch (err) {
if (err instanceof errors.TrackerError) {
response.statusCode = err.statusCode;
} else {
response.statusCode = 500;
}
}
response.end();
}
}