Commit c5af1036 authored by nanahira's avatar nanahira

fix acme thing

parent 8b60f55e
Pipeline #26558 passed with stages
in 26 minutes and 53 seconds
import { createServer, Server } from 'http'; import { createServer } from 'http';
import acme, { Client } from 'acme-client'; import acme, { Client } from 'acme-client';
import fs from 'fs'; import fs from 'fs';
import path from 'path';
let email: string; let email: string;
export const domainsToBeSigned: string[] = []; export const domainsToBeSigned: string[] = [];
...@@ -35,6 +36,9 @@ export async function runSignCert() { ...@@ -35,6 +36,9 @@ export async function runSignCert() {
// 404 // 404
res.writeHead(404); res.writeHead(404);
res.end('Token not Found'); res.end('Token not Found');
console.error(
`${req.socket.remoteAddress}: Invalid request for ${req.url}`,
);
return; return;
} }
const content = contentMap.get(token); const content = contentMap.get(token);
...@@ -42,18 +46,30 @@ export async function runSignCert() { ...@@ -42,18 +46,30 @@ export async function runSignCert() {
// 404 // 404
res.writeHead(404); res.writeHead(404);
res.end('Content not Found'); res.end('Content not Found');
console.error(
`${req.socket.remoteAddress}: Token not found for ${token}`,
);
return; return;
} }
res.writeHead(200); res.writeHead(200);
console.error(
`${req.socket.remoteAddress}: Serving token for ${token}: ${content}`,
);
res.end(content); res.end(content);
}).listen(80); }).listen(80);
await fs.promises.mkdir('/etc/nginx/acme', { recursive: true }); const certDir = `/etc/nginx/certs/${domainsToBeSigned[0]}`;
const accountFile = path.join(certDir, 'account.pem');
const fullchainFile = path.join(certDir, 'fullchain.pem');
const privkeyFile = path.join(certDir, 'privkey.pem');
await fs.promises.mkdir(certDir, {
recursive: true,
});
let accountKey: Buffer; let accountKey: Buffer;
try { try {
accountKey = await fs.promises.readFile('/etc/nginx/acme/account.pem'); accountKey = await fs.promises.readFile(accountFile);
} catch (e) { } catch (e) {
accountKey = await acme.forge.createPrivateKey(); accountKey = await acme.forge.createPrivateKey();
await fs.promises.writeFile('/etc/nginx/acme/account.pem', accountKey); await fs.promises.writeFile(accountFile, accountKey);
} }
const acmeClient = new Client({ const acmeClient = new Client({
directoryUrl: acme.directory.letsencrypt.production, directoryUrl: acme.directory.letsencrypt.production,
...@@ -82,17 +98,8 @@ export async function runSignCert() { ...@@ -82,17 +98,8 @@ export async function runSignCert() {
}); });
console.error('Certificate signed'); console.error('Certificate signed');
// save certificate as fullchain.pem and key as privkey.pem // save certificate as fullchain.pem and key as privkey.pem
await fs.promises.mkdir(`/etc/nginx/certs/${domainsToBeSigned[0]}`, { await fs.promises.writeFile(fullchainFile, certificate);
recursive: true, await fs.promises.writeFile(privkeyFile, certificateKey);
});
await fs.promises.writeFile(
`/etc/nginx/certs/${domainsToBeSigned[0]}/fullchain.pem`,
certificate,
);
await fs.promises.writeFile(
`/etc/nginx/certs/${domainsToBeSigned[0]}/privkey.pem`,
certificateKey,
);
} catch (e) { } catch (e) {
console.error( console.error(
`Failed to sign certificate for ${domainsToBeSigned.join(', ')}: ${ `Failed to sign certificate for ${domainsToBeSigned.join(', ')}: ${
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment