|
let https = require('https'), |
|
httpProxy = require('http-proxy'), |
|
fs = require('fs'); |
|
|
|
var ssl = { |
|
key: fs.readFileSync('/etc/letsencrypt/live/pycontract.tk-0002/privkey.pem', 'utf8'), |
|
cert: fs.readFileSync('/etc/letsencrypt/live/pycontract.tk-0002/fullchain.pem', 'utf8') |
|
}; |
|
|
|
|
|
let HomepageProxy = new httpProxy.createProxyServer({ |
|
target: {host: 'localhost', port: 8000}, |
|
xfwd: true |
|
}); |
|
|
|
|
|
let TestApiProxy = new httpProxy.createProxyServer({ |
|
target: {host: 'localhost', port: 3000}, |
|
xfwd: true |
|
}); |
|
|
|
|
|
let wsProxy = new httpProxy.createProxyServer({ |
|
target: {host: 'localhost', port: 3000}, |
|
xfwd: true |
|
}); |
|
|
|
let server = https.createServer(ssl, function ( req, res ) { |
|
//console.log(req.headers.host); |
|
//console.log(req.headers); |
|
try{ |
|
let domain = req.headers.host; |
|
let host = domain.split(":")[0]; |
|
|
|
if (host==='pycontract.tk') { |
|
res.writeHead(302, {'Location': 'https://www.pycontract.tk'}); |
|
res.end(); |
|
} else if (host==='www.pycontract.tk'){ |
|
HomepageProxy.proxyRequest(req, res); |
|
} else if (host==='testnet.pycontract.tk'){ |
|
TestApiProxy.proxyRequest(req, res); |
|
} else if (host==='pool.pycontract.tk'){ |
|
PoolProxy.proxyRequest(req, res); |
|
} else { |
|
res.writeHead(404); |
|
res.end(); |
|
} |
|
} catch (e) { |
|
console.log('error', e); |
|
res.writeHead(404); |
|
res.end(); |
|
} |
|
}); |
|
|
|
server.on( 'upgrade', function( req, socket, head ) { |
|
try{ |
|
req.headers['x-forwarded-for'] = req.connection.remoteAddress || req.connection.socket.remoteAddress; |
|
//console.log(req.headers); |
|
wsProxy.ws(req, socket, head); |
|
} catch (e) { |
|
res.writeHead(404); |
|
res.end(); |
|
} |
|
}); |
|
|
|
// Listen for the `error` event on `proxy` |
|
server.on('error', function (err, req, res) { |
|
if (res) { |
|
res.writeHead(500, { |
|
'Content-Type': 'text/plain' |
|
}); |
|
res.end('Something went wrong. And we are reporting a custom error message.'); |
|
} |
|
}); |
|
|
|
server.listen(443); |