Compare commits

...

6 Commits

7 changed files with 131 additions and 20 deletions

View File

@ -1,5 +1,27 @@
# Changelog # Changelog
## 2025-02-21 - 3.4.0 - feat(PortProxy)
Enhanced PortProxy with custom target host and improved testing
- PortProxy constructor now accepts 'fromPort', 'toPort', and optional 'toHost' directly from settings
- Refactored test cases to cover forwarding to the custom host
- Added support to handle multiple concurrent connections
- Refactored internal connection handling logic to utilize default configurations
## 2025-02-21 - 3.3.1 - fix(PortProxy)
fixed import usage of net and tls libraries for PortProxy
- Corrected the use of plugins for importing 'tls' and 'net' libraries in the PortProxy module.
- Updated the constructor of PortProxy to accept combined tls options with ProxySettings.
## 2025-02-21 - 3.3.0 - feat(PortProxy)
Enhanced PortProxy with domain and IP filtering, SNI support, and minimatch integration
- Added new ProxySettings interface to configure domain patterns, SNI, and default allowed IPs.
- Integrated minimatch to filter allowed IPs and domains.
- Enabled SNI support for PortProxy connections.
- Updated port proxy test to accommodate new settings.
## 2025-02-04 - 3.2.0 - feat(testing) ## 2025-02-04 - 3.2.0 - feat(testing)
Added a comprehensive test suite for the PortProxy class Added a comprehensive test suite for the PortProxy class

View File

