43 lines
1.2 KiB
JavaScript
43 lines
1.2 KiB
JavaScript
import 'colors'
|
|
import 'tagol'
|
|
import minimist from 'minimist'
|
|
import bouncy from 'bouncy'
|
|
import domainParse from './domains-file-parse'
|
|
|
|
const args = minimist( process.argv.slice(2) )
|
|
|
|
const config = {
|
|
// https: args.secure || args.s,
|
|
port: args.port || args.p || 80,
|
|
domains: args._[0] || 'domains-exemple'
|
|
}
|
|
const domains = domainParse( config.domains )
|
|
//console.log( args, config )
|
|
|
|
|
|
bouncy( (req, res, bounce) => {
|
|
|
|
let domain, match, debug = [],
|
|
[ ,,subDomain, mainDomain, tld ] = /^((.*)\.)*(.*?)\.(.*)$/.exec( req.headers.host )
|
|
log`[${new Date().toUTCString().grey}s]
|
|
Received a ${req.method}s request for ${`http://${req.headers.host}${req.url}`.grey}s`
|
|
log`Detected ${subDomain ? subDomain.cyan : ''}s.${mainDomain.blue}s.${tld.grey}s`
|
|
|
|
for( domain of domains )
|
|
if( match = domain.test.exec(req.headers.host) )
|
|
{
|
|
let dest = domain.resolve(subDomain, mainDomain, tld, match, req.url)
|
|
log`Found a match: ${domain[0].green}s redirecting to ${dest.yellow}s`
|
|
bounce( dest )
|
|
break
|
|
}
|
|
|
|
if( !match )
|
|
{
|
|
log`${'No matching domain!'.red}s`
|
|
res.statusCode = 404
|
|
res.end('no such host')
|
|
}
|
|
|
|
}).listen( config.port )
|