mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-19 04:31:36 +00:00
Merge branch 'master' into server-ipv6
Conflicts: server.js
This commit is contained in:
commit
83b4ecc7f5
13
examples/express-embed/package.json
Normal file
13
examples/express-embed/package.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"name": "bittorrent-tracker-example-express-embed",
|
||||||
|
"version": "0.0.0",
|
||||||
|
"description": "Example for embedding bittorrent-tracker server in express.js",
|
||||||
|
"scripts": {
|
||||||
|
"server": "./server.js"
|
||||||
|
},
|
||||||
|
"author": "Astro <astro@spaceboyz.net>",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.10.5"
|
||||||
|
}
|
||||||
|
}
|
16
examples/express-embed/server.js
Executable file
16
examples/express-embed/server.js
Executable file
@ -0,0 +1,16 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
var Server = require('../..').Server
|
||||||
|
var express = require('express')
|
||||||
|
var app = express()
|
||||||
|
|
||||||
|
var server = new Server({
|
||||||
|
http: false, // we do our own
|
||||||
|
udp: false // not interested
|
||||||
|
})
|
||||||
|
|
||||||
|
var onHttpRequest = server.onHttpRequest.bind(server)
|
||||||
|
app.get('/announce', onHttpRequest)
|
||||||
|
app.get('/scrape', onHttpRequest)
|
||||||
|
|
||||||
|
app.listen(8080)
|
25
server.js
25
server.js
@ -54,20 +54,20 @@ function Server (opts) {
|
|||||||
// default to starting an http server unless the user explictly says no
|
// default to starting an http server unless the user explictly says no
|
||||||
if (opts.http !== false) {
|
if (opts.http !== false) {
|
||||||
self._httpServer = http.createServer()
|
self._httpServer = http.createServer()
|
||||||
self._httpServer.on('request', self._onHttpRequest.bind(self))
|
self._httpServer.on('request', self.onHttpRequest.bind(self))
|
||||||
self._httpServer.on('error', self._onError.bind(self))
|
self._httpServer.on('error', self._onError.bind(self))
|
||||||
self._httpServer.on('listening', onListening)
|
self._httpServer.on('listening', onListening)
|
||||||
}
|
}
|
||||||
|
|
||||||
// default to starting a udp server unless the user explicitly says no
|
// default to starting a udp server unless the user explicitly says no
|
||||||
if (opts.udp !== false) {
|
if (opts.udp !== false) {
|
||||||
self._udpServer = dgram.createSocket('udp4')
|
self._udpSocket = dgram.createSocket('udp4')
|
||||||
self._udpServer.on('message', self._onUdpRequest.bind(self))
|
self._udpSocket.on('message', self._onUdpRequest.bind(self))
|
||||||
self._udpServer.on('error', self._onError.bind(self))
|
self._udpSocket.on('error', self._onError.bind(self))
|
||||||
self._udpServer.on('listening', onListening)
|
self._udpSocket.on('listening', onListening)
|
||||||
}
|
}
|
||||||
|
|
||||||
var num = !!self._httpServer + !!self._udpServer
|
var num = !!self._httpServer + !!self._udpSocket
|
||||||
function onListening () {
|
function onListening () {
|
||||||
num -= 1
|
num -= 1
|
||||||
if (num === 0) {
|
if (num === 0) {
|
||||||
@ -98,7 +98,7 @@ Server.prototype.listen = function (port, onlistening) {
|
|||||||
// binding to :: only receives IPv4 connections if the bindv6only
|
// binding to :: only receives IPv4 connections if the bindv6only
|
||||||
// sysctl is set 0, which is the default on many operating systems.
|
// sysctl is set 0, which is the default on many operating systems.
|
||||||
self._httpServer && self._httpServer.listen(port.http || port, '::')
|
self._httpServer && self._httpServer.listen(port.http || port, '::')
|
||||||
self._udpServer && self._udpServer.bind(port.udp || port)
|
self._udpSocket && self._udpSocket.bind(port.udp || port)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (port) onPort(null, port)
|
if (port) onPort(null, port)
|
||||||
@ -108,8 +108,8 @@ Server.prototype.listen = function (port, onlistening) {
|
|||||||
Server.prototype.close = function (cb) {
|
Server.prototype.close = function (cb) {
|
||||||
var self = this
|
var self = this
|
||||||
cb = cb || function () {}
|
cb = cb || function () {}
|
||||||
if (self._udpServer) {
|
if (self._udpSocket) {
|
||||||
self._udpServer.close()
|
self._udpSocket.close()
|
||||||
}
|
}
|
||||||
if (self._httpServer) {
|
if (self._httpServer) {
|
||||||
self._httpServer.close(cb)
|
self._httpServer.close(cb)
|
||||||
@ -126,7 +126,7 @@ Server.prototype.getSwarm = function (infoHash) {
|
|||||||
return swarm
|
return swarm
|
||||||
}
|
}
|
||||||
|
|
||||||
Server.prototype._onHttpRequest = function (req, res) {
|
Server.prototype.onHttpRequest = function (req, res) {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
var params
|
var params
|
||||||
@ -180,11 +180,10 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var socket = dgram.createSocket('udp4')
|
|
||||||
response.transactionId = params.transactionId
|
response.transactionId = params.transactionId
|
||||||
response.connectionId = params.connectionId
|
response.connectionId = params.connectionId
|
||||||
var buf = makeUdpPacket(response)
|
var buf = makeUdpPacket(response)
|
||||||
socket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () {
|
self._udpSocket.send(buf, 0, buf.length, rinfo.port, rinfo.address, function () {
|
||||||
try {
|
try {
|
||||||
socket.close()
|
socket.close()
|
||||||
} catch (err) {}
|
} catch (err) {}
|
||||||
@ -211,7 +210,7 @@ Server.prototype._onAnnounce = function (params, cb) {
|
|||||||
if (response) {
|
if (response) {
|
||||||
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
if (!response.action) response.action = common.ACTIONS.ANNOUNCE
|
||||||
if (!response.intervalMs) response.intervalMs = self._intervalMs
|
if (!response.intervalMs) response.intervalMs = self._intervalMs
|
||||||
|
|
||||||
if (params.compact === 1) {
|
if (params.compact === 1) {
|
||||||
var peers = response.peers
|
var peers = response.peers
|
||||||
// Find IPv4 peers
|
// Find IPv4 peers
|
||||||
|
Loading…
Reference in New Issue
Block a user