2023-07-31 08:34:36 -04:00
|
|
|
import ipaddr from 'ipaddr.js'
|
|
|
|
import isCidr from 'is-cidr'
|
|
|
|
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'
|
|
|
|
|
|
|
|
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) {
|
2023-06-05 02:53:31 -04:00
|
|
|
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) {
|
2023-06-05 02:53:31 -04:00
|
|
|
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()
|
|
|
|
|
2023-06-05 02:53:31 -04:00
|
|
|
return { allowed: totalUsers < CONFIG.SIGNUP.LIMIT, errorMessage: 'User limit is reached on this instance' }
|
2018-08-14 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function isSignupAllowedForCurrentIP (ip: string) {
|
2021-04-09 08:51:28 -04:00
|
|
|
if (!ip) return false
|
|
|
|
|
2023-07-31 08:34:36 -04:00
|
|
|
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 = ''
|
|
|
|
|
Fix various typos
Found via `codespell -q 3 -S ./CREDITS.md,./CHANGELOG.md,./client/src/locale,./yarn.lock,./client/yarn.lock -L doubleclick,followings,nd,ot,ro,serie,splitted,tread,truthy`
2022-06-07 09:45:06 -04:00
|
|
|
// 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') {
|
2023-07-31 08:34:36 -04:00
|
|
|
const addrV4 = ipaddr.IPv4.parse(ip)
|
2018-08-14 09:28:30 -04:00
|
|
|
const rangeList = {
|
|
|
|
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
|
2023-07-31 08:34:36 -04:00
|
|
|
.map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
|
2018-08-14 09:28:30 -04:00
|
|
|
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
|
2023-07-31 08:34:36 -04:00
|
|
|
.map(cidr => ipaddr.IPv4.parseCIDR(cidr))
|
2018-08-14 09:28:30 -04:00
|
|
|
}
|
2023-07-31 08:34:36 -04:00
|
|
|
matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
|
2018-08-14 09:28:30 -04:00
|
|
|
} else if (addr.kind() === 'ipv6') {
|
2023-07-31 08:34:36 -04:00
|
|
|
const addrV6 = ipaddr.IPv6.parse(ip)
|
2018-08-14 09:28:30 -04:00
|
|
|
const rangeList = {
|
|
|
|
whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
|
2023-07-31 08:34:36 -04:00
|
|
|
.map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
|
2018-08-14 09:28:30 -04:00
|
|
|
blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
|
2023-07-31 08:34:36 -04:00
|
|
|
.map(cidr => ipaddr.IPv6.parseCIDR(cidr))
|
2018-08-14 09:28:30 -04:00
|
|
|
}
|
2023-07-31 08:34:36 -04:00
|
|
|
matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
|
2018-08-14 09:28:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return !excludeList.includes(matched)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
isSignupAllowed,
|
|
|
|
isSignupAllowedForCurrentIP
|
|
|
|
}
|