diff --git a/README.md b/README.md index 6e3ba6e..10998b5 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,17 @@ bun run src/index.ts ## Breaking Changes +### 23 July 2024 (For builds before this date) + +Transition from using ipbase to using geoipupdate for geolocation. + +Please add the following environment variables to your .env file + +```bash +GEOIPUPDATE_ACCOUNT_ID= +GEOIPUPDATE_LICENSE_KEY= +``` + ### 16 June 2024 (For builds before this date) (WARNING) Please backup your database before attempting diff --git a/redirect/.env.example b/redirect/.env.example index 0eddd23..9c42531 100644 --- a/redirect/.env.example +++ b/redirect/.env.example @@ -1,3 +1,5 @@ FALLBACK_URL=https://app.kon.sh DATABASE_URL=postgres://postgres:password@0.0.0.0:5432/link-shortener -APP_URL=kon.sh \ No newline at end of file +APP_URL=kon.sh +GEOIPUPDATE_ACCOUNT_ID= +GEOIPUPDATE_LICENSE_KEY= diff --git a/redirect/bun.lockb b/redirect/bun.lockb index d80d83f..79e61a7 100755 Binary files a/redirect/bun.lockb and b/redirect/bun.lockb differ diff --git a/redirect/package.json b/redirect/package.json index b5ae92e..725a9f3 100644 --- a/redirect/package.json +++ b/redirect/package.json @@ -7,6 +7,7 @@ }, "dependencies": { "@elysiajs/cors": "^0.6.0", + "@maxmind/geoip2-node": "^5.0.0", "@types/pg": "^8.10.2", "@types/ua-parser-js": "^0.7.39", "elysia": "latest", diff --git a/redirect/src/index.ts b/redirect/src/index.ts index 7922f3f..e73a8ab 100644 --- a/redirect/src/index.ts +++ b/redirect/src/index.ts @@ -5,6 +5,8 @@ import { UAParser } from 'ua-parser-js' const fallback_url = Bun.env.FALLBACK_URL ?? 'https://app.kon.sh' const app_url = Bun.env.APP_URL ?? 'kon.sh' +const geoipupdate_account_id = Bun.env.GEOIPUPDATE_ACCOUNT_ID +const geoipupdate_license_key = Bun.env.GEOIPUPDATE_LICENSE_KEY const app = new Elysia().use(cors()) @@ -20,9 +22,16 @@ app.get( const ip = request.headers.get('x-forwarded-for') - const geolocation = await ( - await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`) - ).json() + const WebServiceClient = + require('@maxmind/geoip2-node').WebServiceClient + + const client = new WebServiceClient( + geoipupdate_account_id, + geoipupdate_license_key, + { host: 'geolite.info' } + ) + + const geolocation = await client.city(ip) const user_agent = request.headers.get('User-Agent') @@ -30,19 +39,23 @@ app.get( const query = db .selectFrom('shortener') - .leftJoin('project', 'project.id', 'shortener.project_id') .selectAll('shortener') - .select(['project.custom_domain as domain']) .where('shortener.code', '=', shortenerCode) - .where('project.custom_domain', '=', domain) - .orderBy('created_at', 'desc') if (domain) { - query.where('project.enable_custom_domain', '=', true) + query + .leftJoin('project', 'project.id', 'shortener.project_id') + .select(['project.custom_domain as domain']) + .where('project.custom_domain', '=', domain) + .where('project.enable_custom_domain', '=', true) } + query.orderBy('created_at', 'desc') + const shortener = await query.execute() + console.log('shortener', shortener) + if (!shortener.length || !shortener[0].active) { set.redirect = '/invalid' return @@ -50,10 +63,9 @@ app.get( const visitor_data = { shortener_id: shortener[0].id, - country: geolocation.data.location.country.name as string, - country_code: geolocation.data.location.country - .alpha2 as string, - city: geolocation.data.location.city.name as string, + country: geolocation.country.names.en as string, + country_code: geolocation.country.isoCode as string, + city: geolocation.city.names.en as string, device_type: ua_parser.getDevice().type || 'desktop', device_vendor: ua_parser.getDevice().vendor, browser: ua_parser.getBrowser().name,