]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/utils.ts
feature: IP filtering on signup page
[github/Chocobozzz/PeerTube.git] / server / helpers / utils.ts
CommitLineData
3fd3ab2d 1import { Model } from 'sequelize-typescript'
ff2c1fe8
RK
2import * as ipaddr from 'ipaddr.js'
3const isCidr = require('is-cidr')
6fcd19ba 4import { ResultList } from '../../shared'
3fd3ab2d 5import { VideoResolution } from '../../shared/models/videos'
0626e7af 6import { CONFIG } from '../initializers'
3fd3ab2d 7import { UserModel } from '../models/account/user'
50d6de9c
C
8import { ActorModel } from '../models/activitypub/actor'
9import { ApplicationModel } from '../models/application/application'
8d468a16 10import { pseudoRandomBytesPromise } from './core-utils'
efc32059 11import { logger } from './logger'
cbe2f7c3 12
f5028693
C
13async function generateRandomString (size: number) {
14 const raw = await pseudoRandomBytesPromise(size)
15
16 return raw.toString('hex')
e4c87ec2
C
17}
18
40298b02 19interface FormattableToJSON {
0aef76c4 20 toFormattedJSON ()
154898b0
C
21}
22
40298b02 23function getFormattedObjects<U, T extends FormattableToJSON> (objects: T[], objectsTotal: number) {
0aef76c4 24 const formattedObjects: U[] = []
55fa55a9 25
075f16ca 26 objects.forEach(object => {
0aef76c4 27 formattedObjects.push(object.toFormattedJSON())
55fa55a9
C
28 })
29
6fcd19ba 30 const res: ResultList<U> = {
55fa55a9 31 total: objectsTotal,
0aef76c4 32 data: formattedObjects
55fa55a9 33 }
6fcd19ba
C
34
35 return res
55fa55a9
C
36}
37
f5028693 38async function isSignupAllowed () {
291e8d3e 39 if (CONFIG.SIGNUP.ENABLED === false) {
f5028693 40 return false
291e8d3e
C
41 }
42
43 // No limit and signup is enabled
44 if (CONFIG.SIGNUP.LIMIT === -1) {
f5028693 45 return true
291e8d3e
C
46 }
47
3fd3ab2d 48 const totalUsers = await UserModel.countTotal()
f5028693
C
49
50 return totalUsers < CONFIG.SIGNUP.LIMIT
291e8d3e
C
51}
52
ff2c1fe8
RK
53function 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
40298b02
C
86function 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) {
fd206f0b 99 if (configResolutions[resolution + 'p'] === true && videoFileHeight > resolution) {
40298b02
C
100 resolutionsEnabled.push(resolution)
101 }
102 }
103
104 return resolutionsEnabled
105}
106
3fd3ab2d 107function resetSequelizeInstance (instance: Model<any>, savedFields: object) {
eb080476
C
108 Object.keys(savedFields).forEach(key => {
109 const value = savedFields[key]
110 instance.set(key, value)
111 })
112}
113
50d6de9c
C
114let serverActor: ActorModel
115async function getServerActor () {
116 if (serverActor === undefined) {
117 const application = await ApplicationModel.load()
118 serverActor = application.Account.Actor
7a7724e6
C
119 }
120
50d6de9c
C
121 if (!serverActor) {
122 logger.error('Cannot load server actor.')
efc32059
C
123 process.exit(0)
124 }
125
50d6de9c 126 return Promise.resolve(serverActor)
7a7724e6
C
127}
128
792dbaf0
GS
129type SortType = { sortModel: any, sortValue: string }
130
9f10b292 131// ---------------------------------------------------------------------------
c45f7f84 132
65fcc311 133export {
65fcc311 134 generateRandomString,
0aef76c4 135 getFormattedObjects,
792dbaf0 136 isSignupAllowed,
ff2c1fe8 137 isSignupAllowedForCurrentIP,
40298b02 138 computeResolutionsToTranscode,
eb080476 139 resetSequelizeInstance,
50d6de9c 140 getServerActor,
0626e7af 141 SortType
65fcc311 142}