update redirect to use geolite2 for geolocation

main
TZGyn 1 year ago
parent 9363f9bd7e
commit 933703ee4c
Signed by: TZGyn
GPG Key ID: 122EAF77AE81FD4A

@ -46,6 +46,17 @@ bun run src/index.ts
## Breaking Changes ## 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) ### 16 June 2024 (For builds before this date)
(WARNING) Please backup your database before attempting (WARNING) Please backup your database before attempting

@ -1,3 +1,5 @@
FALLBACK_URL=https://app.kon.sh FALLBACK_URL=https://app.kon.sh
DATABASE_URL=postgres://postgres:password@0.0.0.0:5432/link-shortener DATABASE_URL=postgres://postgres:password@0.0.0.0:5432/link-shortener
APP_URL=kon.sh APP_URL=kon.sh
GEOIPUPDATE_ACCOUNT_ID=
GEOIPUPDATE_LICENSE_KEY=

Binary file not shown.

@ -7,6 +7,7 @@
}, },
"dependencies": { "dependencies": {
"@elysiajs/cors": "^0.6.0", "@elysiajs/cors": "^0.6.0",
"@maxmind/geoip2-node": "^5.0.0",
"@types/pg": "^8.10.2", "@types/pg": "^8.10.2",
"@types/ua-parser-js": "^0.7.39", "@types/ua-parser-js": "^0.7.39",
"elysia": "latest", "elysia": "latest",

@ -5,6 +5,8 @@ import { UAParser } from 'ua-parser-js'
const fallback_url = Bun.env.FALLBACK_URL ?? 'https://app.kon.sh' const fallback_url = Bun.env.FALLBACK_URL ?? 'https://app.kon.sh'
const app_url = Bun.env.APP_URL ?? '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()) const app = new Elysia().use(cors())
@ -20,9 +22,16 @@ app.get(
const ip = request.headers.get('x-forwarded-for') const ip = request.headers.get('x-forwarded-for')
const geolocation = await ( const WebServiceClient =
await fetch(`https://api.ipbase.com/v2/info?ip=${ip}`) require('@maxmind/geoip2-node').WebServiceClient
).json()
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') const user_agent = request.headers.get('User-Agent')
@ -30,19 +39,23 @@ app.get(
const query = db const query = db
.selectFrom('shortener') .selectFrom('shortener')
.leftJoin('project', 'project.id', 'shortener.project_id')
.selectAll('shortener') .selectAll('shortener')
.select(['project.custom_domain as domain'])
.where('shortener.code', '=', shortenerCode) .where('shortener.code', '=', shortenerCode)
.where('project.custom_domain', '=', domain)
.orderBy('created_at', 'desc')
if (domain) { 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() const shortener = await query.execute()
console.log('shortener', shortener)
if (!shortener.length || !shortener[0].active) { if (!shortener.length || !shortener[0].active) {
set.redirect = '/invalid' set.redirect = '/invalid'
return return
@ -50,10 +63,9 @@ app.get(
const visitor_data = { const visitor_data = {
shortener_id: shortener[0].id, shortener_id: shortener[0].id,
country: geolocation.data.location.country.name as string, country: geolocation.country.names.en as string,
country_code: geolocation.data.location.country country_code: geolocation.country.isoCode as string,
.alpha2 as string, city: geolocation.city.names.en as string,
city: geolocation.data.location.city.name as string,
device_type: ua_parser.getDevice().type || 'desktop', device_type: ua_parser.getDevice().type || 'desktop',
device_vendor: ua_parser.getDevice().vendor, device_vendor: ua_parser.getDevice().vendor,
browser: ua_parser.getBrowser().name, browser: ua_parser.getBrowser().name,

Loading…
Cancel
Save