Skip to content

Commit 41b6688

Browse files
committed
fake merge
1 parent 4d73ca3 commit 41b6688

File tree

2 files changed

+120
-3
lines changed

2 files changed

+120
-3
lines changed

lib/server/parse-websocket.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ var common = require('../common')
55
function parseWebSocketRequest (socket, opts, params) {
66
if (!opts) opts = {}
77
params = JSON.parse(params) // may throw
8-
console.log(typeof params.info_hash)
98
if(typeof params.info_hash === 'object'){
109
var info_hash = params.info_hash;
1110
params.info_hash =String.fromCharCode.apply(null, info_hash)
12-
1311
}
14-
console.log(params.java, params)
1512

1613

1714

server.js

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ class Server extends EventEmitter {
436436
onWebSocketConnection (socket, opts = {}) {
437437
opts.trustProxy = opts.trustProxy || this._trustProxy
438438

439+
<<<<<<< HEAD
439440
socket.peerId = null // as hex
440441
socket.infoHashes = [] // swarms that this socket is participating in
441442
socket.onSend = err => {
@@ -446,6 +447,59 @@ class Server extends EventEmitter {
446447
this._onWebSocketRequest(socket, opts, params)
447448
}
448449
socket.on('message', socket.onMessageBound)
450+
=======
451+
function utf8to16(str) {
452+
var out, i, len, c;
453+
var char2, char3;
454+
455+
out = "";
456+
len = str.length;
457+
i = 0;
458+
while(i < len) {
459+
c = str.charCodeAt(i++);
460+
switch(c >> 4)
461+
{
462+
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
463+
// 0xxxxxxx
464+
out += str.charAt(i-1);
465+
break;
466+
case 12: case 13:
467+
// 110x xxxx 10xx xxxx
468+
char2 = str.charCodeAt(i++);
469+
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
470+
break;
471+
case 14:
472+
// 1110 xxxx 10xx xxxx 10xx xxxx
473+
char2 = str.charCodeAt(i++);
474+
char3 = str.charCodeAt(i++);
475+
out += String.fromCharCode(((c & 0x0F) << 12) |
476+
((char2 & 0x3F) << 6) |
477+
((char3 & 0x3F) << 0));
478+
break;
479+
}
480+
}
481+
482+
return out;
483+
}
484+
485+
Server.prototype._onWebSocketRequest = function (socket, opts, params) {
486+
var self = this
487+
console.log("params: ", JSON.stringify(params))
488+
try {
489+
if (JSON.parse(params).torrent){
490+
debug("got torrent: ")
491+
peers.forEach(function (peer, i) {
492+
peer.socket.send(JSON.stringify(params), peer.socket.onSend)
493+
})
494+
return
495+
}
496+
497+
params = parseWebSocketRequest(socket, opts, params)
498+
} catch (err) {
499+
socket.send(JSON.stringify({
500+
'failure reason': err.message
501+
}), socket.onSend)
502+
>>>>>>> 001f51e... changes
449503

450504
socket.onErrorBound = err => {
451505
this._onWebSocketError(socket, err)
@@ -458,10 +512,17 @@ class Server extends EventEmitter {
458512
socket.on('close', socket.onCloseBound)
459513
}
460514

515+
<<<<<<< HEAD
461516
_onWebSocketRequest (socket, opts, params) {
462517
try {
463518
params = parseWebSocketRequest(socket, opts, params)
464519
} catch (err) {
520+
=======
521+
self._onRequest(params, function (err, response) {
522+
//console.log("params:", params)
523+
if (self.destroyed || socket.destroyed) return
524+
if (err) {
525+
>>>>>>> 001f51e... changes
465526
socket.send(JSON.stringify({
466527
'failure reason': err.message
467528
}), socket.onSend)
@@ -474,6 +535,7 @@ class Server extends EventEmitter {
474535

475536
if (!socket.peerId) socket.peerId = params.peer_id // as hex
476537

538+
<<<<<<< HEAD
477539
this._onRequest(params, (err, response) => {
478540
if (this.destroyed || socket.destroyed) return
479541
if (err) {
@@ -482,6 +544,13 @@ class Server extends EventEmitter {
482544
'failure reason': err.message,
483545
info_hash: common.hexToBinary(params.info_hash)
484546
}), socket.onSend)
547+
=======
548+
var peers
549+
if (response.action === 'announce') {
550+
peers = response.peers
551+
delete response.peers
552+
console.log("params hash: " + params.info_hash)
553+
>>>>>>> 001f51e... changes
485554

486555
this.emit('warning', err)
487556
return
@@ -494,11 +563,62 @@ class Server extends EventEmitter {
494563
peers = response.peers
495564
delete response.peers
496565

566+
<<<<<<< HEAD
497567
if (!socket.infoHashes.includes(params.info_hash)) {
498568
socket.infoHashes.push(params.info_hash)
499569
}
500570

501571
response.info_hash = common.hexToBinary(params.info_hash)
572+
=======
573+
574+
if (Array.isArray(params.offers)) {
575+
debug('got %s offers from %s', params.offers.length, params.peer_id)
576+
debug('got %s peers from swarm %s', peers.length, params.info_hash)
577+
peers.forEach(function (peer, i) {
578+
peer.socket.send(JSON.stringify({
579+
action: 'announce',
580+
offer: params.offers[i].offer,
581+
offer_id: params.offers[i].offer_id,
582+
peer_id: common.hexToBinary(params.peer_id),
583+
info_hash: common.hexToBinary(params.info_hash)
584+
}), peer.socket.onSend)
585+
debug('sent offer to %s from %s', peer.peerId, params.peer_id)
586+
})
587+
}
588+
589+
590+
//add trickler support
591+
if (params.candidate && params.peer_id){
592+
debug("got candidate from %s to %s", params.peer_id, params.to_peer_id)
593+
debug("peers length %s",peers.length)
594+
debug('got %s peers from swarm %s', peers.length, params.info_hash)
595+
self.getSwarm(params.info_hash, function (err, swarm) {
596+
if (self.destroyed) return
597+
if (err) return self.emit('warning', err)
598+
if (!swarm) {
599+
return self.emit('warning', new Error('no swarm with that `info_hash`'))
600+
}
601+
// Mark the destination peer as recently used in cache
602+
var toPeer = swarm.peers.get(params.to_peer_id)
603+
if (!toPeer) {
604+
return self.emit('warning', new Error('no peer with that `to_peer_id`'))
605+
}
606+
607+
toPeer.socket.send(JSON.stringify({
608+
action: 'announce',
609+
candidate: params.candidate,
610+
offer_id: params.offer_id,
611+
peer_id: common.hexToBinary(params.peer_id),
612+
info_hash: common.hexToBinary(params.info_hash)
613+
}), toPeer.socket.onSend)
614+
debug('sent candidate to %s from %s', toPeer.peerId, params.peer_id)
615+
616+
done()
617+
})
618+
619+
}else if (params.answer) {
620+
debug('got answer %s from %s', JSON.stringify(params.answer), params.peer_id)
621+
>>>>>>> 001f51e... changes
502622

503623
// WebSocket tracker should have a shorter interval – default: 2 minutes
504624
response.interval = Math.ceil(this.intervalMs / 1000 / 5)

0 commit comments

Comments
 (0)