-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb.js
252 lines (231 loc) · 10.4 KB
/
web.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var Connect = require('connect');
var Formidable = require('formidable');
var BEnc = require('benc');
var Model = require('./model');
var TorrentManager = require('./torrent_manager');
var Html = require('./html');
var MIME = require('./mime');
process.on('uncaughtException', function(e) {
console.log(e.stack ? e.stack : e.toString());
});
Html.setTemplate('./public/template.htm');
var webStats = {
root: 0,
up: 0,
show: 0,
json: 0,
stream: 0
};
/* Recommendations updater */
var recommendations = [];
function updateRecommendations() {
Model.getRecommendations(function(error, r) {
if (r)
recommendations = r;
else
console.log("Cannot get recommendations: " + error);
});
}
setInterval(updateRecommendations, 10 * 1000);
updateRecommendations();
function handleTorrentUpload(part) {
var form = this;
if (part.name == 'torrentfile') {
var parser = new BEnc.TorrentParser(), torrent, infoHex;
parser.on('model', function(t) {
torrent = t;
});
parser.on('infoHex', function(ih) {
infoHex = ih;
});
parser.on('error', function(e) {
console.log({ TorrentParser: e });
});
part.on('data', function(data) {
parser.write(data);
});
part.on('end', function() {
if (torrent && infoHex)
form.emit('file', part.name,
{ torrent: torrent,
infoHex: infoHex });
});
}
}
function acceptTorrent(infoHex, torrent, cb) {
var fileinfo = {
pieceLength: torrent.info['piece length'],
name: torrent.info.name.toString(),
files: []
};
if (torrent.info.files) {
var offset = 0;
torrent.info.files.forEach(
function(fileDict) {
fileinfo.files.push({ name: fileDict.path.join('/'),
offset: offset,
length: fileDict.length
});
offset += fileDict.length;
});
} else if (torrent.info.name) {
fileinfo.files.push({ name: torrent.info.name.toString(),
offset: 0,
length: torrent.info.length
});
} else {
cb('Invalid torrent file');
return;
}
var trackerUrls = [torrent.announce];
if (torrent['announce-list'])
torrent['announce-list'].forEach(
function(list) {
list.forEach(
function(url) {
trackerUrls.push(url.toString());
});
});
Model.putFileinfo(infoHex, fileinfo,
function(error) {
if (error)
cb(error);
else
Model.putTrackers(infoHex, trackerUrls, cb);
});
}
function streamer(req, res, next) {
var m;
var inline = req.url.match(/\?inline$/);
if (inline)
req.url = req.url.replace( '?inline', '' );
if (req.method == 'GET' &&
(m = req.url.match(/^\/([0-9a-f]{40})\/(.+)/))) {
webStats.stream++;
var infoHex = m[1];
var filename = unescape(m[2]);
Model.getFileinfo(infoHex, function(error, fileinfo) {
if (error === 'Not found') {
res.writeHead(404, {});
res.end('Not found');
} else if (fileinfo) {
var file;
fileinfo.files.forEach(function(file1) {
if (file1.name === filename)
file = file1;
});
if (!file) {
res.writeHead(404, {});
res.end('Not found');
} else {
var ctx = TorrentManager.get(infoHex);
var resHeaders = {'Content-Type': MIME.fileType(filename),
'Accept-Ranges': 'bytes'};
if (inline)
resHeaders['Content-Disposition'] = 'inline; filename="'+filename+'"';
var offset = file.offset;
var length = file.length;
var m;
if (req.headers.range &&
(m = req.headers.range.match(/bytes=(\d+)/))) {
var start = parseInt(m[1], 10);
offset += start;
var fullLength = length;
length -= start;
resHeaders['Content-Range'] = 'bytes ' + start +
'-' + (start + length) + '/' + fullLength;
}
resHeaders['Content-Length'] = length;
var stream = ctx.stream(offset, length);
req.socket.on('end', function() {
stream.end();
});
req.socket.on('error', function() {
stream.end();
});
stream.on('data', function(data) {
res.write(data);
});
stream.on('end', function() {
res.end();
});
res.writeHead(resHeaders['Content-Range'] ? 206 : 200, resHeaders);
res.socket.setKeepAlive(true, 5000);
}
} else
throw error;
});
} else {
next();
}
}
function app(app) {
app.get('/stats.json', function(req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
var stats = TorrentManager.getStats();
stats.web = webStats;
res.end(JSON.stringify(stats));
});
app.post('/up', function(req, res) {
webStats.up++;
var form = new Formidable.IncomingForm();
form.encoding = 'binary';
form.bytesExpected = 2 * 1024 * 1024; // 2 MB max
form.handlePart = handleTorrentUpload;
form.parse(req, function(err, fields, forms) {
var torrentfile = forms.torrentfile;
if (torrentfile) {
var infoHex = torrentfile.infoHex;
acceptTorrent(infoHex, torrentfile.torrent,
function() {
res.writeHead(302,
{ Location: '/' + infoHex + '.html' });
res.end();
});
} else {
res.writeHead(400, {});
res.end('Please upload something here');
}
});
});
app.get('/:infoHex.json', function(req, res) {
webStats.json++;
var infoHex = req.params.infoHex;
var ctx = TorrentManager.get(infoHex);
ctx.waitInfo(function(info) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify(info));
});
});
app.get('/', function(req, res) {
webStats.root++;
var torrentItemString = '';
recommendations.forEach(function(r) {
torrentItemString += Html.tag('li',[], Html.tag('a', {href:'/'+r.infoHex + '.html'},r.name));
});
res.writeHead(200, {});
res.end(Html.index( torrentItemString ));
});
app.get('/:infoHex.html', function(req, res) {
webStats.show++;
Model.getFileinfo(req.params.infoHex, function(error, fileinfo) {
if (error === 'Not found') {
res.writeHead(404, {});
res.end('Not found');
} else if (fileinfo) {
var files = Model.parseTreeByFiles(fileinfo.files, req.params.infoHex);
res.writeHead(200, {});
res.write(Html.show(fileinfo.name.toString(),files));
res.end();
} else
throw error;
});
});
}
Connect.createServer(
Connect.logger(),
Connect.staticProvider(__dirname + '/public'),
streamer,
Connect.router(app),
Connect.errorHandler({ dumpExceptions: true, showStack: true })
).listen(parseInt(process.env.PORT || "8001", 10));