Skip to content

Commit

Permalink
Issue #9: set ips from service response to config
Browse files Browse the repository at this point in the history
  • Loading branch information
geirkairam committed Oct 31, 2015
1 parent 9e5998a commit 78f14d4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 25 deletions.
16 changes: 3 additions & 13 deletions src/env/test/ipservice/confirm.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
{
"ipv4": [
{
"net": "10.0.0.0/29"
},
{
"net": "10.0.0.8/25"
}
],
"ipbv6": [
{
"net": "asdf"
}
]
"routername": "name",
"v4prefixes": ["10.0.0.255/29","10.0.0.1/25"],
"v6prefixes": ["2001:bf7:c4ff:3300::/56"]
}
78 changes: 66 additions & 12 deletions src/js/controllers/dialog.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

var ip = require('ip');

module.exports = function(app) {
app.controller('DialogController',
function($scope, $modalInstance, state, wizard, $interval, $http, $translate, downloadFile, $filter, REGISTER_IPS) {
Expand All @@ -12,6 +14,18 @@ module.exports = function(app) {
confirmed: 'notStarted',
configWritten: 'notStarted'
},
meshIPSubnet: function() {
var meshIpsSubnetSize = Object.keys(state.wifi.devices).length;
if (wizard.ip.meshLan) {
meshIpsSubnetSize++
}

if (meshIpsSubnetSize < 3) {
return 30;
} else {
return 29;
}
},
steps: 0,
};
$scope.state.generatevpn = {
Expand All @@ -24,19 +38,17 @@ module.exports = function(app) {
}

$scope.reserveIPs = function() {
var meshIpsSubnetSize = Object.keys(state.wifi.devices).length;
if (wizard.ip.meshLan) {
meshIpsSubnetSize++
var data = {
email: wizard.contact.email,
routerName: wizard.router.name,
v4subnet: [state.registerips.meshIPSubnet(), state.ip.v4ClientSubnetSize],
v6subnet: [56]
}
console.log(data),
$http({
method: REGISTER_IPS.method,
url: REGISTER_IPS.service+REGISTER_IPS.reserve
},{
email: wizard.contact.email,
routerName: wizard.router.name,
v4subnet: [meshIpsSubnetSize, state.ip.v4ClientSubnetSize],
v6subnet: [56]
}).then(function(response) {
}, data).then(function(response) {
//success callback
//TODO handle error in response
$scope.state.registerips.progress.reserved = 'success';
Expand All @@ -59,16 +71,58 @@ module.exports = function(app) {
}).then(
function(response) {
//succuess callback
console.log(response.data)
//TODO handle error in response message and wrecked data
$scope.state.registerips.steps++;
$scope.state.registerips.progress.confirmed = 'success'
$scope.state.registerips.progress.confirmed = 'success';

//assumption: ip registration service only sends back two v4 subnets
//because we only ask for two of them
//one for clients and one for meshing, but there is not hint wich is which
var firstsubnet = ip.cidrSubnet(response.data.v4prefixes[0]);
if (firstsubnet.subnetMaskLength == state.ip.v4ClientSubnetSize) {
$scope.wizard.ip.v4ClientSubnet = response.data.v4prefixes[0];
$scope.setMeshIps(response.data.v4prefixes[1]);
} else {
$scope.setMeshIps(response.data.v4prefixes[0]);
$scope.wizard.ip.v4ClientSubnet = response.data.v4prefixes[1];
}
$scope.wizard.ip.v6Prefix=response.data.v6prefixes[0];
$scope.state.registerips.steps++;
$scope.state.registerips.progress.configWritten = 'success';
}, function(response) {
//error callback
$scope.state.registerips.progress.confirmed = 'error'
$scope.state.registerips.progress.confirmed = 'error';
}
);
}

$scope.setMeshIps = function(subnet) {
var ipAddress = ip.cidrSubnet(subnet).firstAddress;
for (var i=0; i <Object.keys(state.wifi.devices).length; i++) {
$scope.wizard.ip.v4['radio'+i] = ipAddress.toString();
ipAddress = $scope.getNextIP(ipAddress.toString());
}

if ($scope.wizard.ip.meshLan) {
$scope.wizard.ip.v4.lan = ipAddress;
}
}

$scope.getNextIP = function(ipAddress) {
var buf = new Buffer(128);
var offset = 64;
ip.toBuffer(ipAddress, buf, offset);
if (buf[67] < 255) {
buf[67] = buf[67]+1;
} else if (buf[66] < 255) {
buf[66] = buf[66]+1;
buf[67] = 0;
} else {
console.log('too lazy to implement this now. is it even needed? maybe it is not even neccessary, because mesh net is realy small')
}
return ip.toString(buf, offset, 4);
}

$scope.generateVPN03CertAndKey = function() {
/*
* since this is not handled on the client we need a service on the router
Expand Down

0 comments on commit 78f14d4

Please sign in to comment.