/** | |
* httpServer - HTTP server for user's credential | |
*/ | |
var express = require('express'); | |
var path = require('path'); | |
var http = require('http'); | |
var https = require('https'); | |
var radiusOper = require('./startClient'); | |
var body_parser = require('body-parser'); | |
var os=require('os'); | |
var ifaces = os.networkInterfaces(); | |
var app = express(); | |
var fs = require("fs"); | |
var Promise=require('bluebird'); | |
var execAsync=Promise.promisify(require('child_process').exec); | |
app.use(express.static(path.join(__dirname,'authwebapp'))); | |
var config = { | |
key: fs.readFileSync('./certs/server.key'), | |
cert: fs.readFileSync('./certs/server.crt'), | |
ca: fs.readFileSync('./certs/ca.crt'), | |
requestCert: true, | |
rejectUnauthorized: false | |
}; | |
function natRecoverPkt(iface, ipAddr, port) { | |
var cmd = 'ip6tables -w -t nat -D PREROUTING -i ' + iface + | |
' -p tcp --dport ' + port + ' -j DNAT --to-destination [' | |
+ ipAddr + ']:' + port; | |
var checkCmd = 'ip6tables -w -t nat -v -L PREROUTING'; | |
var check = 'to:[' + ipAddr + ']:' + port; | |
console.log(checkCmd + ' to check ' + check); | |
execAsync(checkCmd).then(function (result) { | |
console.log("indexOf:" + result.indexOf(check)); | |
if (result.indexOf(check) > 0) { | |
execAsync(cmd); | |
console.log(cmd); | |
} | |
else { | |
console.log("nat rule not exist."); | |
} | |
}, function (err) {console.error(err);}); | |
} | |
function connDev(iface) { | |
var cmd = 'ip6tables -w -D FORWARD -i ' + iface + ' -j DROP'; | |
var checkCmd = 'ip6tables -w -v -L FORWARD 1'; | |
var check = 'DROP all ' + iface; | |
console.log(checkCmd + ' to check ' + check); | |
execAsync(checkCmd).then(function (result) { | |
console.log("indexOf:" + result.indexOf(check)); | |
if (result.indexOf(check) > 0) { | |
execAsync(cmd); | |
console.log(cmd); | |
} | |
else { | |
console.log("Forward rule not exist."); | |
} | |
}, function (err) {console.error(err);}); | |
} | |
function rmDnsRoute(ipAddr) { | |
//delete blindly, minor side effect | |
var cmd = 'route del ' + ipAddr; | |
execAsync(cmd); | |
console.log(cmd); | |
} | |
function pppoeRmRedirectPkt(ipAddr) { | |
natRecoverPkt('eth1', ipAddr, '80'); | |
natRecoverPkt('eth1', ipAddr, '443'); | |
connDev('eth1'); | |
rmDnsRoute('8.8.8.8'); | |
} | |
app.use(body_parser.json()); | |
app.use(body_parser.urlencoded({ extended: true })); | |
app.get('/', function (req, res) { | |
console.log("===Please login.===" ); | |
execAsync('cat adminState.txt').then(function (result) { | |
if (result.indexOf("enable") > 0) | |
{ | |
res.sendFile(__dirname+'/authwebapp/login.html'); | |
} | |
else | |
{ | |
console.log("PPPoE disabled."); | |
res.send('PPPoE disabled.'); | |
} | |
}, function (err) { | |
console.error(err); | |
}); | |
}) | |
app.post('/',function(req,res){ | |
console.log("===post request==="); | |
var username = req.body.username; | |
var password = req.body.password; | |
console.log("===user:"+username+",pwd:"+password+"==="); | |
var data = radiusOper.verifyFun(username,password); | |
data.then(function(result){ | |
console.log("===verifyFun result:"+result+"==="); | |
if(result) | |
{ | |
setTimeout(function(){ | |
console.log("enter timeout"); | |
execAsync("ifconfig").then(function (result) { | |
console.log("result.indexOf :"+result.indexOf("ppp0")); | |
if(result.indexOf("ppp0") >= 0) | |
{ | |
console.log("===PPPoE session has set up.==="); | |
pppoeRmRedirectPkt(eh1ip); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.jsonp({'result':'Auth successfully!!'}); | |
} | |
else | |
{ | |
execAsync("poff -a"); | |
console.log("===PPPoE session failed.==="); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.jsonp({'result':'error!'}); | |
} | |
}); | |
},5000); | |
} | |
else | |
{ | |
execAsync("poff -a"); | |
console.log("===PPPoE session failed.==="); | |
res.setHeader("Access-Control-Allow-Origin", "*"); | |
res.jsonp({'result':'error!'}); | |
} | |
}); | |
}); | |
var eh1ip='a'; | |
var i = false; | |
ifaces['eth1'].forEach(function(details){ | |
if (details.family=='IPv6' && i == false) | |
{ | |
i = true; | |
eh1ip = details.address; | |
console.log('eh1ip:'+eh1ip); | |
} | |
}); | |
var httpPort = "80"; | |
var httpsPort = "443"; | |
http.createServer(app).listen(httpPort, eh1ip); | |
https.createServer(config, app).listen(httpsPort, eh1ip); | |
console.log("PPPoE Web Server listens on ports " + httpPort + " and " + httpsPort); | |