2017-01-05 11:02:43 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
2017-06-20 11:42:01 +02:00
|
|
|
/* global tld */
|
|
|
|
|
|
2017-01-05 11:02:43 +01:00
|
|
|
// create main application module
|
2017-06-16 10:24:29 +02:00
|
|
|
var app = angular.module('Application', ['angular-md5', 'ui-notification']);
|
2017-01-05 11:02:43 +01:00
|
|
|
|
2017-06-16 10:24:29 +02:00
|
|
|
app.filter('zoneName', function () {
|
|
|
|
|
return function (domain) {
|
|
|
|
|
return tld.getDomain(domain);
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
app.controller('SetupDNSController', ['$scope', '$http', 'Client', function ($scope, $http, Client) {
|
2017-04-03 21:27:40 +02:00
|
|
|
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;
|
2017-04-03 21:27:40 +02:00
|
|
|
$scope.instanceId = '';
|
2017-06-16 10:24:29 +02:00
|
|
|
$scope.explicitZone = search.zone || '';
|
2017-10-04 22:22:23 +02:00
|
|
|
$scope.isEnterprise = !!search.enterprise;
|
2017-06-20 11:42:01 +02:00
|
|
|
$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
|
|
|
|
2017-11-07 00:51:13 +01:00
|
|
|
// keep in sync with domains.js
|
2017-01-05 11:02:43 +01:00
|
|
|
$scope.dnsProvider = [
|
|
|
|
|
{ name: 'AWS Route53', value: 'route53' },
|
2017-07-31 08:56:56 -07:00
|
|
|
{ name: 'Cloudflare (DNS only)', value: 'cloudflare' },
|
2017-09-14 18:12:07 -07:00
|
|
|
{ name: 'Digital Ocean', value: 'digitalocean' },
|
|
|
|
|
{ name: 'Google Cloud DNS', value: 'gcdns' },
|
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: '',
|
2017-09-14 18:12:07 -07:00
|
|
|
gcdnsKey: { keyFileName: '', content: '' },
|
2017-01-05 11:02:43 +01:00
|
|
|
digitalOceanToken: '',
|
|
|
|
|
provider: 'route53'
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-13 20:53:57 +02:00
|
|
|
function readFileLocally(obj, file, fileName) {
|
|
|
|
|
return function (event) {
|
|
|
|
|
$scope.$apply(function () {
|
|
|
|
|
obj[file] = null;
|
|
|
|
|
obj[fileName] = event.target.files[0].name;
|
|
|
|
|
|
|
|
|
|
var reader = new FileReader();
|
|
|
|
|
reader.onload = function (result) {
|
|
|
|
|
if (!result.target || !result.target.result) return console.error('Unable to read local file');
|
|
|
|
|
obj[file] = result.target.result;
|
|
|
|
|
};
|
|
|
|
|
reader.readAsText(event.target.files[0]);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.getElementById('gcdnsKeyFileInput').onchange = readFileLocally($scope.dnsCredentials.gcdnsKey, 'content', 'keyFileName');
|
|
|
|
|
|
2017-01-05 12:31:37 +01:00
|
|
|
$scope.setDnsCredentials = function () {
|
|
|
|
|
$scope.dnsCredentials.busy = true;
|
2017-04-03 22:36:02 +02:00
|
|
|
$scope.dnsCredentials.error = null;
|
|
|
|
|
$scope.error = null;
|
2017-01-05 11:02:43 +01:00
|
|
|
|
|
|
|
|
var data = {
|
|
|
|
|
domain: $scope.dnsCredentials.domain,
|
2017-06-16 10:24:29 +02:00
|
|
|
zoneName: $scope.explicitZone,
|
2017-01-05 11:02:43 +01:00
|
|
|
provider: $scope.dnsCredentials.provider,
|
2017-04-03 21:27:40 +02:00
|
|
|
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;
|
2017-09-13 20:53:57 +02:00
|
|
|
} else if (data.provider === 'gcdns'){
|
|
|
|
|
try {
|
|
|
|
|
var serviceAccountKey = JSON.parse($scope.dnsCredentials.gcdnsKey.content);
|
|
|
|
|
data.projectId = serviceAccountKey.project_id;
|
|
|
|
|
data.credentials = {
|
|
|
|
|
client_email: serviceAccountKey.client_email,
|
|
|
|
|
private_key: serviceAccountKey.private_key
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!data.projectId || !data.credentials || !data.credentials.client_email || !data.credentials.private_key) {
|
2017-11-22 12:02:01 -08:00
|
|
|
throw 'fields_missing';
|
2017-09-13 20:53:57 +02:00
|
|
|
}
|
|
|
|
|
} catch(e) {
|
2017-11-22 12:02:01 -08:00
|
|
|
$scope.dnsCredentials.error = 'Cannot parse Google Service Account Key';
|
2017-09-13 20:53:57 +02:00
|
|
|
$scope.dnsCredentials.busy = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-04-26 10:50:27 +02:00
|
|
|
} 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) {
|
2017-04-03 21:27:40 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-05 20:47:47 -08:00
|
|
|
waitForDnsSetup();
|
2017-11-22 12:02:01 -08:00
|
|
|
});
|
2017-01-05 20:47:47 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function waitForDnsSetup() {
|
2017-01-06 10:54:56 +01:00
|
|
|
$scope.busy = true;
|
|
|
|
|
|
2017-01-05 20:47:47 -08:00
|
|
|
Client.getStatus(function (error, status) {
|
2017-08-28 21:00:29 -07:00
|
|
|
// webadminStatus.dns is intentionally not tested. it can be false if dns creds are invalid
|
|
|
|
|
// runConfigurationChecks() in main.js will pick the .dns and show a notification
|
|
|
|
|
if (!error && status.adminFqdn && status.webadminStatus.tls) {
|
2017-01-10 23:35:15 +01:00
|
|
|
window.location.href = 'https://' + status.adminFqdn + '/setup.html';
|
2017-01-05 20:47:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setTimeout(waitForDnsSetup, 5000);
|
2017-01-05 11:02:43 +01:00
|
|
|
});
|
2017-01-05 20:47:47 -08:00
|
|
|
}
|
2017-01-05 11:02:43 +01:00
|
|
|
|
|
|
|
|
Client.getStatus(function (error, status) {
|
|
|
|
|
if (error) {
|
|
|
|
|
window.location.href = '/error.html';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-06 10:54:56 +01:00
|
|
|
// domain is currently like a lock flag
|
2017-01-10 22:19:41 +01:00
|
|
|
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';
|
2017-09-13 20:53:57 +02:00
|
|
|
if (status.provider === 'gcp') $scope.dnsCredentials.provider = 'gcdns';
|
2017-03-14 16:54:46 +01:00
|
|
|
if (status.provider === 'ami') {
|
|
|
|
|
// remove route53 on ami
|
|
|
|
|
$scope.dnsProvider.shift();
|
|
|
|
|
$scope.dnsCredentials.provider = 'wildcard';
|
|
|
|
|
}
|
2017-01-05 16:32:34 +01:00
|
|
|
|
2017-04-03 21:27:40 +02:00
|
|
|
$scope.instanceId = search.instanceId;
|
2017-01-05 11:02:43 +01:00
|
|
|
$scope.provider = status.provider;
|
|
|
|
|
$scope.initialized = true;
|
|
|
|
|
});
|
|
|
|
|
}]);
|