Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
thevolcanomanishere committed Oct 17, 2022
1 parent 30117a2 commit d0fc817
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 277 deletions.
70 changes: 0 additions & 70 deletions client.js

This file was deleted.

25 changes: 9 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
"main": "index.js",
"author": "Alex McGonagle",
"license": "MIT",
"engines": {
"node": ">=16"
},
"dependencies": {
"b4a": "^1.6.0",
"hi-base32": "^0.5.1",
Expand All @@ -20,21 +23,11 @@
"assets": "node_modules"
},
"scripts": {
"buildServerMacArm": "pkg -t node16-macos-arm64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-mac-arm64'",
"buildClientMacArm": "pkg -t node16-macos-arm64 client.js -c package.json -d --compress Brotli -o './build/folder-beam-client-mac-arm64'",
"buildMacArm": "yarn run buildServerMacArm && yarn run buildClientMacArm",
"buildServerMacIntel": "pkg -t node16-macos-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-mac-intel'",
"buildClientMacIntel": "pkg -t node16-macos-x64 client.js -c package.json -d --compress Brotli -o './build/folder-beam-client-mac-intel'",
"buildMacIntel": "yarn run buildServerMacIntel && yarn run buildClientMacIntel",
"buildServerLinuxArm": "pkg -t node16-linux-arm64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-linux-arm64'",
"buildClientLinuxArm": "pkg -t node16-linux-arm64 client.js -c package.json -d --compress Brotli -o './build/folder-beam-client-linux-arm64'",
"buildLinuxArm": "yarn run buildServerLinuxArm && yarn run buildClientLinuxArm",
"buildServerLinuxX64": "pkg -t node16-linux-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-linux-x64'",
"buildClientLinuxX64": "pkg -t node16-linux-x64 client.js -c package.json -d --compress Brotli -o './build/folder-beam-client-linux-x64'",
"buildLinuxX64": "yarn run buildServerLinuxX64 && yarn run buildClientLinuxX64",
"buildServerWindowsX64": "pkg -t node16-win-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-windows-x64.exe'",
"buildClientWindowsX64": "pkg -t node16-win-x64 client.js -c package.json -d --compress Brotli -o './build/folder-beam-client-windows-x64.exe'",
"buildWindowsX64": "yarn run buildServerWindowsX64 && yarn run buildClientWindowsX64",
"buildAll": "yarn run buildMacArm && yarn run buildMacIntel && yarn run buildLinuxArm && yarn run buildLinuxX64 && yarn run buildWindowsX64"
"buildMacArm": "pkg -t node16-macos-arm64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-mac-arm64'",
"buildMacX64": "pkg -t node16-macos-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-mac-intel'",
"buildLinuxArm": "pkg -t node16-linux-arm64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-linux-arm64'",
"buildLinuxX64": "pkg -t node16-linux-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-linux-x64'",
"buildWindowsX64": "pkg -t node16-win-x64 server.js -c package.json -d --compress Brotli -o './build/folder-beam-server-windows-x64.exe'",
"buildAll": "yarn buildMacArm && yarn buildMacX64 && yarn buildLinuxArm && yarn buildLinuxX64 && yarn buildWindowsX64"
}
}
195 changes: 113 additions & 82 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,132 @@
const Hyperbeam = require("hyperbeam");
const fs = require("fs");
const tar = require("tar-fs");
const b4a = require("b4a");
const sodium = require("sodium-universal");
const b32 = require("hi-base32");
const utils = require("./utils.js");

const toBase32 = (buf) => {
return b32.encode(buf).replace(/=/g, "").toLowerCase();
};

const randomBytes = (length) => {
const buffer = b4a.alloc(length);
sodium.randombytes_buf(buffer);
return buffer;
};

const key = toBase32(randomBytes(32));
const beam = new Hyperbeam(key, { announce: true });
console.log("Folder Beam 5000 📁 ➡️ 🧨");

console.log(`Or pass in the key as an argument to folder-beam-client 🙂`);

