aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/signup.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-08-14 15:28:30 +0200
committerChocobozzz <me@florianbigard.com>2018-08-14 15:28:30 +0200
commit06215f15e0a9fea2ef95b8b49cb2b5868fb64017 (patch)
tree6f7ff9f82ea851ea87fd3fc4b61843c7a38aec43 /server/helpers/signup.ts
parent59c76ffa8f503e962d517c78f033f1beccb1de1a (diff)
downloadPeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.tar.gz
PeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.tar.zst
PeerTube-06215f15e0a9fea2ef95b8b49cb2b5868fb64017.zip
Cleanup utils helper
Diffstat (limited to 'server/helpers/signup.ts')
-rw-r--r--server/helpers/signup.ts59
1 files changed, 59 insertions, 0 deletions
diff --git a/server/helpers/signup.ts b/server/helpers/signup.ts
new file mode 100644
index 000000000..cdce7989d
--- /dev/null
+++ b/server/helpers/signup.ts
@@ -0,0 +1,59 @@
1import { CONFIG } from '../initializers'
2import { UserModel } from '../models/account/user'
3import * as ipaddr from 'ipaddr.js'
4const isCidr = require('is-cidr')
5
6async function isSignupAllowed () {
7 if (CONFIG.SIGNUP.ENABLED === false) {
8 return false
9 }
10
11 // No limit and signup is enabled
12 if (CONFIG.SIGNUP.LIMIT === -1) {
13 return true
14 }
15
16 const totalUsers = await UserModel.countTotal()
17
18 return totalUsers < CONFIG.SIGNUP.LIMIT
19}
20
21function isSignupAllowedForCurrentIP (ip: string) {
22 const addr = ipaddr.parse(ip)
23 let excludeList = [ 'blacklist' ]
24 let matched = ''
25
26 // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
27 if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
28 excludeList.push('unknown')
29 }
30
31 if (addr.kind() === 'ipv4') {
32 const addrV4 = ipaddr.IPv4.parse(ip)
33 const rangeList = {
34 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
35 .map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
36 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
37 .map(cidr => ipaddr.IPv4.parseCIDR(cidr))
38 }
39 matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
40 } else if (addr.kind() === 'ipv6') {
41 const addrV6 = ipaddr.IPv6.parse(ip)
42 const rangeList = {
43 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
44 .map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
45 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
46 .map(cidr => ipaddr.IPv6.parseCIDR(cidr))
47 }
48 matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
49 }
50
51 return !excludeList.includes(matched)
52}
53
54// ---------------------------------------------------------------------------
55
56export {
57 isSignupAllowed,
58 isSignupAllowedForCurrentIP
59}