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

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>