Skip to content

Commit

Permalink
Separate KV version.
Browse files Browse the repository at this point in the history
  • Loading branch information
Natsuki-Kaede committed Feb 19, 2024
1 parent cc8de2e commit 9009440
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 6 deletions.
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,36 @@ This is an spam filter for Misskey API and ActivityPub inbox designed to operate

## Deployment

### great-pari-wall (Recommended) (No bad words filter)

(The bad words filter is no longer effective and cost a lot.)

You can customize the error messages and value of limits in index.js.

0. Install Node.js and Wrangler `npm install wrangler --save-dev`
1. `npx wrangler kv:namespace create KV` Create a Key-Value (KV) store.
2. With the assigned `id`, replace `kv_namespaces.id`(YOUR_KV_ID) in `wrangler.toml`.
3. `pnpm run deploy` Deploy.
4. Register prohibited words in the KV store with the key `badWords` (multiple entries can be separated by `;`)
5. Add a route to Workers Routes.
1. `pnpm run deploy` Deploy.
2. Add a route to Workers Routes.
- `[YourDomain]/api/notes/create`
- `[YourDomain]/api/i/update`
- `[YourDomain]/inbox`
- `[YourDomain]/users/*`

### great-pari-wall-withKV

- Install Node.js and Wrangler `npm install wrangler --save-dev`
- `npx wrangler kv:namespace create KV` Create a Key-Value (KV) store.
- With the assigned `id`, replace `kv_namespaces.id`(YOUR_KV_ID) in `wrangler.toml`.
- `pnpm run deploy` Deploy.
- Register prohibited words in the KV store with the key `badWords` (multiple entries can be separated by `;`)
- Add a route to Workers Routes.
- `[YourDomain]/api/notes/create`
- `[YourDomain]/api/i/update`
- `[YourDomain]/inbox`
- `[YourDomain]/users/*`

## Customize KV

By setting additional keys in KV, you can customize the behavior of the Great Pari Wall.
By setting additional keys in KV, you can customize the behavior of the Great Pari Wall (withKV).

| Key | Value | Examples |
| ------------ | ------------------------------------------------------------------------------------------------- | -------- |
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions great-pari-wall/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"scripts": {
"deploy": "wrangler deploy --minify src/index.ts"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240208.0",
"wrangler": "^3.25.0"
}
}
60 changes: 60 additions & 0 deletions great-pari-wall/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
export default {
async fetch(request: Request) {
const ccLimit = 4;
const atLimit = 3;

// If the request method is HEAD or GET, return it as it is.
if (request.method === 'HEAD' || request.method === 'GET') {
return await fetch(request.url, {
method: request.method,
headers: request.headers,
});
}

const body = await request.text();

try {
const bodyJson = JSON.parse(body);
const cc = bodyJson.cc?.length ?? 0;

// Check if mentions exceed the limit
const mentions = (bodyJson.text || '').match(/@.*?@.*?\..*?/g) || [];

if (mentions.length > atLimit) {
return new Response(JSON.stringify({
error: {
message: 'Too many Ats.',
code: 'TOO_MANY_ATS',
id: 'c7e10ff1-042f-441a-b490-836956560650',
}
}), {
// Note: Returning a 400 in ActivityPub may result in repeated retries from the remote or, in the worst case, delivery suspension. Therefore, return a 202 for 'inbox'.
status: request.url.includes('inbox') ? 202 : 400,
});
}

if (cc > ccLimit) {
return new Response(JSON.stringify({
error: {
message: 'Too many mentions.',
code: 'TOO_MANY_MENTIONS',
id: 'c7e10ff1-042f-441a-b490-836956560650',
}
}), {
// Note: Returning a 400 in ActivityPub may lead to repeated retries from the remote or, in the worst case, delivery suspension. Therefore, return a 202 in the case of 'inbox'.
status: request.url.includes('inbox') ? 202 : 400,
});
}
} catch (e) {
// do nothing
}

// No badWords filtering

return await fetch(request.url, {
method: 'POST',
headers: request.headers,
body,
});
},
};
16 changes: 16 additions & 0 deletions great-pari-wall/src/libs/match.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { regexpMatch } from "./regexpMatch";
import { simpleMatch } from "./simpleMatch";

/**
* Determines whether [word] is included in [text] or matches as a regular expression.
* @param text The text to be evaluated.
* @param word The word or regular expression pattern to check for. If enclosed in /, it is treated as a regular expression.
* @returns True if there is a match, false otherwise.
*/
export const match = (text: string, word: string) => {
if (word.startsWith('/') && word.endsWith('/')) {
return regexpMatch(text, word);
} else {
return simpleMatch(text, word);
}
};
20 changes: 20 additions & 0 deletions great-pari-wall/src/libs/regexpMatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Determines whether [text] matches the regular expression [pattern].
* @param text
* @param pattern
* @returns True if there is a match, false otherwise.
*/

export const regexpMatch = (text: string, pattern: string) => {
try {
const regexp = new RegExp(pattern.slice(1, -1));
return regexp.test(text);
} catch (e) {
if (e instanceof SyntaxError) {
// If the regular expression is invalid, it will be treated as a non-match.
return false;
} else {
throw e;
}
}
};
9 changes: 9 additions & 0 deletions great-pari-wall/src/libs/simpleMatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Determines partial matching of a simple string.
* @param text
* @param word
* @returns True if there is a match, false otherwise.
*/
export const simpleMatch = (text: string, word: string) => {
return text.includes(word);
};
15 changes: 15 additions & 0 deletions great-pari-wall/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"lib": [
"ESNext"
],
"types": [
"@cloudflare/workers-types"
],
"jsx": "react-jsx"
},
}
10 changes: 10 additions & 0 deletions great-pari-wall/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name = "workers-block-ngword-in-post-body"
compatibility_date = "2023-12-01"
main = "./src/index.js"

# [vars]
# MY_VARIABLE = "production_value"

[[kv_namespaces]]
binding = "KV"
id = "YOUR_KV_ID"

0 comments on commit 9009440

Please sign in to comment.