Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
2.1 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import type {IStorageService} from '@app/api/infrastructure/IStorageService';

const BLOCKLIST_S3_BUCKET = 'fluxer-geoip';
export const BLOCKLIST_FEED_CATEGORIES = ['urlhaus', 'phishtank'] as const;
export type BlocklistFeedCategory = (typeof BLOCKLIST_FEED_CATEGORIES)[number];
export const COMBINED_FEED_URLS_KEY = 'blocklists/feed-urls.txt';

export function feedUrlsKey(category: BlocklistFeedCategory): string {
	return `blocklists/feed-urls/${category}.txt`;
}

const MISSING_BLOCKLIST_OBJECT_ERRORS = new Set(['NoSuchBucket', 'NoSuchKey', 'NotFound']);

export function isMissingBlocklistObjectError(error: unknown): boolean {
	return error instanceof Error && MISSING_BLOCKLIST_OBJECT_ERRORS.has(error.name);
}

export async function writeLinesToS3(storage: IStorageService, key: string, lines: Iterable<string>): Promise<number> {
	let totalBytes = 0;
	const collected: Array<string> = [];
	for (const line of lines) {
		collected.push(line);
		totalBytes += Buffer.byteLength(line, 'utf8') + 1;
	}
	const buf = Buffer.allocUnsafe(totalBytes);
	let offset = 0;
	for (const line of collected) {
		offset += buf.write(line, offset, 'utf8');
		buf[offset++] = 0x0a;
	}
	await storage.uploadObject({
		bucket: BLOCKLIST_S3_BUCKET,
		key,
		body: buf,
		contentType: 'text/plain; charset=utf-8',
	});
	return collected.length;
}

export async function readLinesFromS3(storage: IStorageService, key: string): Promise<Array<string>> {
	try {
		const data = await storage.readObject(BLOCKLIST_S3_BUCKET, key);
		const text = Buffer.from(data).toString('utf8');
		const lines: Array<string> = [];
		let start = 0;
		for (let i = 0; i < text.length; i++) {
			if (text.charCodeAt(i) === 0x0a) {
				if (i > start) lines.push(text.slice(start, i));
				start = i + 1;
			}
		}
		if (start < text.length) lines.push(text.slice(start));
		return lines;
	} catch (error) {
		if (isMissingBlocklistObjectError(error)) return [];
		throw error;
	}
}

export async function deleteBlocklistS3Object(storage: IStorageService, key: string): Promise<void> {
	await storage.deleteObject(BLOCKLIST_S3_BUCKET, key);
}