-
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.
Add can-mktorrent and new script makeCanTorrent
- Loading branch information
1 parent
a5d928d
commit edd5293
Showing
5 changed files
with
1,354 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ build/ | |
.env | ||
trackedTorrents.db | ||
bin/.build/* | ||
*.torrent |
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 |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env node | ||
import {checkEnvironmentVariables} from "../utils"; | ||
import {spawn} from "child_process"; | ||
import {statSync, readFileSync} from "fs"; | ||
|
||
checkEnvironmentVariables(); | ||
|
||
const path = require("path"); | ||
const createTorrent = require("can-mktorrent"); | ||
const parseTorrent = require("parse-torrent"); | ||
const fs = require("fs").promises; | ||
const argv = require("minimist")(process.argv.slice(2)); | ||
|
||
const { | ||
addTorrent: addTorrentToDatabase | ||
} = require("../databaseOperations.js"); | ||
|
||
// TODO: Check for env variables and fail if not present with instructions | ||
|
||
const { | ||
CAN_TRACKER_ANNOUNCE_URL, | ||
CAN_TRACKER_WEB_SEED_URL, | ||
CAN_TRACKER_POST_CREATE_SCRIPT_PATH | ||
} = process.env; | ||
|
||
if (!CAN_TRACKER_ANNOUNCE_URL || !CAN_TRACKER_WEB_SEED_URL) { | ||
console.error("Please set the require environment variables"); | ||
process.exit(1); | ||
} | ||
|
||
const processDir = process.cwd(); | ||
|
||
async function writeTorrentFile(fileName, torrentBuffer) { | ||
const filePath = path.join(processDir, fileName); | ||
const fileHandle = await fs.open(filePath, "w"); | ||
return fileHandle | ||
.writeFile(torrentBuffer) | ||
.catch(err => { | ||
console.error(err); | ||
throw err; | ||
}) | ||
.then(() => { | ||
console.info(`Successfully created .torrent file with name: ${fileName}`); | ||
return filePath; | ||
}); | ||
} | ||
|
||
const sanitizedAnnounceUURL = CAN_TRACKER_ANNOUNCE_URL.endsWith("/") | ||
? CAN_TRACKER_ANNOUNCE_URL.slice(0, CAN_TRACKER_ANNOUNCE_URL.length - 1) | ||
: CAN_TRACKER_ANNOUNCE_URL; | ||
|
||
console.info("Creating torrent..."); | ||
console.log(argv._); | ||
const torrentFileName = `can_tracker_${path.basename(argv._[0])}.torrent`; | ||
createTorrent({ | ||
private: true, | ||
announceUrls: [sanitizedAnnounceUURL], | ||
webSeedUrls: [CAN_TRACKER_WEB_SEED_URL], | ||
comment: 'Created by can-tracker', | ||
output: torrentFileName, | ||
sourcePath: argv._[0] | ||
}).then(() => { | ||
const filePath = path.join(process.cwd(), torrentFileName); | ||
console.log('torrent path:', filePath); | ||
const parsedTorrent = parseTorrent(readFileSync(filePath)); | ||
|
||
addTorrentToDatabase(parsedTorrent).then(() => { | ||
if (!CAN_TRACKER_POST_CREATE_SCRIPT_PATH) return; | ||
|
||
try { | ||
statSync(CAN_TRACKER_POST_CREATE_SCRIPT_PATH); | ||
} catch (e) { | ||
console.error("Post create script doesn't exist!"); | ||
process.exit(1); | ||
} | ||
|
||
console.log("Found post installation script in env"); | ||
|
||
let executor = null; | ||
|
||
if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith(".js")) { | ||
executor = "node"; | ||
} else if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith(".sh")) { | ||
executor = "bash"; | ||
} else if (CAN_TRACKER_POST_CREATE_SCRIPT_PATH.endsWith(".zsh")) { | ||
executor = "zsh"; | ||
} else { | ||
console.error("Only .js, .sh or .zsh scripts are supported."); | ||
return; | ||
} | ||
|
||
const cp = spawn(executor, [ | ||
CAN_TRACKER_POST_CREATE_SCRIPT_PATH, | ||
filePath, | ||
...argv._ | ||
]); | ||
|
||
cp.stdout.on("data", data => { | ||
console.log("post create script:", data.toString()); | ||
}); | ||
|
||
cp.stderr.on("data", data => { | ||
console.error("post create script:", data.toString()); | ||
}); | ||
|
||
cp.on("close", code => { | ||
console.log("post create script exited with code:", code); | ||
}); | ||
}); | ||
} | ||
) |
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,9 +1,12 @@ | ||
const mkt = require('can-mktorrent'); | ||
|
||
|
||
mkt({ | ||
announceUrls: ['http://localhost:32323/cantracker/announce'], | ||
announceUrls: ["http://detemps.theia.feralhosting.com/cantracker/announce"], | ||
comment: "created by can-mktorrent", | ||
pieceLength: 20, | ||
sourcePath: "./entryFiles", | ||
webSeedUrls: ["https://detemps.theia.feralhosting.com/canTorrent/webSeed"], | ||
private: true | ||
}).then(() => { | ||
console.log('here'); | ||
console.log("here"); | ||
}); | ||
; | ||
|
Oops, something went wrong.