-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
184 lines (154 loc) · 4.78 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const Hyperbeam = require("hyperbeam");
const utils = require("./utils.js");
const fs = require("fs");
const archiver = require("archiver");
const unzip = require("unzip-stream");
const argv = require("minimist")(process.argv.slice(2));
const progress = require("progress-stream");
console.log("\nFolder Beam 5000 📁 ➡️ 🧨\n");
if (argv.h) utils.commandLineHelp(true);
utils.commandLineHelp(false);
// Get the user provided path
const pathServer = argv.s ? argv.s : "./";
if (argv.s) {
console.log("Beaming files from: " + pathServer);
} else {
console.log("Beaming files from: current working directory");
console.log("Use -s to specify a path");
}
// Determine if we are in server or client mode
const keyAsArgument = argv._.length > 0 ? argv._[0] : null;
const pathClient = argv.d ? argv.d : "./";
if (keyAsArgument) {
if (argv.d) {
console.log("Beaming files to: " + pathClient);
} else {
console.log("Beaming files to: current working directory");
console.log("Use -d to specify a path");
}
}
const serverMode = (password) => {
const key = utils.createKey(password);
console.log(
"\nOn the destination computer you can run:\n./folder-beam " + password
);
console.log(
"\nBinary needs to be executable, so run 'chmod +x folder-beam' via terminal first 🙂"
);
const beam = new Hyperbeam(key, {
announce: true,
});
if (beam.announce) {
console.log("Online 🧨");
} else {
console.error("[hyperbeam] Connecting pipe...");
}
beam.on("connected", () => {
console.error(
"[hyperbeam] Success! Encrypted tunnel established to remote peer"
);
console.log("Beaming files to client 🚀");
connectedToPeer = true;
startTime = Date.now();
});
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();
});
console.log("\n");
console.log("Getting your files ready 📁");
console.log("This might take some time if you have 000's of files 🤓");
console.log("\n");
const files = utils.getFiles(pathServer);
const fileSize = utils.getDirSize(pathServer);
console.log("Files to send: ", files.length); // Don't count the binary itself or the key file
console.log("Total folder size: " + fileSize + " MB");
console.log("Waiting for the client to connect...");
const archive = archiver("zip", {
namePrependSlash: true,
store: true,
});
// for every file in files, append to archive
files.forEach((file) => {
archive.append(fs.createReadStream(file), { name: file });
});
archive.finalize();
const streamProgress = progress({
length: parseInt(fileSize) * 1000000,
time: 100,
});
streamProgress.on("progress", (progress) => {
utils.printStats(progress);
});
archive.pipe(streamProgress).pipe(beam);
};
const clientMode = (password) => {
const key = utils.createKey(password);
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", () => {
utils.printReplace("Transfer finished 🚀");
return beam.end();
});
const streamProgress = progress({
time: 100,
});
streamProgress.on("progress", (progress) => {
utils.printReplace(
`Received ${(progress.transferred / 1000000).toFixed(2)} MB | Runtime: ${
progress.runtime
} seconds`
);
});
const unzipper = unzip.Extract({ path: pathClient });
beam.pipe(streamProgress).pipe(unzipper);
};
if (!keyAsArgument) {
console.log("\n");
console.log("Folder Beam server mode 👍");
console.log(
"If you want to receive files, you need to provide the password as an argument"
);
console.log("Example: ./folder-beam 1234");
console.log("\n");
if (argv.p) {
console.log("Password provided: " + argv.p);
return serverMode(argv.p);
}
return utils.askQuestion("Create a password: ").then((password) => {
return serverMode(password);
});
} else {
return clientMode(keyAsArgument);
}