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

89 lines
2.7 KiB
TypeScript
Raw Normal View History

import ipaddr from 'ipaddr.js'
import { CONFIG } from '../initializers/config.js'
import { UserModel } from '../models/user/user.js'
2018-08-14 09:28:30 -04:00
2023-01-19 03:27:16 -05:00
export type SignupMode = 'direct-registration' | 'request-registration'
2023-10-11 03:20:23 -04:00
export async function isSignupAllowed (options: {
2023-01-19 03:27:16 -05:00
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) {
return { allowed: false, errorMessage: 'User registration is not allowed' }
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, errorMessage: 'User registration requires approval' }
2023-01-19 03:27:16 -05:00
}
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()
return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT, errorMessage: 'User limit is reached on this instance' }
2018-08-14 09:28:30 -04:00
}
2023-10-11 03:20:23 -04:00
export function isSignupAllowedForCurrentIP (ip: string) {
2021-04-09 08:51:28 -04:00
if (!ip) return false
const addr = ipaddr.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
2023-10-11 09:58:16 -04:00
if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.some(cidr => isIPV4Cidr(cidr) || isIPV6Cidr(cidr))) {
2018-08-14 09:28:30 -04:00
excludeList.push('unknown')
}
if (addr.kind() === 'ipv4') {
const addrV4 = ipaddr.IPv4.parse(ip)
2018-08-14 09:28:30 -04:00
const rangeList = {
2023-10-11 03:20:23 -04:00
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isIPV4Cidr(cidr))
.map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
2023-10-11 03:20:23 -04:00
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isIPV4Cidr(cidr))
.map(cidr => ipaddr.IPv4.parseCIDR(cidr))
2018-08-14 09:28:30 -04:00
}
matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
2018-08-14 09:28:30 -04:00
} else if (addr.kind() === 'ipv6') {
const addrV6 = ipaddr.IPv6.parse(ip)
2018-08-14 09:28:30 -04:00
const rangeList = {
2023-10-11 03:20:23 -04:00
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isIPV6Cidr(cidr))
.map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
2023-10-11 03:20:23 -04:00
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isIPV6Cidr(cidr))
.map(cidr => ipaddr.IPv6.parseCIDR(cidr))
2018-08-14 09:28:30 -04:00
}
matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
2018-08-14 09:28:30 -04:00
}
return !excludeList.includes(matched)
}
2023-10-11 03:20:23 -04:00
// ---------------------------------------------------------------------------
// Private
2018-08-14 09:28:30 -04:00
// ---------------------------------------------------------------------------
2023-10-11 03:20:23 -04:00
function isIPV4Cidr (cidr: string) {
try {
ipaddr.IPv4.parseCIDR(cidr)
return true
} catch {
return false
}
}
function isIPV6Cidr (cidr: string) {
try {
ipaddr.IPv6.parseCIDR(cidr)
return true
} catch {
return false
}
2018-08-14 09:28:30 -04:00
}