-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (76 loc) · 2.34 KB
/
server.js
File metadata and controls
90 lines (76 loc) · 2.34 KB
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
var config = require('./config.json');
var ensure_https_handle = require('ensure-https')
.handle
.bind({sslPort: config.https.port});
var fs = require('fs');
var rd = require('./lib/http/redirect-domain.js');
var root = 'http://'+config.domain.root+':'+config.http.port;
var subdomains = config.domain.root.split('.');
var request_domain = function(req){
var host = req.headers.host;
if(host) return host.split(':')[0];
}
var respond = {};
respond.http = function(req, res){
var domain = request_domain(req);
if(!domain) return respond.error('Host header missing.', req, res);
var req_subdomains = domain.split('.').slice(0, -1 * subdomains.length);
var subdomain = req_subdomains[0];
// https for root and api domains
if(!subdomain || (config.domain.api === domain)){
return ensure_https_handle(req, res);
}
var www = rd.redirectWww(req, res);
if(www) return;
if(subdomain){
return respond.subdomain_missing(req, res);
}
respond.notfound(req, res);
}
respond.https = function(req, res){
var www = rd.redirectWww(req, res, true);
if(www) return;
var response_type = (req.url === '/') ? 'home' : 'notfound';
respond[response_type](req, res);
}
respond.error = function (msg, req, res){
res.writeHead(500);
res.end('Error. '+msg);
console.log('error: ', req.headers.host + req.url);
}
respond.notfound = function (req, res){
res.writeHead(404);
res.end("Not Found");
console.log('not found: ', req.headers.host + req.url);
}
respond.home = function (req, res){
res.writeHead(200);
res.end("hackdash");
}
respond.subdomain_missing = function (req, res){
res.writeHead(404);
res.end(
'<a href="'+root+'/?domain='+request_domain(req)+'">'
+ 'Subdomain available.'
+'</a>'
);
// res.writeHead(302, 'Subdomain unregistered.');
// res.setHeader('Location', root+'/?domain='+request_domain(req));
// res.end();
}
require('http')
.createServer(respond.http)
.listen(config.http.port);
console.log('http://'+config.domain.root+':'+config.http.port+'/');
console.log('http://x.'+config.domain.root+':'+config.http.port+'/');
require('https')
.createServer(
{
cert: fs.readFileSync(config.https.cert),
key: fs.readFileSync(config.https.key)
},
respond.https
)
.listen(config.https.port);
console.log('https://api.'+config.domain.root+':'+config.https.port+'/');
console.log('https://'+config.domain.root+':'+config.https.port+'/');