// write key text to file
fs.writeFileSync("key.txt", key, (err) => {
// Determine if we are in server or client mode
const keyAsArgument = process.argv[2];
fs.readFile("key.txt", "utf8", (err, key) => {
if (key || keyAsArgument) {
return clientMode(key ? key : keyAsArgument);
}
if (err) {
return console.log(err);
console.log("No key.txt file found");
console.log("Folder Beam server mode 👍");
return serverMode();
}
console.log("Key file created");
});

// Get the user provided path
const path = process.argv[2] || "./";
const serverMode = () => {
const key = utils.createKey();
utils.writeKeyFile(key);
const beam = new Hyperbeam(key, { announce: true });

if (beam.announce) {
console.log("Online 🧨");
} else {
console.error("[hyperbeam] Connecting pipe...");
}
// Get the user provided path
const path = process.argv[2] || "./";

beam.on("connected", () => {
console.error(
"[hyperbeam] Success! Encrypted tunnel established to remote peer"
);
console.log("Beaming files to client 🚀");
});
if (beam.announce) {
console.log("Online 🧨");
} else {
console.error("[hyperbeam] Connecting pipe...");
}

const closeASAP = () => {
console.error("[hyperbeam] Shutting down beam...");
beam.on("connected", () => {
console.error(
"[hyperbeam] Success! Encrypted tunnel established to remote peer"
);
console.log("Beaming files to client 🚀");
});

const closeASAP = () => {
console.error("[hyperbeam] Shutting down beam...");

const timeout = setTimeout(() => process.exit(1), 2000);
beam.destroy();
beam.on("close", () => {
clearTimeout(timeout);
const timeout = setTimeout(() => process.exit(1), 2000);
beam.destroy();
beam.on("close", () => {
clearTimeout(timeout);
});
};

beam.on("error", (e) => {
console.error("[hyperbeam] Error:", e.message);
closeASAP();
});
};

beam.on("error", (e) => {
console.error("[hyperbeam] Error:", e.message);
closeASAP();
});
beam.on("end", () => beam.end());

beam.on("end", () => beam.end());

// Get list of all files recursively in this folder using fs and print to console
const getFiles = (dir, files_) => {
files_ = files_ || [];
const files = fs.readdirSync(dir);
for (const i in files) {
const name = dir + "/" + files[i];
if (fs.statSync(name).isDirectory()) {
getFiles(name, files_);
} else {
files_.push(name);
}
}
return files_;
};
const files = utils.getFiles(path);
const fileSize = utils.getDirSize(path);
console.log("Files to send: ", files.length); // Don't count the binary itself or the key file
console.log("Total folder size: " + fileSize);

// get size in mb of all files in a directory
const getDirSize = (dir) => {
const files = getFiles(dir);
let size = 0;
for (const i in files) {
const stats = fs.statSync(files[i]);
size += stats["size"];
}
return (size / 1000000.0).toFixed(2) + " MB";
tar
.pack(path, {
ignore: (name) => {
return name.includes("folder-beam") || name === "key.txt";
},
})
.pipe(beam);
};

const files = getFiles(path);
const fileSize = getDirSize(path);
console.log("Files to send: ", files.length - 2); // Don't count the binary itself or the key file
console.log("Total folder size: " + fileSize);

tar
.pack(path, {
ignore: (name) => {
return name.includes("folder-beam") || name === "key.txt";
},
})
.pipe(beam);
const clientMode = (key) => {
console.log("Folder Beam client mode 👍");
console.log("Attempting to connect to source...");
const keyArg = process.argv[2];
if (keyArg) {
console.log("Key argument detected: ", keyArg);
console.log("Key argument overides key provided in key.txt");
key = keyArg;
} // Use key from terminal args if it exists
const beam = new Hyperbeam(key);

beam.on("connected", () => {
console.error(
"[hyperbeam] Success! Encrypted tunnel established to remote peer"
);
});

beam.on("remote-address", ({ host, port }) => {
if (!host) console.error("[hyperbeam] Could not detect remote address");
else
console.error(
"[hyperbeam] Joined the DHT - remote address is " + host + ":" + port
);
});

const closeASAP = () => {
console.error("[hyperbeam] Shutting down beam...");

const timeout = setTimeout(() => process.exit(1), 2000);
beam.destroy();
beam.on("close", () => {
clearTimeout(timeout);
});
};

beam.on("error", (e) => {
console.error("[hyperbeam] Error:", e.message);
closeASAP();
});

beam.on("end", () => {
console.log("[hyperbeam] Connection closed");
beam.end();
});

const printReplace = (text) => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
process.stdout.write(text);
};

let totalData = 0;

beam.on("data", (data) => {
totalData += data.length;
printReplace(`Received ${(totalData / 1000000).toFixed(2)} MB`);
});

beam.pipe(tar.extract("./"));
};
60 changes: 60 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const fs = require("fs");
const b32 = require("hi-base32");
const sodium = require("sodium-universal");
const b4a = require("b4a");

const randomBytes = (length) => {
const buffer = b4a.alloc(length);
sodium.randombytes_buf(buffer);
return buffer;
};

const toBase32 = (buf) => {
return b32.encode(buf).replace(/=/g, "").toLowerCase();
};

// List of files that are not counted in the number of files sent
const filter = ["folder-beam", "key.txt", ".DS_Store"];

const Utils = {
getDirSize: (dir) => {
const files = Utils.getFiles(dir);
let size = 0;
for (const i in files) {
if (files[i].includes("key.txt") || files[i].includes("folder-beam"))
break;
const stats = fs.statSync(files[i]);
size += stats["size"];
}
return (size / 1000000.0).toFixed(2) + " MB";
},
getFiles: (dir, files_) => {
files_ = files_ || [];
const files = fs.readdirSync(dir);
for (const i in files) {
const name = dir + "/" + files[i];
if (fs.statSync(name).isDirectory()) {
Utils.getFiles(name, files_);
} else {
files_.push(name);
}
}

return files_.filter((file) => {
return !filter.some((f) => file.includes(f));
});
},
createKey: () => {
return toBase32(randomBytes(32));
},
writeKeyFile: (key) => {
fs.writeFileSync("key.txt", key, (err) => {
if (err) {
return console.log(err);
}
console.log("Key file created");
});
},
};

module.exports = Utils;
Loading

0 comments on commit d0fc817

Please sign in to comment.