const express = require('express'); const { createHash } = require('crypto'); const app = express(); const port = 8356; const geonJsonTemplate = { type: "FeatureCollection", features: [], "marker-symbol-images": { "star": "data:image/svg+xml;utf8,", "star-stroked": "data:image/svg+xml;utf8," } }; app.all('/locations.geojson', (req, res) => { // Verify Inputs if (!req.query.auth) { res.status(401).send('Unauthorized'); return; } // Query Haze API Websocket let hazePwHash = null; let hazeUser = null; if (req.query.auth) { let paramAuth = Buffer.from(req.query.auth, 'base64').toString('utf-8').replaceAll('\n', ''); hazeUser = paramAuth.split(":")[0]; hazePwHash = paramAuth.split(":")[1]; } const haze = new WebSocket('wss://do.ecven.com:8120/explr'); haze.addEventListener('message', (event) => { const data = JSON.parse(event.data); let haze_authToken = '', haze_username = ''; switch (data['event']) { case "connected": let pld = { "event": "authenticateAccount_request", "body": [ { "password": hazePwHash, "email": hazeUser } ], "socketMessageId": 0 }; haze.send(JSON.stringify(pld)); break; case "authenticateAccount_response": if (data['body']['response'] === 1) { haze_authToken = data['body']['authToken']; haze_username = data['body']['username']; haze.send('{"event": "getMyLocationsRequest_request","body": [{"higlightImages": true}],"socketMessageId": 0}'); } else { console.dir(req.query); res.status(401).json({ ...data, password: req.query.auth }); haze.close(); res.end(); } break; case "getMyLocationsRequest_response": if (data['body']['response'] === 1) { const hazeLocations = data['body']['locations'].map(location => { return { type: "Feature", geometry: { type: "Point", coordinates: [location['coordinates']['longitude'], location['coordinates']['latitude']] }, properties: { id: location['id'], title: location['name'], "marker-symbol": location['isUnconfirmedLocation'] === 1 ? "star-stroked" : "star", } } }); res.setHeader('Content-Type', 'application/json'); haze.close(); res.end(JSON.stringify({ ...geonJsonTemplate, features: hazeLocations })); } else { console.dir(data); res.status(500).send('Failed to retrieve locations'); haze.close(); res.end(); } break; default: console.dir(data); res.status(500).send('Unexpected response from Haze API'); haze.close(); res.end(); } }); }); app.listen(port, () => { console.log(`Server is running on http://localhost:${port}`); });