-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
257 lines (236 loc) · 6.92 KB
/
index.ts
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
const { args } = Deno;
import { parse } from "https://deno.land/std/flags/mod.ts";
import { readLines } from "https://deno.land/std/io/bufio.ts";
import { opine } from "https://deno.land/x/opine@main/mod.ts";
import * as dejs from 'https://deno.land/x/dejs@0.7.0/mod.ts';
import { WebSocket, WebSocketServer } from "https://deno.land/x/websocket/mod.ts";
class WebSocketServerExtended extends WebSocketServer {
broadcast(message: string | object) {
for(var connection of this.clients) {
if(connection.isClosed) continue;
try {
connection.send(typeof message === "string" ? message : JSON.stringify(message));
} catch(e) {
console.warn(e);
}
}
}
}
var flags = parse(args, {
alias: {
"stdin": ["useStdin", "s"],
"port": "p"
}
});
if(flags.port) {
if(!Number.isInteger(flags.port)) {
console.error("Port must be an integer");
Deno.exit(1);
}
} else flags.port = 8080;
const wss = new WebSocketServerExtended(flags.port + 1);
wss.on("connection", function (ws: WebSocket) {
console.log("Connection active");
ws.on("message", function (message: string) {
console.log(message);
ws.send(message)
});
ws.on("close", () => {
console.log("Connection destroyed");
});
});
if(!flags.stdin) {
if(!flags._[0]) {
console.error("You must specify service name if not using stdin!");
Deno.exit(1);
}
}
var type: string;
var errorPt: number = -1;
var changed: {
[key: string]: boolean,
dbl: boolean,
reddit: boolean,
lang: boolean,
serverReady: boolean,
ready: boolean,
loaded: boolean,
uses: boolean,
users: boolean,
guilds: boolean,
errors: boolean
} = {
dbl: false,
reddit: false,
lang: false,
serverReady: false,
ready: false,
loaded: false,
uses: false,
users: false,
guilds: false,
errors: false
}
var data: {
[key: string]: any,
dbl: boolean | null,
reddit: boolean | null,
lang: boolean,
serverReady: boolean,
ready: boolean,
loaded: Map<string, number>,
uses: Map<string, number>,
users: Map<string, number>,
guilds: Map<string, number>,
errors: string[]
} = {
dbl: null,
reddit: null,
lang: false,
serverReady: false,
ready: false,
loaded: new Map(),
uses: new Map(),
users: new Map(),
guilds: new Map(),
errors: []
};
function mapToObj(strMap: Map<string, any>) {
let obj = Object.create(null);
for (let [k,v] of strMap) {
obj[k] = v;
}
return obj;
}
setInterval(() => {
var shouldUpdate = false;
var updated: { [key: string]: any } = {};
for(var val in changed) {
if(changed[val]) shouldUpdate = true;
changed[val] = false;
updated[val] = data[val];
if(updated[val] instanceof Map) {
updated[val] = mapToObj(updated[val]);
}
}
if(!shouldUpdate) return;
wss.broadcast(updated);
}, 500);
async function parseLine(line: string, time: Date = new Date()) {
console.log(line); //pass through
try {
var t = line.match(/\[([a-z1-9 ]+)\](.*)/i);
var text = line;
if(t && t[1]){
type = t[1].trim().toUpperCase();
if(type === "REJECTION") {
if(errorPt !== -1) {
changed.errors = true;
}
errorPt++;
}
text = t[2].trim();
}
} catch(e) {
console.error(e);
return;
}
switch(type) {
case "DBL":
if(text === "Skipping DBL API integration as no token is present in config.") {
data.dbl = false;
} else if(data.dbl === null) {
data.dbl = true;
}
break;
case "REJECTION":
data.errors[errorPt] = (data.errors[errorPt] || "") + text;
break;
case "REDDIT":
if(text === "Reddit connection successful") {
data.reddit = true;
changed.reddit = true;
}
break;
case "LANG":
if(text === "Loaded") {
data.lang = true;
changed.lang = true;
}
break;
case "LOAD":
var num = text.match(/\u001b\[37;1m([ 0-9]+)\u001b\[0m/i)![1];
var group = text.match(/\u001b\[36m([a-z]+)\u001b\[0m/i)![1];
data.loaded.set(group, Number(num));
changed.loaded = true;
break;
case "SERVER":
if(text === "Internal server ready") {
data.serverReady = true;
changed.serverReady = true;
}
break;
case "USE":
try {
var guild = text.match(/\u001b\[35;1m\[([^\u001b]+)\] \u001b\[37;1m/i)![1];
var tag = text.match(/\u001b\[37;1m\(([^\u001b]+)\)\u001b\[0m/i)![1];
// var prefix = text.match(/\u001b\[4m([^\u001b]+)\u001b\[0m/i)![1];
var command = text.match(/\u001b\[7m([^\u001b]+)\u001b\[0m/i)![1];
} catch(e) {
console.warn("[USE ERROR]", e);
return;
}
if(guild) {
data.guilds.set(guild, (data.guilds.get(guild) || 0) + 1);
changed.guilds = true;
}
if(tag) {
data.users.set(tag, (data.users.get(tag) || 0) + 1);
changed.users = true;
}
if(command) {
data.uses.set(command, (data.uses.get(command) || 0) + 1);
changed.uses = true;
}
break;
case "EVENT":
switch(text) {
case "\u001b[37;1m Ready!\u001b[0m":
data.ready = true;
changed.ready = true;
break;
}
}
}
const app = opine();
app.engine("ejs", dejs.renderFileToString);
app.get("/", async (req, res) => {
res.render("main.ejs", {
data,
port: flags.port + 1
});
});
app.listen(flags.port);
console.log("--PARSER server ready at http://localhost:" + flags.port + "/");
if(flags.stdin) {
for await (const line of readLines(Deno.stdin)) {
await parseLine(line);
}
console.log("End of input");
} else {
var service = String(flags._[0]);
const s = Deno.run({
cmd: ["journalctl", "-afu", service, "-o", "short-unix"],
input: "null",
output: "piped",
stderr: "null"
});
// Short-unix format: timestamp hostname process[pid]: message
for await (const line of readLines(s.stdout as Deno.Reader)) {
var timestamp = line.substr(0, line.indexOf(" "));
var l = line.substr(line.indexOf(":"));
await parseLine(l, new Date(parseFloat(timestamp)));
}
console.log("End of service output");
console.log("Something may have went wrong");
}