deployment-engine/dist/apis/npm.js

149 lines
4.3 KiB
JavaScript

import * as console from "console";
import { NPM_BASE_URL, NPM_EMAIL, NPM_PASSWORD } from "../config.js";
let bearer = null;
const headers = () => {
const headers = {
Authorization: "Bearer " + bearer,
"Content-Type": "application/json"
};
return headers;
};
async function _post(url, body) {
const res = await fetch(NPM_BASE_URL + url, {
method: 'POST',
body: JSON.stringify(body),
headers: headers()
});
if (Math.floor(res.status / 100) !== 2) {
throw new Error(JSON.stringify(await res.json(), null, 2));
}
return await res.json();
}
async function _get(url) {
const res = await fetch(NPM_BASE_URL + url, {
method: 'GET',
headers: headers()
});
return await res.json();
}
async function _delete(url) {
const res = await fetch(NPM_BASE_URL + url, {
method: 'DELETE',
headers: headers()
});
return await res.json();
}
function setToHappen(fn, date) {
var now = new Date().getTime();
var diff = date.getTime() - now;
return setTimeout(fn, diff);
}
async function auth() {
console.log("Attempting authentication against nginx");
const { token, expires } = await _post('/tokens', {
identity: NPM_EMAIL,
secret: NPM_PASSWORD
});
setToHappen(auth, new Date(new Date(expires).getTime() - 60 * 1000));
bearer = token;
}
export function getProxyHosts() {
return _get('/nginx/proxy-hosts');
}
export const ready = auth();
export async function isDomainRegistered(domain) {
const hosts = await getProxyHosts();
return hosts.map(h => h.domain_names).flat().some(d => d === domain);
}
export async function getProxyHostIdByDomain(domain) {
const hosts = await getProxyHosts();
for (const host of hosts) {
for (const d of host.domain_names) {
if (domain === d) {
return host.id;
}
}
}
return null;
}
export async function createProxyHost(domains, port, host, additionalConfig) {
const registered = (await Promise.all(domains.map(d => isDomainRegistered(d)))).some(v => v);
if (registered) {
throw new Error("Domain is already registered as a proxy host");
}
const res = await _post('/nginx/proxy-hosts', {
domain_names: domains,
forward_scheme: "http",
forward_host: host,
forward_port: port,
block_exploits: false,
allow_websocket_upgrade: false,
access_list_id: "0",
certificate_id: 0,
ssl_forced: false,
http2_support: false,
meta: {
letsencrypt_agree: false,
dns_challenge: false
},
advanced_config: "",
locations: [],
caching_enabled: false,
hsts_enabled: false,
hsts_subdomains: false,
...additionalConfig
});
console.log('Created proxy host for', domains.join(', '));
return res;
}
export async function createCertificate(domains) {
const certReq = {
domain_names: domains,
meta: {
dns_challenge: false,
letsencrypt_agree: true,
letsencrypt_email: NPM_EMAIL
},
provider: "letsencrypt"
};
const res = await _post('/nginx/certificates', certReq);
return res;
}
export async function getCertificates() {
return await _get('/nginx/certificates');
}
function compareSets(a, b) {
if (a.length !== b.length)
return false;
const _a = a.sort();
const _b = b.sort();
for (let i = 0; i < _a.length; i++) {
if (_a[i] !== _b[i])
return false;
}
return true;
}
export async function deleteCertificate(id) {
const success = await _delete(`/nginx/certificate/${id}`);
if (success !== 'true') {
throw Error('Failed to delete certificate ' + id);
}
}
export async function getCertificateByDomains(domains) {
const certs = await getCertificates();
for (const cert of certs) {
if (compareSets(cert.domain_names, domains)) {
return cert;
}
}
return null;
}
export async function hasCerficateByDomains(domains) {
return (await getCertificateByDomains(domains)) !== null;
}
export async function deleteProxyHostByDomain(domain) {
const id = await getProxyHostIdByDomain(domain);
await _delete(`/nginx/proxy-hosts/${id}`);
console.log('deleted proxy host for', domain);
}