smartproxy/ts/common/port80-adapter.ts

87 lines
2.4 KiB
TypeScript
Raw Normal View History

import * as plugins from '../plugins.js';
import type {
IForwardConfig as ILegacyForwardConfig,
IDomainOptions
} from './types.js';
import type {
IForwardConfig
2025-05-09 21:21:28 +00:00
} from '../forwarding/config/forwarding-types.js';
/**
* Converts a forwarding configuration target to the legacy format
* for Port80Handler
*/
export function convertToLegacyForwardConfig(
forwardConfig: IForwardConfig
): ILegacyForwardConfig {
// Determine host from the target configuration
const host = Array.isArray(forwardConfig.target.host)
? forwardConfig.target.host[0] // Use the first host in the array
: forwardConfig.target.host;
return {
ip: host,
port: forwardConfig.target.port
};
}
/**
* Creates Port80Handler domain options from a domain name and forwarding config
*/
export function createPort80HandlerOptions(
domain: string,
forwardConfig: IForwardConfig
): IDomainOptions {
// Determine if we should redirect HTTP to HTTPS
let sslRedirect = false;
if (forwardConfig.http?.redirectToHttps) {
sslRedirect = true;
}
// Determine if ACME maintenance should be enabled
// Enable by default for termination types, unless explicitly disabled
const requiresTls =
forwardConfig.type === 'https-terminate-to-http' ||
forwardConfig.type === 'https-terminate-to-https';
const acmeMaintenance =
requiresTls &&
forwardConfig.acme?.enabled !== false;
// Set up forwarding configuration
const options: IDomainOptions = {
domainName: domain,
sslRedirect,
acmeMaintenance
};
// Add ACME challenge forwarding if configured
if (forwardConfig.acme?.forwardChallenges) {
options.acmeForward = {
ip: Array.isArray(forwardConfig.acme.forwardChallenges.host)
? forwardConfig.acme.forwardChallenges.host[0]
: forwardConfig.acme.forwardChallenges.host,
port: forwardConfig.acme.forwardChallenges.port
};
}
// Add HTTP forwarding if this is an HTTP-only config or if HTTP is enabled
const supportsHttp =
forwardConfig.type === 'http-only' ||
(forwardConfig.http?.enabled !== false &&
(forwardConfig.type === 'https-terminate-to-http' ||
forwardConfig.type === 'https-terminate-to-https'));
if (supportsHttp) {
options.forward = {
ip: Array.isArray(forwardConfig.target.host)
? forwardConfig.target.host[0]
: forwardConfig.target.host,
port: forwardConfig.target.port
};
}
return options;
}