1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | 'use strict';
|
7 |
|
8 | var fs = require('fs');
|
9 | var http = require('http');
|
10 | var url = require('url');
|
11 | var path = require('path');
|
12 | var debug = require('debug')('pm2:serve');
|
13 | var semver = require('semver')
|
14 |
|
15 | var isNode4 = require('semver').lt(process.version, '6.0.0')
|
16 |
|
17 | if (!isNode4) {
|
18 | var probe = require('@pm2/io');
|
19 | var errorMeter = probe.meter({
|
20 | name : '404/sec',
|
21 | samples : 1,
|
22 | timeframe : 60
|
23 | })
|
24 | }
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | var contentTypes = {
|
30 | '3gp': 'video/3gpp',
|
31 | 'a': 'application/octet-stream',
|
32 | 'ai': 'application/postscript',
|
33 | 'aif': 'audio/x-aiff',
|
34 | 'aiff': 'audio/x-aiff',
|
35 | 'asc': 'application/pgp-signature',
|
36 | 'asf': 'video/x-ms-asf',
|
37 | 'asm': 'text/x-asm',
|
38 | 'asx': 'video/x-ms-asf',
|
39 | 'atom': 'application/atom+xml',
|
40 | 'au': 'audio/basic',
|
41 | 'avi': 'video/x-msvideo',
|
42 | 'bat': 'application/x-msdownload',
|
43 | 'bin': 'application/octet-stream',
|
44 | 'bmp': 'image/bmp',
|
45 | 'bz2': 'application/x-bzip2',
|
46 | 'c': 'text/x-c',
|
47 | 'cab': 'application/vnd.ms-cab-compressed',
|
48 | 'cc': 'text/x-c',
|
49 | 'chm': 'application/vnd.ms-htmlhelp',
|
50 | 'class': 'application/octet-stream',
|
51 | 'com': 'application/x-msdownload',
|
52 | 'conf': 'text/plain',
|
53 | 'cpp': 'text/x-c',
|
54 | 'crt': 'application/x-x509-ca-cert',
|
55 | 'css': 'text/css',
|
56 | 'csv': 'text/csv',
|
57 | 'cxx': 'text/x-c',
|
58 | 'deb': 'application/x-debian-package',
|
59 | 'der': 'application/x-x509-ca-cert',
|
60 | 'diff': 'text/x-diff',
|
61 | 'djv': 'image/vnd.djvu',
|
62 | 'djvu': 'image/vnd.djvu',
|
63 | 'dll': 'application/x-msdownload',
|
64 | 'dmg': 'application/octet-stream',
|
65 | 'doc': 'application/msword',
|
66 | 'dot': 'application/msword',
|
67 | 'dtd': 'application/xml-dtd',
|
68 | 'dvi': 'application/x-dvi',
|
69 | 'ear': 'application/java-archive',
|
70 | 'eml': 'message/rfc822',
|
71 | 'eps': 'application/postscript',
|
72 | 'exe': 'application/x-msdownload',
|
73 | 'f': 'text/x-fortran',
|
74 | 'f77': 'text/x-fortran',
|
75 | 'f90': 'text/x-fortran',
|
76 | 'flv': 'video/x-flv',
|
77 | 'for': 'text/x-fortran',
|
78 | 'gem': 'application/octet-stream',
|
79 | 'gemspec': 'text/x-script.ruby',
|
80 | 'gif': 'image/gif',
|
81 | 'gz': 'application/x-gzip',
|
82 | 'h': 'text/x-c',
|
83 | 'hh': 'text/x-c',
|
84 | 'htm': 'text/html',
|
85 | 'html': 'text/html',
|
86 | 'ico': 'image/vnd.microsoft.icon',
|
87 | 'ics': 'text/calendar',
|
88 | 'ifb': 'text/calendar',
|
89 | 'iso': 'application/octet-stream',
|
90 | 'jar': 'application/java-archive',
|
91 | 'java': 'text/x-java-source',
|
92 | 'jnlp': 'application/x-java-jnlp-file',
|
93 | 'jpeg': 'image/jpeg',
|
94 | 'jpg': 'image/jpeg',
|
95 | 'js': 'application/javascript',
|
96 | 'json': 'application/json',
|
97 | 'log': 'text/plain',
|
98 | 'm3u': 'audio/x-mpegurl',
|
99 | 'm4v': 'video/mp4',
|
100 | 'man': 'text/troff',
|
101 | 'mathml': 'application/mathml+xml',
|
102 | 'mbox': 'application/mbox',
|
103 | 'mdoc': 'text/troff',
|
104 | 'me': 'text/troff',
|
105 | 'mid': 'audio/midi',
|
106 | 'midi': 'audio/midi',
|
107 | 'mime': 'message/rfc822',
|
108 | 'mml': 'application/mathml+xml',
|
109 | 'mng': 'video/x-mng',
|
110 | 'mov': 'video/quicktime',
|
111 | 'mp3': 'audio/mpeg',
|
112 | 'mp4': 'video/mp4',
|
113 | 'mp4v': 'video/mp4',
|
114 | 'mpeg': 'video/mpeg',
|
115 | 'mpg': 'video/mpeg',
|
116 | 'ms': 'text/troff',
|
117 | 'msi': 'application/x-msdownload',
|
118 | 'odp': 'application/vnd.oasis.opendocument.presentation',
|
119 | 'ods': 'application/vnd.oasis.opendocument.spreadsheet',
|
120 | 'odt': 'application/vnd.oasis.opendocument.text',
|
121 | 'ogg': 'application/ogg',
|
122 | 'p': 'text/x-pascal',
|
123 | 'pas': 'text/x-pascal',
|
124 | 'pbm': 'image/x-portable-bitmap',
|
125 | 'pdf': 'application/pdf',
|
126 | 'pem': 'application/x-x509-ca-cert',
|
127 | 'pgm': 'image/x-portable-graymap',
|
128 | 'pgp': 'application/pgp-encrypted',
|
129 | 'pkg': 'application/octet-stream',
|
130 | 'pl': 'text/x-script.perl',
|
131 | 'pm': 'text/x-script.perl-module',
|
132 | 'png': 'image/png',
|
133 | 'pnm': 'image/x-portable-anymap',
|
134 | 'ppm': 'image/x-portable-pixmap',
|
135 | 'pps': 'application/vnd.ms-powerpoint',
|
136 | 'ppt': 'application/vnd.ms-powerpoint',
|
137 | 'ps': 'application/postscript',
|
138 | 'psd': 'image/vnd.adobe.photoshop',
|
139 | 'py': 'text/x-script.python',
|
140 | 'qt': 'video/quicktime',
|
141 | 'ra': 'audio/x-pn-realaudio',
|
142 | 'rake': 'text/x-script.ruby',
|
143 | 'ram': 'audio/x-pn-realaudio',
|
144 | 'rar': 'application/x-rar-compressed',
|
145 | 'rb': 'text/x-script.ruby',
|
146 | 'rdf': 'application/rdf+xml',
|
147 | 'roff': 'text/troff',
|
148 | 'rpm': 'application/x-redhat-package-manager',
|
149 | 'rss': 'application/rss+xml',
|
150 | 'rtf': 'application/rtf',
|
151 | 'ru': 'text/x-script.ruby',
|
152 | 's': 'text/x-asm',
|
153 | 'sgm': 'text/sgml',
|
154 | 'sgml': 'text/sgml',
|
155 | 'sh': 'application/x-sh',
|
156 | 'sig': 'application/pgp-signature',
|
157 | 'snd': 'audio/basic',
|
158 | 'so': 'application/octet-stream',
|
159 | 'svg': 'image/svg+xml',
|
160 | 'svgz': 'image/svg+xml',
|
161 | 'swf': 'application/x-shockwave-flash',
|
162 | 't': 'text/troff',
|
163 | 'tar': 'application/x-tar',
|
164 | 'tbz': 'application/x-bzip-compressed-tar',
|
165 | 'tcl': 'application/x-tcl',
|
166 | 'tex': 'application/x-tex',
|
167 | 'texi': 'application/x-texinfo',
|
168 | 'texinfo': 'application/x-texinfo',
|
169 | 'text': 'text/plain',
|
170 | 'tif': 'image/tiff',
|
171 | 'tiff': 'image/tiff',
|
172 | 'torrent': 'application/x-bittorrent',
|
173 | 'tr': 'text/troff',
|
174 | 'txt': 'text/plain',
|
175 | 'vcf': 'text/x-vcard',
|
176 | 'vcs': 'text/x-vcalendar',
|
177 | 'vrml': 'model/vrml',
|
178 | 'war': 'application/java-archive',
|
179 | 'wav': 'audio/x-wav',
|
180 | 'wma': 'audio/x-ms-wma',
|
181 | 'wmv': 'video/x-ms-wmv',
|
182 | 'wmx': 'video/x-ms-wmx',
|
183 | 'wrl': 'model/vrml',
|
184 | 'wsdl': 'application/wsdl+xml',
|
185 | 'xbm': 'image/x-xbitmap',
|
186 | 'xhtml': 'application/xhtml+xml',
|
187 | 'xls': 'application/vnd.ms-excel',
|
188 | 'xml': 'application/xml',
|
189 | 'xpm': 'image/x-xpixmap',
|
190 | 'xsl': 'application/xml',
|
191 | 'xslt': 'application/xslt+xml',
|
192 | 'yaml': 'text/yaml',
|
193 | 'yml': 'text/yaml',
|
194 | 'zip': 'application/zip',
|
195 | 'woff': 'application/font-woff',
|
196 | 'woff2': 'application/font-woff',
|
197 | 'otf': 'application/font-sfnt',
|
198 | 'otc': 'application/font-sfnt',
|
199 | 'ttf': 'application/font-sfnt'
|
200 | };
|
201 |
|
202 | var options = {
|
203 | port: process.env.PM2_SERVE_PORT || process.argv[3] || 8080,
|
204 | host: process.env.PM2_SERVE_HOST || process.argv[4] || '0.0.0.0',
|
205 | path: path.resolve(process.env.PM2_SERVE_PATH || process.argv[2] || '.'),
|
206 | spa: process.env.PM2_SERVE_SPA === 'true',
|
207 | homepage: process.env.PM2_SERVE_HOMEPAGE || '/index.html',
|
208 | basic_auth: process.env.PM2_SERVE_BASIC_AUTH === 'true' ? {
|
209 | username: process.env.PM2_SERVE_BASIC_AUTH_USERNAME,
|
210 | password: process.env.PM2_SERVE_BASIC_AUTH_PASSWORD
|
211 | } : null,
|
212 | monitor: process.env.PM2_SERVE_MONITOR
|
213 | };
|
214 |
|
215 | if (typeof options.port === 'string') {
|
216 | options.port = parseInt(options.port) || 8080
|
217 | }
|
218 |
|
219 | if (typeof options.monitor === 'string' && options.monitor !== '') {
|
220 | try {
|
221 | let fileContent = fs.readFileSync(path.join(process.env.PM2_HOME, 'agent.json5')).toString()
|
222 |
|
223 | fileContent = fileContent.replace(/\s(\w+):/g, '"$1":')
|
224 |
|
225 | let conf = JSON.parse(fileContent)
|
226 | options.monitorBucket = conf.public_key
|
227 | } catch (e) {
|
228 | console.log('Interaction file does not exist')
|
229 | }
|
230 | }
|
231 |
|
232 |
|
233 | http.createServer(function (request, response) {
|
234 | if (options.basic_auth) {
|
235 | if (!request.headers.authorization || request.headers.authorization.indexOf('Basic ') === -1) {
|
236 | return sendBasicAuthResponse(response)
|
237 | }
|
238 |
|
239 | var user = parseBasicAuth(request.headers.authorization)
|
240 | if (user.username !== options.basic_auth.username || user.password !== options.basic_auth.password) {
|
241 | return sendBasicAuthResponse(response)
|
242 | }
|
243 | }
|
244 |
|
245 | serveFile(request.url, request, response);
|
246 |
|
247 | }).listen(options.port, options.host, function (err) {
|
248 | if (err) {
|
249 | console.error(err);
|
250 | process.exit(1);
|
251 | }
|
252 | console.log('Exposing %s directory on %s:%d', options.path, options.host, options.port);
|
253 | });
|
254 |
|
255 | function serveFile(uri, request, response) {
|
256 | var file = decodeURIComponent(url.parse(uri || request.url).pathname);
|
257 |
|
258 | if (file === '/' || file === '') {
|
259 | file = options.homepage;
|
260 | request.wantHomepage = true;
|
261 | }
|
262 | var filePath = path.resolve(options.path + file);
|
263 |
|
264 |
|
265 |
|
266 | if (filePath.indexOf(options.path) !== 0) {
|
267 | response.writeHead(403, { 'Content-Type': 'text/html' });
|
268 | return response.end('403 Forbidden');
|
269 | }
|
270 |
|
271 | var contentType = contentTypes[filePath.split('.').pop().toLowerCase()] || 'text/plain';
|
272 |
|
273 | fs.readFile(filePath, function (error, content) {
|
274 | if (error) {
|
275 | if ((!options.spa || request.wantHomepage)) {
|
276 | console.error('[%s] Error while serving %s with content-type %s : %s',
|
277 | new Date(), filePath, contentType, error.message || error);
|
278 | }
|
279 | if (!isNode4)
|
280 | errorMeter.mark();
|
281 | if (error.code === 'ENOENT') {
|
282 | if (options.spa && !request.wantHomepage) {
|
283 | request.wantHomepage = true;
|
284 | return serveFile(`/${path.basename(file)}`, request, response);
|
285 | } else if (options.spa && file !== options.homepage) {
|
286 | return serveFile(options.homepage, request, response);
|
287 | }
|
288 | fs.readFile(options.path + '/404.html', function (err, content) {
|
289 | content = err ? '404 Not Found' : content;
|
290 | response.writeHead(404, { 'Content-Type': 'text/html' });
|
291 | return response.end(content, 'utf-8');
|
292 | });
|
293 | return;
|
294 | }
|
295 | response.writeHead(500);
|
296 | return response.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
|
297 | }
|
298 | response.writeHead(200, { 'Content-Type': contentType });
|
299 | if (options.monitorBucket && contentType === 'text/html') {
|
300 | content = content.toString().replace('</body>', `
|
301 | <script>
|
302 | ;(function (b,e,n,o,i,t) {
|
303 | b[o]=b[o]||function(f){(b[o].c=b[o].c||[]).push(f)};
|
304 | t=e.createElement(i);e=e.getElementsByTagName(i)[0];
|
305 | t.async=1;t.src=n;e.parentNode.insertBefore(t,e);
|
306 | }(window,document,'https://apm.pm2.io/pm2-io-apm-browser.v1.js','pm2Ready','script'))
|
307 |
|
308 | pm2Ready(function(apm) {
|
309 | apm.setBucket('${options.monitorBucket}')
|
310 | apm.setApplication('${options.monitor}')
|
311 | apm.reportTimings()
|
312 | apm.reportIssues()
|
313 | })
|
314 | </script>
|
315 | </body>
|
316 | `);
|
317 | }
|
318 | response.end(content, 'utf-8');
|
319 | debug('[%s] Serving %s with content-type %s', Date.now(), filePath, contentType);
|
320 | });
|
321 | }
|
322 |
|
323 | function parseBasicAuth(auth) {
|
324 |
|
325 | var tmp = auth.split(' ');
|
326 |
|
327 | var buf = Buffer.from(tmp[1], 'base64');
|
328 | var plain = buf.toString();
|
329 |
|
330 | var creds = plain.split(':');
|
331 | return {
|
332 | username: creds[0],
|
333 | password: creds[1]
|
334 | }
|
335 | }
|
336 |
|
337 | function sendBasicAuthResponse(response) {
|
338 | response.writeHead(401, {
|
339 | 'Content-Type': 'text/html',
|
340 | 'WWW-Authenticate': 'Basic realm="Authentication service"'
|
341 | });
|
342 | return response.end('401 Unauthorized');
|
343 | }
|