]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/signup.ts
Bumped to version v5.2.1
[github/Chocobozzz/PeerTube.git] / server / lib / signup.ts
CommitLineData
41fb13c3 1import { IPv4, IPv6, parse, subnetMatch } from 'ipaddr.js'
6dd9de95 2import { CONFIG } from '../initializers/config'
41fb13c3 3import { UserModel } from '../models/user/user'
6dd9de95 4
06215f15
C
5const isCidr = require('is-cidr')
6
e364e31e
C
7export type SignupMode = 'direct-registration' | 'request-registration'
8
9async 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
06215f15 17 if (CONFIG.SIGNUP.ENABLED === false) {
4e9a9866 18 return { allowed: false, errorMessage: 'User registration is not allowed' }
06215f15
C
19 }
20
e364e31e 21 if (signupMode === 'direct-registration' && CONFIG.SIGNUP.REQUIRES_APPROVAL === true) {
4e9a9866 22 return { allowed: false, errorMessage: 'User registration requires approval' }
e364e31e
C
23 }
24
06215f15
C
25 // No limit and signup is enabled
26 if (CONFIG.SIGNUP.LIMIT === -1) {
4ce7eb71 27 return { allowed: true }
06215f15
C
28 }
29
30 const totalUsers = await UserModel.countTotal()
31
4e9a9866 32 return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT, errorMessage: 'User limit is reached on this instance' }
06215f15
C
33}
34
35function isSignupAllowedForCurrentIP (ip: string) {
22820226
C
36 if (!ip) return false
37
41fb13c3 38 const addr = parse(ip)
a1587156 39 const excludeList = [ 'blacklist' ]
06215f15
C
40 let matched = ''
41
7a4fd56c 42 // if there is a valid, non-empty whitelist, we exclude all unknown addresses too
06215f15
C
43 if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
44 excludeList.push('unknown')
45 }
46
47 if (addr.kind() === 'ipv4') {
41fb13c3 48 const addrV4 = IPv4.parse(ip)
06215f15
C
49 const rangeList = {
50 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
41fb13c3 51 .map(cidr => IPv4.parseCIDR(cidr)),
06215f15 52 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
41fb13c3 53 .map(cidr => IPv4.parseCIDR(cidr))
06215f15 54 }
41fb13c3 55 matched = subnetMatch(addrV4, rangeList, 'unknown')
06215f15 56 } else if (addr.kind() === 'ipv6') {
41fb13c3 57 const addrV6 = IPv6.parse(ip)
06215f15
C
58 const rangeList = {
59 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
41fb13c3 60 .map(cidr => IPv6.parseCIDR(cidr)),
06215f15 61 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
41fb13c3 62 .map(cidr => IPv6.parseCIDR(cidr))
06215f15 63 }
41fb13c3 64 matched = subnetMatch(addrV6, rangeList, 'unknown')
06215f15
C
65 }
66
67 return !excludeList.includes(matched)
68}
69
70// ---------------------------------------------------------------------------
71
72export {
73 isSignupAllowed,
74 isSignupAllowedForCurrentIP
75}