forked from mthenw/frontail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (136 loc) · 3.88 KB
/
index.js
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
'use strict';
const connect = require('connect');
const cookieParser = require('cookie');
const crypto = require('crypto');
const path = require('path');
const SocketIO = require('socket.io');
const tail = require('./lib/tail');
const connectBuilder = require('./lib/connect_builder');
const program = require('./lib/options_parser');
const serverBuilder = require('./lib/server_builder');
const daemonize = require('./lib/daemonize');
const fs = require('fs');
const untildify = require('untildify');
/**
* Parse args
*/
program.parse(process.argv);
if (program.args.length === 0) {
console.error('Arguments needed, use --help');
process.exit();
}
/**
* Validate params
*/
const doAuthorization = !!(program.user && program.password);
const doSecure = !!(program.key && program.certificate);
const sessionSecret = String(+new Date()) + Math.random();
const sessionKey = 'sid';
const files = program.args.join(' ');
const filesNamespace = crypto
.createHash('md5')
.update(files)
.digest('hex');
const urlPath = program.urlPath.replace(/\/$/, ''); // remove trailing slash
if (program.daemonize) {
daemonize(__filename, program, {
doAuthorization,
doSecure,
});
} else {
/**
* HTTP(s) server setup
*/
const appBuilder = connectBuilder(urlPath);
if (doAuthorization) {
appBuilder.session(sessionSecret, sessionKey);
appBuilder.authorize(program.user, program.password);
}
appBuilder
.static(path.join(__dirname, 'web/assets'))
.index(path.join(__dirname, 'web/index.html'), files, filesNamespace, program.theme);
const builder = serverBuilder();
if (doSecure) {
builder.secure(program.key, program.certificate);
}
const server = builder
.use(appBuilder.build())
.port(program.port)
.host(program.host)
.build();
/**
* socket.io setup
*/
const io = new SocketIO({ path: path.join(urlPath, '/socket.io') });
io.attach(server);
if (doAuthorization) {
io.use((socket, next) => {
const handshakeData = socket.request;
if (handshakeData.headers.cookie) {
const cookies = cookieParser.parse(handshakeData.headers.cookie);
const sessionIdEncoded = cookies[sessionKey];
if (!sessionIdEncoded) {
return next(new Error('Session cookie not provided'), false);
}
const sessionId = connect.utils.parseSignedCookie(sessionIdEncoded, sessionSecret);
if (sessionId) {
return next(null);
}
return next(new Error('Invalid cookie'), false);
}
return next(new Error('No cookie in header'), false);
});
}
/**
* Setup UI highlights
*/
let highlightConfig;
if (program.uiHighlight) {
let presetPath;
if (!program.uiHighlightPreset) {
presetPath = path.join(__dirname, 'preset/default.json');
} else {
presetPath = path.resolve(untildify(program.uiHighlightPreset));
}
if (fs.existsSync(presetPath)) {
highlightConfig = JSON.parse(fs.readFileSync(presetPath));
} else {
throw new Error(`Preset file ${presetPath} doesn't exists`);
}
}
/**
* When connected send starting data
*/
const tailer = tail(program.args, {
buffer: program.number,
});
const filesSocket = io.of(`/${filesNamespace}`).on('connection', (socket) => {
socket.emit('options:lines', program.lines);
if (program.uiHideTopbar) {
socket.emit('options:hide-topbar');
}
if (!program.uiIndent) {
socket.emit('options:no-indent');
}
if (program.uiHighlight) {
socket.emit('options:highlightConfig', highlightConfig);
}
tailer.getBuffer().forEach((line) => {
socket.emit('line', line);
});
});
/**
* Send incoming data
*/
tailer.on('line', (line) => {
filesSocket.emit('line', line);
});
/**
* Handle signals
*/
const cleanExit = () => {
process.exit();
};
process.on('SIGINT', cleanExit);
process.on('SIGTERM', cleanExit);
}