Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC: Trust certs in /trusted-certs dir #172

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import chalk from 'chalk';
import fetchCookie from 'fetch-cookie';
import fs from 'fs';
import https from 'https';
import nodeFetch, {
Blob,
FormData,
Expand All @@ -8,11 +10,32 @@ import nodeFetch, {
Response
} from 'node-fetch';
import { parse } from 'node-html-parser';
import tls from 'tls';
import type { Host } from './config.js';
import { Config } from './config.js';
import { Log } from './log.js';
import { ErrorNotification } from './notify.js';

const customCAPath = "/trusted-certs";
const trustedCAsFetch = (url: RequestInfo, options: RequestInit = {}): Promise<Response> => {
let agent: https.Agent;
try {
const certFiles = fs.readdirSync(customCAPath);
if (certFiles.length > 0) {
const suppliedCerts = certFiles.map(certFile => fs.readFileSync(`${customCAPath}/${certFile}`));
agent = new https.Agent({ ca: [...tls.rootCertificates, ...suppliedCerts] });
} else {
Log.verbose(`No custom certificates found in directory: ${customCAPath}`);
agent = new https.Agent();
}
} catch (error) {
Log.error(`Error loading custom certificates: ${error}`);
agent = new https.Agent();
}

return nodeFetch(url, { agent, ...options });
};

export class Client {
private constructor(
private fetch: NodeFetchCookie,
Expand All @@ -22,7 +45,7 @@ export class Client {

public static async create(host: Host): Promise<Client> {
Log.info(chalk.yellow(`➡️ Signing in to ${host.fullUrl}...`));
const fetch = fetchCookie(nodeFetch);
const fetch = fetchCookie(trustedCAsFetch);

await fetch(`${host.fullUrl}/index.php?login`, { method: 'GET' });
const response = await fetch(`${host.fullUrl}/index.php?login`, {
Expand Down