-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlivecaidoreload.js
executable file
·124 lines (103 loc) · 2.83 KB
/
livecaidoreload.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
#! /usr/bin/env node
import fs from "fs";
import chalk from "chalk";
import { WebSocketServer } from "ws";
import crypto from "crypto";
import path from "path";
import chokidar from "chokidar";
const __dirname = path.resolve();
const pluginZipPath = process.argv[2];
const wss = new WebSocketServer({ port: 8081 });
const createMD5 = async (filePath) => {
return new Promise((res, rej) => {
const hash = crypto.createHash("md5");
const rStream = fs.createReadStream(filePath);
rStream.on("data", (data) => {
hash.update(data);
});
rStream.on("end", () => {
res(hash.digest("hex"));
});
rStream.on("error", (err) => {
rej(err);
});
});
};
const sendEvent = async (data) => {
wss.clients.forEach((client) => {
if (client.readyState === 1) {
client.send(data);
}
});
console.log(chalk.green(`Sent event to clients`));
};
let previousFileHash = "";
const handleFileChange = async (filePath) => {
if (!fs.existsSync(filePath)) {
console.log(chalk.red(`File not found: ${filePath}`));
return;
}
const currentFileHash = await createMD5(filePath);
if (currentFileHash === previousFileHash) return;
previousFileHash = currentFileHash;
if (filePath.endsWith(".zip")) {
console.log(chalk.yellow("Detected plugin zip file change"));
const fileStream = fs.createReadStream(filePath);
fileStream.on('data', (chunk) => {
console.log(chalk.green("Sending chunk"));
sendEvent(chunk);
});
fileStream.on('end', () => {
sendEvent(JSON.stringify({ end: true }));
});
}
};
const files = [];
if (pluginZipPath) {
files.push(pluginZipPath);
} else {
files.push(...fs.readdirSync(process.cwd()));
}
if (!files.length) {
console.log(chalk.red("No files found."));
process.exit(1);
}
files.forEach((path) => {
if (!fs.existsSync(path)) {
console.log(chalk.red(`File not found: ${path}`));
process.exit(1);
}
});
const currentDir = process.cwd();
const watcher = chokidar.watch(currentDir);
watcher.on('all', (event, path) => {
const fileName = path.split("/").pop();
if ((event === "change" || event === "add") && files.includes(fileName)) {
handleFileChange(path);
}
});
wss.on("connection", (ws) => {
console.log(chalk.green("Client connected"));
ws.on("close", () => {
console.log(chalk.red("Client disconnected"));
});
});
console.log(
chalk.green("Live reload server started on"),
chalk.blue("ws://localhost:8081")
);
console.log(chalk.green("Watching files:"));
files.forEach((file) => {
let output = chalk.green(`- ${file}`);
if (file.endsWith(".zip")) {
output += chalk.yellow(" (PLUGIN ZIP)");
} else {
output += chalk.red(" (Unsupported)");
}
console.log(output);
});
console.log(
chalk.yellow(
"Make sure you have installed LiveCaidoReloadPlugin in your Caido instance. Good luck!"
)
);