]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/check.ts
Add delete button to my videos
[github/Chocobozzz/PeerTube.git] / scripts / check.ts
1 import * as series from 'async/series'
2 import * as request from 'request'
3 import * as WebSocket from 'ws'
4
5 import { CONFIG } from '../server/initializers/constants'
6
7 const requestHeaders = {
8 'Range': '',
9 'Keep-Alive': '',
10 'User-Agent': 'Mozilla',
11 'Cache-Control': 'no-cache',
12 'Content-Type': '',
13 'Host': 'example.com',
14 'Access-Control-Request-Method': 'GET',
15 'Access-Control-Request-Headers': 'range'
16 }
17
18 series([
19 pingServer,
20 checkCORSTorrent,
21 checkCORSWebSeed,
22 checkTracker
23 ], function () {
24 process.exit(0)
25 })
26
27 // ---------------------------------------------------------------------------
28
29 function pingServer (callback: () => void) {
30 const pingUrl = CONFIG.WEBSERVER.URL + '/api/v1/ping'
31 console.log('Checking server is up (%s)...', pingUrl)
32
33 request(pingUrl, function (err, res, body) {
34 if (!err && res.statusCode === 200 && body === 'pong') {
35 console.log('SUCCESS.')
36 } else {
37 console.log('FAIL.')
38 }
39
40 callback()
41 })
42 }
43
44 function checkCORSTorrent (callback: () => void) {
45 const torrentUrl = CONFIG.WEBSERVER.URL + '/static/torrents/test.torrent'
46 console.log('Checking CORS headers for the torrent (%s)...', torrentUrl)
47
48 request({
49 method: 'OPTIONS',
50 uri: torrentUrl,
51 headers: requestHeaders
52 }, function (err, res) {
53 if (err || isThereValidCORSHeaders(res) === false) {
54 console.error('FAIL.')
55 } else {
56 console.log('SUCCESS.')
57 }
58
59 callback()
60 })
61 }
62
63 function checkCORSWebSeed (callback: () => void) {
64 const webseedUrl = CONFIG.WEBSERVER.URL + '/static/webseed/test.mp4'
65 console.log('Checking CORS headers for the video (%s)...', webseedUrl)
66
67 request({
68 method: 'OPTIONS',
69 uri: webseedUrl,
70 headers: requestHeaders
71 }, function (err, res) {
72 if (err || isThereValidCORSHeaders(res) === false) {
73 console.error('FAIL.')
74 } else {
75 console.log('SUCCESS.')
76 }
77
78 callback()
79 })
80 }
81
82 function checkTracker (callback: () => void) {
83 const trackerUrl = CONFIG.WEBSERVER.WS + '://' +
84 CONFIG.WEBSERVER.HOST +
85 '/tracker/socket'
86 console.log('Checking tracker websocket (%s)...', trackerUrl)
87
88 let ws = null
89 ws = new WebSocket(trackerUrl)
90
91 const timeout = setTimeout(failed, 1000)
92 ws.on('open', onOpen)
93
94 function onOpen () {
95 clearTimeout(timeout)
96 ws.close()
97
98 console.log('SUCCESS.')
99 callback()
100 }
101
102 function failed () {
103 ws.removeListener('open', onOpen)
104 ws.close()
105
106 console.log('FAILED.')
107 callback()
108 }
109 }
110
111 function isThereValidCORSHeaders (res: request.RequestResponse) {
112 let fail = false
113
114 // Check Access-Control-Allow-Origin
115 const headerAllowOriginKey = 'access-control-allow-origin'
116 const headerAllowOrigin = res.headers[headerAllowOriginKey]
117
118 if (!headerAllowOrigin) {
119 console.error(headerAllowOriginKey + ' is not present.')
120 fail = true
121 } else if (headerAllowOrigin !== '*') {
122 console.error(headerAllowOriginKey + ' does not equal "*".')
123 fail = true
124 }
125
126 // Check Access-Control-Allow-Methods
127 const headerAllowMethodsKey = 'access-control-allow-methods'
128 const headerAllowMethods = res.headers[headerAllowMethodsKey]
129 if (!headerAllowMethods) {
130 console.error(headerAllowMethodsKey + ' is not present.')
131 fail = true
132 } else {
133 const allowMethodsMissed = findPatternNotInString(headerAllowMethods as string, [ 'get' ])
134 if (allowMethodsMissed !== null) {
135 console.error(headerAllowMethodsKey + ' misses the ' + allowMethodsMissed + ' method.')
136 fail = true
137 }
138 }
139
140 // Check Access-Control-Allow-Headers
141 const headerAllowHeadersKey = 'access-control-allow-headers'
142 const headerAllowHeaders = res.headers[headerAllowHeadersKey]
143 if (!headerAllowHeaders) {
144 console.error(headerAllowHeadersKey + ' is not present.')
145 fail = true
146 } else {
147 const headersThatShouldBePresent = [
148 'Range'
149 ]
150 const allowHeadersMissed = findPatternNotInString(headerAllowHeaders as string, headersThatShouldBePresent)
151 if (allowHeadersMissed !== null) {
152 console.error(headerAllowHeadersKey + ' misses the ' + allowHeadersMissed + ' header.')
153 fail = true
154 }
155 }
156
157 return !fail
158 }
159
160 function findPatternNotInString (stringChain: string, patterns: string[]) {
161 let res = null
162 const stringChainLowerCase = stringChain.toLowerCase()
163
164 patterns.forEach(function (pattern) {
165 if (stringChainLowerCase.indexOf(pattern.toLowerCase()) === -1) {
166 res = pattern
167 }
168 })
169
170 return res
171 }