From 78f14d4bb0f77b986445e48529ab61d3abcc78dd Mon Sep 17 00:00:00 2001 From: maria Date: Sat, 31 Oct 2015 16:39:06 +0100 Subject: [PATCH] Issue #9: set ips from service response to config --- src/env/test/ipservice/confirm.json | 16 ++---- src/js/controllers/dialog.js | 78 ++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 25 deletions(-) diff --git a/src/env/test/ipservice/confirm.json b/src/env/test/ipservice/confirm.json index a62a982..f3769b0 100644 --- a/src/env/test/ipservice/confirm.json +++ b/src/env/test/ipservice/confirm.json @@ -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"] } diff --git a/src/js/controllers/dialog.js b/src/js/controllers/dialog.js index ef3d82f..c0fd047 100644 --- a/src/js/controllers/dialog.js +++ b/src/js/controllers/dialog.js @@ -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) { @@ -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 = { @@ -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'; @@ -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