@push.rocks/smartproxy 22.4.2 → 22.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/changelog.md +28 -0
- package/dist_rust/rustproxy +0 -0
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +1 -5
- package/dist_ts/index.js +3 -9
- package/dist_ts/protocols/common/fragment-handler.js +5 -1
- package/dist_ts/proxies/index.d.ts +1 -5
- package/dist_ts/proxies/index.js +2 -6
- package/dist_ts/proxies/smart-proxy/index.d.ts +5 -10
- package/dist_ts/proxies/smart-proxy/index.js +7 -13
- package/dist_ts/proxies/smart-proxy/models/interfaces.d.ts +5 -2
- package/dist_ts/proxies/smart-proxy/route-preprocessor.d.ts +37 -0
- package/dist_ts/proxies/smart-proxy/route-preprocessor.js +103 -0
- package/dist_ts/proxies/smart-proxy/rust-binary-locator.d.ts +23 -0
- package/dist_ts/proxies/smart-proxy/rust-binary-locator.js +104 -0
- package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.d.ts +74 -0
- package/dist_ts/proxies/smart-proxy/rust-metrics-adapter.js +146 -0
- package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.d.ts +49 -0
- package/dist_ts/proxies/smart-proxy/rust-proxy-bridge.js +259 -0
- package/dist_ts/proxies/smart-proxy/smart-proxy.d.ts +39 -157
- package/dist_ts/proxies/smart-proxy/smart-proxy.js +224 -621
- package/dist_ts/proxies/smart-proxy/socket-handler-server.d.ts +45 -0
- package/dist_ts/proxies/smart-proxy/socket-handler-server.js +253 -0
- package/dist_ts/routing/index.d.ts +1 -1
- package/dist_ts/routing/index.js +3 -3
- package/dist_ts/routing/models/http-types.d.ts +119 -4
- package/dist_ts/routing/models/http-types.js +93 -5
- package/package.json +1 -1
- package/readme.md +470 -219
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +4 -12
- package/ts/protocols/common/fragment-handler.ts +4 -0
- package/ts/proxies/index.ts +1 -9
- package/ts/proxies/smart-proxy/index.ts +6 -13
- package/ts/proxies/smart-proxy/models/interfaces.ts +6 -4
- package/ts/proxies/smart-proxy/route-preprocessor.ts +122 -0
- package/ts/proxies/smart-proxy/rust-binary-locator.ts +112 -0
- package/ts/proxies/smart-proxy/rust-metrics-adapter.ts +161 -0
- package/ts/proxies/smart-proxy/rust-proxy-bridge.ts +310 -0
- package/ts/proxies/smart-proxy/smart-proxy.ts +282 -798
- package/ts/proxies/smart-proxy/socket-handler-server.ts +279 -0
- package/ts/routing/index.ts +2 -2
- package/ts/routing/models/http-types.ts +147 -4
- package/ts/proxies/http-proxy/connection-pool.ts +0 -228
- package/ts/proxies/http-proxy/context-creator.ts +0 -145
- package/ts/proxies/http-proxy/default-certificates.ts +0 -150
- package/ts/proxies/http-proxy/function-cache.ts +0 -279
- package/ts/proxies/http-proxy/handlers/index.ts +0 -5
- package/ts/proxies/http-proxy/http-proxy.ts +0 -669
- package/ts/proxies/http-proxy/http-request-handler.ts +0 -331
- package/ts/proxies/http-proxy/http2-request-handler.ts +0 -255
- package/ts/proxies/http-proxy/index.ts +0 -18
- package/ts/proxies/http-proxy/models/http-types.ts +0 -148
- package/ts/proxies/http-proxy/models/index.ts +0 -5
- package/ts/proxies/http-proxy/models/types.ts +0 -125
- package/ts/proxies/http-proxy/request-handler.ts +0 -878
- package/ts/proxies/http-proxy/security-manager.ts +0 -413
- package/ts/proxies/http-proxy/websocket-handler.ts +0 -581
- package/ts/proxies/smart-proxy/acme-state-manager.ts +0 -112
- package/ts/proxies/smart-proxy/cert-store.ts +0 -92
- package/ts/proxies/smart-proxy/certificate-manager.ts +0 -895
- package/ts/proxies/smart-proxy/connection-manager.ts +0 -809
- package/ts/proxies/smart-proxy/http-proxy-bridge.ts +0 -213
- package/ts/proxies/smart-proxy/metrics-collector.ts +0 -453
- package/ts/proxies/smart-proxy/nftables-manager.ts +0 -271
- package/ts/proxies/smart-proxy/port-manager.ts +0 -358
- package/ts/proxies/smart-proxy/route-connection-handler.ts +0 -1712
- package/ts/proxies/smart-proxy/route-orchestrator.ts +0 -297
- package/ts/proxies/smart-proxy/security-manager.ts +0 -269
- package/ts/proxies/smart-proxy/throughput-tracker.ts +0 -138
- package/ts/proxies/smart-proxy/timeout-manager.ts +0 -196
- package/ts/proxies/smart-proxy/tls-manager.ts +0 -171
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import * as plugins from '../../plugins.js';
|
|
2
|
-
import '../../core/models/socket-augmentation.js';
|
|
3
|
-
import type { IRouteContext, IHttpRouteContext, IHttp2RouteContext } from '../../core/models/route-context.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Context creator for NetworkProxy
|
|
7
|
-
* Creates route contexts for matching and function evaluation
|
|
8
|
-
*/
|
|
9
|
-
export class ContextCreator {
|
|
10
|
-
/**
|
|
11
|
-
* Create a route context from HTTP request information
|
|
12
|
-
*/
|
|
13
|
-
public createHttpRouteContext(req: any, options: {
|
|
14
|
-
tlsVersion?: string;
|
|
15
|
-
connectionId: string;
|
|
16
|
-
clientIp: string;
|
|
17
|
-
serverIp: string;
|
|
18
|
-
}): IHttpRouteContext {
|
|
19
|
-
// Parse headers
|
|
20
|
-
const headers: Record<string, string> = {};
|
|
21
|
-
for (const [key, value] of Object.entries(req.headers)) {
|
|
22
|
-
if (typeof value === 'string') {
|
|
23
|
-
headers[key.toLowerCase()] = value;
|
|
24
|
-
} else if (Array.isArray(value) && value.length > 0) {
|
|
25
|
-
headers[key.toLowerCase()] = value[0];
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
// Parse domain from Host header
|
|
30
|
-
const domain = headers['host']?.split(':')[0] || '';
|
|
31
|
-
|
|
32
|
-
// Parse URL
|
|
33
|
-
const url = new URL(`http://${domain}${req.url || '/'}`);
|
|
34
|
-
|
|
35
|
-
return {
|
|
36
|
-
// Connection basics
|
|
37
|
-
port: req.socket.localPort || 0,
|
|
38
|
-
domain,
|
|
39
|
-
clientIp: options.clientIp,
|
|
40
|
-
serverIp: options.serverIp,
|
|
41
|
-
|
|
42
|
-
// HTTP specifics
|
|
43
|
-
path: url.pathname,
|
|
44
|
-
query: url.search ? url.search.substring(1) : '',
|
|
45
|
-
headers,
|
|
46
|
-
|
|
47
|
-
// TLS information
|
|
48
|
-
isTls: !!req.socket.encrypted,
|
|
49
|
-
tlsVersion: options.tlsVersion,
|
|
50
|
-
|
|
51
|
-
// Request objects
|
|
52
|
-
req,
|
|
53
|
-
|
|
54
|
-
// Metadata
|
|
55
|
-
timestamp: Date.now(),
|
|
56
|
-
connectionId: options.connectionId
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Create a route context from HTTP/2 stream and headers
|
|
62
|
-
*/
|
|
63
|
-
public createHttp2RouteContext(
|
|
64
|
-
stream: plugins.http2.ServerHttp2Stream,
|
|
65
|
-
headers: plugins.http2.IncomingHttpHeaders,
|
|
66
|
-
options: {
|
|
67
|
-
connectionId: string;
|
|
68
|
-
clientIp: string;
|
|
69
|
-
serverIp: string;
|
|
70
|
-
}
|
|
71
|
-
): IHttp2RouteContext {
|
|
72
|
-
// Parse headers, excluding HTTP/2 pseudo-headers
|
|
73
|
-
const processedHeaders: Record<string, string> = {};
|
|
74
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
75
|
-
if (!key.startsWith(':') && typeof value === 'string') {
|
|
76
|
-
processedHeaders[key.toLowerCase()] = value;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Get domain from :authority pseudo-header
|
|
81
|
-
const authority = headers[':authority'] as string || '';
|
|
82
|
-
const domain = authority.split(':')[0];
|
|
83
|
-
|
|
84
|
-
// Get path from :path pseudo-header
|
|
85
|
-
const path = headers[':path'] as string || '/';
|
|
86
|
-
|
|
87
|
-
// Parse the path to extract query string
|
|
88
|
-
const pathParts = path.split('?');
|
|
89
|
-
const pathname = pathParts[0];
|
|
90
|
-
const query = pathParts.length > 1 ? pathParts[1] : '';
|
|
91
|
-
|
|
92
|
-
// Get the socket from the session
|
|
93
|
-
const socket = (stream.session as any)?.socket;
|
|
94
|
-
|
|
95
|
-
return {
|
|
96
|
-
// Connection basics
|
|
97
|
-
port: socket?.localPort || 0,
|
|
98
|
-
domain,
|
|
99
|
-
clientIp: options.clientIp,
|
|
100
|
-
serverIp: options.serverIp,
|
|
101
|
-
|
|
102
|
-
// HTTP specifics
|
|
103
|
-
path: pathname,
|
|
104
|
-
query,
|
|
105
|
-
headers: processedHeaders,
|
|
106
|
-
|
|
107
|
-
// HTTP/2 specific properties
|
|
108
|
-
method: headers[':method'] as string,
|
|
109
|
-
stream,
|
|
110
|
-
|
|
111
|
-
// TLS information - HTTP/2 is always on TLS in browsers
|
|
112
|
-
isTls: true,
|
|
113
|
-
tlsVersion: socket?.getTLSVersion?.() || 'TLSv1.3',
|
|
114
|
-
|
|
115
|
-
// Metadata
|
|
116
|
-
timestamp: Date.now(),
|
|
117
|
-
connectionId: options.connectionId
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* Create a basic route context from socket information
|
|
123
|
-
*/
|
|
124
|
-
public createSocketRouteContext(socket: plugins.net.Socket, options: {
|
|
125
|
-
domain?: string;
|
|
126
|
-
tlsVersion?: string;
|
|
127
|
-
connectionId: string;
|
|
128
|
-
}): IRouteContext {
|
|
129
|
-
return {
|
|
130
|
-
// Connection basics
|
|
131
|
-
port: socket.localPort || 0,
|
|
132
|
-
domain: options.domain,
|
|
133
|
-
clientIp: socket.remoteAddress?.replace('::ffff:', '') || '0.0.0.0',
|
|
134
|
-
serverIp: socket.localAddress?.replace('::ffff:', '') || '0.0.0.0',
|
|
135
|
-
|
|
136
|
-
// TLS information
|
|
137
|
-
isTls: options.tlsVersion !== undefined,
|
|
138
|
-
tlsVersion: options.tlsVersion,
|
|
139
|
-
|
|
140
|
-
// Metadata
|
|
141
|
-
timestamp: Date.now(),
|
|
142
|
-
connectionId: options.connectionId
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
}
|
|
@@ -1,150 +0,0 @@
|
|
|
1
|
-
import * as plugins from '../../plugins.js';
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
import { fileURLToPath } from 'url';
|
|
5
|
-
import { AsyncFileSystem } from '../../core/utils/fs-utils.js';
|
|
6
|
-
import type { ILogger, ICertificateEntry } from './models/types.js';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Interface for default certificate data
|
|
10
|
-
*/
|
|
11
|
-
export interface IDefaultCertificates {
|
|
12
|
-
key: string;
|
|
13
|
-
cert: string;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Provides default SSL certificates for HttpProxy.
|
|
18
|
-
* This is a minimal replacement for the deprecated CertificateManager.
|
|
19
|
-
*
|
|
20
|
-
* For production certificate management, use SmartCertManager instead.
|
|
21
|
-
*/
|
|
22
|
-
export class DefaultCertificateProvider {
|
|
23
|
-
private defaultCertificates: IDefaultCertificates | null = null;
|
|
24
|
-
private certificateCache: Map<string, ICertificateEntry> = new Map();
|
|
25
|
-
private initialized = false;
|
|
26
|
-
|
|
27
|
-
constructor(private logger?: ILogger) {}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Load default certificates asynchronously (preferred)
|
|
31
|
-
*/
|
|
32
|
-
public async loadDefaultCertificatesAsync(): Promise<IDefaultCertificates> {
|
|
33
|
-
if (this.defaultCertificates) {
|
|
34
|
-
return this.defaultCertificates;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
38
|
-
const certPath = path.join(__dirname, '..', '..', '..', 'assets', 'certs');
|
|
39
|
-
|
|
40
|
-
try {
|
|
41
|
-
const [key, cert] = await Promise.all([
|
|
42
|
-
AsyncFileSystem.readFile(path.join(certPath, 'key.pem')),
|
|
43
|
-
AsyncFileSystem.readFile(path.join(certPath, 'cert.pem'))
|
|
44
|
-
]);
|
|
45
|
-
|
|
46
|
-
this.defaultCertificates = { key, cert };
|
|
47
|
-
this.logger?.info?.('Loaded default certificates from filesystem');
|
|
48
|
-
this.initialized = true;
|
|
49
|
-
return this.defaultCertificates;
|
|
50
|
-
} catch (error) {
|
|
51
|
-
this.logger?.warn?.(`Failed to load default certificates: ${error}`);
|
|
52
|
-
this.defaultCertificates = this.generateFallbackCertificate();
|
|
53
|
-
this.initialized = true;
|
|
54
|
-
return this.defaultCertificates;
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Load default certificates synchronously (for backward compatibility)
|
|
60
|
-
* @deprecated Use loadDefaultCertificatesAsync instead
|
|
61
|
-
*/
|
|
62
|
-
public loadDefaultCertificatesSync(): IDefaultCertificates {
|
|
63
|
-
if (this.defaultCertificates) {
|
|
64
|
-
return this.defaultCertificates;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
68
|
-
const certPath = path.join(__dirname, '..', '..', '..', 'assets', 'certs');
|
|
69
|
-
|
|
70
|
-
try {
|
|
71
|
-
this.defaultCertificates = {
|
|
72
|
-
key: fs.readFileSync(path.join(certPath, 'key.pem'), 'utf8'),
|
|
73
|
-
cert: fs.readFileSync(path.join(certPath, 'cert.pem'), 'utf8')
|
|
74
|
-
};
|
|
75
|
-
this.logger?.info?.('Loaded default certificates from filesystem (sync)');
|
|
76
|
-
} catch (error) {
|
|
77
|
-
this.logger?.warn?.(`Failed to load default certificates: ${error}`);
|
|
78
|
-
this.defaultCertificates = this.generateFallbackCertificate();
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.initialized = true;
|
|
82
|
-
return this.defaultCertificates;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* Gets the default certificates (loads synchronously if not already loaded)
|
|
87
|
-
*/
|
|
88
|
-
public getDefaultCertificates(): IDefaultCertificates {
|
|
89
|
-
if (!this.defaultCertificates) {
|
|
90
|
-
return this.loadDefaultCertificatesSync();
|
|
91
|
-
}
|
|
92
|
-
return this.defaultCertificates;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Updates a certificate in the cache
|
|
97
|
-
*/
|
|
98
|
-
public updateCertificate(domain: string, cert: string, key: string): void {
|
|
99
|
-
this.certificateCache.set(domain, {
|
|
100
|
-
cert,
|
|
101
|
-
key,
|
|
102
|
-
expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000) // 90 days
|
|
103
|
-
});
|
|
104
|
-
|
|
105
|
-
this.logger?.info?.(`Certificate updated for ${domain}`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Gets a cached certificate
|
|
110
|
-
*/
|
|
111
|
-
public getCachedCertificate(domain: string): ICertificateEntry | null {
|
|
112
|
-
return this.certificateCache.get(domain) || null;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Gets statistics for metrics
|
|
117
|
-
*/
|
|
118
|
-
public getStats(): { cachedCertificates: number; defaultCertEnabled: boolean } {
|
|
119
|
-
return {
|
|
120
|
-
cachedCertificates: this.certificateCache.size,
|
|
121
|
-
defaultCertEnabled: this.defaultCertificates !== null
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Generate a fallback self-signed certificate placeholder
|
|
127
|
-
* Note: This is just a placeholder - real apps should provide proper certificates
|
|
128
|
-
*/
|
|
129
|
-
private generateFallbackCertificate(): IDefaultCertificates {
|
|
130
|
-
this.logger?.warn?.('Using fallback self-signed certificate placeholder');
|
|
131
|
-
|
|
132
|
-
// Minimal self-signed certificate for fallback only
|
|
133
|
-
// In production, proper certificates should be provided via SmartCertManager
|
|
134
|
-
const selfSignedCert = `-----BEGIN CERTIFICATE-----
|
|
135
|
-
MIIBkTCB+wIJAKHHIgIIA0/cMA0GCSqGSIb3DQEBBQUAMA0xCzAJBgNVBAYTAlVT
|
|
136
|
-
MB4XDTE0MDEwMTAwMDAwMFoXDTI0MDEwMTAwMDAwMFowDTELMAkGA1UEBhMCVVMw
|
|
137
|
-
gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMRiH0VwnOH3jCV7c6JFZWYrvuqy
|
|
138
|
-
-----END CERTIFICATE-----`;
|
|
139
|
-
|
|
140
|
-
const selfSignedKey = `-----BEGIN PRIVATE KEY-----
|
|
141
|
-
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAMRiH0VwnOH3jCV7
|
|
142
|
-
c6JFZWYrvuqyALCLXj0pcr1iqNdHjegNXnkl5zjdaUjq4edNOKl7M1AlFiYjG2xk
|
|
143
|
-
-----END PRIVATE KEY-----`;
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
key: selfSignedKey,
|
|
147
|
-
cert: selfSignedCert
|
|
148
|
-
};
|
|
149
|
-
}
|
|
150
|
-
}
|
|
@@ -1,279 +0,0 @@
|
|
|
1
|
-
import type { IRouteContext } from '../../core/models/route-context.js';
|
|
2
|
-
import type { ILogger } from './models/types.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Interface for cached function result
|
|
6
|
-
*/
|
|
7
|
-
interface ICachedResult<T> {
|
|
8
|
-
value: T;
|
|
9
|
-
expiry: number;
|
|
10
|
-
hash: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Function cache for NetworkProxy function-based targets
|
|
15
|
-
*
|
|
16
|
-
* This cache improves performance for function-based targets by storing
|
|
17
|
-
* the results of function evaluations and reusing them for similar contexts.
|
|
18
|
-
*/
|
|
19
|
-
export class FunctionCache {
|
|
20
|
-
// Cache storage
|
|
21
|
-
private hostCache: Map<string, ICachedResult<string | string[]>> = new Map();
|
|
22
|
-
private portCache: Map<string, ICachedResult<number>> = new Map();
|
|
23
|
-
|
|
24
|
-
// Maximum number of entries to store in each cache
|
|
25
|
-
private maxCacheSize: number;
|
|
26
|
-
|
|
27
|
-
// Default TTL for cache entries in milliseconds (default: 5 seconds)
|
|
28
|
-
private defaultTtl: number;
|
|
29
|
-
|
|
30
|
-
// Logger
|
|
31
|
-
private logger: ILogger;
|
|
32
|
-
|
|
33
|
-
// Cleanup interval timer
|
|
34
|
-
private cleanupInterval: NodeJS.Timeout | null = null;
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Creates a new function cache
|
|
38
|
-
*
|
|
39
|
-
* @param logger Logger for debug output
|
|
40
|
-
* @param options Cache options
|
|
41
|
-
*/
|
|
42
|
-
constructor(
|
|
43
|
-
logger: ILogger,
|
|
44
|
-
options: {
|
|
45
|
-
maxCacheSize?: number;
|
|
46
|
-
defaultTtl?: number;
|
|
47
|
-
} = {}
|
|
48
|
-
) {
|
|
49
|
-
this.logger = logger;
|
|
50
|
-
this.maxCacheSize = options.maxCacheSize || 1000;
|
|
51
|
-
this.defaultTtl = options.defaultTtl || 5000; // 5 seconds default
|
|
52
|
-
|
|
53
|
-
// Start the cache cleanup timer
|
|
54
|
-
this.cleanupInterval = setInterval(() => this.cleanupCache(), 30000); // Cleanup every 30 seconds
|
|
55
|
-
|
|
56
|
-
// Make sure the interval doesn't keep the process alive
|
|
57
|
-
if (this.cleanupInterval.unref) {
|
|
58
|
-
this.cleanupInterval.unref();
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Compute a hash for a context object
|
|
64
|
-
* This is used to identify similar contexts for caching
|
|
65
|
-
*
|
|
66
|
-
* @param context The route context to hash
|
|
67
|
-
* @param functionId Identifier for the function (usually route name or ID)
|
|
68
|
-
* @returns A string hash
|
|
69
|
-
*/
|
|
70
|
-
private computeContextHash(context: IRouteContext, functionId: string): string {
|
|
71
|
-
// Extract relevant properties for the hash
|
|
72
|
-
const hashBase = {
|
|
73
|
-
functionId,
|
|
74
|
-
port: context.port,
|
|
75
|
-
domain: context.domain,
|
|
76
|
-
clientIp: context.clientIp,
|
|
77
|
-
path: context.path,
|
|
78
|
-
query: context.query,
|
|
79
|
-
isTls: context.isTls,
|
|
80
|
-
tlsVersion: context.tlsVersion
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
// Generate a hash string
|
|
84
|
-
return JSON.stringify(hashBase);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Get cached host result for a function and context
|
|
89
|
-
*
|
|
90
|
-
* @param context Route context
|
|
91
|
-
* @param functionId Identifier for the function
|
|
92
|
-
* @returns Cached host value or undefined if not found
|
|
93
|
-
*/
|
|
94
|
-
public getCachedHost(context: IRouteContext, functionId: string): string | string[] | undefined {
|
|
95
|
-
const hash = this.computeContextHash(context, functionId);
|
|
96
|
-
const cached = this.hostCache.get(hash);
|
|
97
|
-
|
|
98
|
-
// Return if no cached value or expired
|
|
99
|
-
if (!cached || cached.expiry < Date.now()) {
|
|
100
|
-
if (cached) {
|
|
101
|
-
// If expired, remove from cache
|
|
102
|
-
this.hostCache.delete(hash);
|
|
103
|
-
this.logger.debug(`Cache miss (expired) for host function: ${functionId}`);
|
|
104
|
-
} else {
|
|
105
|
-
this.logger.debug(`Cache miss for host function: ${functionId}`);
|
|
106
|
-
}
|
|
107
|
-
return undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
this.logger.debug(`Cache hit for host function: ${functionId}`);
|
|
111
|
-
return cached.value;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
/**
|
|
115
|
-
* Get cached port result for a function and context
|
|
116
|
-
*
|
|
117
|
-
* @param context Route context
|
|
118
|
-
* @param functionId Identifier for the function
|
|
119
|
-
* @returns Cached port value or undefined if not found
|
|
120
|
-
*/
|
|
121
|
-
public getCachedPort(context: IRouteContext, functionId: string): number | undefined {
|
|
122
|
-
const hash = this.computeContextHash(context, functionId);
|
|
123
|
-
const cached = this.portCache.get(hash);
|
|
124
|
-
|
|
125
|
-
// Return if no cached value or expired
|
|
126
|
-
if (!cached || cached.expiry < Date.now()) {
|
|
127
|
-
if (cached) {
|
|
128
|
-
// If expired, remove from cache
|
|
129
|
-
this.portCache.delete(hash);
|
|
130
|
-
this.logger.debug(`Cache miss (expired) for port function: ${functionId}`);
|
|
131
|
-
} else {
|
|
132
|
-
this.logger.debug(`Cache miss for port function: ${functionId}`);
|
|
133
|
-
}
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
this.logger.debug(`Cache hit for port function: ${functionId}`);
|
|
138
|
-
return cached.value;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Store a host function result in the cache
|
|
143
|
-
*
|
|
144
|
-
* @param context Route context
|
|
145
|
-
* @param functionId Identifier for the function
|
|
146
|
-
* @param value Host value to cache
|
|
147
|
-
* @param ttl Optional TTL in milliseconds
|
|
148
|
-
*/
|
|
149
|
-
public cacheHost(
|
|
150
|
-
context: IRouteContext,
|
|
151
|
-
functionId: string,
|
|
152
|
-
value: string | string[],
|
|
153
|
-
ttl?: number
|
|
154
|
-
): void {
|
|
155
|
-
const hash = this.computeContextHash(context, functionId);
|
|
156
|
-
const expiry = Date.now() + (ttl || this.defaultTtl);
|
|
157
|
-
|
|
158
|
-
// Check if we need to prune the cache before adding
|
|
159
|
-
if (this.hostCache.size >= this.maxCacheSize) {
|
|
160
|
-
this.pruneOldestEntries(this.hostCache);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
// Store the result
|
|
164
|
-
this.hostCache.set(hash, { value, expiry, hash });
|
|
165
|
-
this.logger.debug(`Cached host function result for: ${functionId}`);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Store a port function result in the cache
|
|
170
|
-
*
|
|
171
|
-
* @param context Route context
|
|
172
|
-
* @param functionId Identifier for the function
|
|
173
|
-
* @param value Port value to cache
|
|
174
|
-
* @param ttl Optional TTL in milliseconds
|
|
175
|
-
*/
|
|
176
|
-
public cachePort(
|
|
177
|
-
context: IRouteContext,
|
|
178
|
-
functionId: string,
|
|
179
|
-
value: number,
|
|
180
|
-
ttl?: number
|
|
181
|
-
): void {
|
|
182
|
-
const hash = this.computeContextHash(context, functionId);
|
|
183
|
-
const expiry = Date.now() + (ttl || this.defaultTtl);
|
|
184
|
-
|
|
185
|
-
// Check if we need to prune the cache before adding
|
|
186
|
-
if (this.portCache.size >= this.maxCacheSize) {
|
|
187
|
-
this.pruneOldestEntries(this.portCache);
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// Store the result
|
|
191
|
-
this.portCache.set(hash, { value, expiry, hash });
|
|
192
|
-
this.logger.debug(`Cached port function result for: ${functionId}`);
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
/**
|
|
196
|
-
* Remove expired entries from the cache
|
|
197
|
-
*/
|
|
198
|
-
private cleanupCache(): void {
|
|
199
|
-
const now = Date.now();
|
|
200
|
-
let expiredCount = 0;
|
|
201
|
-
|
|
202
|
-
// Clean up host cache
|
|
203
|
-
for (const [hash, cached] of this.hostCache.entries()) {
|
|
204
|
-
if (cached.expiry < now) {
|
|
205
|
-
this.hostCache.delete(hash);
|
|
206
|
-
expiredCount++;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
// Clean up port cache
|
|
211
|
-
for (const [hash, cached] of this.portCache.entries()) {
|
|
212
|
-
if (cached.expiry < now) {
|
|
213
|
-
this.portCache.delete(hash);
|
|
214
|
-
expiredCount++;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
if (expiredCount > 0) {
|
|
219
|
-
this.logger.debug(`Cleaned up ${expiredCount} expired cache entries`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
/**
|
|
224
|
-
* Prune oldest entries from a cache map
|
|
225
|
-
* Used when the cache exceeds the maximum size
|
|
226
|
-
*
|
|
227
|
-
* @param cache The cache map to prune
|
|
228
|
-
*/
|
|
229
|
-
private pruneOldestEntries<T>(cache: Map<string, ICachedResult<T>>): void {
|
|
230
|
-
// Find the oldest entries
|
|
231
|
-
const now = Date.now();
|
|
232
|
-
const itemsToRemove = Math.floor(this.maxCacheSize * 0.2); // Remove 20% of the cache
|
|
233
|
-
|
|
234
|
-
// Convert to array for sorting
|
|
235
|
-
const entries = Array.from(cache.entries());
|
|
236
|
-
|
|
237
|
-
// Sort by expiry (oldest first)
|
|
238
|
-
entries.sort((a, b) => a[1].expiry - b[1].expiry);
|
|
239
|
-
|
|
240
|
-
// Remove oldest entries
|
|
241
|
-
const toRemove = entries.slice(0, itemsToRemove);
|
|
242
|
-
for (const [hash] of toRemove) {
|
|
243
|
-
cache.delete(hash);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
this.logger.debug(`Pruned ${toRemove.length} oldest cache entries`);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
/**
|
|
250
|
-
* Get current cache stats
|
|
251
|
-
*/
|
|
252
|
-
public getStats(): { hostCacheSize: number; portCacheSize: number } {
|
|
253
|
-
return {
|
|
254
|
-
hostCacheSize: this.hostCache.size,
|
|
255
|
-
portCacheSize: this.portCache.size
|
|
256
|
-
};
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* Clear all cached entries
|
|
261
|
-
*/
|
|
262
|
-
public clearCache(): void {
|
|
263
|
-
this.hostCache.clear();
|
|
264
|
-
this.portCache.clear();
|
|
265
|
-
this.logger.info('Function cache cleared');
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Destroy the cache and cleanup resources
|
|
270
|
-
*/
|
|
271
|
-
public destroy(): void {
|
|
272
|
-
if (this.cleanupInterval) {
|
|
273
|
-
clearInterval(this.cleanupInterval);
|
|
274
|
-
this.cleanupInterval = null;
|
|
275
|
-
}
|
|
276
|
-
this.clearCache();
|
|
277
|
-
this.logger.debug('Function cache destroyed');
|
|
278
|
-
}
|
|
279
|
-
}
|