-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3d62974
commit 0751987
Showing
9 changed files
with
194 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,94 @@ | ||
const sqlite = require('sqlite3').verbose(); | ||
const path = require('path'); | ||
const sqlite = require("sqlite3").verbose(); | ||
const path = require("path"); | ||
|
||
import { checkEnvironmentVariables } from './utils'; | ||
import { checkEnvironmentVariables, DEBUG, WARN, INFO, ERROR } from "./utils"; | ||
|
||
checkEnvironmentVariables(); | ||
|
||
const { | ||
CAN_TRACKER_PATH: DB_PATH | ||
} = process.env; | ||
const { CAN_TRACKER_PATH: DB_PATH } = process.env; | ||
|
||
function getDatabase() { | ||
return new sqlite.Database(path.join(DB_PATH, 'trackedTorrents.db')); | ||
return new sqlite.Database(path.join(DB_PATH, "trackedTorrents.db")); | ||
} | ||
|
||
|
||
export async function addTorrent(parsedTorrent) { | ||
const { infoHash } = parsedTorrent; | ||
const isTracked = await checkTorrentIsTracked(infoHash); | ||
|
||
if (isTracked) { | ||
console.warn(`Torrent with infoHash ${infoHash} is already tracked!`); | ||
WARN(`Torrent with infoHash ${infoHash} is already tracked!`); | ||
return Promise.resolve(); | ||
} | ||
|
||
return new Promise(async (resolve, reject) => { | ||
const db = getDatabase(); | ||
db.run('INSERT INTO tracked_torrents(info_hash) VALUES (?)', [infoHash], (err) => { | ||
db.close(); | ||
if (err !== null) { | ||
console.error(`Couldn't add torrent with info_hash: ${infoHash} to tracking database!`); | ||
console.error(err); | ||
return reject(err); | ||
db.run( | ||
"INSERT INTO tracked_torrents(info_hash) VALUES (?)", | ||
[infoHash], | ||
err => { | ||
db.close(); | ||
if (err !== null) { | ||
ERROR( | ||
`Couldn't add torrent with info_hash: ${infoHash} to tracking database!` | ||
); | ||
DEBUG(err); | ||
return reject(err); | ||
} | ||
INFO(`Torrent with infoHash ${infoHash} is now being tracked!`); | ||
resolve(); | ||
} | ||
console.info('Torrent with infoHash ${infoHash} is now being tracked!') | ||
resolve(); | ||
}) | ||
}) | ||
|
||
); | ||
}); | ||
} | ||
|
||
|
||
export function checkTorrentIsTracked(infoHash) { | ||
const db = getDatabase(); | ||
|
||
return new Promise((resolve, reject) => { | ||
db.get(`SELECT 1 FROM tracked_torrents WHERE info_hash = $infoHash`, { | ||
$infoHash: infoHash, | ||
}, (err, row) => { | ||
db.close(); | ||
|
||
if (err !== null) { | ||
console.error('Something happened while running DB query!'); | ||
console.error(err); | ||
reject(err); | ||
return; | ||
} | ||
|
||
if (row === undefined) { | ||
// We're not tracking this torrent | ||
resolve(false); | ||
db.get( | ||
`SELECT 1 FROM tracked_torrents WHERE info_hash = $infoHash`, | ||
{ | ||
$infoHash: infoHash | ||
}, | ||
(err, row) => { | ||
db.close(); | ||
|
||
if (err !== null) { | ||
ERROR("Something happened while running DB query!"); | ||
DEBUG(err); | ||
reject(err); | ||
return; | ||
} | ||
|
||
if (row === undefined) { | ||
// We're not tracking this torrent | ||
resolve(false); | ||
} | ||
resolve(true); | ||
} | ||
resolve(true); | ||
}); | ||
}) | ||
); | ||
}); | ||
} | ||
|
||
export function createAndInitDatabase() { | ||
const db = getDatabase(); | ||
|
||
return new Promise((resolve, reject) => { | ||
db.run(`CREATE TABLE IF NOT EXISTS tracked_torrents ( | ||
db.run( | ||
`CREATE TABLE IF NOT EXISTS tracked_torrents ( | ||
info_hash TEXT PRIMARY KEY | ||
)`, [], (err) => { | ||
if (err !== null) { | ||
console.error('Something happened during DB creation!'); | ||
console.error(err); | ||
reject(err); | ||
return; | ||
)`, | ||
[], | ||
err => { | ||
if (err !== null) { | ||
ERROR("Something happened during DB creation!"); | ||
DEBUG(err); | ||
reject(err); | ||
return; | ||
} | ||
db.close(); | ||
resolve(1); | ||
} | ||
db.close(); | ||
resolve(1); | ||
}); | ||
}) | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/usr/bin/env node | ||
const { wrapBinaryCommands } = require("./wrapBinaryCommands"); | ||
const { INFO } = require("./utils"); | ||
|
||
Promise.all([wrapBinaryCommands()]).then(() => { | ||
console.log("Done settin up"); | ||
INFO("Finished post installation!"); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
import { Server } from 'bittorrent-tracker'; | ||
import { checkTorrentIsTracked} from "./databaseOperations"; | ||
|
||
const { | ||
CAN_TRACKER_PORT: PORT | ||
} = process.env; | ||
import { Server } from "bittorrent-tracker"; | ||
import { checkTorrentIsTracked } from "./databaseOperations"; | ||
import { ERROR, INFO } from "./utils"; | ||
|
||
const { CAN_TRACKER_PORT: PORT } = process.env; | ||
|
||
function filterTorrents(infoHash, params, cb) { | ||
return checkTorrentIsTracked(infoHash).then(isTracked => { | ||
if (isTracked) { | ||
cb(null); | ||
} else { | ||
cb(new Error('Untracked torrent file')); | ||
} | ||
}).catch(err => { | ||
cb(new Error(err)); | ||
}); | ||
return checkTorrentIsTracked(infoHash) | ||
.then(isTracked => { | ||
if (isTracked) { | ||
cb(null); | ||
} else { | ||
cb(new Error("Untracked torrent file")); | ||
} | ||
}) | ||
.catch(err => { | ||
DEBUG("Error responding to announce!", err); | ||
cb(new Error("Server error")); | ||
}); | ||
} | ||
|
||
|
||
export function startServer() { | ||
const server = new Server({ | ||
filter: filterTorrents, | ||
trustProxy: false, | ||
trustProxy: false | ||
}); | ||
|
||
server.on('error', err => { | ||
console.error(err); | ||
server.on("error", err => { | ||
ERROR(err); | ||
}); | ||
|
||
server.on('listening', () => { | ||
console.info('listening on http port:', server.http.address().port); | ||
console.info('listening on udp port:', server.udp.address().port); | ||
server.on("listening", () => { | ||
INFO("listening on http port:", server.http.address().port); | ||
INFO("listening on udp port:", server.udp.address().port); | ||
}); | ||
|
||
server.listen(PORT); | ||
}; | ||
} |
Oops, something went wrong.