UNPKG

@push.rocks/smartproxy

Version:

A powerful proxy package that effectively handles high traffic, with features such as SSL/TLS support, port proxying, WebSocket handling, dynamic routing with authentication options, and automatic ACME certificate management.

216 lines (215 loc) 6.43 kB
import * as plugins from './plugins.js'; /** * Custom error classes for better error handling */ export declare class Port80HandlerError extends Error { constructor(message: string); } export declare class CertificateError extends Port80HandlerError { readonly domain: string; readonly isRenewal: boolean; constructor(message: string, domain: string, isRenewal?: boolean); } export declare class ServerError extends Port80HandlerError { readonly code?: string; constructor(message: string, code?: string); } /** * Domain forwarding configuration */ export interface IForwardConfig { ip: string; port: number; } /** * Domain configuration options */ export interface IDomainOptions { domainName: string; sslRedirect: boolean; acmeMaintenance: boolean; forward?: IForwardConfig; acmeForward?: IForwardConfig; } /** * Configuration options for the Port80Handler */ interface IPort80HandlerOptions { port?: number; contactEmail?: string; useProduction?: boolean; renewThresholdDays?: number; httpsRedirectPort?: number; renewCheckIntervalHours?: number; } /** * Certificate data that can be emitted via events or set from outside */ export interface ICertificateData { domain: string; certificate: string; privateKey: string; expiryDate: Date; } /** * Events emitted by the Port80Handler */ export declare enum Port80HandlerEvents { CERTIFICATE_ISSUED = "certificate-issued", CERTIFICATE_RENEWED = "certificate-renewed", CERTIFICATE_FAILED = "certificate-failed", CERTIFICATE_EXPIRING = "certificate-expiring", MANAGER_STARTED = "manager-started", MANAGER_STOPPED = "manager-stopped", REQUEST_FORWARDED = "request-forwarded" } /** * Certificate failure payload type */ export interface ICertificateFailure { domain: string; error: string; isRenewal: boolean; } /** * Certificate expiry payload type */ export interface ICertificateExpiring { domain: string; expiryDate: Date; daysRemaining: number; } /** * Port80Handler with ACME certificate management and request forwarding capabilities * Now with glob pattern support for domain matching */ export declare class Port80Handler extends plugins.EventEmitter { private domainCertificates; private server; private acmeClient; private accountKey; private renewalTimer; private isShuttingDown; private options; /** * Creates a new Port80Handler * @param options Configuration options */ constructor(options?: IPort80HandlerOptions); /** * Starts the HTTP server for ACME challenges */ start(): Promise<void>; /** * Stops the HTTP server and renewal timer */ stop(): Promise<void>; /** * Adds a domain with configuration options * @param options Domain configuration options */ addDomain(options: IDomainOptions): void; /** * Removes a domain from management * @param domain The domain to remove */ removeDomain(domain: string): void; /** * Sets a certificate for a domain directly (for externally obtained certificates) * @param domain The domain for the certificate * @param certificate The certificate (PEM format) * @param privateKey The private key (PEM format) * @param expiryDate Optional expiry date */ setCertificate(domain: string, certificate: string, privateKey: string, expiryDate?: Date): void; /** * Gets the certificate for a domain if it exists * @param domain The domain to get the certificate for */ getCertificate(domain: string): ICertificateData | null; /** * Check if a domain is a glob pattern * @param domain Domain to check * @returns True if the domain is a glob pattern */ private isGlobPattern; /** * Get domain info for a specific domain, using glob pattern matching if needed * @param requestDomain The actual domain from the request * @returns The domain info or null if not found */ private getDomainInfoForRequest; /** * Check if a domain matches a glob pattern * @param domain The domain to check * @param pattern The pattern to match against * @returns True if the domain matches the pattern */ private domainMatchesPattern; /** * Lazy initialization of the ACME client * @returns An ACME client instance */ private getAcmeClient; /** * Handles incoming HTTP requests * @param req The HTTP request * @param res The HTTP response */ private handleRequest; /** * Forwards an HTTP request to the specified target * @param req The original request * @param res The response object * @param target The forwarding target (IP and port) * @param requestType Type of request for logging */ private forwardRequest; /** * Serves the ACME HTTP-01 challenge response * @param req The HTTP request * @param res The HTTP response * @param domain The domain for the challenge */ private handleAcmeChallenge; /** * Obtains a certificate for a domain using ACME HTTP-01 challenge * @param domain The domain to obtain a certificate for * @param isRenewal Whether this is a renewal attempt */ private obtainCertificate; /** * Process ACME authorizations by verifying and completing challenges * @param client ACME client * @param domain Domain name * @param authorizations Authorizations to process */ private processAuthorizations; /** * Starts the certificate renewal timer */ private startRenewalTimer; /** * Checks for certificates that need renewal */ private checkForRenewals; /** * Extract expiry date from certificate using a more robust approach * @param certificate Certificate PEM string * @param domain Domain for logging * @returns Extracted expiry date or default */ private extractExpiryDateFromCertificate; /** * Get a default expiry date (90 days from now) * @returns Default expiry date */ private getDefaultExpiryDate; /** * Emits a certificate event with the certificate data * @param eventType The event type to emit * @param data The certificate data */ private emitCertificateEvent; } export {};