remoteingress/ts/connector.private.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2024-04-14 01:38:00 +00:00
import * as plugins from './plugins.js';
2024-03-24 13:44:44 +00:00
export class ConnectorPrivate {
private targetHost: string;
private targetPort: number;
2024-04-14 01:38:00 +00:00
constructor(targetHost: string, targetPort: number = 4000) {
2024-03-24 13:44:44 +00:00
this.targetHost = targetHost;
this.targetPort = targetPort;
this.connectToPublicRemoteConnector();
}
private connectToPublicRemoteConnector(): void {
const options = {
2024-04-14 01:38:00 +00:00
// Include CA certificate if necessary, for example:
// ca: fs.readFileSync('path/to/ca.pem'),
rejectUnauthorized: true // Only set this to true if you are sure about the server's certificate
2024-03-24 13:44:44 +00:00
};
2024-04-14 01:38:00 +00:00
const tunnel = plugins.tls.connect(this.targetPort, options, () => {
2024-03-24 13:44:44 +00:00
console.log('Connected to PublicRemoteConnector on port 4000');
});
tunnel.on('data', (data: Buffer) => {
2024-04-14 01:38:00 +00:00
const targetConnection = plugins.tls.connect({
host: this.targetHost,
port: this.targetPort,
// Include necessary options for the target connection
}, () => {
targetConnection.write(data);
});
targetConnection.on('data', (backData: Buffer) => {
tunnel.write(backData); // Send data back through the tunnel
});
2024-03-24 13:44:44 +00:00
});
}
}