Files
cloudron-box/webadmin/src/js/setupdns.js

135 lines
4.4 KiB
JavaScript
Raw Normal View History

2017-01-05 11:02:43 +01:00
'use strict';
/* global tld */
2017-01-05 11:02:43 +01:00
// create main application module
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
2017-01-05 11:02:43 +01:00
app.filter('zoneName', function () {
return function (domain) {
return tld.getDomain(domain);
};
});
app.controller('SetupDNSController', ['$scope', '$http', 'Client', function ($scope, $http, Client) {
var search = decodeURIComponent(window.location.search).slice(1).split('&').map(function (item) { return item.split('='); }).reduce(function (o, k) { o[k[0]] = k[1]; return o; }, {});
2017-01-05 11:02:43 +01:00
$scope.initialized = false;
$scope.busy = false;
$scope.error = null;
$scope.provider = '';
$scope.showDNSSetup = false;
$scope.instanceId = '';
$scope.explicitZone = search.zone || '';
$scope.isDomain = false;
$scope.isSubdomain = false;
$scope.$watch('dnsCredentials.domain', function (newVal) {
if (!newVal) {
$scope.isDomain = false;
$scope.isSubdomain = false;
} else if (!tld.getDomain(newVal) || newVal[newVal.length-1] === '.') {
$scope.isDomain = false;
$scope.isSubdomain = false;
} else {
$scope.isDomain = true;
$scope.isSubdomain = tld.getDomain(newVal) !== newVal;
}
});
2017-01-28 18:27:22 -08:00
// keep in sync with certs.js
2017-01-05 11:02:43 +01:00
$scope.dnsProvider = [
{ name: 'AWS Route53', value: 'route53' },
2017-01-05 12:08:52 +01:00
{ name: 'Digital Ocean', value: 'digitalocean' },
2017-07-31 08:56:56 -07:00
{ name: 'Cloudflare (DNS only)', value: 'cloudflare' },
2017-01-05 12:08:52 +01:00
{ name: 'Wildcard', value: 'wildcard' },
2017-01-10 12:34:28 +01:00
{ name: 'Manual (not recommended)', value: 'manual' },
2017-01-05 12:08:52 +01:00
{ name: 'No-op (only for development)', value: 'noop' }
2017-01-05 11:02:43 +01:00
];
$scope.dnsCredentials = {
error: null,
busy: false,
domain: '',
accessKeyId: '',
secretAccessKey: '',
digitalOceanToken: '',
provider: 'route53'
};
2017-01-05 12:31:37 +01:00
$scope.setDnsCredentials = function () {
$scope.dnsCredentials.busy = true;
$scope.dnsCredentials.error = null;
$scope.error = null;
2017-01-05 11:02:43 +01:00
var data = {
domain: $scope.dnsCredentials.domain,
zoneName: $scope.explicitZone,
2017-01-05 11:02:43 +01:00
provider: $scope.dnsCredentials.provider,
providerToken: $scope.instanceId
2017-01-05 11:02:43 +01:00
};
// special case the wildcard provider
if (data.provider === 'wildcard') {
data.provider = 'manual';
data.wildcard = true;
}
2017-04-26 10:50:27 +02:00
if (data.provider === 'route53') {
data.accessKeyId = $scope.dnsCredentials.accessKeyId;
data.secretAccessKey = $scope.dnsCredentials.secretAccessKey;
} else if (data.provider === 'digitalocean') {
data.token = $scope.dnsCredentials.digitalOceanToken;
} else if (data.provider === 'cloudflare') {
data.email = $scope.dnsCredentials.cloudflareEmail;
data.token = $scope.dnsCredentials.cloudflareToken;
}
2017-01-05 11:53:45 +01:00
Client.setupDnsConfig(data, function (error) {
if (error && error.statusCode === 403) {
$scope.dnsCredentials.busy = false;
$scope.error = 'Wrong instance id provided.';
return;
} else if (error) {
2017-01-05 12:31:37 +01:00
$scope.dnsCredentials.busy = false;
2017-01-05 11:02:43 +01:00
$scope.dnsCredentials.error = error.message;
return;
}
waitForDnsSetup();
});
};
function waitForDnsSetup() {
$scope.busy = true;
Client.getStatus(function (error, status) {
if (!error && status.adminFqdn && status.webadminStatus.dns && status.webadminStatus.tls) {
2017-01-10 23:35:15 +01:00
window.location.href = 'https://' + status.adminFqdn + '/setup.html';
}
setTimeout(waitForDnsSetup, 5000);
2017-01-05 11:02:43 +01:00
});
}
2017-01-05 11:02:43 +01:00
Client.getStatus(function (error, status) {
if (error) {
window.location.href = '/error.html';
return;
}
// domain is currently like a lock flag
if (status.adminFqdn) return waitForDnsSetup();
2017-01-05 11:02:43 +01:00
2017-01-05 16:32:34 +01:00
if (status.provider === 'digitalocean') $scope.dnsCredentials.provider = 'digitalocean';
if (status.provider === 'ami') {
// remove route53 on ami
$scope.dnsProvider.shift();
$scope.dnsCredentials.provider = 'wildcard';
}
2017-01-05 16:32:34 +01:00
$scope.instanceId = search.instanceId;
2017-01-05 11:02:43 +01:00
$scope.provider = status.provider;
$scope.initialized = true;
});
}]);