Skip to content

Commit

Permalink
Add can-mktorrent and new script makeCanTorrent
Browse files Browse the repository at this point in the history
  • Loading branch information
canibanoglu committed Nov 18, 2019
1 parent a5d928d commit edd5293
Show file tree
Hide file tree
Showing 5 changed files with 1,354 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build/
.env
trackedTorrents.db
bin/.build/*
*.torrent
111 changes: 111 additions & 0 deletions bin/mkt.js
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);
});
});
}
)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"bin": {
"createCanTorrent": "./bin/.build/createTorrent.js",
"startCanTracker": "./bin/.build/startTrackerServer.js",
"trackCanTorrents": "./bin/.build/addTorrentToTracker.js"
"trackCanTorrents": "./bin/.build/addTorrentToTracker.js",
"makeCanTorrent": "./bin/.build/mkt.js"
},
"files": [
"bin/",
Expand Down
13 changes: 8 additions & 5 deletions test.js
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");
});
;

Loading

0 comments on commit edd5293

Please sign in to comment.