diff options
author | Chocobozzz <me@florianbigard.com> | 2018-08-14 15:28:30 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-08-14 15:28:30 +0200 |
commit | 06215f15e0a9fea2ef95b8b49cb2b5868fb64017 (patch) | |
tree | 6f7ff9f82ea851ea87fd3fc4b61843c7a38aec43 /server/helpers/signup.ts | |
parent | 59c76ffa8f503e962d517c78f033f1beccb1de1a (diff) | |
download | PeerTube-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.ts | 59 |
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 @@ | |||
1 | import { CONFIG } from '../initializers' | ||
2 | import { UserModel } from '../models/account/user' | ||
3 | import * as ipaddr from 'ipaddr.js' | ||
4 | const isCidr = require('is-cidr') | ||
5 | |||
6 | async 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 | |||
21 | function 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 | |||
56 | export { | ||
57 | isSignupAllowed, | ||
58 | isSignupAllowedForCurrentIP | ||
59 | } | ||