5 const series = require('async/series')
6 const request = require('request')
7 const WebSocket = require('ws')
9 const constants = require('../server/initializers/constants')
10 const requestHeaders = {
13 'User-Agent': 'Mozilla',
14 'Cache-Control': 'no-cache',
16 'Host': 'example.com',
17 'Access-Control-Request-Method': 'GET',
18 'Access-Control-Request-Headers': 'range'
30 // ---------------------------------------------------------------------------
32 function pingServer (callback) {
33 const pingUrl = constants.CONFIG.WEBSERVER.URL + '/api/v1/ping'
34 console.log('Checking server is up (%s)...', pingUrl)
36 request(pingUrl, function (err, res, body) {
37 if (!err && res.statusCode === 200 && body === 'pong') {
38 console.log('SUCCESS.')
47 function checkCORSTorrent (callback) {
48 const torrentUrl = constants.CONFIG.WEBSERVER.URL + '/static/torrents/test.torrent'
49 console.log('Checking CORS headers for the torrent (%s)...', torrentUrl)
54 headers: requestHeaders
55 }, function (err, res) {
56 if (err || isThereValidCORSHeaders(res) === false) {
57 console.error('FAIL.')
59 console.log('SUCCESS.')
66 function checkCORSWebSeed (callback) {
67 const webseedUrl = constants.CONFIG.WEBSERVER.URL + '/static/webseed/test.mp4'
68 console.log('Checking CORS headers for the video (%s)...', webseedUrl)
73 headers: requestHeaders
74 }, function (err, res) {
75 if (err || isThereValidCORSHeaders(res) === false) {
76 console.error('FAIL.')
78 console.log('SUCCESS.')
85 function checkTracker (callback) {
86 const trackerUrl = constants.CONFIG.WEBSERVER.WS + '://' +
87 constants.CONFIG.WEBSERVER.HOST +
89 console.log('Checking tracker websocket (%s)...', trackerUrl)
92 ws = new WebSocket(trackerUrl)
94 const timeout = setTimeout(failed, 1000)
101 console.log('SUCCESS.')
106 ws.removeListener('open', onOpen)
109 console.log('FAILED.')
114 function isThereValidCORSHeaders (res) {
117 // Check Access-Control-Allow-Origin
118 const headerAllowOriginKey = 'access-control-allow-origin'
119 const headerAllowOrigin = res.headers[headerAllowOriginKey]
121 if (!headerAllowOrigin) {
122 console.error(headerAllowOriginKey + ' is not present.')
124 } else if (headerAllowOrigin !== '*') {
125 console.error(headerAllowOriginKey + ' does not equal "*".')
129 // Check Access-Control-Allow-Methods
130 const headerAllowMethodsKey = 'access-control-allow-methods'
131 const headerAllowMethods = res.headers[headerAllowMethodsKey]
132 if (!headerAllowMethods) {
133 console.error(headerAllowMethodsKey + ' is not present.')
136 const allowMethodsMissed = findPatternNotInString(headerAllowMethods, [ 'get' ])
137 if (allowMethodsMissed !== null) {
138 console.error(headerAllowMethodsKey + ' misses the ' + allowMethodsMissed + ' method.')
143 // Check Access-Control-Allow-Headers
144 const headerAllowHeadersKey = 'access-control-allow-headers'
145 const headerAllowHeaders = res.headers[headerAllowHeadersKey]
146 if (!headerAllowHeaders) {
147 console.error(headerAllowHeadersKey + ' is not present.')
150 const headersThatShouldBePresent = [
153 const allowHeadersMissed = findPatternNotInString(headerAllowHeaders, headersThatShouldBePresent)
154 if (allowHeadersMissed !== null) {
155 console.error(headerAllowHeadersKey + ' misses the ' + allowHeadersMissed + ' header.')
163 function findPatternNotInString (stringChain, patterns) {
165 const stringChainLowerCase = stringChain.toLowerCase()
167 patterns.forEach(function (pattern) {
168 if (stringChainLowerCase.indexOf(pattern.toLowerCase()) === -1) {