@stamhoofd/redirecter 2.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.template.json +4 -0
- package/.eslintrc.js +61 -0
- package/README.md +1 -0
- package/index.ts +77 -0
- package/package.json +19 -0
- package/src/classes/Geolocator.ts +81 -0
- package/src/data/belgium.csv +225 -0
- package/src/data/netherlands.csv +979 -0
- package/src/endpoints/RedirectEndpoint.ts +58 -0
- package/stamhoofd.d.ts +15 -0
- package/tsconfig.json +37 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { DecodedRequest, Endpoint, Request, Response } from '@simonbackx/simple-endpoints'
|
|
2
|
+
import { Country } from '@stamhoofd/structures';
|
|
3
|
+
import { URL } from "url";
|
|
4
|
+
|
|
5
|
+
import { Geolocator } from '../classes/Geolocator';
|
|
6
|
+
|
|
7
|
+
type Params = Record<string, never>;
|
|
8
|
+
type Body = undefined
|
|
9
|
+
type Query = undefined
|
|
10
|
+
type ResponseBody = string
|
|
11
|
+
|
|
12
|
+
function getRequestIP(request: Request): string {
|
|
13
|
+
let ipAddress = request.request?.socket.remoteAddress;
|
|
14
|
+
if (request.headers["x-real-ip"] && typeof request.headers["x-real-ip"] == "string" && (ipAddress == "127.0.0.1" || ipAddress == "0.0.0.0")) {
|
|
15
|
+
ipAddress = request.headers["x-real-ip"];
|
|
16
|
+
}
|
|
17
|
+
if (!ipAddress) {
|
|
18
|
+
ipAddress = '?';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return ipAddress.split(":", 2)[0]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
export class RedirectEndpoint extends Endpoint<Params, Query, Body, ResponseBody> {
|
|
26
|
+
protected doesMatch(request: Request): [true, Params] | [false] {
|
|
27
|
+
return [true, {}]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async handle(request: DecodedRequest<Params, Query, Body>) {
|
|
31
|
+
const ip = getRequestIP(request.request)
|
|
32
|
+
|
|
33
|
+
const country = Geolocator.shared.getCountry(ip)
|
|
34
|
+
|
|
35
|
+
const path = request.request.request?.url ?? ""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
if (country === Country.Belgium) {
|
|
39
|
+
const url = "https://www.stamhoofd.be"+path
|
|
40
|
+
const response = new Response("Doorverwijzen naar "+url)
|
|
41
|
+
response.status = 302
|
|
42
|
+
response.headers["Location"] = url
|
|
43
|
+
|
|
44
|
+
// Prevent encoding and version requirement
|
|
45
|
+
response.headers["Content-Type"] = "text/html"
|
|
46
|
+
return Promise.resolve(response);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const url = "https://www.stamhoofd.nl"+path
|
|
50
|
+
const response = new Response("Doorverwijzen naar "+url)
|
|
51
|
+
response.status = 302
|
|
52
|
+
response.headers["Location"] = url
|
|
53
|
+
|
|
54
|
+
// Prevent encoding and version requirement
|
|
55
|
+
response.headers["Content-Type"] = "text/html"
|
|
56
|
+
return Promise.resolve(response);
|
|
57
|
+
}
|
|
58
|
+
}
|
package/stamhoofd.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
|
|
2
|
+
export {};
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Stamhoofd uses a global variable to store some configurations. We don't use process.env because we can only store
|
|
6
|
+
* strings into those files. And we need objects for our localized domains (different domains for each locale).
|
|
7
|
+
* Having to encode and decode those values would be inefficient.
|
|
8
|
+
*
|
|
9
|
+
* So we use our own global configuration variable: STAMHOOFD. Available everywhere and contains
|
|
10
|
+
* other information depending on the environment (frontend/backend/shared). TypeScript will
|
|
11
|
+
* always suggest the possible keys.
|
|
12
|
+
*/
|
|
13
|
+
declare global {
|
|
14
|
+
const STAMHOOFD: RedirecterEnvironment
|
|
15
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es2019", // needs to be es2019 to support optional chaining. Node.js doesn't support optional chaining yet, so we need the transpiling
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"jsx": "preserve",
|
|
6
|
+
"importHelpers": true,
|
|
7
|
+
"moduleResolution": "node",
|
|
8
|
+
"experimentalDecorators": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true,
|
|
11
|
+
"sourceMap": true,
|
|
12
|
+
"strictNullChecks": true,
|
|
13
|
+
"declaration": true,
|
|
14
|
+
"declarationMap": true,
|
|
15
|
+
"outDir": "dist",
|
|
16
|
+
"lib": [
|
|
17
|
+
"es2019",
|
|
18
|
+
"ES2021.String"
|
|
19
|
+
],
|
|
20
|
+
"types": [
|
|
21
|
+
"node",
|
|
22
|
+
"jest",
|
|
23
|
+
"@stamhoofd/backend-i18n",
|
|
24
|
+
]
|
|
25
|
+
},
|
|
26
|
+
"include": [
|
|
27
|
+
"src/**/*.ts",
|
|
28
|
+
"tests/**/*.ts",
|
|
29
|
+
"index.ts",
|
|
30
|
+
"migrations.ts",
|
|
31
|
+
"../../../*.d.ts",
|
|
32
|
+
"*.d.ts"
|
|
33
|
+
],
|
|
34
|
+
"exclude": [
|
|
35
|
+
"node_modules"
|
|
36
|
+
]
|
|
37
|
+
}
|