diff --git a/src/policies/anti-duplication-policy.ts b/src/policies/anti-duplication-policy.ts index c51c3e0..7bb032f 100755 --- a/src/policies/anti-duplication-policy.ts +++ b/src/policies/anti-duplication-policy.ts @@ -3,33 +3,11 @@ import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts'; +import type { InputMessage, OutputMessage } from '../types.ts'; + const ANTI_DUPLICATION_TTL = Number(Deno.env.get('ANTI_DUPLICATION_TTL') || 60000); const ANTI_DUPLICATION_MIN_LENGTH = Number(Deno.env.get('ANTI_DUPLICATION_MIN_LENGTH') || 50); -interface InputMessage { - type: 'new' | 'lookback'; - event: Event; - receivedAt: number; - sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; - sourceInfo: string; -} - -interface OutputMessage { - id: string; - action: 'accept' | 'reject' | 'shadowReject'; - msg: string; -} - -interface Event { - id: string; - sig: string; - kind: number; - tags: string[][]; - pubkey: string; - content: string; - created_at: number; -} - /** https://stackoverflow.com/a/8831937 */ function hashCode(str: string): number { let hash = 0; diff --git a/src/policies/hellthread-policy.ts b/src/policies/hellthread-policy.ts index d89f9c9..2120dbf 100755 --- a/src/policies/hellthread-policy.ts +++ b/src/policies/hellthread-policy.ts @@ -1,32 +1,10 @@ #!/usr/bin/env -S deno run import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; +import type { InputMessage, OutputMessage } from '../types.ts'; + const HELLTHREAD_LIMIT = Number(Deno.env.get('HELLTHREAD_LIMIT') || 100); -interface InputMessage { - type: 'new' | 'lookback'; - event: Event; - receivedAt: number; - sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; - sourceInfo: string; -} - -interface OutputMessage { - id: string; - action: 'accept' | 'reject' | 'shadowReject'; - msg: string; -} - -interface Event { - id: string; - sig: string; - kind: number; - tags: string[][]; - pubkey: string; - content: string; - created_at: number; -} - function handleMessage(msg: InputMessage): OutputMessage { if (msg.event.kind === 1) { const p = msg.event.tags.filter((tag) => tag[0] === 'p'); diff --git a/src/policies/noop-policy.ts b/src/policies/noop-policy.ts index e3361df..912ed46 100755 --- a/src/policies/noop-policy.ts +++ b/src/policies/noop-policy.ts @@ -1,29 +1,7 @@ #!/usr/bin/env -S deno run import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; -interface InputMessage { - type: 'new' | 'lookback'; - event: Event; - receivedAt: number; - sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; - sourceInfo: string; -} - -interface OutputMessage { - id: string; - action: 'accept' | 'reject' | 'shadowReject'; - msg: string; -} - -interface Event { - id: string; - sig: string; - kind: number; - tags: string[][]; - pubkey: string; - content: string; - created_at: number; -} +import type { InputMessage, OutputMessage } from '../types.ts'; function handleMessage(msg: InputMessage): OutputMessage { return { diff --git a/src/policies/rate-limit-policy.ts b/src/policies/rate-limit-policy.ts index 54da6ec..1e14f2f 100755 --- a/src/policies/rate-limit-policy.ts +++ b/src/policies/rate-limit-policy.ts @@ -3,41 +3,19 @@ import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; import { Keydb } from 'https://deno.land/x/keydb@1.0.0/sqlite.ts'; +import type { InputMessage, OutputMessage } from '../types.ts'; + const IP_WHITELIST = (Deno.env.get('IP_WHITELIST') || '').split(','); const RATE_LIMIT_INTERVAL = Number(Deno.env.get('RATE_LIMIT_INTERVAL') || 60000); const RATE_LIMIT_MAX = Number(Deno.env.get('RATE_LIMIT_MAX') || 10); -interface InputMessage { - type: 'new' | 'lookback'; - event: Event; - receivedAt: number; - sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; - sourceInfo: string; -} - -interface OutputMessage { - id: string; - action: 'accept' | 'reject' | 'shadowReject'; - msg: string; -} - -interface Event { - id: string; - sig: string; - kind: number; - tags: string[][]; - pubkey: string; - content: string; - created_at: number; -} - async function handleMessage(msg: InputMessage): Promise { if ((msg.sourceType === 'IP4' || msg.sourceType === 'IP6') && !IP_WHITELIST.includes(msg.sourceInfo)) { const db = new Keydb('sqlite:///tmp/strfry-rate-limit-policy.sqlite3'); const count = await db.get(msg.sourceInfo) || 0; await db.set(msg.sourceInfo, count + 1, RATE_LIMIT_INTERVAL); - + if (count >= RATE_LIMIT_MAX) { return { id: msg.event.id, diff --git a/src/policies/read-only-policy.ts b/src/policies/read-only-policy.ts index f1e11af..998dcc6 100755 --- a/src/policies/read-only-policy.ts +++ b/src/policies/read-only-policy.ts @@ -2,29 +2,7 @@ //bin/true; exec deno run -A "$0" "$@" import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; -interface InputMessage { - type: 'new' | 'lookback'; - event: Event; - receivedAt: number; - sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; - sourceInfo: string; -} - -interface OutputMessage { - id: string; - action: 'accept' | 'reject' | 'shadowReject'; - msg: string; -} - -interface Event { - id: string; - sig: string; - kind: number; - tags: string[][]; - pubkey: string; - content: string; - created_at: number; -} +import type { InputMessage, OutputMessage } from '../types.ts'; function handleMessage(msg: InputMessage): OutputMessage { return { diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..473a3f7 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,25 @@ +interface InputMessage { + type: 'new' | 'lookback'; + event: Event; + receivedAt: number; + sourceType: 'IP4' | 'IP6' | 'Import' | 'Stream' | 'Sync'; + sourceInfo: string; +} + +interface OutputMessage { + id: string; + action: 'accept' | 'reject' | 'shadowReject'; + msg: string; +} + +interface Event { + id: string; + sig: string; + kind: K; + tags: string[][]; + pubkey: string; + content: string; + created_at: number; +} + +export type { Event, InputMessage, OutputMessage };