fixes to cwg and catch

merge-requests/1/merge
Matthew 2019-10-28 19:46:03 -04:00
parent f3668d06ab
commit 88a619134a
No known key found for this signature in database
GPG Key ID: 766BE43AE75F7559
1 changed files with 35 additions and 27 deletions

View File

@ -95,6 +95,7 @@ export default class CWG extends Command {
* @example await CWG.createDomain('mydomain.cloud.libraryofcode.org', 6781); * @example await CWG.createDomain('mydomain.cloud.libraryofcode.org', 6781);
*/ */
public async createDomain(account: AccountInterface, domain: string, port: number, x509Certificate: { cert?: string, key?: string } = { cert: '/etc/nginx/ssl/cloud-org.chain.crt', key: '/etc/nginx/ssl/cloud-org.key.pem' }) { public async createDomain(account: AccountInterface, domain: string, port: number, x509Certificate: { cert?: string, key?: string } = { cert: '/etc/nginx/ssl/cloud-org.chain.crt', key: '/etc/nginx/ssl/cloud-org.key.pem' }) {
try {
if (port <= 1024 || port >= 65535) throw new RangeError(`Port range must be between 1024 and 65535, received ${port}.`); if (port <= 1024 || port >= 65535) throw new RangeError(`Port range must be between 1024 and 65535, received ${port}.`);
if (await this.client.db.Domain.exists({ port })) throw new Error(`Port ${port} already exists in the database.`); if (await this.client.db.Domain.exists({ port })) throw new Error(`Port ${port} already exists in the database.`);
if (await this.client.db.Domain.exists({ domain })) throw new Error(`Domain ${domain} already exists in the database.`); if (await this.client.db.Domain.exists({ domain })) throw new Error(`Domain ${domain} already exists in the database.`);
@ -116,13 +117,20 @@ export default class CWG extends Command {
enabled: true, enabled: true,
}); });
if (domain.includes('cloud.libraryofcode.org')) { if (domain.includes('cloud.libraryofcode.org')) {
const dmn = domain.split('.');
await axios({ await axios({
method: 'post', method: 'post',
url: 'https://api.cloudflare.com/client/v4/zones/5e82fc3111ed4fbf9f58caa34f7553a7/dns_records', url: 'https://api.cloudflare.com/client/v4/zones/5e82fc3111ed4fbf9f58caa34f7553a7/dns_records',
headers: { Authorization: `Bearer ${this.client.config.cloudflare}`, 'Content-Type': 'application/json' }, headers: { Authorization: `Bearer ${this.client.config.cloudflare}`, 'Content-Type': 'application/json' },
data: JSON.stringify({ type: 'CNAME', name: domain, content: 'cloud.libraryofcode.org', proxied: false }), data: JSON.stringify({ type: 'CNAME', name: `${dmn[0]}.${dmn[1]}`, content: 'cloud.libraryofcode.org', proxied: false }),
}); });
} }
return entry.save(); return entry.save();
} catch (error) {
await fs.unlink(`/etc/nginx/sites-available/${domain}`);
await fs.unlink(`/etc/nginx/sites-enabled/${domain}`);
await this.client.db.Domain.deleteMany({ domain });
throw error;
}
} }
} }