fix(typescript): Refactor types and interfaces to use consistent I prefix and update related tests
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
import type { ForwardConfig } from './forwarding-types.js';
|
||||
import type { IForwardConfig } from './forwarding-types.js';
|
||||
|
||||
/**
|
||||
* Domain configuration with unified forwarding configuration
|
||||
*/
|
||||
export interface DomainConfig {
|
||||
export interface IDomainConfig {
|
||||
// Core properties - domain patterns
|
||||
domains: string[];
|
||||
|
||||
// Unified forwarding configuration
|
||||
forwarding: ForwardConfig;
|
||||
forwarding: IForwardConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -16,8 +16,8 @@ export interface DomainConfig {
|
||||
*/
|
||||
export function createDomainConfig(
|
||||
domains: string | string[],
|
||||
forwarding: ForwardConfig
|
||||
): DomainConfig {
|
||||
forwarding: IForwardConfig
|
||||
): IDomainConfig {
|
||||
// Normalize domains to an array
|
||||
const domainArray = Array.isArray(domains) ? domains : [domains];
|
||||
|
||||
@ -25,7 +25,4 @@ export function createDomainConfig(
|
||||
domains: domainArray,
|
||||
forwarding
|
||||
};
|
||||
}
|
||||
|
||||
// Backwards compatibility
|
||||
export interface IDomainConfig extends DomainConfig {}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type { DomainConfig } from './domain-config.js';
|
||||
import type { IDomainConfig } from './domain-config.js';
|
||||
import { ForwardingHandler } from '../handlers/base-handler.js';
|
||||
import { ForwardingHandlerEvents } from './forwarding-types.js';
|
||||
import { ForwardingHandlerFactory } from '../factory/forwarding-factory.js';
|
||||
@ -21,14 +21,14 @@ export enum DomainManagerEvents {
|
||||
* Manages domains and their forwarding handlers
|
||||
*/
|
||||
export class DomainManager extends plugins.EventEmitter {
|
||||
private domainConfigs: DomainConfig[] = [];
|
||||
private domainConfigs: IDomainConfig[] = [];
|
||||
private domainHandlers: Map<string, ForwardingHandler> = new Map();
|
||||
|
||||
|
||||
/**
|
||||
* Create a new DomainManager
|
||||
* @param initialDomains Optional initial domain configurations
|
||||
*/
|
||||
constructor(initialDomains?: DomainConfig[]) {
|
||||
constructor(initialDomains?: IDomainConfig[]) {
|
||||
super();
|
||||
|
||||
if (initialDomains) {
|
||||
@ -40,7 +40,7 @@ export class DomainManager extends plugins.EventEmitter {
|
||||
* Set or replace all domain configurations
|
||||
* @param configs Array of domain configurations
|
||||
*/
|
||||
public async setDomainConfigs(configs: DomainConfig[]): Promise<void> {
|
||||
public async setDomainConfigs(configs: IDomainConfig[]): Promise<void> {
|
||||
// Clear existing handlers
|
||||
this.domainHandlers.clear();
|
||||
|
||||
@ -57,7 +57,7 @@ export class DomainManager extends plugins.EventEmitter {
|
||||
* Add a new domain configuration
|
||||
* @param config The domain configuration to add
|
||||
*/
|
||||
public async addDomainConfig(config: DomainConfig): Promise<void> {
|
||||
public async addDomainConfig(config: IDomainConfig): Promise<void> {
|
||||
// Check if any of these domains already exist
|
||||
for (const domain of config.domains) {
|
||||
if (this.domainHandlers.has(domain)) {
|
||||
@ -193,7 +193,7 @@ export class DomainManager extends plugins.EventEmitter {
|
||||
* Create handlers for a domain configuration
|
||||
* @param config The domain configuration
|
||||
*/
|
||||
private async createHandlersForDomain(config: DomainConfig): Promise<void> {
|
||||
private async createHandlersForDomain(config: IDomainConfig): Promise<void> {
|
||||
try {
|
||||
// Create a handler for this forwarding configuration
|
||||
const handler = ForwardingHandlerFactory.createHandler(config.forwarding);
|
||||
@ -221,7 +221,7 @@ export class DomainManager extends plugins.EventEmitter {
|
||||
* @param handler The handler
|
||||
* @param config The domain configuration for this handler
|
||||
*/
|
||||
private setupHandlerEvents(handler: ForwardingHandler, config: DomainConfig): void {
|
||||
private setupHandlerEvents(handler: ForwardingHandler, config: IDomainConfig): void {
|
||||
// Forward relevant events
|
||||
handler.on(ForwardingHandlerEvents.CERTIFICATE_NEEDED, (data) => {
|
||||
this.emit(DomainManagerEvents.CERTIFICATE_NEEDED, {
|
||||
@ -277,7 +277,7 @@ export class DomainManager extends plugins.EventEmitter {
|
||||
* Get all domain configurations
|
||||
* @returns Array of domain configurations
|
||||
*/
|
||||
public getDomainConfigs(): DomainConfig[] {
|
||||
public getDomainConfigs(): IDomainConfig[] {
|
||||
return [...this.domainConfigs];
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ import type * as plugins from '../../plugins.js';
|
||||
/**
|
||||
* The primary forwarding types supported by SmartProxy
|
||||
*/
|
||||
export type ForwardingType =
|
||||
export type TForwardingType =
|
||||
| 'http-only' // HTTP forwarding only (no HTTPS)
|
||||
| 'https-passthrough' // Pass-through TLS traffic (SNI forwarding)
|
||||
| 'https-terminate-to-http' // Terminate TLS and forward to HTTP backend
|
||||
@ -12,7 +12,7 @@ export type ForwardingType =
|
||||
/**
|
||||
* Target configuration for forwarding
|
||||
*/
|
||||
export interface TargetConfig {
|
||||
export interface ITargetConfig {
|
||||
host: string | string[]; // Support single host or round-robin
|
||||
port: number;
|
||||
}
|
||||
@ -20,7 +20,7 @@ export interface TargetConfig {
|
||||
/**
|
||||
* HTTP-specific options for forwarding
|
||||
*/
|
||||
export interface HttpOptions {
|
||||
export interface IHttpOptions {
|
||||
enabled?: boolean; // Whether HTTP is enabled
|
||||
redirectToHttps?: boolean; // Redirect HTTP to HTTPS
|
||||
headers?: Record<string, string>; // Custom headers for HTTP responses
|
||||
@ -29,7 +29,7 @@ export interface HttpOptions {
|
||||
/**
|
||||
* HTTPS-specific options for forwarding
|
||||
*/
|
||||
export interface HttpsOptions {
|
||||
export interface IHttpsOptions {
|
||||
customCert?: { // Use custom cert instead of auto-provisioned
|
||||
key: string;
|
||||
cert: string;
|
||||
@ -40,8 +40,8 @@ export interface HttpsOptions {
|
||||
/**
|
||||
* ACME certificate handling options
|
||||
*/
|
||||
export interface AcmeForwardingOptions {
|
||||
enabled?: boolean; // Enable ACME certificate provisioning
|
||||
export interface IAcmeForwardingOptions {
|
||||
enabled?: boolean; // Enable ACME certificate provisioning
|
||||
maintenance?: boolean; // Auto-renew certificates
|
||||
production?: boolean; // Use production ACME servers
|
||||
forwardChallenges?: { // Forward ACME challenges
|
||||
@ -54,7 +54,7 @@ export interface AcmeForwardingOptions {
|
||||
/**
|
||||
* Security options for forwarding
|
||||
*/
|
||||
export interface SecurityOptions {
|
||||
export interface ISecurityOptions {
|
||||
allowedIps?: string[]; // IPs allowed to connect
|
||||
blockedIps?: string[]; // IPs blocked from connecting
|
||||
maxConnections?: number; // Max simultaneous connections
|
||||
@ -63,7 +63,7 @@ export interface SecurityOptions {
|
||||
/**
|
||||
* Advanced options for forwarding
|
||||
*/
|
||||
export interface AdvancedOptions {
|
||||
export interface IAdvancedOptions {
|
||||
portRanges?: Array<{ from: number; to: number }>; // Allowed port ranges
|
||||
networkProxyPort?: number; // Custom NetworkProxy port if using terminate mode
|
||||
keepAlive?: boolean; // Enable TCP keepalive
|
||||
@ -74,21 +74,21 @@ export interface AdvancedOptions {
|
||||
/**
|
||||
* Unified forwarding configuration interface
|
||||
*/
|
||||
export interface ForwardConfig {
|
||||
export interface IForwardConfig {
|
||||
// Define the primary forwarding type - use-case driven approach
|
||||
type: ForwardingType;
|
||||
|
||||
type: TForwardingType;
|
||||
|
||||
// Target configuration
|
||||
target: TargetConfig;
|
||||
|
||||
target: ITargetConfig;
|
||||
|
||||
// Protocol options
|
||||
http?: HttpOptions;
|
||||
https?: HttpsOptions;
|
||||
acme?: AcmeForwardingOptions;
|
||||
|
||||
http?: IHttpOptions;
|
||||
https?: IHttpsOptions;
|
||||
acme?: IAcmeForwardingOptions;
|
||||
|
||||
// Security and advanced options
|
||||
security?: SecurityOptions;
|
||||
advanced?: AdvancedOptions;
|
||||
security?: ISecurityOptions;
|
||||
advanced?: IAdvancedOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,8 +118,8 @@ export interface IForwardingHandler extends plugins.EventEmitter {
|
||||
* Helper function types for common forwarding patterns
|
||||
*/
|
||||
export const httpOnly = (
|
||||
partialConfig: Partial<ForwardConfig> & Pick<ForwardConfig, 'target'>
|
||||
): ForwardConfig => ({
|
||||
partialConfig: Partial<IForwardConfig> & Pick<IForwardConfig, 'target'>
|
||||
): IForwardConfig => ({
|
||||
type: 'http-only',
|
||||
target: partialConfig.target,
|
||||
http: { enabled: true, ...(partialConfig.http || {}) },
|
||||
@ -128,8 +128,8 @@ export const httpOnly = (
|
||||
});
|
||||
|
||||
export const tlsTerminateToHttp = (
|
||||
partialConfig: Partial<ForwardConfig> & Pick<ForwardConfig, 'target'>
|
||||
): ForwardConfig => ({
|
||||
partialConfig: Partial<IForwardConfig> & Pick<IForwardConfig, 'target'>
|
||||
): IForwardConfig => ({
|
||||
type: 'https-terminate-to-http',
|
||||
target: partialConfig.target,
|
||||
https: { ...(partialConfig.https || {}) },
|
||||
@ -140,8 +140,8 @@ export const tlsTerminateToHttp = (
|
||||
});
|
||||
|
||||
export const tlsTerminateToHttps = (
|
||||
partialConfig: Partial<ForwardConfig> & Pick<ForwardConfig, 'target'>
|
||||
): ForwardConfig => ({
|
||||
partialConfig: Partial<IForwardConfig> & Pick<IForwardConfig, 'target'>
|
||||
): IForwardConfig => ({
|
||||
type: 'https-terminate-to-https',
|
||||
target: partialConfig.target,
|
||||
https: { ...(partialConfig.https || {}) },
|
||||
@ -152,20 +152,11 @@ export const tlsTerminateToHttps = (
|
||||
});
|
||||
|
||||
export const httpsPassthrough = (
|
||||
partialConfig: Partial<ForwardConfig> & Pick<ForwardConfig, 'target'>
|
||||
): ForwardConfig => ({
|
||||
partialConfig: Partial<IForwardConfig> & Pick<IForwardConfig, 'target'>
|
||||
): IForwardConfig => ({
|
||||
type: 'https-passthrough',
|
||||
target: partialConfig.target,
|
||||
https: { forwardSni: true, ...(partialConfig.https || {}) },
|
||||
...(partialConfig.security ? { security: partialConfig.security } : {}),
|
||||
...(partialConfig.advanced ? { advanced: partialConfig.advanced } : {})
|
||||
});
|
||||
|
||||
// Backwards compatibility interfaces with 'I' prefix
|
||||
export interface ITargetConfig extends TargetConfig {}
|
||||
export interface IHttpOptions extends HttpOptions {}
|
||||
export interface IHttpsOptions extends HttpsOptions {}
|
||||
export interface IAcmeForwardingOptions extends AcmeForwardingOptions {}
|
||||
export interface ISecurityOptions extends SecurityOptions {}
|
||||
export interface IAdvancedOptions extends AdvancedOptions {}
|
||||
export interface IForwardConfig extends ForwardConfig {}
|
||||
});
|
@ -1,5 +1,5 @@
|
||||
import type { ForwardConfig } from '../config/forwarding-types.js';
|
||||
import type { ForwardingHandler } from '../handlers/base-handler.js';
|
||||
import type { IForwardConfig } from '../config/forwarding-types.js';
|
||||
import { ForwardingHandler } from '../handlers/base-handler.js';
|
||||
import { HttpForwardingHandler } from '../handlers/http-handler.js';
|
||||
import { HttpsPassthroughHandler } from '../handlers/https-passthrough-handler.js';
|
||||
import { HttpsTerminateToHttpHandler } from '../handlers/https-terminate-to-http-handler.js';
|
||||
@ -14,35 +14,35 @@ export class ForwardingHandlerFactory {
|
||||
* @param config The forwarding configuration
|
||||
* @returns The appropriate forwarding handler
|
||||
*/
|
||||
public static createHandler(config: ForwardConfig): ForwardingHandler {
|
||||
public static createHandler(config: IForwardConfig): ForwardingHandler {
|
||||
// Create the appropriate handler based on the forwarding type
|
||||
switch (config.type) {
|
||||
case 'http-only':
|
||||
return new HttpForwardingHandler(config);
|
||||
|
||||
|
||||
case 'https-passthrough':
|
||||
return new HttpsPassthroughHandler(config);
|
||||
|
||||
|
||||
case 'https-terminate-to-http':
|
||||
return new HttpsTerminateToHttpHandler(config);
|
||||
|
||||
|
||||
case 'https-terminate-to-https':
|
||||
return new HttpsTerminateToHttpsHandler(config);
|
||||
|
||||
|
||||
default:
|
||||
// Type system should prevent this, but just in case:
|
||||
throw new Error(`Unknown forwarding type: ${(config as any).type}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Apply default values to a forwarding configuration based on its type
|
||||
* @param config The original forwarding configuration
|
||||
* @returns A configuration with defaults applied
|
||||
*/
|
||||
public static applyDefaults(config: ForwardConfig): ForwardConfig {
|
||||
public static applyDefaults(config: IForwardConfig): IForwardConfig {
|
||||
// Create a deep copy of the configuration
|
||||
const result: ForwardConfig = JSON.parse(JSON.stringify(config));
|
||||
const result: IForwardConfig = JSON.parse(JSON.stringify(config));
|
||||
|
||||
// Apply defaults based on forwarding type
|
||||
switch (config.type) {
|
||||
@ -112,7 +112,7 @@ export class ForwardingHandlerFactory {
|
||||
* @param config The configuration to validate
|
||||
* @throws Error if the configuration is invalid
|
||||
*/
|
||||
public static validateConfig(config: ForwardConfig): void {
|
||||
public static validateConfig(config: IForwardConfig): void {
|
||||
// Validate common properties
|
||||
if (!config.target) {
|
||||
throw new Error('Forwarding configuration must include a target');
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import type {
|
||||
ForwardConfig,
|
||||
IForwardConfig,
|
||||
IForwardingHandler
|
||||
} from '../config/forwarding-types.js';
|
||||
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
|
||||
@ -13,7 +13,7 @@ export abstract class ForwardingHandler extends plugins.EventEmitter implements
|
||||
* Create a new ForwardingHandler
|
||||
* @param config The forwarding configuration
|
||||
*/
|
||||
constructor(protected config: ForwardConfig) {
|
||||
constructor(protected config: IForwardConfig) {
|
||||
super();
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import { ForwardingHandler } from './base-handler.js';
|
||||
import type { ForwardConfig } from '../config/forwarding-types.js';
|
||||
import type { IForwardConfig } from '../config/forwarding-types.js';
|
||||
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
|
||||
|
||||
/**
|
||||
@ -11,14 +11,23 @@ export class HttpForwardingHandler extends ForwardingHandler {
|
||||
* Create a new HTTP forwarding handler
|
||||
* @param config The forwarding configuration
|
||||
*/
|
||||
constructor(config: ForwardConfig) {
|
||||
constructor(config: IForwardConfig) {
|
||||
super(config);
|
||||
|
||||
|
||||
// Validate that this is an HTTP-only configuration
|
||||
if (config.type !== 'http-only') {
|
||||
throw new Error(`Invalid configuration type for HttpForwardingHandler: ${config.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the handler
|
||||
* HTTP handler doesn't need special initialization
|
||||
*/
|
||||
public async initialize(): Promise<void> {
|
||||
// Basic initialization from parent class
|
||||
await super.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a raw socket connection
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import { ForwardingHandler } from './base-handler.js';
|
||||
import type { ForwardConfig } from '../config/forwarding-types.js';
|
||||
import type { IForwardConfig } from '../config/forwarding-types.js';
|
||||
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
|
||||
|
||||
/**
|
||||
@ -11,14 +11,23 @@ export class HttpsPassthroughHandler extends ForwardingHandler {
|
||||
* Create a new HTTPS passthrough handler
|
||||
* @param config The forwarding configuration
|
||||
*/
|
||||
constructor(config: ForwardConfig) {
|
||||
constructor(config: IForwardConfig) {
|
||||
super(config);
|
||||
|
||||
|
||||
// Validate that this is an HTTPS passthrough configuration
|
||||
if (config.type !== 'https-passthrough') {
|
||||
throw new Error(`Invalid configuration type for HttpsPassthroughHandler: ${config.type}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the handler
|
||||
* HTTPS passthrough handler doesn't need special initialization
|
||||
*/
|
||||
public async initialize(): Promise<void> {
|
||||
// Basic initialization from parent class
|
||||
await super.initialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a TLS/SSL socket connection by forwarding it without termination
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import { ForwardingHandler } from './base-handler.js';
|
||||
import type { ForwardConfig } from '../config/forwarding-types.js';
|
||||
import type { IForwardConfig } from '../config/forwarding-types.js';
|
||||
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
|
||||
|
||||
/**
|
||||
@ -14,7 +14,7 @@ export class HttpsTerminateToHttpHandler extends ForwardingHandler {
|
||||
* Create a new HTTPS termination with HTTP backend handler
|
||||
* @param config The forwarding configuration
|
||||
*/
|
||||
constructor(config: ForwardConfig) {
|
||||
constructor(config: IForwardConfig) {
|
||||
super(config);
|
||||
|
||||
// Validate that this is an HTTPS terminate to HTTP configuration
|
||||
|
@ -1,6 +1,6 @@
|
||||
import * as plugins from '../../plugins.js';
|
||||
import { ForwardingHandler } from './base-handler.js';
|
||||
import type { ForwardConfig } from '../config/forwarding-types.js';
|
||||
import type { IForwardConfig } from '../config/forwarding-types.js';
|
||||
import { ForwardingHandlerEvents } from '../config/forwarding-types.js';
|
||||
|
||||
/**
|
||||
@ -13,7 +13,7 @@ export class HttpsTerminateToHttpsHandler extends ForwardingHandler {
|
||||
* Create a new HTTPS termination with HTTPS backend handler
|
||||
* @param config The forwarding configuration
|
||||
*/
|
||||
constructor(config: ForwardConfig) {
|
||||
constructor(config: IForwardConfig) {
|
||||
super(config);
|
||||
|
||||
// Validate that this is an HTTPS terminate to HTTPS configuration
|
||||
|
Reference in New Issue
Block a user