-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
382 lines (329 loc) · 12.5 KB
/
server.js
File metadata and controls
382 lines (329 loc) · 12.5 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
//---------------section One---------------------------------
const http = require("http"); // require is used to import a module
// http -> built in Node.js module for creating web servers
const fs = require("fs"); // lets us read/write files
const path = require("path"); // Helps build file paths correctly
const crypto = require("crypto");
const PORT = 3000;
const WEBHOOKS_DIR = path.join(__dirname, "webhooks");
const LOGS_DIR = path.join(__dirname, "logs");
// Create directories if they don't exist
// Sync = Do it now, wait for it to finish (synchronous)
if (!fs.existsSync(WEBHOOKS_DIR)) fs.mkdirSync(WEBHOOKS_DIR);
if (!fs.existsSync(LOGS_DIR)) fs.mkdirSync(LOGS_DIR);
//-----------------------Section End------------------------------
// ----------------------section Two------------------------------
// Logging utility
//What Does This Function Do?
// Simple answer: Saves messages to both:
// Console (so you can see them in terminal)
// Log file (so you have a permanent record)
function log(message, type = "INFO") {
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] [${type}] ${message}\n`;
console.log(logMessage.trim());
// result : C:\Users\chava\Desktop\webhook-logger\logs\2025-10-11.log
// Want to see what happened on October 11? Open 2025-10-11.log
const logFile = path.join(
LOGS_DIR,
`${new Date().toISOString().split("T")[0]}.log`
);
// log file just give us the path and the file name that we want
// .appendFileSync() actually takes the path as first parameter
// second parameter as the actual data !!
//Adds to end of file (keeps old content)
fs.appendFileSync(logFile, logMessage);
}
// ------------------------Section End--------------------------------
//-----------------------section Three-------------------------------
// Parse request body
// Reads data from POST requests
//Data doesn't arrive all at once - it comes in pieces (chunks).
// We need to collect all pieces before using it.
function parseBody(req) {
// req = The request object (contains incoming data from GitHub/etc)
return new Promise((resolve, reject) => {
// promise ->When I'm done, I'll call you back.
// Because reading data takes time (it's asynchronous). We can't return the data immediately.
let body = ""; // we'll sstore everything in here
//---------------1 Event Listerner----------
req.on("data", (chunk) => (body += chunk.toString()));
// Data arrives as raw bytes (Buffer). We convert it to readable text (String).
// ---------------2 Event Listerner----------
req.on("end", () => {
// this event is for -> "When all data has arrived, do this"
try {
resolve(body ? JSON.parse(body) : {});
} catch (e) {
//What if JSON.parse() fails?
// Example: Body is not valid JSON
//body = "This is not JSON!"
// JSON.parse(body); // ERROR!
resolve({ raw: body });
}
});
req.on("error", reject);
});
}
// ---------------------------Section end---------------------------------
//-------------------------section four-----------------------------------
// Verify GitHub signature
// What Does This Function Do?
// Simple answer: Proves that a webhook really came from GitHub
// (not a hacker pretending to be GitHub).
// Like verifying someone's ID before letting them in.
function verifyGitHubSignature(payload, signature, secret) {
// payload = The webhook data (JavaScript object)
// signature = Signature GitHub sent (from header)
//secret = Your secret password
if (!signature) return false;
//HMAC = Hash-based Message Authentication Code
// HMAC - Special algorithm that creates signatures
const hmac = crypto.createHmac("sha256", secret);
//hmac.update(...) = Feed the string into the HMAC calculator
// Like putting ingredients into a blender!
hmac.update(JSON.stringify(payload));
const calculatedSignature = "sha256=" + hmac.digest("hex");
// .digest() -> Blend it and give me the result
// Calculate the signature
//Always takes the same time regardless of where the difference is!
//Regular comparison (===) compares character by character and stops at the first difference
// Why use timingSafeEqual instead of ===?
// Prevents timing attacks
return crypto.timingSafeEqual(
//Converts strings to Buffers (raw bytes) for secure comparison
Buffer.from(signature),
Buffer.from(calculatedSignature)
);
// syntax -> crypto.timingSafeEqual(signature1, signature2)
}
//------------------------Example for Hmac signature creation-----------
/*
// Step 1: Create HMAC with algorithm + secret
const hmac = crypto.createHmac('sha256', 'my_secret_123');
// Step 2: Add the message/data
hmac.update('This is the message'); // ← Message goes here!
// Step 3: Generate signature
const signature = hmac.digest('hex');
*/
// ------------------------Section End------------------------------------
// -------------------------section five ---------------------------------
// Save webhook to file
/*
What Does This Function Do?
Simple answer: Saves webhook data as a JSON file on your computer.
Think of it as: Taking a photo (webhook)
and saving it to your photo album (webhooks folder).
*/
// this function specific saves in log dir and then parllel calls the
// log function to also save the data in logs dir (message,status/type)
function saveWebhook(webhookData) {
//webhookData = An object containing all the webhook information
const timestamp = Date.now();
const filename = `webhook_${timestamp}.json`;
//Combines folder + filename:
const filepath = path.join(WEBHOOKS_DIR, filename);
/*
fs.writeFileSync(
filepath, // WHERE to save
data // WHAT to save
);
*/
//Sync = Synchronous
// Waits until file is saved before continuing
fs.writeFileSync(filepath, JSON.stringify(webhookData, null, 2));
/*
Parameter 1: webhookData = The object to convert
Parameter 2: null = Replacer function (we don't need it, so null)
Parameter 3: 2 = Number of spaces for indentation (makes it pretty!)
*/
log(`Webhook saved: ${filename}`, "SUCCESS");
//Uses our log function from earlier to record
return filename;
//Returns the filename so other parts of the code know what file was created.
// Example usage:
// const savedFile = saveWebhook(data);
// console.log('Saved as:', savedFile);
}
// ---------------------------Section end-------------------------------
//-------------------------section six---------------------------------
//What Does This Function Do?
// Simple answer: Reads all webhook files from the webhooks/
// folder and returns them as an array.
// Get all webhooks
function getAllWebhooks() {
//Result: Array of JSON filenames, newest first!
const files = fs // Each method operates on result of previous(method chaining)
.readdirSync(WEBHOOKS_DIR) // Get all files names in array format
.filter((f) => f.endsWith(".json")) // Keep only JSON
.sort() // Sort oldest→newest
.reverse(); // Flip to newest→oldest
// .map() -> transforms each item in an array.
/*
Complete flow:
filename
→ build path
→ read file (get text)
→ parse JSON (get object)
→ store in `data`
*/
return files.map((file) => {
const data = JSON.parse(fs.readFileSync(path.join(WEBHOOKS_DIR, file)));
return {
id: file,
...data, //The ... is called the spread operator.
// It "spreads out" all properties from an object
};
});
}
/*
With spread
{
id: "webhook_123.json",
...data // Spreads properties
}
Result: { id: "...", timestamp: "...", source: "..." }
*/
//-------------------------------Section end--------------------------
// ---------------------------section seven---------------------------
// Serve static files
//Sends files (HTML, CSS, JS) from your computer to the browser.
function serveStaticFile(res, filepath, contentType) {
/*
res = Response object (what we send back to browser)
filepath = Path to the file we want to send
contentType = What type of file it is
serveStaticFile(res, 'public/index.html', 'text/html');
*/
//Start reading file (async)
fs.readFile(filepath, (err, data) => {
if (err) {
//Send status and headers-> .writehead()
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
//res.end() sends it to the browser and closes the response.
return;
}
res.writeHead(200, { "Content-Type": contentType });
// The type we received as parameter (text/html, text/css, etc.)
res.end(data); //res.end() sends it to the browser and closes the response.
});
}
//------------------------Section end---------------------------------
// Main server
const server = http.createServer(async (req, res) => {
const url = req.url;
const method = req.method;
log(`${method} ${url}`);
// Serve frontend
if (url === "/" || url === "/index.html") {
serveStaticFile(
res,
path.join(__dirname, "public", "index.html"),
"text/html"
);
} else if (url === "/style.css") {
serveStaticFile(
res,
path.join(__dirname, "public", "style.css"),
"text/css"
);
} else if (url === "/app.js") {
serveStaticFile(
res,
path.join(__dirname, "public", "app.js"),
"application/javascript"
);
}
// API: Get all webhooks
else if (url === "/api/webhooks" && method === "GET") {
try {
const webhooks = getAllWebhooks();
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify(webhooks));
} catch (error) {
log(`Error fetching webhooks: ${error.message}`, "ERROR");
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Internal Server Error" }));
}
}
// Webhook receiver endpoint
else if (url.startsWith("/webhook/") && method === "POST") {
try {
const body = await parseBody(req);
const source = url.split("/webhook/")[1];
const webhookData = {
timestamp: new Date().toISOString(),
source: source,
headers: req.headers,
payload: body,
processed: false,
status: "received",
};
// GitHub signature verification (optional - set GITHUB_SECRET env variable)
if (source === "github" && process.env.GITHUB_SECRET) {
const signature = req.headers["x-hub-signature-256"];
const isValid = verifyGitHubSignature(
body,
signature,
process.env.GITHUB_SECRET
);
webhookData.signatureValid = isValid;
if (!isValid) {
log("Invalid GitHub signature!", "WARNING");
res.writeHead(401, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Invalid signature" }));
return;
}
}
// Save webhook
const filename = saveWebhook(webhookData);
// Process webhook (you'll add logic here later)
processWebhook(webhookData);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
success: true,
message: "Webhook received",
id: filename,
})
);
log(`Webhook received from ${source}`, "SUCCESS");
} catch (error) {
log(`Error processing webhook: ${error.message}`, "ERROR");
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Failed to process webhook" }));
}
}
// 404 for unknown routes
else {
res.writeHead(404, { "Content-Type": "text/plain" });
res.end("404 Not Found");
}
});
// Process webhook (placeholder for your logic)
function processWebhook(webhookData) {
// This is where you'll add your processing logic
// Examples:
// - Send notifications
// - Trigger deployments
// - Update databases
// - Call other APIs
log(`Processing webhook from ${webhookData.source}...`, "INFO");
// For GitHub webhooks, you can check the event type
if (webhookData.source === "github") {
const event = webhookData.headers["x-github-event"];
log(`GitHub event: ${event}`, "INFO");
// Example: Handle push event
if (event === "push") {
const commits = webhookData.payload.commits?.length || 0;
const branch = webhookData.payload.ref?.split("/").pop();
log(`Received ${commits} commits to branch ${branch}`, "INFO");
}
}
}
server.listen(PORT, () => {
log(`Webhook server running on http://localhost:${PORT}`, "SUCCESS");
log(`Webhook endpoint: http://localhost:${PORT}/webhook/{source}`, "INFO");
log(`Example: http://localhost:${PORT}/webhook/github`, "INFO");
});