]>
Commit | Line | Data |
---|---|---|
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' | |
12 | ||
13 | async function generateRandomString (size: number) { | |
14 | const raw = await pseudoRandomBytesPromise(size) | |
15 | ||
16 | return raw.toString('hex') | |
17 | } | |
18 | ||
19 | interface FormattableToJSON { | |
20 | toFormattedJSON () | |
21 | } | |
22 | ||
23 | function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) { | |
24 | const formattedObjects: U[] = [] | |
25 | ||
26 | objects.forEach(object => { | |
27 | formattedObjects.push(object.toFormattedJSON()) | |
28 | }) | |
29 | ||
30 | const res: ResultList<U> = { | |
31 | total: objectsTotal, | |
32 | data: formattedObjects | |
33 | } | |
34 | ||
35 | return res | |
36 | } | |
37 | ||
38 | async function isSignupAllowed () { | |
39 | if (CONFIG.SIGNUP.ENABLED === false) { | |
40 | return false | |
41 | } | |
42 | ||
43 | // No limit and signup is enabled | |
44 | if (CONFIG.SIGNUP.LIMIT === -1) { | |
45 | return true | |
46 | } | |
47 | ||
48 | const totalUsers = await UserModel.countTotal() | |
49 | ||
50 | return totalUsers < CONFIG.SIGNUP.LIMIT | |
51 | } | |
52 | ||
53 | function isSignupAllowedForCurrentIP (ip: string) { | |
54 | const addr = ipaddr.parse(ip) | |
55 | let excludeList = [ 'blacklist' ] | |
56 | let matched: string | |
57 | ||
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') | |
61 | } | |
62 | ||
63 | if (addr.kind() === 'ipv4') { | |
64 | const addrV4 = ipaddr.IPv4.parse(ip) | |
65 | const rangeList = { | |
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)) | |
70 | } | |
71 | matched = ipaddr.subnetMatch(addrV4, rangeList, 'unknown') | |
72 | } else if (addr.kind() === 'ipv6') { | |
73 | const addrV6 = ipaddr.IPv6.parse(ip) | |
74 | const rangeList = { | |
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)) | |
79 | } | |
80 | matched = ipaddr.subnetMatch(addrV6, rangeList, 'unknown') | |
81 | } | |
82 | ||
83 | return !excludeList.includes(matched) | |
84 | } | |
85 | ||
86 | function computeResolutionsToTranscode (videoFileHeight: number) { | |
87 | const resolutionsEnabled: number[] = [] | |
88 | const configResolutions = CONFIG.TRANSCODING.RESOLUTIONS | |
89 | ||
90 | const resolutions = [ | |
91 | VideoResolution.H_240P, | |
92 | VideoResolution.H_360P, | |
93 | VideoResolution.H_480P, | |
94 | VideoResolution.H_720P, | |
95 | VideoResolution.H_1080P | |
96 | ] | |
97 | ||
98 | for (const resolution of resolutions) { | |
99 | if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) { | |
100 | resolutionsEnabled.push(resolution) | |
101 | } | |
102 | } | |
103 | ||
104 | return resolutionsEnabled | |
105 | } | |
106 | ||
107 | function resetSequelizeInstance (instance: Model<any>, savedFields: object) { | |
108 | Object.keys(savedFields).forEach(key => { | |
109 | const value = savedFields[key] | |
110 | instance.set(key, value) | |
111 | }) | |
112 | } | |
113 | ||
114 | let serverActor: ActorModel | |
115 | async function getServerActor () { | |
116 | if (serverActor === undefined) { | |
117 | const application = await ApplicationModel.load() | |
118 | serverActor = application.Account.Actor | |
119 | } | |
120 | ||
121 | if (!serverActor) { | |
122 | logger.error('Cannot load server actor.') | |
123 | process.exit(0) | |
124 | } | |
125 | ||
126 | return Promise.resolve(serverActor) | |
127 | } | |
128 | ||
129 | type SortType = { sortModel: any, sortValue: string } | |
130 | ||
131 | // --------------------------------------------------------------------------- | |
132 | ||
133 | export { | |
134 | generateRandomString, | |
135 | getFormattedObjects, | |
136 | isSignupAllowed, | |
137 | isSignupAllowedForCurrentIP, | |
138 | computeResolutionsToTranscode, | |
139 | resetSequelizeInstance, | |
140 | getServerActor, | |
141 | SortType | |
142 | } |