aboutsummaryrefslogtreecommitdiffhomepage
path: root/scripts/check.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-06-12 21:06:32 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-06-12 21:06:32 +0200
commit75d612ce3ca9d6f69fe8e4e83dc3070d9ab56615 (patch)
tree016f64d0fc6cfee533516be40d76eaca1b1f837e /scripts/check.ts
parent7593a21d13baad422efedfb658d7ceda4e0a8c6b (diff)
downloadPeerTube-75d612ce3ca9d6f69fe8e4e83dc3070d9ab56615.tar.gz
PeerTube-75d612ce3ca9d6f69fe8e4e83dc3070d9ab56615.tar.zst
PeerTube-75d612ce3ca9d6f69fe8e4e83dc3070d9ab56615.zip
Convert scripts to typescript
Diffstat (limited to 'scripts/check.ts')
-rwxr-xr-xscripts/check.ts171
1 files changed, 171 insertions, 0 deletions
diff --git a/scripts/check.ts b/scripts/check.ts
new file mode 100755
index 000000000..28167482c
--- /dev/null
+++ b/scripts/check.ts
@@ -0,0 +1,171 @@
1import * as series from 'async/series'
2import * as request from 'request'
3import * as WebSocket from 'ws'
4
5import { CONFIG } from '../server/initializers/constants'
6
7const 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
18series([
19 pingServer,
20 checkCORSTorrent,
21 checkCORSWebSeed,
22 checkTracker
23], function () {
24 process.exit(0)
25})
26
27// ---------------------------------------------------------------------------
28
29function 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
44function 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
63function 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
82function 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
111function 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, [ '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, headersThatShouldBePresent)
151 if (allowHeadersMissed !== null) {
152 console.error(headerAllowHeadersKey + ' misses the ' + allowHeadersMissed + ' header.')
153 fail = true
154 }
155 }
156
157 return !fail
158}
159
160function 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}