1
0
Fork 0
peertube/server/lib/signup.ts

76 lines
2.3 KiB
TypeScript
Raw Normal View History

2021-08-27 08:32:44 -04:00
import { IPv4, IPv6, parse, subnetMatch } from 'ipaddr.js'
2019-04-11 05:33:44 -04:00
import { CONFIG } from '../initializers/config'
2021-08-27 08:32:44 -04:00
import { UserModel } from '../models/user/user'
2019-04-11 05:33:44 -04:00
2018-08-14 09:28:30 -04:00
const isCidr = require('is-cidr')
2023-01-19 03:27:16 -05:00
export type SignupMode = 'direct-registration' | 'request-registration'
async function isSignupAllowed (options: {
signupMode: SignupMode
ip: string // For plugins
body?: any
}): Promise<{ allowed: boolean, errorMessage?: string }> {
const { signupMode } = options
2018-08-14 09:28:30 -04:00
if (CONFIG.SIGNUP.ENABLED === false) {
2019-10-25 07:54:32 -04:00
return { allowed: false }
2018-08-14 09:28:30 -04:00
}
2023-01-19 03:27:16 -05:00
if (signupMode === 'direct-registration' && CONFIG.SIGNUP.REQUIRES_APPROVAL === true) {
return { allowed: false }
}
2018-08-14 09:28:30 -04:00
// No limit and signup is enabled
if (CONFIG.SIGNUP.LIMIT === -1) {
2019-10-25 07:54:32 -04:00
return { allowed: true }
2018-08-14 09:28:30 -04:00
}
const totalUsers = await UserModel.countTotal()
2019-10-25 07:54:32 -04:00
return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT }
2018-08-14 09:28:30 -04:00
}
function isSignupAllowedForCurrentIP (ip: string) {
2021-04-09 08:51:28 -04:00
if (!ip) return false
2021-08-27 08:32:44 -04:00
const addr = parse(ip)
2020-01-31 10:56:52 -05:00
const excludeList = [ 'blacklist' ]
2018-08-14 09:28:30 -04:00
let matched = ''
// if there is a valid, non-empty whitelist, we exclude all unknown addresses too
2018-08-14 09:28:30 -04:00
if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
excludeList.push('unknown')
}
if (addr.kind() === 'ipv4') {
2021-08-27 08:32:44 -04:00
const addrV4 = IPv4.parse(ip)
2018-08-14 09:28:30 -04:00
const rangeList = {
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
2021-08-27 08:32:44 -04:00
.map(cidr => IPv4.parseCIDR(cidr)),
2018-08-14 09:28:30 -04:00
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
2021-08-27 08:32:44 -04:00
.map(cidr => IPv4.parseCIDR(cidr))
2018-08-14 09:28:30 -04:00
}
2021-08-27 08:32:44 -04:00
matched = subnetMatch(addrV4, rangeList, 'unknown')
2018-08-14 09:28:30 -04:00
} else if (addr.kind() === 'ipv6') {
2021-08-27 08:32:44 -04:00
const addrV6 = IPv6.parse(ip)
2018-08-14 09:28:30 -04:00
const rangeList = {
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
2021-08-27 08:32:44 -04:00
.map(cidr => IPv6.parseCIDR(cidr)),
2018-08-14 09:28:30 -04:00
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
2021-08-27 08:32:44 -04:00
.map(cidr => IPv6.parseCIDR(cidr))
2018-08-14 09:28:30 -04:00
}
2021-08-27 08:32:44 -04:00
matched = subnetMatch(addrV6, rangeList, 'unknown')
2018-08-14 09:28:30 -04:00
}
return !excludeList.includes(matched)
}
// ---------------------------------------------------------------------------
export {
isSignupAllowed,
isSignupAllowedForCurrentIP
}