/
server.js
219 lines (192 loc) · 6.53 KB
/
server.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var crypto = require('crypto');
var fs = require('fs');
var path = require('path');
var Busboy = require('busboy');
var express = require('express');
var http = require('http');
var https = require('https');
var request = require('request');
var tmp = require('tmp');
// Different headers can be pushed depending on data format
// to allow for changes with backwards compatibility
var UP1_HEADERS = {
v1: new Buffer.from("UP1\0", 'binary')
}
function handle_upload(req, res) {
var config = req.app.locals.config
var busboy = new Busboy({
headers: req.headers,
limits: {
fileSize: config.maximum_file_size,
files: 1,
parts: 3
}
});
var fields = {};
var tmpfname = null;
busboy.on('field', function(fieldname, value) {
fields[fieldname] = value;
});
busboy.on('file', function(fieldname, file, filename) {
try {
var ftmp = tmp.fileSync({ postfix: '.tmp', dir: req.app.locals.config.path.i, keep: true });
tmpfname = ftmp.name;
var fstream = fs.createWriteStream('', {fd: ftmp.fd, defaultEncoding: 'binary'});
fstream.write(UP1_HEADERS.v1, 'binary', () => file.pipe(fstream));
} catch (err) {
console.error("Error on file:", err);
res.send("Internal Server Error");
req.unpipe(busboy);
res.close();
}
});
busboy.on('finish', function() {
try {
if (!tmpfname) {
res.send("Internal Server Error");
} else if (fields.api_key !== config['api_key']) {
res.send('{"error": "API key doesn\'t match", "code": 2}');
} else if (!fields.ident) {
res.send('{"error": "Ident not provided", "code": 11}');
} else if (fields.ident.length !== 22) {
res.send('{"error": "Ident length is incorrect", "code": 3}');
} else if (ident_exists(fields.ident)) {
res.send('{"error": "Ident is already taken.", "code": 4}');
} else {
var delhmac = crypto.createHmac('sha256', config.delete_key)
.update(fields.ident)
.digest('hex');
fs.rename(tmpfname, ident_path(fields.ident), function() {
res.json({delkey: delhmac});
});
}
} catch (err) {
console.error("Error on finish:", err);
res.send("Internal Server Error");
}
});
return req.pipe(busboy);
};
function handle_delete(req, res) {
var config = req.app.locals.config
if (!req.query.ident) {
res.send('{"error": "Ident not provided", "code": 11}');
return;
}
if (!req.query.delkey) {
res.send('{"error": "Delete key not provided", "code": 12}');
return;
}
var delhmac = crypto.createHmac('sha256', config.delete_key)
.update(req.query.ident)
.digest('hex');
if (req.query.ident.length !== 22) {
res.send('{"error": "Ident length is incorrect", "code": 3}');
} else if (delhmac !== req.query.delkey) {
res.send('{"error": "Incorrect delete key", "code": 10}');
} else if (!ident_exists(req.query.ident)) {
res.send('{"error": "Ident does not exist", "code": 9}');
} else {
fs.unlink(ident_path(req.query.ident), function() {
cf_invalidate(req.query.ident, config);
res.redirect('/');
});
}
};
function ident_path(ident) {
return '../i/' + path.basename(ident);
}
function ident_exists(ident) {
try {
fs.lstatSync(ident_path(ident));
return true;
} catch (err) {
return false;
}
}
function cf_do_invalidate(ident, mode, cfconfig) {
var inv_url = mode + '://' + cfconfig.url + '/i/' + ident;
request.post({
url: 'https://www.cloudflare.com/api_json.html',
form: {
a: 'zone_file_purge',
tkn: cfconfig.token,
email: cfconfig.email,
z: cfconfig.domain,
url: inv_url
}
}, function(err, response, body) {
if (err) {
console.error("Cache invalidate failed for", ident);
console.error("Body:", body);
return;
}
try {
var result = JSON.parse(body)
if (result.result === 'error') {
console.error("Cache invalidate failed for", ident);
console.error("Message:", msg);
}
} catch(err) {}
});
}
function cf_invalidate(ident, config) {
var cfconfig = config['cloudflare-cache-invalidate']
if (!cfconfig.enabled) {
return;
}
if (config.http.enabled)
cf_do_invalidate(ident, 'http', cfconfig);
if (config.https.enabled)
cf_do_invalidate(ident, 'https', cfconfig);
}
function create_app(config) {
var app = express();
app.locals.config = config
app.use('', express.static(config.path.client));
app.use('/i', express.static(config.path.i));
app.post('/up', handle_upload);
app.get('/del', handle_delete);
return app
}
/* Convert an IP:port string to a split IP and port */
function get_addr_port(s) {
var spl = s.split(":");
if (spl.length === 1)
return { host: spl[0], port: 80 };
else if (spl[0] === '')
return { port: parseInt(spl[1]) };
else
return { host: spl[0], port: parseInt(spl[1]) };
}
function serv(server, serverconfig, callback) {
var ap = get_addr_port(serverconfig.listen);
return server.listen(ap.port, ap.host, callback);
}
function init_defaults(config) {
config.path = config.path ? config.path : {};
config.path.i = config.path.i ? config.path.i : "../i";
config.path.client = config.path.client ? config.path.client : "../client";
}
function init(config) {
init_defaults(config)
var app = create_app(config);
if (config.http.enabled) {
serv(http.createServer(app), config.http, function() {
console.info('Started server at http://%s:%s', this.address().address, this.address().port);
});
}
if (config.https.enabled) {
var sec_creds = {
key: fs.readFileSync(config.https.key),
cert: fs.readFileSync(config.https.cert)
};
serv(https.createServer(sec_creds, app), config.https, function() {
console.info('Started server at https://%s:%s', this.address().address, this.address().port);
});
}
}
function main(configpath) {
init(JSON.parse(fs.readFileSync(configpath)));
}
main('./server.conf')