This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
290 lines (257 loc) · 7.23 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
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
const axios_constructor = require("axios");
const hmacSHA512 = require("crypto-js/hmac-sha512");
const { NodeSSH } = require("node-ssh");
const instances = require("./instances.json");
const TIMEOUT = 100 * 20 * 1000;
const FREQUENCY = 60 * 1000;
const axios_timeout = axios_constructor.create({
timeout: TIMEOUT,
});
const axios = {
get: (url, options) => {
const abort = axios_constructor.CancelToken.source();
const id = setTimeout(
() => abort.cancel(`Timeout of ${TIMEOUT}ms.`),
TIMEOUT
);
return axios_timeout
.get(url, { cancelToken: abort.token, ...options })
.then((response) => {
clearTimeout(id);
return response;
});
},
post: (url, body, options) => {
const abort = axios_constructor.CancelToken.source();
const id = setTimeout(
() => abort.cancel(`Timeout of ${TIMEOUT}ms.`),
TIMEOUT
);
return axios_timeout
.post(url, body, { cancelToken: abort.token, ...options })
.then((response) => {
clearTimeout(id);
return response;
});
},
};
function obtain_access_token(user_id, homeserver_api_url, shared_secret) {
const login_api_url = homeserver_api_url + "/_matrix/client/r0/login";
const password = hmacSHA512(user_id, shared_secret).toString();
const payload = {
type: "m.login.password",
user: user_id,
initial_device_display_name: "availability_check",
device_id: "sync_availability_check",
password: password,
};
//console.debug(JSON.stringify(payload));
return axios.post(login_api_url, payload).then((response) => {
const session = {
userId: user_id,
homeserverUrl: homeserver_api_url,
accessToken: response.data.access_token,
};
return session;
});
}
async function testSynapse(instance) {
const result = {};
const domain = instance.baseDomain || `${instance.key}.messenger.schule`;
let accessToken = instance.accessToken;
if (!accessToken) {
const start_time = new Date().getTime()
await obtain_access_token(
`@sync:${domain}`,
`https://matrix.${domain}`,
instance.sharedSecret
)
.then((res) => {
accessToken = res.accessToken;
result.syncConnection = true;
result.loginTimeMS = new Date().getTime() - start_time;
})
.catch((err) => {
if (err.response) {
console.error(err.response.status, err.response.statusText, err.config.url);
} else {
console.error(err.code, err.config.url);
}
result.syncConnection = false;
});
}
if (accessToken) {
await axios
.get(`https://matrix.${domain}/_synapse/admin/v1/rooms`, {
headers: { Authorization: `Bearer ${accessToken}` },
})
.then((res) => {
result.createdRooms = res.data.total_rooms;
result.syncConnection = true;
})
.catch((err) => {
console.error(err);
result.createdRooms = "FAILED";
});
await axios
.get(`https://matrix.${domain}/_synapse/admin/v2/users`, {
headers: { Authorization: `Bearer ${accessToken}` },
})
.then((res) => {
result.createdUsers = res.data.total || res.data.users.length;
result.syncConnection = true;
})
.catch(() => {
result.createdUsers = "FAILED";
});
}
return result;
}
async function testEmbed(instance) {
const result = {};
await axios
.get(`https://embed.${instance.key}.messenger.schule/embed.js`)
.then((res) => {
result.embedAccessible = true;
})
.catch(() => {
result.embedAccessible = false;
});
return result;
}
async function testCors(instance) {
const result = {};
let domain = instance.alternativeDomain
? `https://${instance.alternativeDomain}`
: `https://${instance.key}.hpi-schul-cloud.org`;
await axios
.get(domain)
.then((res) => {
const cors = res.headers["content-security-policy"];
let foundHeaders = 0;
foundHeaders += (
cors.match(
new RegExp(`https:\/\/${instance.key}.messenger.schule`, "g")
) || []
).length;
foundHeaders += (
cors.match(
new RegExp(`https:\/\/embed.${instance.key}.messenger.schule`, "g")
) || []
).length;
foundHeaders += (
cors.match(
new RegExp(`https:\/\/matrix.${instance.key}.messenger.schule`, "g")
) || []
).length;
result.corsHeaders = `${foundHeaders} / 8`;
})
.catch((err) => {
result.corsHeaders = `FAILED`;
});
return result;
}
async function testHydra(instance) {
const result = {};
let domain = instance.alternativeDomain
? `https://oauth.${instance.alternativeDomain}`
: `https://oauth.${instance.key}.hpi-schul-cloud.org`;
await axios
.get(domain + "/health/alive")
.then((res) => {
result.hydraAlive = res.data.status === "ok";
})
.catch((err) => {
result.hydraAlive = `FAILED`;
});
await axios
.get(
`https://matrix.${instance.key}.messenger.schule/_matrix/client/r0/login/sso/redirect?redirectUrl=https%3A%2F%2Fapp.element.io%2F%23%2F`
)
.then((res) => {
result.oauth = res.request._redirectable._currentUrl;
})
.catch((err) => {
if (
err.response &&
JSON.stringify(err.response.data) ===
JSON.stringify({
errcode: "M_UNRECOGNIZED",
error: "Homeserver not configured for SSO.",
})
) {
result.oauth = "DISABLED";
} else {
result.oauth = "FAILED";
}
});
return result;
}
async function testSSH(instance) {
const result = {};
if (instance.privateKey) {
await new NodeSSH()
.connect({
host: instance.host || `https://${instance.key}.messenger.schule`,
username: instance.user || "root",
privateKey: instance.privateKey,
})
.then(() => {
result.ssh = true;
})
.catch((err) => {
result.ssh = false;
});
}
return result;
}
async function checkInstances() {
let instancesToCheck = instances;
// filer instances if names where passed as arguments
if (process.argv.length > 2) {
const instantKeys = process.argv.slice(2);
instancesToCheck = instancesToCheck.filter((instance) => {
return (
instantKeys.includes(instance.key) ||
instantKeys.includes(instance.name)
);
});
}
// init
const result = {};
for (const instance of instancesToCheck) {
result[instance.key] = {
//instance: instance.name,
embedAccessible: "N/A",
syncConnection: "N/A",
createdRooms: "N/A",
createdUsers: "N/A",
corsHeaders: "N/A",
};
}
// checks
const checks = [testSynapse, testEmbed, testCors, testHydra, testSSH];
const promises = [];
for (const instance of instancesToCheck) {
for (const check of checks) {
promises.push(
check(instance).then((res) => {
result[instance.key] = Object.assign(result[instance.key], res);
})
);
}
}
await Promise.all(promises);
return result;
}
function printCheck(results) {
console.log(new Date());
console.table(results);
}
function start() {
checkInstances().then(printCheck);
setInterval(() => {
checkInstances().then(printCheck);
}, FREQUENCY);
}
start();