diff --git a/dashboard/src/js/client.js b/dashboard/src/js/client.js index d7ba46520..6e3e4423d 100644 --- a/dashboard/src/js/client.js +++ b/dashboard/src/js/client.js @@ -1897,6 +1897,15 @@ angular.module('Application').service('Client', ['$http', '$interval', '$timeout }); }; + Client.prototype.detectIp = function (callback) { + post('/api/v1/provision/detect_ip', {}, null, function (error, data, status) { + if (error) return callback(error); + if (status !== 200) return callback(new ClientError(status, data)); + + callback(null, data); + }); + }; + Client.prototype.setup = function (data, callback) { post('/api/v1/provision/setup', data, null, function (error, data, status) { if (error) return callback(error); diff --git a/dashboard/src/js/restore.js b/dashboard/src/js/restore.js index b3cd81991..5f3c86eb7 100644 --- a/dashboard/src/js/restore.js +++ b/dashboard/src/js/restore.js @@ -381,7 +381,13 @@ app.controller('RestoreController', ['$scope', 'Client', function ($scope, Clien $scope.instanceId = search.instanceId; $scope.setupToken = search.setupToken; - $scope.initialized = true; + + Client.detectIp(function (error, ip) { // this is never supposed to error + if (!error) $scope.ipv4Config.provider = ip.ipv4 ? 'generic' : 'noop'; + if (!error) $scope.ipv6Config.provider = ip.ipv6 ? 'generic' : 'noop'; + + $scope.initialized = true; + }); }); }); } diff --git a/dashboard/src/js/setup.js b/dashboard/src/js/setup.js index 272d9fef5..24a5abab3 100644 --- a/dashboard/src/js/setup.js +++ b/dashboard/src/js/setup.js @@ -310,9 +310,15 @@ app.controller('SetupDNSController', ['$scope', '$http', '$timeout', 'Client', f $scope.instanceId = search.instanceId; $scope.setupToken = search.setupToken; $scope.provider = status.provider; - $scope.state = 'initialized'; - setTimeout(function () { $("[autofocus]:first").focus(); }, 100); + Client.detectIp(function (error, ip) { // this is never supposed to error + if (!error) $scope.ipv4Config.provider = ip.ipv4 ? 'generic' : 'noop'; + if (!error) $scope.ipv6Config.provider = ip.ipv6 ? 'generic' : 'noop'; + + $scope.state = 'initialized'; + + setTimeout(function () { $("[autofocus]:first").focus(); }, 100); + }); }); } diff --git a/src/network.js b/src/network.js index 22a7b0df4..a63eac78c 100644 --- a/src/network.js +++ b/src/network.js @@ -18,7 +18,8 @@ exports = module.exports = { getIPv4, hasIPv6, - getIPv6 + getIPv6, + detectIP }; const assert = require('assert'), @@ -162,3 +163,15 @@ async function getIPv6() { if (!result) return null; return ipaddr.parse(result).toRFC5952String(); } + +async function detectIP() { + const genericProvider = require('./network/generic.js'); + + const [error4, ipv4] = await safe(genericProvider.getIPv4({})); + const [error6, ipv6] = await safe(genericProvider.getIPv6({})); + + return { + ipv4: error4 ? null : ipv4, + ipv6: error6 ? null : ipv6 + }; +} diff --git a/src/routes/provision.js b/src/routes/provision.js index 5253d4f0e..b63a8babb 100644 --- a/src/routes/provision.js +++ b/src/routes/provision.js @@ -9,6 +9,7 @@ exports = module.exports = { getStatus, setupTokenAuth, getBlockDevices, + detectIP }; const assert = require('assert'), @@ -16,6 +17,7 @@ const assert = require('assert'), BoxError = require('../boxerror.js'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess, + network = require('../network.js'), paths = require('../paths.js'), provision = require('../provision.js'), safe = require('safetydance'), @@ -149,3 +151,8 @@ async function getBlockDevices(req, res, next) { next(new HttpSuccess(200, { devices })); } + +async function detectIP(req, res, next) { + const result = await network.detectIP(); + next(new HttpSuccess(200, { ipv4: result.ipv4, ipv6: result.ipv6 })); +} diff --git a/src/server.js b/src/server.js index 25845d089..e615c269a 100644 --- a/src/server.js +++ b/src/server.js @@ -87,6 +87,7 @@ async function initializeExpressSync() { router.post('/api/v1/provision/restore', json, verifyUnprovisioned, setupTokenAuth, routes.provision.restore); router.post('/api/v1/provision/activate', json, verifyUnprovisioned, setupTokenAuth, routes.provision.activate); router.get ('/api/v1/provision/block_devices', verifyUnprovisioned, routes.provision.getBlockDevices); + router.post('/api/v1/provision/detect_ip', verifyUnprovisioned, routes.provision.detectIP); router.get ('/api/v1/provision/status', routes.provision.getStatus); // auth routes