diff options
author | Chocobozzz <me@florianbigard.com> | 2023-07-31 14:34:36 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2023-08-11 15:02:33 +0200 |
commit | 3a4992633ee62d5edfbb484d9c6bcb3cf158489d (patch) | |
tree | e4510b39bdac9c318fdb4b47018d08f15368b8f0 /server/lib/signup.ts | |
parent | 04d1da5621d25d59bd5fa1543b725c497bf5d9a8 (diff) | |
download | PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.gz PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.tar.zst PeerTube-3a4992633ee62d5edfbb484d9c6bcb3cf158489d.zip |
Migrate server to ESM
Sorry for the very big commit that may lead to git log issues and merge
conflicts, but it's a major step forward:
* Server can be faster at startup because imports() are async and we can
easily lazy import big modules
* Angular doesn't seem to support ES import (with .js extension), so we
had to correctly organize peertube into a monorepo:
* Use yarn workspace feature
* Use typescript reference projects for dependencies
* Shared projects have been moved into "packages", each one is now a
node module (with a dedicated package.json/tsconfig.json)
* server/tools have been moved into apps/ and is now a dedicated app
bundled and published on NPM so users don't have to build peertube
cli tools manually
* server/tests have been moved into packages/ so we don't compile
them every time we want to run the server
* Use isolatedModule option:
* Had to move from const enum to const
(https://www.typescriptlang.org/docs/handbook/enums.html#objects-vs-enums)
* Had to explictely specify "type" imports when used in decorators
* Prefer tsx (that uses esbuild under the hood) instead of ts-node to
load typescript files (tests with mocha or scripts):
* To reduce test complexity as esbuild doesn't support decorator
metadata, we only test server files that do not import server
models
* We still build tests files into js files for a faster CI
* Remove unmaintained peertube CLI import script
* Removed some barrels to speed up execution (less imports)
Diffstat (limited to 'server/lib/signup.ts')
-rw-r--r-- | server/lib/signup.ts | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/server/lib/signup.ts b/server/lib/signup.ts deleted file mode 100644 index 6702c22cb..000000000 --- a/server/lib/signup.ts +++ /dev/null | |||
@@ -1,75 +0,0 @@ | |||
1 | import { IPv4, IPv6, parse, subnetMatch } from 'ipaddr.js' | ||
2 | import { CONFIG } from '../initializers/config' | ||
3 | import { UserModel } from '../models/user/user' | ||
4 | |||
5 | const isCidr = require('is-cidr') | ||
6 | |||
7 | export type SignupMode = 'direct-registration' | 'request-registration' | ||
8 | |||
9 | async function isSignupAllowed (options: { | ||
10 | signupMode: SignupMode | ||
11 | |||
12 | ip: string // For plugins | ||
13 | body?: any | ||
14 | }): Promise<{ allowed: boolean, errorMessage?: string }> { | ||
15 | const { signupMode } = options | ||
16 | |||
17 | if (CONFIG.SIGNUP.ENABLED === false) { | ||
18 | return { allowed: false, errorMessage: 'User registration is not allowed' } | ||
19 | } | ||
20 | |||
21 | if (signupMode === 'direct-registration' && CONFIG.SIGNUP.REQUIRES_APPROVAL === true) { | ||
22 | return { allowed: false, errorMessage: 'User registration requires approval' } | ||
23 | } | ||
24 | |||
25 | // No limit and signup is enabled | ||
26 | if (CONFIG.SIGNUP.LIMIT === -1) { | ||
27 | return { allowed: true } | ||
28 | } | ||
29 | |||
30 | const totalUsers = await UserModel.countTotal() | ||
31 | |||
32 | return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT, errorMessage: 'User limit is reached on this instance' } | ||
33 | } | ||
34 | |||
35 | function isSignupAllowedForCurrentIP (ip: string) { | ||
36 | if (!ip) return false | ||
37 | |||
38 | const addr = parse(ip) | ||
39 | const excludeList = [ 'blacklist' ] | ||
40 | let matched = '' | ||
41 | |||
42 | // if there is a valid, non-empty whitelist, we exclude all unknown addresses too | ||
43 | if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) { | ||
44 | excludeList.push('unknown') | ||
45 | } | ||
46 | |||
47 | if (addr.kind() === 'ipv4') { | ||
48 | const addrV4 = IPv4.parse(ip) | ||
49 | const rangeList = { | ||
50 | whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr)) | ||
51 | .map(cidr => IPv4.parseCIDR(cidr)), | ||
52 | blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr)) | ||
53 | .map(cidr => IPv4.parseCIDR(cidr)) | ||
54 | } | ||
55 | matched = subnetMatch(addrV4, rangeList, 'unknown') | ||
56 | } else if (addr.kind() === 'ipv6') { | ||
57 | const addrV6 = IPv6.parse(ip) | ||
58 | const rangeList = { | ||
59 | whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr)) | ||
60 | .map(cidr => IPv6.parseCIDR(cidr)), | ||
61 | blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr)) | ||
62 | .map(cidr => IPv6.parseCIDR(cidr)) | ||
63 | } | ||
64 | matched = subnetMatch(addrV6, rangeList, 'unknown') | ||
65 | } | ||
66 | |||
67 | return !excludeList.includes(matched) | ||
68 | } | ||
69 | |||
70 | // --------------------------------------------------------------------------- | ||
71 | |||
72 | export { | ||
73 | isSignupAllowed, | ||
74 | isSignupAllowedForCurrentIP | ||
75 | } | ||