This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
119 lines (97 loc) · 3.11 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
const express = require('express');
const app = express();
const mongodb = require('mongodb').MongoClient;
const request = require('request-promise-native');
const port = 3000;
const dbName = 'blubblubTest';
const ipAddressApiUrl = 'https://api.ipify.org?format=json';
const secretToken = 'thisIsASuperSecretToken';
let db = {};
const client = new mongodb('mongodb://localhost:27017');
app.get('/', async (req, res) => {
console.log(db);
let response = [];
try {
response = await db.collection('addresses').find().toArray();
console.log(`response: ${JSON.stringify(response)}`)
} catch(err) {
console.log(`Error: ${err}`);
}
res.send(response)
});
app.get('/status', async (req, res) => {
const {authorization: bearerToken} = req.headers;
if (!bearerToken) {
return res.status(403).send(`Error no token provided`);
}
let token = '';
try {
[,,token] = /^(Bearer\s)(.*)/.exec(bearerToken);
} catch (err) {
return res.status(500).send(`Error parsing token`);
}
if (!token) {
return res.status(403).send(`Error no token provided`);
}
if (token !== secretToken) {
return res.status(403).send(`Error invalid token provided`);
}
console.log(`Token: ${token}`)
const addressesCollection = db.collection('addresses');
let ipApiResponse = '';
let parsedIpApiResponse = {};
let ipAddress = '';
console.log(`Req headers: ${JSON.stringify(req.headers)}`)
if (req.headers.hasOwnProperty('x-forwarded-for')) {
const {'x-forwarded-for': forwardedRequestHeader} = req.headers;
[ipAddress] = forwardedRequestHeader.split(',');
} else {
try {
ipApiResponse = await request(ipAddressApiUrl)
} catch(err) {
console.log(`Error making ip request: ${err}`);
return res.status(500).send(err);
}
try {
parsedIpApiResponse = JSON.parse(ipApiResponse);
} catch(err) {
console.log(`Error parsing: ${err}`);
return res.status(500).send(err);
}
ipAddress = parsedIpApiResponse.ip
}
console.log(`Ip address: ${ipAddress}`);
let dbResponse = {};
try {
dbResponse = await addressesCollection.findOne({
ipAddress
});
} catch(err) {
return res.status(500).send(err);
}
console.log(`Found address: ${JSON.stringify(dbResponse)}`);
if (dbResponse !== null) {
console.log(`Found record: ${dbResponse}`);
return res.send(dbResponse);
}
try {
await addressesCollection.insertOne({
ipAddress
})
} catch(err) {
console.log(`Error inserting record: ${err}`);
return res.status(500).send(err);
}
res.send(ipAddress);
});
client.connect((err, database) => {
if (err) {
console.log(`Error connection to db: ${err}`);
throw new Error(err);
}
console.log("Connected correctly to server");
db = database.db(dbName);
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
});