Check ip range for public/private view.

This commit is contained in:
2023-09-20 11:24:10 +02:00
parent e58713b7ac
commit c05de10192
5 changed files with 50 additions and 8 deletions

17
package-lock.json generated
View File

@@ -10,6 +10,7 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^6.4.2",
"dotenv": "^16.3.1",
"ip-range-check": "^0.2.0",
"js-yaml": "^4.1.0"
},
"devDependencies": {
@@ -2588,6 +2589,22 @@
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==",
"dev": true
},
"node_modules/ip-range-check": {
"version": "0.2.0",
"resolved": "https://registry.npmjs.org/ip-range-check/-/ip-range-check-0.2.0.tgz",
"integrity": "sha512-oaM3l/3gHbLlt/tCWLvt0mj1qUaI+STuRFnUvARGCujK9vvU61+2JsDpmkMzR4VsJhuFXWWgeKKVnwwoFfzCqw==",
"dependencies": {
"ipaddr.js": "^1.0.1"
}
},
"node_modules/ipaddr.js": {
"version": "1.9.1",
"resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.1.tgz",
"integrity": "sha512-0KI/607xoxSToH7GjN1FfSbLoU0+btTicjsQSWQlh/hZykN8KpmMf7uYwPW3R+akZ6R/w18ZlXSHBYXiYUPO3g==",
"engines": {
"node": ">= 0.10"
}
},
"node_modules/is-binary-path": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz",

View File

@@ -44,6 +44,7 @@
"dependencies": {
"@fortawesome/fontawesome-free": "^6.4.2",
"dotenv": "^16.3.1",
"ip-range-check": "^0.2.0",
"js-yaml": "^4.1.0"
}
}

View File

@@ -9,6 +9,8 @@ import type {
ServiceGroupConfig
} from '$lib/config';
import * as ipRangeCheck from 'ip-range-check';
const requiredService: Required<ServiceConfig> = {
title: '',
subtitle: '',
@@ -19,7 +21,7 @@ const requiredService: Required<ServiceConfig> = {
target: '',
type: '',
tag: '',
keywords: [],
keywords: []
};
const requiredServiceGroup: Required<ServiceGroupConfig> = {
@@ -82,9 +84,8 @@ function merge<Type extends SPOJO>(a: Type, b: SPOJO): Type {
return res;
}
export function mergeConfig(a: Config, b: SPOJO): Config {
return merge<Config>(a,b);
return merge<Config>(a, b);
}
const defaultLightConfig: ColorConfig = {};
@@ -132,7 +133,7 @@ export const defaultConfig: Config = {
function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
const res: Type = { ...toStrip };
if ( reference == undefined ) {
if (reference == undefined) {
return res;
}
const referenceNames = Object.entries(reference).map(([key, value]) => key);
@@ -157,7 +158,6 @@ function strip<Type extends SPOJO>(toStrip: Type, reference: Type): Type {
continue;
}
// it is a child object, we strip it further
const childReference: SPOJO = reference[key] as SPOJO;
(res as SPOJO)[key] = strip(value as SPOJO, childReference);
@@ -173,6 +173,7 @@ export function stripPrivateFields(config: Config): Config {
if (config?.colors?.light != undefined) {
res.colors.light = config.colors.light;
}
res.f;
return res;
}
@@ -215,3 +216,15 @@ export function watchDymamicConfig() {
}
})();
}
export function clientAddressIsPrivate(clientAddress: string, config: Config): boolean {
const ranges: string[] = (config.privateIPs as string[]) || [
'192.168.0.0/16',
'100.64.0.0/10',
'::1',
'127.0.0.1',
'10.0.0.0/8'
];
return ipRangeCheck.default(clientAddress, ranges);
}

View File

@@ -1,4 +1,4 @@
import { clientConfig, serverConfig } from '$lib/server/config';
import { clientAddressIsPrivate, clientConfig, serverConfig } from '$lib/server/config';
import type { PageServerLoad } from './$types';
import { getServiceHandler } from '$lib/services/services';
@@ -22,5 +22,9 @@ export const load: PageServerLoad = ({ fetch, getClientAddress }) => {
}
}
return { config, serviceData, location: getClientAddress() };
return {
config,
serviceData,
privateAccess: clientAddressIsPrivate(getClientAddress(), privateConfig)
};
};

View File

@@ -19,7 +19,14 @@
</main>
<footer class="flex h-8 w-full flex-row">
<div class="mx-auto">Made with ♥ by atuleu. Your IP is {data.location}</div>
<div class="mx-auto">
Made with ♥ by atuleu.
{#if data.privateAccess === true}
Private view
{:else}
Public view
{/if}
</div>
</footer>
<style>