@ -1,6 +1,6 @@
{ {
"name": "@push.rocks/smartproxy", "name": "@push.rocks/smartproxy",
"version": "3.2.0", "version": "3.4.0",
"private": false, "private": false,
"description": "a proxy for handling high workloads of proxying", "description": "a proxy for handling high workloads of proxying",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",
@ -30,7 +30,9 @@
"@push.rocks/smartstring": "^4.0.15", "@push.rocks/smartstring": "^4.0.15",
"@tsclass/tsclass": "^4.4.0", "@tsclass/tsclass": "^4.4.0",
"@types/ws": "^8.5.14", "@types/ws": "^8.5.14",
"ws": "^8.18.0" "ws": "^8.18.0",
"minimatch": "^9.0.3",
"@types/minimatch": "^5.1.2"
}, },
"files": [ "files": [
"ts/**/*", "ts/**/*",

6
pnpm-lock.yaml generated
View File

@ -26,9 +26,15 @@ importers:
'@tsclass/tsclass': '@tsclass/tsclass':
specifier: ^4.4.0 specifier: ^4.4.0
version: 4.4.0 version: 4.4.0
'@types/minimatch':
specifier: ^5.1.2
version: 5.1.2
'@types/ws': '@types/ws':
specifier: ^8.5.14 specifier: ^8.5.14
version: 8.5.14 version: 8.5.14
minimatch:
specifier: ^9.0.3
version: 9.0.5
ws: ws:
specifier: ^8.18.0 specifier: ^8.18.0
version: 8.18.0 version: 8.18.0

View File

@ -58,7 +58,14 @@ function createTestClient(port: number, data: string): Promise<string> {
// Setup test environment // Setup test environment
tap.test('setup port proxy test environment', async () => { tap.test('setup port proxy test environment', async () => {
testServer = await createTestServer(TEST_SERVER_PORT); testServer = await createTestServer(TEST_SERVER_PORT);
portProxy = new PortProxy(PROXY_PORT, TEST_SERVER_PORT); portProxy = new PortProxy({
fromPort: PROXY_PORT,
toPort: TEST_SERVER_PORT,
toHost: 'localhost',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
});
}); });
tap.test('should start port proxy', async () => { tap.test('should start port proxy', async () => {
@ -66,11 +73,28 @@ tap.test('should start port proxy', async () => {
expect(portProxy.netServer.listening).toBeTrue(); expect(portProxy.netServer.listening).toBeTrue();
}); });
tap.test('should forward TCP connections and data', async () => { tap.test('should forward TCP connections and data to localhost', async () => {
const response = await createTestClient(PROXY_PORT, TEST_DATA); const response = await createTestClient(PROXY_PORT, TEST_DATA);
expect(response).toEqual(`Echo: ${TEST_DATA}`); expect(response).toEqual(`Echo: ${TEST_DATA}`);
}); });
tap.test('should forward TCP connections to custom host', async () => {
// Create a new proxy instance with a custom host
const customHostProxy = new PortProxy({
fromPort: PROXY_PORT + 1,
toPort: TEST_SERVER_PORT,
toHost: '127.0.0.1',
domains: [],
sniEnabled: false,
defaultAllowedIPs: ['127.0.0.1', '::ffff:127.0.0.1']
});
await customHostProxy.start();
const response = await createTestClient(PROXY_PORT + 1, TEST_DATA);
expect(response).toEqual(`Echo: ${TEST_DATA}`);
await customHostProxy.stop();
});
tap.test('should handle multiple concurrent connections', async () => { tap.test('should handle multiple concurrent connections', async () => {
const concurrentRequests = 5; const concurrentRequests = 5;
const requests = Array(concurrentRequests).fill(null).map((_, i) => const requests = Array(concurrentRequests).fill(null).map((_, i) =>

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartproxy', name: '@push.rocks/smartproxy',
version: '3.2.0', version: '3.4.0',
description: 'a proxy for handling high workloads of proxying' description: 'a proxy for handling high workloads of proxying'
} }

View File

@ -2,9 +2,10 @@
import * as http from 'http'; import * as http from 'http';
import * as https from 'https'; import * as https from 'https';
import * as net from 'net'; import * as net from 'net';
import * as tls from 'tls';
import * as url from 'url'; import * as url from 'url';
export { http, https, net, url }; export { http, https, net, tls, url };
// tsclass scope // tsclass scope
import * as tsclass from '@tsclass/tsclass'; import * as tsclass from '@tsclass/tsclass';
@ -23,5 +24,6 @@ export { lik, smartdelay, smartrequest, smartpromise, smartstring };
// third party scope // third party scope
import * as ws from 'ws'; import * as ws from 'ws';
import wsDefault from 'ws'; import wsDefault from 'ws';
import { minimatch } from 'minimatch';
export { wsDefault, ws }; export { wsDefault, ws, minimatch };

View File

@ -1,14 +1,32 @@
import * as plugins from './smartproxy.plugins.js'; import * as plugins from './smartproxy.plugins.js';
import * as net from 'net';
export class PortProxy {
netServer: plugins.net.Server; export interface DomainConfig {
domain: string; // glob pattern for domain
allowedIPs: string[]; // glob patterns for IPs allowed to access this domain
}
export interface ProxySettings extends plugins.tls.TlsOptions {
// Port configuration
fromPort: number; fromPort: number;
toPort: number; toPort: number;
toHost?: string; // Target host to proxy to, defaults to 'localhost'
constructor(fromPortArg: number, toPortArg: number) { // Domain and security settings
this.fromPort = fromPortArg; domains: DomainConfig[];
this.toPort = toPortArg; sniEnabled?: boolean;
defaultAllowedIPs?: string[]; // Optional default IP patterns if no matching domain found
}
export class PortProxy {
netServer: plugins.net.Server | plugins.tls.Server;
settings: ProxySettings;
constructor(settings: ProxySettings) {
this.settings = {
...settings,
toHost: settings.toHost || 'localhost'
};
} }
public async start() { public async start() {
@ -22,11 +40,48 @@ export class PortProxy {
from.destroy(); from.destroy();
to.destroy(); to.destroy();
}; };
this.netServer = net const isAllowed = (value: string, patterns: string[]): boolean => {
.createServer((from) => { return patterns.some(pattern => plugins.minimatch(value, pattern));
const to = net.createConnection({ };
host: 'localhost',
port: this.toPort, const findMatchingDomain = (serverName: string): DomainConfig | undefined => {
return this.settings.domains.find(config => plugins.minimatch(serverName, config.domain));
};
const server = this.settings.sniEnabled
? plugins.tls.createServer(this.settings)
: plugins.net.createServer();
this.netServer = server.on('connection', (from: plugins.net.Socket) => {
const remoteIP = from.remoteAddress || '';
if (this.settings.sniEnabled && from instanceof plugins.tls.TLSSocket) {
const serverName = (from as any).servername || '';
const domainConfig = findMatchingDomain(serverName);
if (!domainConfig) {
// If no matching domain config found, check default IPs if available
if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
console.log(`Connection rejected: No matching domain config for ${serverName} from IP ${remoteIP}`);
from.end();
return;
}
} else {
// Check if IP is allowed for this domain
if (!isAllowed(remoteIP, domainConfig.allowedIPs)) {
console.log(`Connection rejected: IP ${remoteIP} not allowed for domain ${serverName}`);
from.end();
return;
}
}
} else if (!this.settings.defaultAllowedIPs || !isAllowed(remoteIP, this.settings.defaultAllowedIPs)) {
console.log(`Connection rejected: IP ${remoteIP} not allowed for non-SNI connection`);
from.end();
return;
}
const to = plugins.net.createConnection({
host: this.settings.toHost!,
port: this.settings.toPort,
}); });
from.setTimeout(120000); from.setTimeout(120000);
from.pipe(to); from.pipe(to);
@ -56,8 +111,8 @@ export class PortProxy {
cleanUpSockets(from, to); cleanUpSockets(from, to);
}); });
}) })
.listen(this.fromPort); .listen(this.settings.fromPort);
console.log(`PortProxy -> OK: Now listening on port ${this.fromPort}`); console.log(`PortProxy -> OK: Now listening on port ${this.settings.fromPort}`);
} }
public async stop() { public async stop() {