-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (102 loc) · 4.16 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
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const dotenv_1 = __importDefault(require("dotenv"));
const express_1 = __importDefault(require("express"));
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const readline_1 = __importDefault(require("readline"));
dotenv_1.default.config();
const app = (0, express_1.default)();
const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 3000;
const API_KEY = process.env.API_KEY; // Use a pre-defined API key for authentication
const CONFIDENTIAL_WORDS = process.env.CONFIDENTIAL_WORDS ? process.env.CONFIDENTIAL_WORDS.split(',') : [];
// Middleware to block all other request methods except GET
app.use((req, res, next) => {
if (req.method !== 'GET') {
return res.status(405).send('Method Not Allowed');
}
next();
});
// Middleware for API authentication (enabled if API_AUTH_ENABLED is true)
const apiAuthMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).send('Unauthorized Access');
}
if (authHeader !== `Bearer ${API_KEY}`) {
return res.status(401).send('Unauthorized Access');
}
next();
};
// Apply the authentication middleware globally
app.use(apiAuthMiddleware);
// Function to read the last n lines of a file
const readLastLines = (filePath, n) => {
return new Promise((resolve, reject) => {
const lines = [];
const readStream = fs_1.default.createReadStream(filePath);
const rl = readline_1.default.createInterface({
input: readStream,
crlfDelay: Infinity,
});
rl.on('line', (line) => {
if (containsConfidentialWord(line)) {
lines.push('***');
}
else
lines.push(line);
if (lines.length > n) {
lines.shift(); // Keep only the last n lines in memory
}
});
rl.on('close', () => resolve(lines));
rl.on('error', (err) => reject(err));
});
};
const containsConfidentialWord = (line) => {
return CONFIDENTIAL_WORDS.some(word => line.toLowerCase().includes(word.toLowerCase()));
};
app.get('/rlogs', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const fileName = req.query.file;
const n = parseInt(req.query.n, 10) || 100;
if (!fileName) {
return res.status(400).send('File name is required');
}
const absolutePath = process.env.ABSOLUTE_PATH;
if (!absolutePath) {
return res.status(500).send('Server configuration error: ABSOLUTE_PATH is not set');
}
const filePath = path_1.default.join(absolutePath, fileName);
// Check if file exists before proceeding
if (!fs_1.default.existsSync(filePath)) {
return res.status(404).send('File not found');
}
try {
// If n is specified, read the last n lines, otherwise download the entire file
if (n) {
const lines = yield readLastLines(filePath, n);
res.type('text/plain').send(lines.join('\n'));
}
else {
return res.status(401).send('Inavlid request');
}
}
catch (error) {
res.status(500).send(`Error reading the file: ${error.message}`);
}
}));
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});