-
Notifications
You must be signed in to change notification settings - Fork 1
/
statics.js
78 lines (73 loc) · 2.29 KB
/
statics.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
var config = require('./config').config;
var http = require('http')
, https = require('https')
, url = require('url')
, path = require('path')
, fs = require('fs')
, config = require('./config').config
var ssl =
{ ca:fs.readFileSync(config.sslDir +'sub.class1.server.ca.pem')
, key:fs.readFileSync(config.sslDir +'ssl.key')
, cert:fs.readFileSync(config.sslDir +'ssl.crt')
}
function serve(req, res) {
var uri = url.parse(req.url).pathname
.replace(new RegExp('/$', 'g'), '/index.html')
var host = req.headers.host
if(host.substring(0, 4) == 'www.') {
res.writeHead(302, {'Location': 'http://'+host.substring(4)});
res.write('302 Location: http://'+host.substring(4)+'\n');
res.end()
return
}
console.log(host)
host = host
.replace(new RegExp('([a-f0-9]+)\.apptorrent\.net', 'g'), 'apptorrent.net')//wildcard hosting
console.log('>:'+host)
var filename = path.join(config.domainsDir, host, uri)
if(filename.substring(0, config.domainsDir.length) != config.domainsDir) {
res.writeHead(403, {'Content-Type': 'text/plain'})
res.write('403 Naughty!\n'+filename)
res.end()
return
}
var contentType
if(/\.appcache$/g.test(uri)) {
contentType='text/cache-manifest'
} else if(/\.html$/g.test(uri)) {
contentType='text/html'
} else if(/\.css$/g.test(uri)) {
contentType='text/css'
} else if(/\.js$/g.test(uri)) {
contentType='text/javascript'
} else if(/\.png$/g.test(uri)) {
contentType='image/png'
} else {
contentType='text/plain'
}
path.exists(filename, function(exists) {
if(!exists) {
res.writeHead(404, {'Content-Type': 'text/plain'})
res.write('404 Not Found\n'+filename)
res.end()
return
}
fs.readFile(filename, 'binary', function(err, file) {
if(err) {
res.writeHead(500, {'Content-Type': 'text/plain'})
res.end(err + '\n')
return
}
res.writeHead(200,
{ 'Access-Control-Allow-Origin': '*'
, 'Access-Control-Allow-Headers': 'Content-Type'
, 'Content-Type': contentType
})
res.write(file, 'binary')
res.end()
})
})
}
http.createServer(serve).listen(config.backends.statics)
https.createServer(ssl, serve).listen(443)
console.log('Server running at ports '+config.backends.statics+' and 443')