From 1d3bc34b91c8071692978bc2c9323ede6b898b58 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 28 Feb 2023 20:22:14 -0600 Subject: [PATCH] Add noop and hellthread policies --- .vscode/extensions.json | 5 +++++ .vscode/settings.json | 4 ++++ LICENSE | 24 ++++++++++++++++++++ README.md | 9 ++++++++ deno.json | 27 ++++++++++++++++++++++ hellthread-policy.ts | 50 +++++++++++++++++++++++++++++++++++++++++ noop-policy.ts | 38 +++++++++++++++++++++++++++++++ 7 files changed, 157 insertions(+) create mode 100644 .vscode/extensions.json create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 deno.json create mode 100755 hellthread-policy.ts create mode 100755 noop-policy.ts diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..09cf720 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,5 @@ +{ + "recommendations": [ + "denoland.vscode-deno" + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..173811e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "deno.enable": true, + "path-intellisense.extensionOnImport": true +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..3c577b0 --- /dev/null +++ b/LICENSE @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR +OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +OTHER DEALINGS IN THE SOFTWARE. + +For more information, please refer to \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..b993bac --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# strfry policies + +A collection of policies for the [strfry](https://github.com/hoytech/strfry) relay, written in Deno. + +For more information about installing these policies and how they work, see [Write policy plugins](https://github.com/hoytech/strfry/blob/master/docs/plugins.md). + +## License + +This is free and unencumbered software released into the public domain. diff --git a/deno.json b/deno.json new file mode 100644 index 0000000..0b7b419 --- /dev/null +++ b/deno.json @@ -0,0 +1,27 @@ +{ + "lock": false, + "tasks": { + }, + "lint": { + "files": { + "include": ["."] + }, + "rules": { + "tags": ["recommended"], + "exclude": ["no-explicit-any"] + } + }, + "fmt": { + "files": { + "include": ["."] + }, + "options": { + "useTabs": false, + "lineWidth": 120, + "indentWidth": 2, + "semiColons": true, + "singleQuote": true, + "proseWrap": "preserve" + } + } +} diff --git a/hellthread-policy.ts b/hellthread-policy.ts new file mode 100755 index 0000000..1f67eb7 --- /dev/null +++ b/hellthread-policy.ts @@ -0,0 +1,50 @@ +#!/usr/bin/env -S deno run +import { readLines } from 'https://deno.land/std@0.178.0/io/mod.ts'; + +const HELLTHREAD_LIMIT = Number(Deno.env.get('HELLTHREAD_LIMIT') || 20); + +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 { + const p = msg.event.tags.filter((tag) => tag[0] === 'p'); + + if (p.length > HELLTHREAD_LIMIT) { + return { + id: msg.event.id, + action: 'reject', + msg: `Event rejected due to ${p.length} "p" tags (${HELLTHREAD_LIMIT} is the limit).`, + }; + } else { + return { + id: msg.event.id, + action: 'accept', + msg: '', + }; + } +} + +for await (const line of readLines(Deno.stdin)) { + console.log(JSON.stringify(handleMessage(JSON.parse(line)))); +} diff --git a/noop-policy.ts b/noop-policy.ts new file mode 100755 index 0000000..e3361df --- /dev/null +++ b/noop-policy.ts @@ -0,0 +1,38 @@ +#!/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; +} + +function handleMessage(msg: InputMessage): OutputMessage { + return { + id: msg.event.id, + action: 'accept', + msg: '', + }; +} + +for await (const line of readLines(Deno.stdin)) { + console.log(JSON.stringify(handleMessage(JSON.parse(line)))); +}