-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathclient.btapp.js
executable file
·516 lines (489 loc) · 24.7 KB
/
client.btapp.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// (c) 2012 Patrick Williams, BitTorrent Inc.
// Btapp may be freely distributed under the MIT license.
// For all details and documentation:
// http://pwmckenna.github.com/btapp
// Torrent Client (base functionality for Falcon/Local Torrent Clients)
// -------------
// TorrentClient provides a very thin wrapper around the web api
// It should facilitate finding the correct port and connecting if
// necessary to the user computer through falcon...by default uses localhost
// TorrentClient is responsible for the mapping between functions
// passed to as arguments and the string that proxies them to the client
// TorrentClient is responsible for creating functions that wrap around
// specific urls that can can be dangled off of models so that when we call
// stop on a torrent file, the torrent specific url is accessed without any
// effort on the part of the client
// Keep in mind that because jsonp won't time out naturally, we impose our own
// timeouts...this can lead to some less than desirable code :(
(function() {
"use strict";
function assert(b, err) { if(!b) { throw err; } }
//validate dependencies
assert(typeof JSON !== 'undefined', 'JSON is a hard dependency');
assert(typeof _ !== 'undefined', 'underscore/lodash is a hard dependency');
assert(typeof PluginManager !== 'undefined', 'plugin.btapp.js is a hard dependency');
assert(typeof Pairing !== 'undefined', 'pairing.btapp.js is a hard dependency');
assert(typeof jQuery !== 'undefined', 'jQuery is a hard dependency');
assert(typeof jQuery.jStorage !== 'undefined', 'jQuery.jStorage is a hard dependency');
//we will sadly need to fiddle with some globals for falcon one offs.
var root = this;
this.TorrentClient = Backbone.Model.extend({
initialize: function(attributes) {
this.btappCallbacks = {};
},
// We can't send function pointers to the torrent client server, so we'll send
// the name of the callback, and the server can call this by sending an event with
// the name and args back to us. We're responsible for making the call to the function
// when we detect this. This is the same way that jquery handles ajax callbacks.
storeCallbackFunction: function(cb) {
cb = cb || function() {};
var str = 'bt_';
for(var i = 0; i < 20 || (str in this.btappCallbacks); i++) { str += Math.floor(Math.random() * 10); }
this.btappCallbacks[str] = cb;
return str;
},
// We expect function signatures that come from the client to have a specific syntax
isRPCFunctionSignature: function(f) {
assert(typeof f === 'string', 'do not check function signature of non-strings');
return f.match(/\[native function\](\([^\)]*\))+/) ||
f.match(/\[nf\](\([^\)]*\))+/);
},
isJSFunctionSignature: function(f) {
assert(typeof f === 'string', 'do not check function signature of non-strings');
return f.match(/\[nf\]bt_/);
},
getStoredFunction: function(f) {
assert(TorrentClient.prototype.isJSFunctionSignature(f), 'only store functions that match the pattern "[nf]bt_*"');
var key = f.substring(4);
assert(key in this.btappCallbacks, 'trying to get a function with a key that is not recognized');
return this.btappCallbacks[key];
},
// Seeing as we're interfacing with a strongly typed language c/c++ we need to
// ensure that our types are at least close enough to coherse into the desired types
// takes something along the lines of "[native function](string,unknown)(string)".
validateArguments: function(functionValue, variables) {
assert(typeof functionValue === 'string', 'expected functionValue to be a string');
assert(typeof variables === 'object', 'expected variables to be an object');
var signatures = functionValue.match(/\(.*?\)/g);
return _.any(signatures, function(signature) {
signature = signature.match(/\w+/g) || []; //["string","unknown"]
return signature.length === variables.length && _.all(signature, function(type,index) {
if(typeof variables[index] === 'undefined') {
throw 'client functions do not support undefined arguments';
} else if(typeof variables[index] === 'null') {
return true;
}
switch(type) {
//Most of these types that the client sends up match the typeof values of the javascript
//types themselves so we can do a direct comparison
case 'number':
case 'string':
case 'boolean':
return typeof variables[index] === type;
//In the case of unknown, we have no choice but to trust the argument as
//the client hasn't specified what type it should be
case 'unknown':
return true;
case 'array':
return typeof variables[index] === 'object';
case 'dispatch':
return typeof variables[index] === 'object' || typeof variables[index] === 'function';
default:
//has the client provided a type that we weren't expecting?
throw 'there is an invalid type in the function signature exposed by the client';
}
});
});
},
convertCallbackFunctionArgs: function(args) {
_.each(args, function(value, key) {
// We are responsible for converting functions to variable names...
// this will be called later via a event with a callback and arguments variables
if(typeof value === 'function') {
args[key] = this.storeCallbackFunction(value);
} else if(typeof value === 'object' && value) {
this.convertCallbackFunctionArgs(value);
}
}, this);
},
// Functions are simply urls that we make ajax request to. The cb is called with the
// result of that ajax request.
createFunction: function(session, path, signatures) {
assert(session, 'cannot create a function without a session id');
var func = _.bind(function() {
var args = [];
// Lets do a bit of validation of the arguments that we're passing into the client
// unfortunately arguments isn't a completely authetic javascript array, so we'll have
// to "splice" by hand. All this just to validate the correct types! sheesh...
var i;
for(i = 0; i < arguments.length; i++) {
args.push(arguments[i]);
}
// This is as close to a static class function as you can get in javascript i guess
// we should be able to use verifySignaturesArguments to determine if the client will
// consider the arguments that we're passing to be valid
if(!TorrentClient.prototype.validateArguments.call(this, signatures, args)) {
throw 'arguments do not match any of the function signatures exposed by the client';
}
this.convertCallbackFunctionArgs(args);
var ret = new jQuery.Deferred();
var success = _.bind(function(data) {
//lets strip down to the relevent path data
_.each(path, function(segment) {
var decoded = decodeURIComponent(segment);
if(typeof data !== 'undefined') {
data = data[decoded];
}
});
if(typeof data === 'undefined') {
ret.reject('return value parsing error ' + JSON.stringify(data));
} else if(typeof data === 'string' && this.isJSFunctionSignature(data)) {
var func = this.getStoredFunction(data);
assert(func, 'the client is returning a function name that does not exist');
ret.resolve(func);
} else {
ret.resolve(data);
}
}, this);
var error = function(data) {
ret.reject(data);
};
this.query({
type: 'function',
path: JSON.stringify(path),
args: JSON.stringify(args),
session: session
}).done(success).fail(error);
this.trigger('queries', path);
return ret;
}, this);
func.valueOf = function() { return signatures; };
return func;
},
query: function(args) {
var abort = false;
var ret = new jQuery.Deferred();
assert(args.type === 'update' || args.type === 'state' || args.type === 'function' || args.type === 'disconnect', 'the query type must be either "update", "state", or "function"');
args.hostname = window.location.hostname || window.location.pathname;
var success_callback = _.bind(function(data) {
if (data === 'invalid request') {
setTimeout(_.bind(this.reset, this), 1000);
throw 'pairing occured with a torrent client that does not support the btapp api';
} else if(typeof data !== 'object' || 'error' in data) {
ret.reject();
this.trigger('client:error', data);
} else {
ret.resolve(data);
}
}, this);
this.send_query(args)
.done(function() {
if(!abort) {
success_callback.apply(this, arguments);
}
}).fail(function() {
if(!abort) {
ret.reject.apply(this, arguments);
}
});
ret.abort = function() {
abort = true;
};
return ret;
}
});
// Falcon Torrent Client
// -------------
// Falcon torrent client connections are a bit more involved than a client on the local machine
// We have additional javascript dependencies that are substantial enough that we don't load them
// unless you open a falcon connection. In addition we have some handshaking with the falcon proxy
// that we need to wait for.
this.FalconTorrentClient = TorrentClient.extend({
initialize: function(attributes) {
TorrentClient.prototype.initialize.call(this, attributes);
assert(('username' in attributes && 'password' in attributes) || 'remote_data' in attributes,
'attempting to connect to client through falcon without providing the necessary credentials');
this.username = attributes.username;
this.password = attributes.password;
if ('remote_data' in attributes) {
this.remote_data = attributes.remote_data;
}
if ('login_success' in attributes) {
this.login_success = attributes.login_success;
}
if ('login_error' in attributes) {
this.login_error = attributes.login_error;
}
if ('login_progress' in attributes) {
this.login_progress = attributes.login_progress;
}
// We only have to load all those expensive js dependencies once...
// We can just skip straight to the good stuff (signing in) if we've
// done this previously...the last dependency we load sets the
// window.falcon variable, so we can just check for that
if(typeof falcon !== 'undefined') {
_.defer(_.bind(this.reset, this));
return;
}
// If we choose to use falcon we need this specific global config variable defined
root.config = {
srp_root:'https://remote.utorrent.com'
};
var jsload = 'https://remote.utorrent.com/static/js/jsloadv2.js?v=0.57';
jQuery.getScript(jsload, _.bind(function(data, textStatus) {
function create_tags(list) {
var tags = [];
var deps = [];
for (var i = 0; i < list.length - 1; i++) {
var current = list[i];
tags.push( { name: current } );
deps.push( current );
}
tags.push( { name: list[list.length-1],
requires: deps } );
return tags;
}
var dependencies = [
'falcon/deps/SHA-1.js',
'falcon/deps/jsbn.js',
'falcon/deps/jsbn2.js',
'falcon/deps/sjcl.js',
'falcon/falcon.js',
'falcon/falcon.encryption.js',
'falcon/falcon.api.js',
'falcon/falcon.session.js'
];
var tags = create_tags(dependencies);
(new JSLoad(tags, "https://remote.utorrent.com/static/js/")).load(['falcon/falcon.session.js'], _.bind(function() {
if (this.remote_data) {
this.session = new falcon.session( { client_data: this.remote_data } );
this.falcon = this.session.api;
this.trigger('client:connected');
} else {
this.delayed_reset();
}
}, this));
}, this));
},
connect: function() {
assert(!this.falcon, 'trying to connect with falcon already set');
// set up some connection variables
var opts = {
success: _.bind(function(session) {
if (this.login_success) { this.login_success(session); }
this.falcon = this.session;
this.trigger('client:connected', session);
}, this),
error: _.bind(function(xhr, status, data) {
if (this.login_error) { this.login_error(xhr, status, data); }
this.trigger('client:error_connecting', data);
this.trigger('client:error', data);
}, this)
};
this.session = new falcon.session();
this.session.negotiate(this.username, this.password, { success: opts.success, error: opts.error, progress: this.login_progress } );
},
disconnect: function() {
//TODO...how do we disconnect from a remote connection?
},
// This is the Btapp object's gateway to the actual client requests. These requests look slightly
// different than those headed to a local client because they are encrypted.
send_query: function(args) {
var ret = new jQuery.Deferred();
assert(this.falcon, 'cannot send a query to the client without falcon properly connected');
this.falcon.request(
'/gui/',
{'btapp':'backbone.btapp.js'},
args,
function(data) {
assert('build' in data, 'expected build information in the falcon response');
assert('result' in data, 'expected result information in the falcon response');
ret.resolve(data.result);
},
_.bind(function() {
ret.reject();
this.delayed_reset();
}, this),
{}
);
return ret;
},
delayed_reset: function() {
this.reset_timeout = setTimeout(_.bind(function() {
this.reset();
this.reset_timeout = null;
}, this), 1000 );
},
reset: function() {
this.falcon = null;
this.connect();
}
});
// Local Torrent Client
// -------------
// For clients on the local machine very little setup is neeeded. We have a known port that
// the client listens on, so we can just make requests to that. We can also immediately
// consider ourselves "connected", which indicates that we're connected to the machine
// (for falcon clients we may not ever reach the client even if it is logged into falcon)
this.LocalTorrentClient = TorrentClient.extend({
defaults: {
product: 'Torque'
},
initialize: function(attributes) {
TorrentClient.prototype.initialize.call(this, attributes);
this.btapp = attributes.btapp;
this.initialize_objects(attributes);
this.reset_timeout = null;
},
disconnect: function() {
if(this.pairing) {
this.pairing.disconnect();
}
if(this.plugin_manager) {
this.plugin_manager.disconnect();
}
if(this.reset_timeout) {
clearTimeout(this.reset_timeout);
}
},
initialize_objects: function(attributes) {
this.initialize_plugin(attributes);
this.initialize_pairing(attributes);
},
// We have a seperate object that is responsible for managing the browser
// plugin and using that plugin to ensure that the client is downloaded and
// running on the local machine.
initialize_plugin: function(attributes) {
if(this.get('plugin') === false) {
return;
}
this.plugin_manager = new PluginManager(attributes);
new PluginManagerView({'model': this.plugin_manager});
this.plugin_manager.on('all', this.trigger, this);
},
// We have a seperate object responsible for determining exactly which
// port the torrent client is bound to.
initialize_pairing: function(attributes) {
assert(this.get('plugin') === false || typeof this.plugin_manager !== 'undefined', 'you must initialize the plugin manager before the pairing object');
//all right...ready to roll.
this.pairing = new Pairing(_.extend(attributes, {'plugin_manager': this.plugin_manager}));
if(this.pairing.get('pairing_type') !== 'native') {
new PairingView({'model': this.pairing});
}
this.pairing.on('all', this.trigger, this);
assert(this.has('product'), 'client does not know what product to connect to');
var product = this.get('product');
this.pairing.on('pairing:found', function(options) {
if(options && options.name.toLowerCase() === product.toLowerCase()) {
var key = jQuery.jStorage.get(product + '_pairing_key');
if(key) {
// Let whoever triggered the pairing:found event know that they don't need
// to continue scanning, nor do they need to handle aquiring a pairing key
options.abort = true;
options.authorize = false;
this.connect_to_authenticated_port(options.port, key);
} else {
// We've found the port we want to work with, but we don't have a pairing key.
// We'll set authorize to true so that a pairing dialog is presented
// to the user
options.abort = true;
options.authorize = true;
}
} else {
options.abort = false;
options.authorize = false;
}
}, this);
this.pairing.on('pairing:authorized', _.bind(function(options) {
// Holy smokes! We found a port, and the client that's listening likes our pairing key.
// Store the key off so that we don't have to bother the user again.
jQuery.jStorage.set(product + '_pairing_key', options.key);
this.connect_to_authenticated_port(options.port, options.key);
}, this));
this.pairing.on('pairing:stop', this.delayed_reset, this);
if(this.get('plugin') === false) {
this.delayed_reset();
} else {
this.plugin_manager.on('plugin:client_running', this.reset, this);
}
},
ajax: function(url, cb, err) {
if(this.get('plugin') === false) {
jQuery.ajax({
url: url,
success: cb,
error: err,
dataType: 'jsonp'
});
} else {
this.plugin_manager.get_plugin().ajax(url, _.bind(function(response) {
var obj;
if(response.allowed && response.success && response.data !== 'invalid request') {
try {
obj = JSON.parse(response.data);
} catch(e) {
var msg = 'parsererror';
err(msg);
this.trigger('client:error', msg);
return;
}
cb(obj);
} else {
this.trigger('client:error', response);
err(response);
}
}, this));
}
},
// Before we actual start making requests against a client, we need to make sure
// we have a valid pairing key. This might be redundant if we just got one from the
// client, but its very very necessary if we've stored off a pairing key that's invalid
// for some reason.
connect_to_authenticated_port: function(port, key) {
// Called if the pairing key is good to go. Tell whoever is listening that we're
// ready to roll.
var cb = _.bind(function() {
this.url = 'http://127.0.0.1:' + port + '/btapp/?pairing=' + key;
this.trigger('client:connected');
}, this);
// Handle the case of an invalid pairing key. Flush it out and start over.
var err = _.bind(function() {
jQuery.jStorage.deleteKey(this.get('product') + '_pairing_key');
this.delayed_reset();
}, this);
var url = 'http://127.0.0.1:' + port + '/btapp/?pairing=' + key;
this.ajax(url, cb, err);
},
delayed_reset: function() {
this.reset_timeout = setTimeout(_.bind(function() {
this.reset();
this.reset_timeout = null;
}, this), 1000 );
},
reset: function() {
// Reset is called upon initialization (or when we load pairing.btapp.js)
// and whenever the btapp object errors out trying to communicate with the
// torrent client. In both cases we probably need to scan through the ports
// again as the torrent client won't necessarily be able to connect to the
// same port when it is relaunched.
this.pairing.connect();
},
send_query: function(args) {
var ret = new jQuery.Deferred();
this.trigger('client:query', this.url, args);
var url = this.url;
_.each(args, function(value, key) {
url += '&' + encodeURIComponent(key) + '=' + encodeURIComponent(value);
});
this.ajax(url,
function(data) {
ret.resolve(data);
},
function() {
ret.reject();
}
);
return ret;
}
});
}).call(this);