@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.
244 lines (243 loc) • 7.37 kB
TypeScript
import * as plugins from './plugins.js';
import { ProxyRouter } from './classes.router.js';
export interface INetworkProxyOptions {
port: number;
maxConnections?: number;
keepAliveTimeout?: number;
headersTimeout?: number;
logLevel?: 'error' | 'warn' | 'info' | 'debug';
cors?: {
allowOrigin?: string;
allowMethods?: string;
allowHeaders?: string;
maxAge?: number;
};
connectionPoolSize?: number;
portProxyIntegration?: boolean;
acme?: {
enabled?: boolean;
port?: number;
contactEmail?: string;
useProduction?: boolean;
renewThresholdDays?: number;
autoRenew?: boolean;
certificateStore?: string;
skipConfiguredCerts?: boolean;
};
}
export declare class NetworkProxy {
options: INetworkProxyOptions;
proxyConfigs: plugins.tsclass.network.IReverseProxyConfig[];
defaultHeaders: {
[key: string]: string;
};
httpsServer: plugins.https.Server;
wsServer: plugins.ws.WebSocketServer;
router: ProxyRouter;
socketMap: plugins.lik.ObjectMap<plugins.net.Socket>;
activeContexts: Set<string>;
connectedClients: number;
startTime: number;
requestsServed: number;
failedRequests: number;
private portProxyConnections;
private tlsTerminatedConnections;
private heartbeatInterval;
private metricsInterval;
private connectionPoolCleanupInterval;
private defaultCertificates;
private certificateCache;
private port80Handler;
private certificateStoreDir;
private connectionPool;
private roundRobinPositions;
/**
* Creates a new NetworkProxy instance
*/
constructor(optionsArg: INetworkProxyOptions);
/**
* Loads default certificates from the filesystem
*/
private loadDefaultCertificates;
/**
* Returns the port number this NetworkProxy is listening on
* Useful for PortProxy to determine where to forward connections
*/
getListeningPort(): number;
/**
* Updates the server capacity settings
* @param maxConnections Maximum number of simultaneous connections
* @param keepAliveTimeout Keep-alive timeout in milliseconds
* @param connectionPoolSize Size of the connection pool per backend
*/
updateCapacity(maxConnections?: number, keepAliveTimeout?: number, connectionPoolSize?: number): void;
/**
* Returns current server metrics
* Useful for PortProxy to determine which NetworkProxy to use for load balancing
*/
getMetrics(): any;
/**
* Cleanup the connection pool by removing idle connections
* or reducing pool size if it exceeds the configured maximum
*/
private cleanupConnectionPool;
/**
* Get a connection from the pool or create a new one
*/
private getConnectionFromPool;
/**
* Return a connection to the pool for reuse
*/
private returnConnectionToPool;
/**
* Initializes the Port80Handler for ACME certificate management
* @private
*/
private initializePort80Handler;
/**
* Registers domains from proxy configs with the Port80Handler
* @private
*/
private registerDomainsWithPort80Handler;
/**
* Handles newly issued or renewed certificates from Port80Handler
* @private
*/
private handleCertificateIssued;
/**
* Handles certificate issuance failures
* @private
*/
private handleCertificateFailed;
/**
* Saves certificate and private key to the filesystem
* @private
*/
private saveCertificateToStore;
/**
* Handles SNI (Server Name Indication) for TLS connections
* Used by the HTTPS server to select the correct certificate for each domain
* @private
*/
private handleSNI;
/**
* Starts the proxy server
*/
start(): Promise<void>;
/**
* Sets up tracking of TCP connections
*/
private setupConnectionTracking;
/**
* Sets up WebSocket support
*/
private setupWebsocketSupport;
/**
* Sets up metrics collection
*/
private setupMetricsCollection;
/**
* Sets up connection pool cleanup
*/
private setupConnectionPoolCleanup;
/**
* Handles an incoming WebSocket connection
*/
private handleWebSocketConnection;
/**
* Handles an HTTP/HTTPS request
*/
private handleRequest;
/**
* Handles a CORS preflight request
*/
private handleCorsRequest;
/**
* Authenticates a request against the destination config
*/
private authenticateRequest;
/**
* Forwards a request to the destination using connection pool
* for optimized connection reuse from PortProxy
*/
private forwardRequestUsingConnectionPool;
/**
* Forwards a request to the destination (standard method)
*/
private forwardRequest;
/**
* Prepares headers to forward to the backend
*/
private prepareForwardHeaders;
/**
* Sets up request streaming for the proxy
*/
private setupRequestStreaming;
/**
* Processes a proxy response
*/
private processProxyResponse;
/**
* Sends an error response to the client
*/
private sendErrorResponse;
/**
* Selects a destination IP from the array using round-robin
* @param config The proxy configuration
* @returns A destination IP address
*/
private selectDestinationIp;
/**
* Selects a destination port from the array using round-robin
* @param config The proxy configuration
* @returns A destination port number
*/
private selectDestinationPort;
/**
* Updates proxy configurations
*/
updateProxyConfigs(proxyConfigsArg: plugins.tsclass.network.IReverseProxyConfig[]): Promise<void>;
/**
* Converts PortProxy domain configurations to NetworkProxy configs
* @param domainConfigs PortProxy domain configs
* @param sslKeyPair Default SSL key pair to use if not specified
* @returns Array of NetworkProxy configs
*/
convertPortProxyConfigs(domainConfigs: Array<{
domains: string[];
targetIPs?: string[];
allowedIPs?: string[];
}>, sslKeyPair?: {
key: string;
cert: string;
}): plugins.tsclass.network.IReverseProxyConfig[];
/**
* Adds default headers to be included in all responses
*/
addDefaultHeaders(headersArg: {
[key: string]: string;
}): Promise<void>;
/**
* Stops the proxy server
*/
stop(): Promise<void>;
/**
* Requests a new certificate for a domain
* This can be used to manually trigger certificate issuance
* @param domain The domain to request a certificate for
* @returns A promise that resolves when the request is submitted (not when the certificate is issued)
*/
requestCertificate(domain: string): Promise<boolean>;
/**
* Updates the certificate cache for a domain
* @param domain The domain name
* @param certificate The certificate (PEM format)
* @param privateKey The private key (PEM format)
* @param expiryDate Optional expiry date
*/
private updateCertificateCache;
/**
* Logs a message according to the configured log level
*/
private log;
}