plugins: api.addBlock
Massimo Melina committed
Jun 25, 2024 at 09:53 UTC
693b93bd43efab1b582b4ab5d9ead4b2384e5bfd
5 files changed
+23
-8
admin/src/useBlockIp.ts
+1
-1
@@ -16,7 +16,7 @@ export function useBlockIp() {
16
...isBlocked(ip) && { disabled: true, title: "Blocked" },
17
...options,
18
onClick() {
19
- return apiCall('add_block', { ip, comment, merge: { comment } })
19
+ return apiCall('add_block', { ip, merge: { comment } })
20
.then(reload).then(() => toast("Blocked", 'success'))
21
},
22
}),
dev-plugins.md
+9
-1
@@ -171,6 +171,13 @@ The `api` object you get as parameter of the `init` contains the following:
171
172
- `log(...args)` print log in a standard form for plugins.
173
174
+- `addBlock({ ip, expire?, comment?, disabled? }, merge?)` add a blocking rule on specified IP. You can use merge to
175
+ append the IP to an existing rule (if any, otherwise is created). Eg:
176
+ ```js
177
+ // try to append to existing rule, by comment
178
+ addBlock({ ip: '1.2.3.4' }, { comment: "banned by my plugin" })
179
+ ```
180
+
181
- `Const: object` all constants of the `const.ts` file are exposed here. E.g. BUILD_TIMESTAMP, API_VERSION, etc.
182
183
- `getConnections(): Connections[]` retrieve current list of active connections.
@@ -614,7 +621,8 @@ If you want to override a text regardless of the language, use the special langu
621
## API version history
622
623
- 8.9 (v0.54.0)
617
- - frontend event: showPlay
624
+ - frontend event: showPlay
625
+ - api.addBlock
626
- 8.891 (v0.53.0)
627
- api.openDb
628
- frontend event: menuZip
src/adminApis.ts
+2
-6
@@ -36,7 +36,7 @@ import { ip2country } from './geo'
36
import { roots } from './roots'
37
import { SendListReadable } from './SendList'
38
import { get_dynamic_dns_error } from './ddns'
39
-import { block, BlockingRule } from './block'
39
+import { addBlock, BlockingRule } from './block'
40
41
export const adminApis = {
42
@@ -143,11 +143,7 @@ export const adminApis = {
143
string_undefined: { comment, expire },
144
object_undefined: { merge },
145
})
146
- block.set(was => {
147
- const found = merge && _.findIndex(was, merge)
148
- return found ? was.map((x, i) => i === found ? { ...x, ip: `${x.ip}|${ip}` } : x)
149
- : [...was, { ip, expire, comment }]
150
- })
146
+ addBlock({ ip, expire, comment }, merge)
147
return {}
148
}
149
src/block.ts
+9
@@ -4,6 +4,7 @@ import { defineConfig } from './config'
4
import { disconnect, getConnections, normalizeIp } from './connections'
5
import { makeNetMatcher, MINUTE, onlyTruthy } from './misc'
6
import { Socket } from 'net'
7
+import _ from 'lodash'
8
9
export interface BlockingRule { ip: string, comment?: string, expire?: Date, disabled?: boolean }
10
@@ -33,3 +34,11 @@ setInterval(() => { // twice a minute, check if any block has expired
34
console.log("blocking rules:", n, "expired")
35
block.set(next)
36
}, MINUTE/2)
37
+
38
+export function addBlock(rule: BlockingRule, merge?: Partial<BlockingRule>) {
39
+ block.set(was => {
40
+ const found = merge && _.findIndex(was, merge)
41
+ return found ? was.map((x, i) => i === found ? { ...x, ...rule, ip: `${x.ip}|${rule.ip}` } : x)
42
+ : [...was, { ...merge, ...rule }]
43
+ })
44
+}
\ No newline at end of file
src/plugins.ts
+2
@@ -25,6 +25,7 @@ import { KvStorage, KvStorageOptions } from '@rejetto/kvstorage'
25
import { onProcessExit } from './first'
26
import { notifyClient } from './frontEndApis'
27
import { app } from './index'
28
+import { addBlock } from './block'
29
30
export const PATH = 'plugins'
31
export const DISABLING_SUFFIX = '-disabled'
@@ -113,6 +114,7 @@ async function initPlugin<T>(pl: any, morePassedToInit?: T) {
114
getHfsConfig: getConfig,
115
customApiCall,
116
notifyClient,
117
+ addBlock,
118
...morePassedToInit
119
}))
120
}