-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
409 lines (357 loc) · 14.4 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
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
require("dotenv").config();
const express = require('express');
const axios = require('axios');
const cors = require("cors");
const {config} = require("./config/config");
// const jwt = require('jsonwebtoken');
let app = express();
app.use(cors())
/*app.get('/me', (req, res) => {
const apiUrl = "https://graph.microsoft.com/v1.0/me/";
const options = {
headers: {
...req.headers // Forward headers from the client, including Authorization
},
};
axios.get(apiUrl, options)
.then(response => {
res.json(response.data);
})
.catch(error => {
console.error(error);
res.status(500).send("Error occurred while fetching data from Microsoft Graph API: " + error);
});
});*/
/*app.get('/user-groups', (req, res) => {
const token = req.headers.authorization?.split(' ')[1] || '';
let userGroups = [];
try {
const decodedToken = jwt.decode(token);
userGroups = decodedToken.groups;
} catch (error) {
return res.status(401).json({ error: 'Invalid token' });
}
res.json({ groups: userGroups });
});*/
app.get('/siteimprov*', (req, res) => {
let apiUrl = "https://api.siteimprove.com/v2";
req.url = req.url.replace(/\/siteimprove/, '')
if (req.url.match(/users/)) {
res.end("APIet har blitt blokkert av Team ResearchOps i NAV, ta kontakt med oss for hjelp.")
} else {
const options = {
headers: {Authorization: "Basic " + process.env.SITEIMPROVE},
};
axios.get(apiUrl + req.url, options).then(function (response) {
res.json(response.data)
}).catch(function (error) {
console.log(error)
res.end("Kunne ikke koble til Siteimprove APIet: " + error)
});
}
});
app.get('/umami/api/*', (req, res) => {
let apiUrl = "https://umami.intern.nav.no/api";
req.url = req.url.replace(/\/umami\/api/, '');
if (req.url.match(/users/)) {
res.end("APIet har blitt blokkert av Team ResearchOps i NAV, ta kontakt med oss for hjelp.");
} else {
const options = {
headers: { Authorization: "Basic " + process.env.UMAMI },
};
axios.get(apiUrl + req.url, options)
.then(function (response) {
res.json(response.data);
})
.catch(function (error) {
console.log(error);
res.end("Kunne ikke koble til Umami APIet: " + error);
});
}
});
app.get('/umami/v2/api/*', (req, res) => {
let apiUrl = "https://umami.intern.nav.no/api";
req.url = req.url.replace(/\/umami\/v2\/api/, '');
// Check if the URL contains the parameter endsAt=yesterday
if (req.url.includes('endAt=yesterday')) {
// Get the current date
let yesterday = new Date();
// Set the date to yesterday
yesterday.setDate(yesterday.getDate() - 1);
// Set the time to midnight
yesterday.setHours(0, 0, 0, 0);
// Get the timestamp in milliseconds
let yesterdayMidnight = yesterday.getTime();
// Replace 'yesterday' with the timestamp
req.url = req.url.replace('endAt=yesterday', `endAt=${yesterdayMidnight}`);
}
if (req.url.includes('endAt=now')) {
// Get the current date and time
let now = new Date();
// Get the timestamp in milliseconds
let nowTimestamp = now.getTime();
// Replace 'now' with the timestamp
req.url = req.url.replace('endAt=now', `endAt=${nowTimestamp}`);
}
if (req.url.includes('range=day')) {
// Get the current date and time
let now = new Date();
// Get the timestamp in milliseconds
let nowTimestamp = now.getTime();
// Calculate the timestamp for 360 days ago
let startAtTimestamp = new Date(now.setDate(now.getDate() - 196)).getTime();
// Append the parameters to the URL
req.url += `&startAt=${startAtTimestamp}&endAt=${nowTimestamp}&unit=day`;
console.log(req.url);
}
if (req.url.includes('range=hour')) {
// Get the current date and time
let now = new Date();
// Get the timestamp in milliseconds
let nowTimestamp = now.getTime();
// Calculate the timestamp for 48 hours ago
let startAtTimestamp = new Date(now.setHours(now.getHours() - 48)).getTime();
// Append the parameters to the URL
req.url += `&startAt=${startAtTimestamp}&endAt=${nowTimestamp}&unit=hour`;
console.log(req.url);
}
if (req.url.match(/users/)) {
res.end("APIet har blitt blokkert av Team ResearchOps i NAV, ta kontakt med oss for hjelp.");
} else {
const options = {
headers: { Authorization: "Basic " + process.env.UMAMI },
};
axios.get(apiUrl + req.url, options)
.then(function (response) {
let data = response.data;
// Convert date strings to milliseconds
data = convertDatesToMilliseconds(data);
res.json(data);
})
.catch(function (error) {
console.log(error);
res.end("Kunne ikke koble til Umami APIet: " + error);
});
}
});
function convertDatesToMilliseconds(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(item => convertDatesToMilliseconds(item));
}
const result = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
if (typeof obj[key] === 'string') {
const date = new Date(obj[key]);
if (!isNaN(date.getTime())) { // Check if it's a valid date
result[key] = date.getTime(); // Convert to milliseconds
} else {
result[key] = obj[key];
}
} else if (typeof obj[key] === 'object') {
result[key] = convertDatesToMilliseconds(obj[key]);
} else {
result[key] = obj[key];
}
}
}
return result;
}
// Amplitude Prosjekt: NAV.no - Produksjon
app.get('/amplitude/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude/, '')
const authToken = process.env.AMPLITUDE_100000009
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: NAV.no - Produksjon 2.0
app.get('/amplitude/100000009/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000009/, '')
const authToken = process.env.AMPLITUDE_100000009
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: PO Arbeid - prod
app.get('/amplitude/100000264/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000264/, '')
const authToken = process.env.AMPLITUDE_100000264
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: PO Arbeidsplassen - dev
app.get('/amplitude/100000243/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000243/, '')
const authToken = process.env.AMPLITUDE_100000243
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: PO Arbeidsplassen - prod
app.get('/amplitude/100000244/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000244/, '')
const authToken = process.env.AMPLITUDE_100000244
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: MEMU - prod
app.get('/amplitude/100002286/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002286/, '')
const authToken = process.env.AMPLITUDE_100002286
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Aksel - prod
app.get('/amplitude/100002016/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002016/, '')
const authToken = process.env.AMPLITUDE_100002016
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Speil - dev
app.get('/amplitude/100003868/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100003868/, '')
const authToken = process.env.AMPLITUDE_100003868
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Speil - prod
app.get('/amplitude/100003867/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100003867/, '')
const authToken = process.env.AMPLITUDE_100003867
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Delta - prod
app.get('/amplitude/100005528/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100005528/, '')
const authToken = process.env.AMPLITUDE_100005528
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Work in norway - prod
app.get('/amplitude/100002233/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002233/, '')
const authToken = process.env.AMPLITUDE_100002233
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Kunnskapsbanken - prod
app.get('/amplitude/100001607/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100001607/, '')
const authToken = process.env.AMPLITUDE_100001607
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Idebanken - prod
app.get('/amplitude/100002226/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002226/, '')
const authToken = process.env.AMPLITUDE_100002226
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Arbeid og velferd - prod
app.get('/amplitude/100004122/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100004122/, '')
const authToken = process.env.AMPLITUDE_100004122
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Teknologiradar - prod
app.get('/amplitude/100005544/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100005544/, '')
const authToken = process.env.AMPLITUDE_100005544
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Det som betyr noe - prod
app.get('/amplitude/100000251/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000251/, '')
const authToken = process.env.AMPLITUDE_100000251
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Hjelpemiddeldatabasen - prod
app.get('/amplitude/100000965/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100000965/, '')
const authToken = process.env.AMPLITUDE_100000965
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Navet - prod
app.get('/amplitude/100002261/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002261/, '')
const authToken = process.env.AMPLITUDE_100002261
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Mangfoldimai- prod
app.get('/amplitude/100002287/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100002287/, '')
const authToken = process.env.AMPLITUDE_100002287
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: ResearchOps- prod
app.get('/amplitude/100004171/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100004171/, '')
const authToken = process.env.AMPLITUDE_100004171
amplitudeProxy(authToken, requestUrl, res)
});
// Amplitude Prosjekt: Innblikk- prod
app.get('/amplitude/100006162/api/*', (req, res) => {
const requestUrl = req.url.replace(/\/amplitude\/100006162/, '')
const authToken = process.env.AMPLITUDE_100006162
amplitudeProxy(authToken, requestUrl, res)
});
const amplitudeProxy = (authToken, requestUrl, proxyResponse) => {
let apiUrl = "https://analytics.eu.amplitude.com";
const options = {headers: {Authorization: "Basic " + authToken}};
if (proxyRouteIsBlocked(requestUrl)) {
proxyResponse.end("APIet har blitt blokkert av Team ResearchOps i NAV, ta kontakt med oss for hjelp.")
} else if (requestUrl.match(/export/)) {
fetchAmplitudeExportData(apiUrl + requestUrl, options, proxyResponse)
} else {
fetchAmplitudeJsonData(apiUrl + requestUrl, options, proxyResponse)
}
}
const proxyRouteIsBlocked = (requestUrl) => {
const blockedRoutes = [/deletions/, /taxonomy/, /release/, /scim/, /batch/]
return blockedRoutes.some((route) => requestUrl.match(route))
}
const fetchAmplitudeExportData = (url, options, proxyResponse) => {
axios.get(url, {...options, responseType: "stream"})
.then((resp) => {
resp.data.on('data', data => proxyResponse.write(data));
resp.data.on('end', () => proxyResponse.end());
})
.catch((error) => {
console.log(error)
const statusCode = error.response ? error.response.status : 500
proxyResponse.status(statusCode)
proxyResponse.end("Kunne ikke koble til Amplitude APIet: " + error)
});
}
const fetchAmplitudeJsonData = (url, options, proxyResponse) => {
axios.get(url, options)
.then((resp) => proxyResponse.json(resp.data))
.catch((error) => {
console.log(error)
const statusCode = error.response ? error.response.status : 500
proxyResponse.status(statusCode)
proxyResponse.end("Kunne ikke koble til Amplitude APIet: " + error)
});
}
app.get('/url/*', (req, res) => {
// Decode the URL component
let urlToFetch = decodeURIComponent(req.params[0]);
// Check if the URL starts with http:// or https://
if (!urlToFetch.match(/^https?:\/\//)) {
urlToFetch = 'http://' + urlToFetch;
}
// Perform the GET request to the extracted URL
axios.get(urlToFetch)
.then(response => {
res.send(response.data);
})
.catch(error => {
console.error(error);
res.status(500).send(`Error occurred while fetching data from ${urlToFetch}`);
});
});
app.get('/reops', (req, res) => {
res.end("it works")
});
app.use(cors()).get('/config', (req, res) => {
let authsecret = `Basic ${Buffer.from("123:123", "utf-8").toString("base64")}`
console.log(authsecret)
res.sendStatus(200)
});
app.get("/isAlive", (req, res) => res.sendStatus(200));
app.get("/isReady", (req, res) => res.sendStatus(200));
app.get("/", (req, res) => res.sendStatus(200));
app.listen(8080, function () {
console.log("Express Started");
});