1 import { Model } from 'sequelize-typescript'
2 import * as ipaddr from 'ipaddr.js'
3 const isCidr = require('is-cidr')
4 import { ResultList } from '../../shared'
5 import { VideoResolution } from '../../shared/models/videos'
6 import { CONFIG } from '../initializers'
7 import { UserModel } from '../models/account/user'
8 import { ActorModel } from '../models/activitypub/actor'
9 import { ApplicationModel } from '../models/application/application'
10 import { pseudoRandomBytesPromise } from './core-utils'
11 import { logger } from './logger'
13 async function generateRandomString (size: number) {
14 const raw = await pseudoRandomBytesPromise(size)
16 return raw.toString('hex')
19 interface FormattableToJSON {
23 function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
24 const formattedObjects: U[] = []
26 objects.forEach(object => {
27 formattedObjects.push(object.toFormattedJSON())
30 const res: ResultList<U> = {
32 data: formattedObjects
38 async function isSignupAllowed () {
39 if (CONFIG.SIGNUP.ENABLED === false) {
43 // No limit and signup is enabled
44 if (CONFIG.SIGNUP.LIMIT === -1) {
48 const totalUsers = await UserModel.countTotal()
50 return totalUsers < CONFIG.SIGNUP.LIMIT
53 function isSignupAllowedForCurrentIP (ip: string) {
54 const addr = ipaddr.parse(ip)
55 let excludeList = [ 'blacklist' ]
58 // if there is a valid, non-empty whitelist, we exclude all unknown adresses too
59 if (CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr(cidr)).length > 0) {
60 excludeList.push('unknown')
63 if (addr.kind() === 'ipv4') {
64 const addrV4 = ipaddr.IPv4.parse(ip)
66 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v4(cidr))
67 .map(cidr => ipaddr.IPv4.parseCIDR(cidr)),
68 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v4(cidr))
69 .map(cidr => ipaddr.IPv4.parseCIDR(cidr))
71 matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown')
72 } else if (addr.kind() === 'ipv6') {
73 const addrV6 = ipaddr.IPv6.parse(ip)
75 whitelist: CONFIG.SIGNUP.FILTERS.CIDR.WHITELIST.filter(cidr => isCidr.v6(cidr))
76 .map(cidr => ipaddr.IPv6.parseCIDR(cidr)),
77 blacklist: CONFIG.SIGNUP.FILTERS.CIDR.BLACKLIST.filter(cidr => isCidr.v6(cidr))
78 .map(cidr => ipaddr.IPv6.parseCIDR(cidr))
80 matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown')
83 return !excludeList.includes(matched)
86 function computeResolutionsToTranscode (videoFileHeight: number) {
87 const resolutionsEnabled: number[] = []
88 const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS
91 VideoResolution.H_240P,
92 VideoResolution.H_360P,
93 VideoResolution.H_480P,
94 VideoResolution.H_720P,
95 VideoResolution.H_1080P
98 for (const resolution of resolutions) {
99 if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) {
100 resolutionsEnabled.push(resolution)
104 return resolutionsEnabled
107 function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
108 Object.keys(savedFields).forEach(key => {
109 const value = savedFields[key]
110 instance.set(key, value)
114 let serverActor: ActorModel
115 async function getServerActor () {
116 if (serverActor === undefined) {
117 const application = await ApplicationModel.load()
118 serverActor = application.Account.Actor
122 logger.error('Cannot load server actor.')
126 return Promise.resolve(serverActor)
129 type SortType = { sortModel: any, sortValue: string }
131 // ---------------------------------------------------------------------------
134 generateRandomString,
137 isSignupAllowedForCurrentIP,
138 computeResolutionsToTranscode,
139 resetSequelizeInstance,