From df9c84e9df8166331c365238107ab91e10c7465d2abb0e4fba799d800a39e1a8 Mon Sep 17 00:00:00 2001 From: localhorst Date: Mon, 6 Jan 2025 21:47:13 +0100 Subject: [PATCH] add wifilocation flags --- server/.env.template | 5 +- server/package-lock.json | 169 +----------------- server/package.json | 4 +- server/sql/tables.sql | 6 +- .../middleware/authentificationMiddleware.ts | 3 +- server/src/models/wifiLocation.ts | 12 +- server/src/proxy/wigle.ts | 18 +- server/src/services/locationService.ts | 10 +- .../services/wifiLocationHistoryService.ts | 6 +- server/src/services/wifiLocationService.ts | 46 +++-- 10 files changed, 65 insertions(+), 214 deletions(-) diff --git a/server/.env.template b/server/.env.template index 7d3ca64..2f08814 100644 --- a/server/.env.template +++ b/server/.env.template @@ -7,7 +7,4 @@ DB_PORT="" WEBHOOK_TOKEN="" #Token that is placed a the TTN Webhook auth WIGLE_TOKEN="" # Go to account and generate token "Encoded for use" WIGLE_BASE_URL="https://api.wigle.net" -WIGLE_NETWORK_SEARCH="/api/v2/network/search" -GET_LOCATION_WIFI_MAX_AGE=1209600000 # 14 days in milliseconds (14 * 24 * 60 * 60 * 1000) -GET_LOCATION_WIFI_MAX=10000 -GET_LOCATION_WIFI_PRIMITIVE=true \ No newline at end of file +WIGLE_NETWORK_SEARCH="/api/v2/network/search" \ No newline at end of file diff --git a/server/package-lock.json b/server/package-lock.json index 96f0aec..b8236af 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -7,14 +7,13 @@ "": { "name": "locationhub", "version": "1.0.0", - "license": "ISC", + "license": "MIT", "dependencies": { "cors": "^2.8.5", "dotenv": "^16.4.7", "express": "^4.21.2", "http-status-codes": "^2.3.0", "mariadb": "^3.4.0", - "memoizee": "^0.4.17", "reflect-metadata": "^0.2.2", "sequelize": "^6.37.5", "swagger-jsdoc": "^6.2.8", @@ -24,7 +23,6 @@ }, "devDependencies": { "@types/express": "^5.0.0", - "@types/memoizee": "^0.4.11", "@types/node": "^22.10.2", "nodemon": "^3.1.9", "ts-node": "^10.9.2", @@ -232,13 +230,6 @@ "integrity": "sha512-5+fP8P8MFNC+AyZCDxrB2pkZFPGzqQWUzpSeuuVLvm8VMcorNYavBqoFcxK8bQz4Qsbn4oUEEem4wDLfcysGHA==", "license": "MIT" }, - "node_modules/@types/memoizee": { - "version": "0.4.11", - "resolved": "https://registry.npmjs.org/@types/memoizee/-/memoizee-0.4.11.tgz", - "integrity": "sha512-2gyorIBZu8GoDr9pYjROkxWWcFtHCquF7TVbN2I+/OvgZhnIGQS0vX5KJz4lXNKb8XOSfxFOSG5OLru1ESqLUg==", - "dev": true, - "license": "MIT" - }, "node_modules/@types/mime": { "version": "1.3.5", "resolved": "https://registry.npmjs.org/@types/mime/-/mime-1.3.5.tgz", @@ -582,19 +573,6 @@ "dev": true, "license": "MIT" }, - "node_modules/d": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/d/-/d-1.0.2.tgz", - "integrity": "sha512-MOqHvMWF9/9MX6nza0KgvFH4HpMU0EF5uUDXqX/BtxtU8NfB0QzRtJ8Oe/6SuS4kbhyzVJwjd97EA4PKrzJ8bw==", - "license": "ISC", - "dependencies": { - "es5-ext": "^0.10.64", - "type": "^2.7.2" - }, - "engines": { - "node": ">=0.12" - } - }, "node_modules/debug": { "version": "2.6.9", "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", @@ -731,79 +709,12 @@ "node": ">= 0.4" } }, - "node_modules/es5-ext": { - "version": "0.10.64", - "resolved": "https://registry.npmjs.org/es5-ext/-/es5-ext-0.10.64.tgz", - "integrity": "sha512-p2snDhiLaXe6dahss1LddxqEm+SkuDvV8dnIQG0MWjyHpcMNfXKPE+/Cc0y+PhxJX3A4xGNeFCj5oc0BUh6deg==", - "hasInstallScript": true, - "license": "ISC", - "dependencies": { - "es6-iterator": "^2.0.3", - "es6-symbol": "^3.1.3", - "esniff": "^2.0.1", - "next-tick": "^1.1.0" - }, - "engines": { - "node": ">=0.10" - } - }, - "node_modules/es6-iterator": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/es6-iterator/-/es6-iterator-2.0.3.tgz", - "integrity": "sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g==", - "license": "MIT", - "dependencies": { - "d": "1", - "es5-ext": "^0.10.35", - "es6-symbol": "^3.1.1" - } - }, - "node_modules/es6-symbol": { - "version": "3.1.4", - "resolved": "https://registry.npmjs.org/es6-symbol/-/es6-symbol-3.1.4.tgz", - "integrity": "sha512-U9bFFjX8tFiATgtkJ1zg25+KviIXpgRvRHS8sau3GfhVzThRQrOeksPeT0BWW2MNZs1OEWJ1DPXOQMn0KKRkvg==", - "license": "ISC", - "dependencies": { - "d": "^1.0.2", - "ext": "^1.7.0" - }, - "engines": { - "node": ">=0.12" - } - }, - "node_modules/es6-weak-map": { - "version": "2.0.3", - "resolved": "https://registry.npmjs.org/es6-weak-map/-/es6-weak-map-2.0.3.tgz", - "integrity": "sha512-p5um32HOTO1kP+w7PRnB+5lQ43Z6muuMuIMffvDN8ZB4GcnjLBV6zGStpbASIMk4DCAvEaamhe2zhyCb/QXXsA==", - "license": "ISC", - "dependencies": { - "d": "1", - "es5-ext": "^0.10.46", - "es6-iterator": "^2.0.3", - "es6-symbol": "^3.1.1" - } - }, "node_modules/escape-html": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", "integrity": "sha512-NiSupZ4OeuGwr68lGIeym/ksIZMJodUGOSCZ/FSnTxcrekbvqrgdUxlJOMpijaKZVjAJrWrGs/6Jy8OMuyj9ow==", "license": "MIT" }, - "node_modules/esniff": { - "version": "2.0.1", - "resolved": "https://registry.npmjs.org/esniff/-/esniff-2.0.1.tgz", - "integrity": "sha512-kTUIGKQ/mDPFoJ0oVfcmyJn4iBDRptjNVIzwIFR7tqWXdVI9xfA2RMwY/gbSpJG3lkdWNEjLap/NqVHZiJsdfg==", - "license": "ISC", - "dependencies": { - "d": "^1.0.1", - "es5-ext": "^0.10.62", - "event-emitter": "^0.3.5", - "type": "^2.7.2" - }, - "engines": { - "node": ">=0.10" - } - }, "node_modules/esutils": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/esutils/-/esutils-2.0.3.tgz", @@ -822,16 +733,6 @@ "node": ">= 0.6" } }, - "node_modules/event-emitter": { - "version": "0.3.5", - "resolved": "https://registry.npmjs.org/event-emitter/-/event-emitter-0.3.5.tgz", - "integrity": "sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA==", - "license": "MIT", - "dependencies": { - "d": "1", - "es5-ext": "~0.10.14" - } - }, "node_modules/express": { "version": "4.21.2", "resolved": "https://registry.npmjs.org/express/-/express-4.21.2.tgz", @@ -878,15 +779,6 @@ "url": "https://opencollective.com/express" } }, - "node_modules/ext": { - "version": "1.7.0", - "resolved": "https://registry.npmjs.org/ext/-/ext-1.7.0.tgz", - "integrity": "sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==", - "license": "ISC", - "dependencies": { - "type": "^2.7.2" - } - }, "node_modules/fill-range": { "version": "7.1.1", "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", @@ -1192,12 +1084,6 @@ "node": ">=0.12.0" } }, - "node_modules/is-promise": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.2.2.tgz", - "integrity": "sha512-+lP4/6lKUBfQjZ2pdxThZvLUAafmZb8OAxFb8XXtiQmS35INgr85hdOGoEs124ez1FCnZJt6jau/T+alh58QFQ==", - "license": "MIT" - }, "node_modules/js-yaml": { "version": "4.1.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.1.0.tgz", @@ -1240,15 +1126,6 @@ "integrity": "sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==", "license": "ISC" }, - "node_modules/lru-queue": { - "version": "0.1.0", - "resolved": "https://registry.npmjs.org/lru-queue/-/lru-queue-0.1.0.tgz", - "integrity": "sha512-BpdYkt9EvGl8OfWHDQPISVpcl5xZthb+XPsbELj5AQXxIC8IriDZIQYjBJPEm5rS420sjZ0TLEzRcq5KdBhYrQ==", - "license": "MIT", - "dependencies": { - "es5-ext": "~0.10.2" - } - }, "node_modules/make-error": { "version": "1.3.6", "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", @@ -1302,25 +1179,6 @@ "node": ">= 0.6" } }, - "node_modules/memoizee": { - "version": "0.4.17", - "resolved": "https://registry.npmjs.org/memoizee/-/memoizee-0.4.17.tgz", - "integrity": "sha512-DGqD7Hjpi/1or4F/aYAspXKNm5Yili0QDAFAY4QYvpqpgiY6+1jOfqpmByzjxbWd/T9mChbCArXAbDAsTm5oXA==", - "license": "ISC", - "dependencies": { - "d": "^1.0.2", - "es5-ext": "^0.10.64", - "es6-weak-map": "^2.0.3", - "event-emitter": "^0.3.5", - "is-promise": "^2.2.2", - "lru-queue": "^0.1.0", - "next-tick": "^1.1.0", - "timers-ext": "^0.1.7" - }, - "engines": { - "node": ">=0.12" - } - }, "node_modules/merge-descriptors": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.3.tgz", @@ -1420,12 +1278,6 @@ "node": ">= 0.6" } }, - "node_modules/next-tick": { - "version": "1.1.0", - "resolved": "https://registry.npmjs.org/next-tick/-/next-tick-1.1.0.tgz", - "integrity": "sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==", - "license": "ISC" - }, "node_modules/nodemon": { "version": "3.1.9", "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-3.1.9.tgz", @@ -2021,19 +1873,6 @@ "express": ">=4.0.0 || >=5.0.0-beta" } }, - "node_modules/timers-ext": { - "version": "0.1.8", - "resolved": "https://registry.npmjs.org/timers-ext/-/timers-ext-0.1.8.tgz", - "integrity": "sha512-wFH7+SEAcKfJpfLPkrgMPvvwnEtj8W4IurvEyrKsDleXnKLCDw71w8jltvfLa8Rm4qQxxT4jmDBYbJG/z7qoww==", - "license": "ISC", - "dependencies": { - "es5-ext": "^0.10.64", - "next-tick": "^1.1.0" - }, - "engines": { - "node": ">=0.12" - } - }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", @@ -2134,12 +1973,6 @@ "node": ">= 6.0.0" } }, - "node_modules/type": { - "version": "2.7.3", - "resolved": "https://registry.npmjs.org/type/-/type-2.7.3.tgz", - "integrity": "sha512-8j+1QmAbPvLZow5Qpi6NCaN8FB60p/6x8/vfNqOk/hC+HuvFZhL4+WfekuhQLiqFZXOgQdrs3B+XxEmCc6b3FQ==", - "license": "ISC" - }, "node_modules/type-is": { "version": "1.6.18", "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", diff --git a/server/package.json b/server/package.json index d550a8e..d2fde30 100644 --- a/server/package.json +++ b/server/package.json @@ -10,10 +10,9 @@ }, "keywords": [], "author": "Hendrik Schutter, Philipp Schweizer", - "license": "ISC", + "license": "MIT", "devDependencies": { "@types/express": "^5.0.0", - "@types/memoizee": "^0.4.11", "@types/node": "^22.10.2", "nodemon": "^3.1.9", "ts-node": "^10.9.2", @@ -25,7 +24,6 @@ "express": "^4.21.2", "http-status-codes": "^2.3.0", "mariadb": "^3.4.0", - "memoizee": "^0.4.17", "reflect-metadata": "^0.2.2", "sequelize": "^6.37.5", "swagger-jsdoc": "^6.2.8", diff --git a/server/sql/tables.sql b/server/sql/tables.sql index 276b639..e416098 100644 --- a/server/sql/tables.sql +++ b/server/sql/tables.sql @@ -23,8 +23,10 @@ CREATE TABLE IF NOT EXISTS wifi_scan ( CREATE TABLE IF NOT EXISTS wifi_location ( mac VARCHAR(255) PRIMARY KEY, - latitude DOUBLE NOT NULL, - longitude DOUBLE NOT NULL, + latitude DOUBLE, + longitude DOUBLE, + request_limit_exceeded boolean NOT NULL, + location_not_resolvable boolean NOT NULL, created_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at_utc TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ); diff --git a/server/src/middleware/authentificationMiddleware.ts b/server/src/middleware/authentificationMiddleware.ts index a6629a4..3b96b21 100644 --- a/server/src/middleware/authentificationMiddleware.ts +++ b/server/src/middleware/authentificationMiddleware.ts @@ -29,8 +29,7 @@ export function authenticateHeader(req: Request, res: Response, next: NextFuncti res.status(StatusCodes.UNAUTHORIZED).json({ error: "Authentication failed" }); return; } - console.log("Bearer token is correct!"); - + //console.log("Bearer token is correct!"); next(); } catch (error) { res.status(StatusCodes.INTERNAL_SERVER_ERROR) diff --git a/server/src/models/wifiLocation.ts b/server/src/models/wifiLocation.ts index 1c74299..f109f74 100644 --- a/server/src/models/wifiLocation.ts +++ b/server/src/models/wifiLocation.ts @@ -5,6 +5,8 @@ export class WifiLocation extends Model { public mac!: string; public latitude!: number; public longitude!: number; + public request_limit_exceeded!: boolean; + public location_not_resolvable!: boolean; public created_at_utc!: Date; public updated_at_utc!: Date; } @@ -18,11 +20,17 @@ WifiLocation.init( }, latitude: { type: DataTypes.NUMBER, - allowNull: false, }, longitude: { type: DataTypes.NUMBER, - allowNull: false, + }, + request_limit_exceeded: { + type: DataTypes.BOOLEAN, + defaultValue: false, + }, + location_not_resolvable: { + type: DataTypes.BOOLEAN, + defaultValue: false, }, created_at_utc: { type: DataTypes.DATE, diff --git a/server/src/proxy/wigle.ts b/server/src/proxy/wigle.ts index 68155d8..5bc5747 100644 --- a/server/src/proxy/wigle.ts +++ b/server/src/proxy/wigle.ts @@ -1,4 +1,7 @@ -import memoizee from "memoizee"; +interface WigleApiResonse { + response?: WifiLocationResponse, + status_code: number, +} interface WifiLocationResponse { success: boolean; @@ -43,7 +46,7 @@ interface Result { export const getLocationForWifi = async ( mac: string -): Promise => { +): Promise => { try { const url = `${process.env.WIGLE_BASE_URL!}${process.env .WIGLE_NETWORK_SEARCH!}?netid=${encodeURIComponent(mac)}`; @@ -57,17 +60,12 @@ export const getLocationForWifi = async ( }); if (response.ok) { - return await response.json(); + return { status_code: response.status, response: await response.json() }; } console.log(response.status); - return undefined; + return { status_code: response.status }; } catch (error) { console.error("Error during call of API wigle.net:", error); + return undefined; } }; - -export const getLocationForWifiMemoized = memoizee(getLocationForWifi, { - maxAge: Number(process.env.GET_LOCATION_WIFI_MAX_AGE), - max: Number(process.env.GET_LOCATION_WIFI_MAX), - primitive: process.env.GET_LOCATION_WIFI_PRIMITIVE === "true", -}); diff --git a/server/src/services/locationService.ts b/server/src/services/locationService.ts index 31332a9..cb8f10b 100644 --- a/server/src/services/locationService.ts +++ b/server/src/services/locationService.ts @@ -46,7 +46,7 @@ export class LocationService { private repository: LocationRepository, @inject(WifiLocationService) private wifiLocationService: WifiLocationService - ) {} + ) { } public async getAllLocations() { return this.repository.findAll(); @@ -104,10 +104,10 @@ export class LocationService { return location?.latitude !== undefined && location.longitude !== undefined ? { - rssi: wifi.rssi, - latitude: location.latitude, - longitude: location.longitude, - } + rssi: wifi.rssi, + latitude: location.latitude, + longitude: location.longitude, + } : null; }) ); diff --git a/server/src/services/wifiLocationHistoryService.ts b/server/src/services/wifiLocationHistoryService.ts index 7e2b59d..2062888 100644 --- a/server/src/services/wifiLocationHistoryService.ts +++ b/server/src/services/wifiLocationHistoryService.ts @@ -18,7 +18,7 @@ export class WifiLocationHistoryService { constructor( @inject(WifiLocationHistoryRepository) private repository: WifiLocationHistoryRepository - ) {} + ) { } public async getAllWifiLocationHistories() { return this.repository.findAll(); @@ -34,13 +34,11 @@ export class WifiLocationHistoryService { const existingEntry = await this.repository.findOne({ where: { mac: data.mac, - latitude: data.latitude, - longitude: data.longitude, }, order: [["updated_at_utc", "DESC"]], }); - if (!existingEntry) { + if (!existingEntry || existingEntry.latitude !== data.latitude || existingEntry.longitude !== data.longitude) { return await this.repository.create(data); } diff --git a/server/src/services/wifiLocationService.ts b/server/src/services/wifiLocationService.ts index df0fff0..2617db8 100644 --- a/server/src/services/wifiLocationService.ts +++ b/server/src/services/wifiLocationService.ts @@ -3,6 +3,7 @@ import { inject, injectable } from "tsyringe"; import { getLocationForWifi } from "../proxy/wigle"; import { WifiLocationRepository } from "../repositories/wifiLocationRepository"; import { WifiLocationHistoryService } from "./wifiLocationHistoryService"; +import { StatusCodes } from "http-status-codes"; interface UpdateWifiLocationParams { mac: string; @@ -16,7 +17,7 @@ export class WifiLocationService { @inject(WifiLocationRepository) private repository: WifiLocationRepository, @inject(WifiLocationHistoryService) private wifiLocationHistory: WifiLocationHistoryService - ) {} + ) { } public async getAllWifiLocations() { return this.repository.findAll(); @@ -31,25 +32,42 @@ export class WifiLocationService { public async getWifiLocationByMac(mac: string) { let wifiLocation = await this.repository.findById(mac); - if (wifiLocation?.longitude && wifiLocation.latitude) return wifiLocation; + if (wifiLocation) return wifiLocation; - const apiLocation = await getLocationForWifi(mac); + const apiResponse = await getLocationForWifi(mac); - if (apiLocation && apiLocation.totalResults >= 0) { - wifiLocation = await this.repository.create({ + if (apiResponse == undefined) { + await this.repository.create({ mac, - latitude: apiLocation.results[0].trilat, - longitude: apiLocation.results[0].trilong, }); - - await this.wifiLocationHistory.createWifiLocationHistory( - wifiLocation.dataValues - ); - - return wifiLocation; + return undefined; } - return undefined; + if (apiResponse.response == undefined) { + const request_limit_exceeded = apiResponse.status_code === StatusCodes.TOO_MANY_REQUESTS; + await this.repository.create({ + mac, + request_limit_exceeded, + }); + return undefined; + } + + if (apiResponse.response.totalResults == 0) { + await this.repository.create({ mac, location_not_resolvable: true }); + return undefined; + } + + wifiLocation = await this.repository.create({ + mac, + latitude: apiResponse.response.results[0].trilat, + longitude: apiResponse.response.results[0].trilong, + }); + + await this.wifiLocationHistory.createWifiLocationHistory( + wifiLocation.dataValues + ); + + return wifiLocation; } public async updateWifiLocation(data: UpdateWifiLocationParams) {