Files
cloudron-box/src/views/domains.js

718 lines
29 KiB
JavaScript
Raw Normal View History

2018-01-22 13:01:38 -08:00
'use strict';
2020-06-03 23:17:06 +02:00
/* global async */
/* global angular */
2021-02-24 22:18:39 -08:00
/* global $, TASK_TYPES */
angular.module('Application').controller('DomainsController', ['$scope', '$location', 'Client', function ($scope, $location, Client) {
2020-02-24 12:56:13 +01:00
Client.onReady(function () { if (!Client.getUserInfo().isAtLeastAdmin) $location.path('/'); });
2018-01-22 13:01:38 -08:00
$scope.config = Client.getConfig();
$scope.domains = [];
$scope.ready = false;
2020-11-11 17:39:19 +01:00
$scope.translationLinks = {
linodeDocsLink: 'https://docs.cloudron.io/domains/#linode-dns',
customCertLink: 'https://docs.cloudron.io/certificates/#custom-certificates'
};
$scope.openSubscriptionSetup = function () {
Client.openSubscriptionSetup($scope.$parent.subscription);
};
2018-09-12 11:45:07 -07:00
// currently, validation of wildcard with various provider is done server side
$scope.tlsProvider = [
{ name: 'Let\'s Encrypt Prod', value: 'letsencrypt-prod' },
{ name: 'Let\'s Encrypt Prod - Wildcard', value: 'letsencrypt-prod-wildcard' },
{ name: 'Let\'s Encrypt Staging', value: 'letsencrypt-staging' },
{ name: 'Let\'s Encrypt Staging - Wildcard', value: 'letsencrypt-staging-wildcard' },
{ name: 'Custom Wildcard Certificate', value: 'fallback' },
];
2018-01-22 13:01:38 -08:00
// keep in sync with setupdns.js
$scope.dnsProvider = [
{ name: 'AWS Route53', value: 'route53' },
2018-10-26 15:03:27 -07:00
{ name: 'Cloudflare', value: 'cloudflare' },
2019-03-11 18:43:23 -07:00
{ name: 'DigitalOcean', value: 'digitalocean' },
{ name: 'Gandi LiveDNS', value: 'gandi' },
2018-05-06 21:52:25 -07:00
{ name: 'GoDaddy', value: 'godaddy' },
2018-01-22 13:01:38 -08:00
{ name: 'Google Cloud DNS', value: 'gcdns' },
2022-05-02 22:08:49 -07:00
{ name: 'Hetzner', value: 'hetzner' },
2020-03-12 17:13:21 -07:00
{ name: 'Linode', value: 'linode' },
2018-05-09 12:24:46 +02:00
{ name: 'Name.com', value: 'namecom' },
2019-01-22 11:26:24 +01:00
{ name: 'Namecheap', value: 'namecheap' },
2021-01-18 19:43:20 +01:00
{ name: 'Netcup', value: 'netcup' },
2021-05-29 22:30:52 -07:00
{ name: 'Vultr', value: 'vultr' },
2018-01-22 13:01:38 -08:00
{ name: 'Wildcard', value: 'wildcard' },
{ name: 'Manual (not recommended)', value: 'manual' },
{ name: 'No-op (only for development)', value: 'noop' }
];
$scope.prettyProviderName = function (domain) {
switch (domain.provider) {
2020-03-12 17:07:17 -07:00
case 'route53': return 'AWS Route53';
case 'cloudflare': return 'Cloudflare';
case 'digitalocean': return 'DigitalOcean';
case 'gandi': return 'Gandi LiveDNS';
2022-05-02 22:08:49 -07:00
case 'hetzner': return 'Hetzner DNS';
2020-03-12 17:13:21 -07:00
case 'linode': return 'Linode';
2020-03-12 17:07:17 -07:00
case 'namecom': return 'Name.com';
case 'namecheap': return 'Namecheap';
2021-01-18 19:43:20 +01:00
case 'netcup': return 'Netcup';
2020-03-12 17:07:17 -07:00
case 'gcdns': return 'Google Cloud';
case 'godaddy': return 'GoDaddy';
2021-05-29 22:30:52 -07:00
case 'vultr': return 'Vultr';
2020-03-12 17:07:17 -07:00
case 'manual': return 'Manual';
case 'wildcard': return 'Wildcard';
case 'noop': return 'No-op';
default: return 'Unknown';
}
};
2018-09-12 14:40:12 -07:00
$scope.needsPort80 = function (dnsProvider, tlsProvider) {
return ((dnsProvider === 'manual' || dnsProvider === 'noop' || dnsProvider === 'wildcard') &&
(tlsProvider === 'letsencrypt-prod' || tlsProvider === 'letsencrypt-staging'));
};
2018-01-22 13:01:38 -08: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]);
});
};
}
2019-01-06 14:52:36 -08:00
function refreshDomains(callback) {
var domains = [ ];
Client.getDomains(function (error, results) {
if (error) return console.error(error);
2020-06-03 23:17:06 +02:00
async.eachSeries(results, function (result, iteratorDone) {
Client.getDomain(result.domain, function (error, domain) {
if (error) return iteratorDone(error);
domains.push(domain);
iteratorDone();
});
}, function (error) {
2019-01-06 14:52:36 -08:00
angular.copy(domains, $scope.domains);
$scope.changeDashboard.selectedDomain = $scope.changeDashboard.adminDomain = $scope.domains.find(function (d) { return d.domain === $scope.config.adminDomain; });
if (error) console.error(error);
if (callback) callback(error);
});
});
}
$scope.domainAdd = {
show: function () {
$scope.domainConfigure.show();
}
};
$scope.domainWellKnown = {
busy: false,
error: null,
domain: null,
mastodonHostname: '',
matrixHostname: '',
2021-12-03 19:16:30 -08:00
jitsiHostname: '',
reset: function () {
$scope.domainWellKnown.busy = false;
$scope.domainWellKnown.error = null;
$scope.domainWellKnown.domain = null;
$scope.domainWellKnown.matrixHostname = '';
$scope.domainWellKnown.mastodonHostname = '';
2021-12-03 19:16:30 -08:00
$scope.domainWellKnown.jitsiHostname = '';
},
show: function (domain) {
$scope.domainWellKnown.reset();
$scope.domainWellKnown.domain = domain;
try {
if (domain.wellKnown && domain.wellKnown['matrix/server']) {
$scope.domainWellKnown.matrixHostname = JSON.parse(domain.wellKnown['matrix/server'])['m.server'];
}
if (domain.wellKnown && domain.wellKnown['host-meta']) {
$scope.domainWellKnown.mastodonHostname = domain.wellKnown['host-meta'].match(new RegExp('template="https://(.*?)/'))[1];
}
2021-12-03 19:16:30 -08:00
if (domain.wellKnown && domain.wellKnown['matrix/client']) {
let parsed = JSON.parse(domain.wellKnown['matrix/client']);
if (parsed['im.vector.riot.jitsi'] && parsed['im.vector.riot.jitsi']['preferredDomain']) {
$scope.domainWellKnown.jitsiHostname = parsed['im.vector.riot.jitsi']['preferredDomain'];
}
}
} catch (e) {
console.error(e);
}
$('#domainWellKnownModal').modal('show');
},
submit: function () {
$scope.domainWellKnown.busy = true;
$scope.domainWellKnown.error = null;
var wellKnown = {};
if ($scope.domainWellKnown.matrixHostname) {
wellKnown['matrix/server'] = JSON.stringify({ 'm.server': $scope.domainWellKnown.matrixHostname });
// https://matrix.org/docs/spec/client_server/latest#get-well-known-matrix-client
wellKnown['matrix/client'] = JSON.stringify({
'm.homeserver': {
'base_url': 'https://' + $scope.domainWellKnown.matrixHostname
2021-12-03 19:16:30 -08:00
},
'im.vector.riot.jitsi': {
'preferredDomain': $scope.domainWellKnown.jitsiHostname
}
});
} else if ($scope.domainWellKnown.jitsiHostname) { // only if matrixHostname is not set
wellKnown['matrix/client'] = JSON.stringify({
'im.vector.riot.jitsi': {
'preferredDomain': $scope.domainWellKnown.jitsiHostname
}
});
}
if ($scope.domainWellKnown.mastodonHostname) {
wellKnown['host-meta'] = '<?xml version="1.0" encoding="UTF-8"?>\n'
+ '<XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">\n'
+ '<Link rel="lrdd" type="application/xrd+xml" template="https://' + $scope.domainWellKnown.mastodonHostname + '/.well-known/webfinger?resource={uri}"/>\n'
+ '</XRD>';
}
Client.updateDomainWellKnown($scope.domainWellKnown.domain.domain, wellKnown, function (error) {
$scope.domainWellKnown.busy = false;
if (error) {
$scope.domainWellKnown.error = error.message;
return;
}
$('#domainWellKnownModal').modal('hide');
$scope.domainWellKnown.reset();
refreshDomains();
});
}
};
2018-01-22 13:01:38 -08:00
// We reused configure also for adding domains to avoid much code duplication
$scope.domainConfigure = {
adding: false,
error: null,
busy: false,
domain: null,
advancedVisible: false,
2018-01-22 13:01:38 -08:00
// form model
newDomain: '',
accessKeyId: '',
secretAccessKey: '',
gcdnsKey: { keyFileName: '', content: '' },
digitalOceanToken: '',
gandiApiKey: '',
2018-05-06 21:52:25 -07:00
godaddyApiKey: '',
godaddyApiSecret: '',
2018-01-22 13:01:38 -08:00
cloudflareToken: '',
cloudflareEmail: '',
2019-12-31 16:45:40 -08:00
cloudflareTokenType: 'GlobalApiKey',
2020-03-12 17:13:21 -07:00
linodeToken: '',
2022-05-02 22:08:49 -07:00
hetznerToken: '',
2021-05-29 22:30:52 -07:00
vultrToken: '',
2018-05-09 12:24:46 +02:00
nameComToken: '',
nameComUsername: '',
2019-01-22 11:26:24 +01:00
namecheapUsername: '',
namecheapApiKey: '',
2021-01-18 19:43:20 +01:00
netcupCustomerNumber: '',
netcupApiKey: '',
netcupApiPassword: '',
2018-01-22 13:01:38 -08:00
provider: 'route53',
zoneName: '',
tlsConfig: {
provider: 'letsencrypt-prod-wildcard'
},
2018-01-22 13:01:38 -08:00
fallbackCert: {
certificateFile: null,
certificateFileName: '',
keyFile: null,
keyFileName: ''
},
setDefaultTlsProvider: function () {
var dnsProvider = $scope.domainConfigure.provider;
// wildcard LE won't work without automated DNS
if (dnsProvider === 'manual' || dnsProvider === 'noop' || dnsProvider === 'wildcard') {
$scope.domainConfigure.tlsConfig.provider = 'letsencrypt-prod';
} else {
$scope.domainConfigure.tlsConfig.provider = 'letsencrypt-prod-wildcard';
}
},
2018-01-22 13:01:38 -08:00
show: function (domain) {
$scope.domainConfigure.reset();
if (domain) {
$scope.domainConfigure.domain = domain;
$scope.domainConfigure.accessKeyId = domain.config.accessKeyId;
$scope.domainConfigure.secretAccessKey = domain.config.secretAccessKey;
$scope.domainConfigure.gcdnsKey.keyFileName = '';
$scope.domainConfigure.gcdnsKey.content = '';
2020-03-04 18:53:53 -08:00
if (domain.provider === 'gcdns') {
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.gcdnsKey.keyFileName = domain.config.credentials && domain.config.credentials.client_email;
2020-03-17 22:51:47 -07:00
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.gcdnsKey.content = JSON.stringify({
2019-05-20 18:36:23 -07:00
project_id: domain.config.projectId,
2020-03-17 22:51:47 -07:00
client_email: domain.config.credentials.client_email,
private_key: domain.config.credentials.private_key
2018-01-22 13:01:38 -08:00
});
}
$scope.domainConfigure.digitalOceanToken = domain.provider === 'digitalocean' ? domain.config.token : '';
2020-03-12 17:13:21 -07:00
$scope.domainConfigure.linodeToken = domain.provider === 'linode' ? domain.config.token : '';
2022-05-02 22:08:49 -07:00
$scope.domainConfigure.hetznerToken = domain.provider === 'hetzner' ? domain.config.token : '';
2021-05-29 22:30:52 -07:00
$scope.domainConfigure.vultrToken = domain.provider === 'vultr' ? domain.config.token : '';
$scope.domainConfigure.gandiApiKey = domain.provider === 'gandi' ? domain.config.token : '';
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.cloudflareToken = domain.provider === 'cloudflare' ? domain.config.token : '';
2018-05-06 21:52:25 -07:00
$scope.domainConfigure.cloudflareEmail = domain.provider === 'cloudflare' ? domain.config.email : '';
2020-01-01 16:02:50 -08:00
$scope.domainConfigure.cloudflareTokenType = domain.provider === 'cloudflare' ? domain.config.tokenType : 'GlobalApiKey';
2018-05-06 21:52:25 -07:00
$scope.domainConfigure.godaddyApiKey = domain.provider === 'godaddy' ? domain.config.apiKey : '';
$scope.domainConfigure.godaddyApiSecret = domain.provider === 'godaddy' ? domain.config.apiSecret : '';
2018-01-22 13:01:38 -08:00
2018-05-09 12:24:46 +02:00
$scope.domainConfigure.nameComToken = domain.provider === 'namecom' ? domain.config.token : '';
$scope.domainConfigure.nameComUsername = domain.provider === 'namecom' ? domain.config.username : '';
2019-02-08 20:33:21 -08:00
$scope.domainConfigure.namecheapApiKey = domain.provider === 'namecheap' ? domain.config.token : '';
2019-01-22 11:26:24 +01:00
$scope.domainConfigure.namecheapUsername = domain.provider === 'namecheap' ? domain.config.username : '';
2021-01-18 19:43:20 +01:00
$scope.domainConfigure.netcupCustomerNumber = domain.provider === 'netcup' ? domain.config.customerNumber : '';
$scope.domainConfigure.netcupApiKey = domain.provider === 'netcup' ? domain.config.apiKey : '';
$scope.domainConfigure.netcupApiPassword = domain.provider === 'netcup' ? domain.config.apiPassword : '';
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.provider = domain.provider;
$scope.domainConfigure.tlsConfig.provider = domain.tlsConfig.provider;
2018-09-12 11:45:07 -07:00
if (domain.tlsConfig.provider.indexOf('letsencrypt') === 0) {
if (domain.tlsConfig.wildcard) $scope.domainConfigure.tlsConfig.provider += '-wildcard';
}
$scope.domainConfigure.zoneName = domain.zoneName;
2018-01-22 13:01:38 -08:00
} else {
$scope.domainConfigure.adding = true;
}
$('#domainConfigureModal').modal('show');
},
submit: function () {
$scope.domainConfigure.busy = true;
$scope.domainConfigure.error = null;
var provider = $scope.domainConfigure.provider;
var data = {};
2018-01-22 13:01:38 -08:00
if (provider === 'route53') {
data.accessKeyId = $scope.domainConfigure.accessKeyId;
data.secretAccessKey = $scope.domainConfigure.secretAccessKey;
2018-05-07 13:18:51 -07:00
} else if (provider === 'gcdns') {
2018-01-22 13:01:38 -08:00
try {
var serviceAccountKey = JSON.parse($scope.domainConfigure.gcdnsKey.content);
data.projectId = serviceAccountKey.project_id;
data.credentials = {
2020-03-17 22:51:47 -07:00
client_email: serviceAccountKey.client_email,
private_key: serviceAccountKey.private_key
2018-01-22 13:01:38 -08:00
};
if (!data.projectId || !data.credentials || !data.credentials.client_email || !data.credentials.private_key) {
2020-03-03 11:07:56 -08:00
throw new Error('One or more fields are missing in the JSON');
2018-01-22 13:01:38 -08:00
}
} catch (e) {
$scope.domainConfigure.error = 'Cannot parse Google Service Account Key: ' + e.message;
$scope.domainConfigure.busy = false;
return;
}
} else if (provider === 'digitalocean') {
data.token = $scope.domainConfigure.digitalOceanToken;
2020-03-12 17:13:21 -07:00
} else if (provider === 'linode') {
data.token = $scope.domainConfigure.linodeToken;
2022-05-02 22:08:49 -07:00
} else if (provider === 'hetzner') {
data.token = $scope.domainConfigure.hetznerToken;
2021-05-29 22:30:52 -07:00
} else if (provider === 'vultr') {
data.token = $scope.domainConfigure.vultrToken;
} else if (provider === 'gandi') {
data.token = $scope.domainConfigure.gandiApiKey;
2018-05-06 21:52:25 -07:00
} else if (provider === 'godaddy') {
data.apiKey = $scope.domainConfigure.godaddyApiKey;
data.apiSecret = $scope.domainConfigure.godaddyApiSecret;
2018-01-22 13:01:38 -08:00
} else if (provider === 'cloudflare') {
data.token = $scope.domainConfigure.cloudflareToken;
data.email = $scope.domainConfigure.cloudflareEmail;
2019-12-31 16:45:40 -08:00
data.tokenType = $scope.domainConfigure.cloudflareTokenType;
2018-05-09 12:24:46 +02:00
} else if (provider === 'namecom') {
data.token = $scope.domainConfigure.nameComToken;
data.username = $scope.domainConfigure.nameComUsername;
2019-01-22 11:26:24 +01:00
} else if (provider === 'namecheap') {
2019-02-08 20:33:21 -08:00
data.token = $scope.domainConfigure.namecheapApiKey;
2019-01-22 11:26:24 +01:00
data.username = $scope.domainConfigure.namecheapUsername;
2021-01-18 19:43:20 +01:00
} else if (provider === 'netcup') {
data.customerNumber = $scope.domainConfigure.netcupCustomerNumber;
data.apiKey = $scope.domainConfigure.netcupApiKey;
data.apiPassword = $scope.domainConfigure.netcupApiPassword;
2018-01-22 13:01:38 -08:00
}
var fallbackCertificate = null;
if ($scope.domainConfigure.fallbackCert.certificateFile && $scope.domainConfigure.fallbackCert.keyFile) {
fallbackCertificate = {
cert: $scope.domainConfigure.fallbackCert.certificateFile,
key: $scope.domainConfigure.fallbackCert.keyFile
};
}
2018-09-12 11:45:07 -07:00
var tlsConfig = {
provider: $scope.domainConfigure.tlsConfig.provider,
wildcard: false
};
if ($scope.domainConfigure.tlsConfig.provider.indexOf('-wildcard') !== -1) {
tlsConfig.provider = tlsConfig.provider.replace('-wildcard', '');
tlsConfig.wildcard = true;
}
2018-01-22 13:01:38 -08:00
// choose the right api, since we reuse this for adding and configuring domains
var func;
if ($scope.domainConfigure.adding) func = Client.addDomain.bind(Client, $scope.domainConfigure.newDomain, $scope.domainConfigure.zoneName, provider, data, fallbackCertificate, tlsConfig);
else func = Client.updateDomainConfig.bind(Client, $scope.domainConfigure.domain.domain, $scope.domainConfigure.zoneName, provider, data, fallbackCertificate, tlsConfig);
2018-01-22 13:01:38 -08:00
func(function (error) {
$scope.domainConfigure.busy = false;
if (error) {
$scope.domainConfigure.error = error.message;
return;
}
$('#domainConfigureModal').modal('hide');
$scope.domainConfigure.reset();
2019-01-06 14:52:36 -08:00
refreshDomains();
2018-01-22 13:01:38 -08:00
});
},
reset: function () {
$scope.domainConfigure.adding = false;
$scope.domainConfigure.advancedVisible = false;
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.newDomain = '';
$scope.domainConfigure.busy = false;
$scope.domainConfigure.error = null;
$scope.domainConfigure.provider = '';
$scope.domainConfigure.accessKeyId = '';
$scope.domainConfigure.secretAccessKey = '';
$scope.domainConfigure.gcdnsKey.keyFileName = '';
$scope.domainConfigure.gcdnsKey.content = '';
$scope.domainConfigure.digitalOceanToken = '';
$scope.domainConfigure.gandiApiKey = '';
2018-05-06 21:52:25 -07:00
$scope.domainConfigure.godaddyApiKey = '';
$scope.domainConfigure.godaddyApiSecret = '';
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.cloudflareToken = '';
$scope.domainConfigure.cloudflareEmail = '';
2020-01-01 16:02:50 -08:00
$scope.domainConfigure.cloudflareTokenType = 'GlobalApiKey';
2018-05-09 12:24:46 +02:00
$scope.domainConfigure.nameComToken = '';
$scope.domainConfigure.nameComUsername = '';
2019-01-22 11:26:24 +01:00
$scope.domainConfigure.namecheapApiKey = '';
$scope.domainConfigure.namecheapUsername = '';
2021-01-18 19:43:20 +01:00
$scope.domainConfigure.netcupCustomerNumber = '';
$scope.domainConfigure.netcupApiKey = '';
$scope.domainConfigure.netcupApiPassword = '';
2021-06-15 11:04:34 -07:00
$scope.domainConfigure.vultrToken = '';
2018-01-22 13:01:38 -08:00
$scope.domainConfigure.tlsConfig.provider = 'letsencrypt-prod';
$scope.domainConfigure.zoneName = '';
2018-01-22 13:01:38 -08:00
$scope.domainConfigureForm.$setPristine();
$scope.domainConfigureForm.$setUntouched();
}
};
$scope.renewCerts = {
2018-10-24 15:24:23 -07:00
busy: false,
percent: 0,
message: '',
errorMessage: '',
taskId: '',
checkStatus: function () {
2023-01-25 10:06:35 +01:00
Client.getLatestTaskByType(TASK_TYPES.TASK_CHECK_CERTS, function (error, task) {
if (error) return console.error(error);
2018-10-24 15:24:23 -07:00
if (!task) return;
$scope.renewCerts.taskId = task.id;
$scope.renewCerts.updateStatus();
});
2018-10-24 15:24:23 -07:00
},
updateStatus: function () {
Client.getTask($scope.renewCerts.taskId, function (error, data) {
if (error) return window.setTimeout($scope.renewCerts.updateStatus, 5000);
2018-10-24 15:24:23 -07:00
if (!data.active) {
$scope.renewCerts.busy = false;
$scope.renewCerts.message = '';
$scope.renewCerts.percent = 100; // indicates that 'result' is valid
$scope.renewCerts.errorMessage = data.success ? '' : data.error.message;
return;
2018-10-24 15:24:23 -07:00
}
$scope.renewCerts.busy = true;
$scope.renewCerts.percent = data.percent;
$scope.renewCerts.message = data.message;
window.setTimeout($scope.renewCerts.updateStatus, 500);
2018-10-24 15:24:23 -07:00
});
},
renew: function () {
$scope.renewCerts.busy = true;
$scope.renewCerts.percent = 0;
$scope.renewCerts.message = '';
$scope.renewCerts.errorMessage = '';
2022-11-29 18:12:51 +01:00
// always rebuild the nginx configs when triggered via the UI. we assume user is clicking this because something is wrong
Client.renewCerts({ rebuild: true }, function (error, taskId) {
if (error) {
console.error(error);
$scope.renewCerts.errorMessage = error.message;
$scope.renewCerts.busy = false;
} else {
$scope.renewCerts.taskId = taskId;
$scope.renewCerts.updateStatus();
}
});
2018-10-24 15:24:23 -07:00
}
};
2021-02-24 22:18:39 -08:00
$scope.syncDns = {
busy: false,
percent: 0,
message: '',
errorMessage: '',
taskId: '',
checkStatus: function () {
Client.getLatestTaskByType(TASK_TYPES.TASK_SYNC_DNS_RECORDS, function (error, task) {
if (error) return console.error(error);
if (!task) return;
$scope.syncDns.taskId = task.id;
$scope.syncDns.updateStatus();
});
},
updateStatus: function () {
Client.getTask($scope.syncDns.taskId, function (error, data) {
if (error) return window.setTimeout($scope.syncDns.updateStatus, 5000);
if (!data.active) {
$scope.syncDns.busy = false;
$scope.syncDns.message = '';
$scope.syncDns.percent = 100; // indicates that 'result' is valid
$scope.syncDns.errorMessage = data.success ? '' : data.error.message;
return;
}
$scope.syncDns.busy = true;
$scope.syncDns.percent = data.percent;
$scope.syncDns.message = data.message;
window.setTimeout($scope.syncDns.updateStatus, 500);
});
},
sync: function () {
$scope.syncDns.busy = true;
$scope.syncDns.percent = 0;
$scope.syncDns.message = '';
$scope.syncDns.errorMessage = '';
Client.setDnsRecords({}, function (error, taskId) {
if (error) {
console.error(error);
$scope.syncDns.errorMessage = error.message;
$scope.syncDns.busy = false;
} else {
$scope.syncDns.taskId = taskId;
$scope.syncDns.updateStatus();
}
});
}
};
2018-01-22 13:01:38 -08:00
$scope.domainRemove = {
busy: false,
error: null,
domain: null,
show: function (domain) {
$scope.domainRemove.reset();
$scope.domainRemove.domain = domain;
$('#domainRemoveModal').modal('show');
},
submit: function () {
$scope.domainRemove.busy = true;
$scope.domainRemove.error = null;
Client.removeDomain($scope.domainRemove.domain.domain, function (error) {
2018-06-18 18:57:00 -07:00
if (error && (error.statusCode === 403 || error.statusCode === 409)) {
2018-01-22 13:01:38 -08:00
$scope.domainRemove.error = error.message;
} else if (error) {
Client.error(error);
} else {
$('#domainRemoveModal').modal('hide');
$scope.domainRemove.reset();
2019-01-06 14:52:36 -08:00
refreshDomains();
2018-01-22 13:01:38 -08:00
}
$scope.domainRemove.busy = false;
});
},
reset: function () {
$scope.domainRemove.busy = false;
$scope.domainRemove.error = null;
$scope.domainRemove.domain = null;
}
};
2018-12-18 15:05:11 -08:00
$scope.changeDashboard = {
busy: false,
percent: 0,
message: '',
errorMessage: '',
taskId: '',
selectedDomain: null,
adminDomain: null,
2019-01-06 14:52:36 -08:00
stop: function () {
Client.stopTask($scope.changeDashboard.taskId, function (error) {
if (error) console.error(error);
$scope.changeDashboard.busy = false;
});
},
// this function is not called intentionally. currently, we do switching in two steps - prepare and set
// if the user refreshed the UI in the middle of prepare, then it would be awkward to resume/call 'set' when the
// user visits the UI the next time around.
checkStatus: function () {
Client.getLatestTaskByType('prepareDashboardDomain', function (error, task) {
if (error) return console.error(error);
if (!task) return;
$scope.changeDashboard.taskId = task.id;
$scope.changeDashboard.updateStatus();
});
},
2018-12-18 15:05:11 -08:00
updateStatus: function () {
2019-01-06 14:52:36 -08:00
if (!$scope.changeDashboard.busy) return; // task got stopped
2018-12-18 15:05:11 -08:00
Client.getTask($scope.changeDashboard.taskId, function (error, data) {
if (error) return window.setTimeout($scope.changeDashboard.updateStatus, 5000);
if (!data.active) {
$scope.changeDashboard.busy = false;
$scope.changeDashboard.message = '';
$scope.changeDashboard.percent = 100; // indicates that 'result' is valid
$scope.changeDashboard.errorMessage = data.success ? '' : data.error.message;
2018-12-18 15:05:11 -08:00
if (!$scope.changeDashboard.errorMessage) $scope.changeDashboard.setDashboardDomain();
return;
}
$scope.changeDashboard.busy = true;
$scope.changeDashboard.percent = data.percent;
$scope.changeDashboard.message = data.message;
window.setTimeout($scope.changeDashboard.updateStatus, 500);
});
},
setDashboardDomain: function () {
Client.setDashboardDomain($scope.changeDashboard.selectedDomain.domain, function (error) {
if (error) {
console.error(error);
$scope.changeDashboard.errorMessage = error.message;
$scope.changeDashboard.busy = false;
} else {
window.location.href = 'https://my.' + $scope.changeDashboard.selectedDomain.domain;
}
});
},
change: function () {
2019-01-06 14:52:36 -08:00
$scope.changeDashboard.busy = true;
$scope.changeDashboard.message = 'Preparing dashboard domain';
$scope.changeDashboard.percent = 0;
$scope.changeDashboard.errorMessage = '';
2018-12-18 15:05:11 -08:00
Client.prepareDashboardDomain($scope.changeDashboard.selectedDomain.domain, function (error, taskId) {
if (error) {
console.error(error);
$scope.changeDashboard.errorMessage = error.message;
$scope.changeDashboard.busy = false;
} else {
$scope.changeDashboard.taskId = taskId;
$scope.changeDashboard.updateStatus();
}
});
}
};
2018-01-22 13:01:38 -08:00
Client.onReady(function () {
2019-01-06 14:52:36 -08:00
refreshDomains(function (error) {
2018-01-22 13:01:38 -08:00
if (error) return console.error(error);
$scope.ready = true;
});
$scope.renewCerts.checkStatus();
2018-01-22 13:01:38 -08:00
});
document.getElementById('gcdnsKeyFileInput').onchange = readFileLocally($scope.domainConfigure.gcdnsKey, 'content', 'keyFileName');
document.getElementById('fallbackCertFileInput').onchange = readFileLocally($scope.domainConfigure.fallbackCert, 'certificateFile', 'certificateFileName');
document.getElementById('fallbackKeyFileInput').onchange = readFileLocally($scope.domainConfigure.fallbackCert, 'keyFile', 'keyFileName');
// setup all the dialog focus handling
2019-05-20 18:36:23 -07:00
['domainConfigureModal', 'domainRemoveModal'].forEach(function (id) {
2018-01-22 13:01:38 -08:00
$('#' + id).on('shown.bs.modal', function () {
$(this).find('[autofocus]:first').focus();
2018-01-22 13:01:38 -08:00
});
});
$('.modal-backdrop').remove();
}]);