aboutsummaryrefslogtreecommitdiff
path: root/src/modules/webserver.js
blob: 5d8b2c9ea5701691e28e2a13e22edcec9d00aeea (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const http = require('http');
const mime = require('mime');
const url = require('url');
const fs = require('fs');
const moment = require('moment');
const config = require('../config');
const threads = require('../data/threads');
const attachments = require('../data/attachments');

const {THREAD_MESSAGE_TYPE} = require('../data/constants');

function notfound(res) {
  res.statusCode = 404;
  res.end('Page Not Found');
}

async function serveLogs(res, pathParts) {
  const threadId = pathParts[pathParts.length - 1];
  if (threadId.match(/^[0-9a-f\-]+$/) === null) return notfound(res);

  const thread = await threads.findById(threadId);
  if (! thread) return notfound(res);

  const threadMessages = await thread.getThreadMessages();
  const lines = threadMessages.map(message => {
    // Legacy messages are the entire log in one message, so just serve them as they are
    if (message.message_type === THREAD_MESSAGE_TYPE.LEGACY) {
      return message.body;
    }

    let line = `[${moment.utc(message.created_at).format('YYYY-MM-DD HH:mm:ss')}] `;

    if (message.message_type === THREAD_MESSAGE_TYPE.SYSTEM) {
      // System messages don't need the username
      line += message.body;
    } else if (message.message_type === THREAD_MESSAGE_TYPE.FROM_USER) {
      line += `[FROM USER] ${message.user_name}: ${message.body}`;
    } else if (message.message_type === THREAD_MESSAGE_TYPE.TO_USER) {
      line += `[TO USER] ${message.user_name}: ${message.body}`;
    } else {
      line += `${message.user_name}: ${message.body}`;
    }

    return line;
  });

  res.setHeader('Content-Type', 'text/plain; charset=UTF-8');
  res.end(lines.join('\n'));
}

function serveAttachments(res, pathParts) {
  const desiredFilename = pathParts[pathParts.length - 1];
  const id = pathParts[pathParts.length - 2];

  if (id.match(/^[0-9]+$/) === null) return notfound(res);
  if (desiredFilename.match(/^[0-9a-z._-]+$/i) === null) return notfound(res);

  const attachmentPath = attachments.getLocalAttachmentPath(id);
  fs.access(attachmentPath, (err) => {
    if (err) return notfound(res);

    const filenameParts = desiredFilename.split('.');
    const ext = (filenameParts.length > 1 ? filenameParts[filenameParts.length - 1] : 'bin');
    const fileMime = mime.getType(ext);

    res.setHeader('Content-Type', fileMime);

    const read = fs.createReadStream(attachmentPath);
    read.pipe(res);
  })
}

module.exports = () => {
  const server = http.createServer((req, res) => {
    const parsedUrl = url.parse(`http://${req.url}`);
    const pathParts = parsedUrl.path.split('/').filter(v => v !== '');

    if (parsedUrl.path.startsWith('/logs/')) {
      serveLogs(res, pathParts);
    } else if (parsedUrl.path.startsWith('/attachments/')) {
      serveAttachments(res, pathParts);
    } else {
      notfound(res);
    }
  });

  server.on('error', err => {
    console.log('[WARN] Web server error:', err.message);
  });

  server.listen(config.port);
};