1 import * as series from 'async/series'
2 import * as request from 'request'
3 import * as WebSocket from 'ws'
5 import { CONFIG } from '../server/initializers/constants'
7 const requestHeaders = {
10 'User-Agent': 'Mozilla',
11 'Cache-Control': 'no-cache',
13 'Host': 'example.com',
14 'Access-Control-Request-Method': 'GET',
15 'Access-Control-Request-Headers': 'range'
27 // ---------------------------------------------------------------------------
29 function pingServer (callback: () => void) {
30 const pingUrl = CONFIG.WEBSERVER.URL + '/api/v1/ping'
31 console.log('Checking server is up (%s)...', pingUrl)
33 request(pingUrl, function (err, res, body) {
34 if (!err && res.statusCode === 200 && body === 'pong') {
35 console.log('SUCCESS.')
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)
51 headers: requestHeaders
52 }, function (err, res) {
53 if (err || isThereValidCORSHeaders(res) === false) {
54 console.error('FAIL.')
56 console.log('SUCCESS.')
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)
70 headers: requestHeaders
71 }, function (err, res) {
72 if (err || isThereValidCORSHeaders(res) === false) {
73 console.error('FAIL.')
75 console.log('SUCCESS.')
82 function checkTracker (callback: () => void) {
83 const trackerUrl = CONFIG.WEBSERVER.WS + '://' +
84 CONFIG.WEBSERVER.HOST +
86 console.log('Checking tracker websocket (%s)...', trackerUrl)
89 ws = new WebSocket(trackerUrl)
91 const timeout = setTimeout(failed, 1000)
98 console.log('SUCCESS.')
103 ws.removeListener('open', onOpen)
106 console.log('FAILED.')
111 function isThereValidCORSHeaders (res: request.RequestResponse) {
114 // Check Access-Control-Allow-Origin
115 const headerAllowOriginKey = 'access-control-allow-origin'
116 const headerAllowOrigin = res.headers[headerAllowOriginKey]
118 if (!headerAllowOrigin) {
119 console.error(headerAllowOriginKey + ' is not present.')
121 } else if (headerAllowOrigin !== '*') {
122 console.error(headerAllowOriginKey + ' does not equal "*".')
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.')
133 const allowMethodsMissed = findPatternNotInString(headerAllowMethods as string, [ 'get' ])
134 if (allowMethodsMissed !== null) {
135 console.error(headerAllowMethodsKey + ' misses the ' + allowMethodsMissed + ' method.')
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.')
147 const headersThatShouldBePresent = [
150 const allowHeadersMissed = findPatternNotInString(headerAllowHeaders as string, headersThatShouldBePresent)
151 if (allowHeadersMissed !== null) {
152 console.error(headerAllowHeadersKey + ' misses the ' + allowHeadersMissed + ' header.')
160 function findPatternNotInString (stringChain: string, patterns: string[]) {
162 const stringChainLowerCase = stringChain.toLowerCase()
164 patterns.forEach(function (pattern) {
165 if (stringChainLowerCase.indexOf(pattern.toLowerCase()) === -1) {