remoteingress/ts/connector.public.ts

46 lines
1.4 KiB
TypeScript
Raw Permalink 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
2024-04-14 01:38:00 +00:00
class PublicRemoteConnector {
private tunnel: plugins.tls.TLSSocket | null = null;
2024-03-24 13:44:44 +00:00
constructor() {
this.createTunnel();
this.listenOnPorts();
}
private createTunnel(): void {
const options = {
2024-04-14 01:38:00 +00:00
key: plugins.fs.readFileSync('path/to/key.pem'),
cert: plugins.fs.readFileSync('path/to/cert.pem'),
2024-03-24 13:44:44 +00:00
};
2024-04-14 01:38:00 +00:00
const server = plugins.tls.createServer(options, (socket: plugins.tls.TLSSocket) => {
2024-03-24 13:44:44 +00:00
this.tunnel = socket;
console.log('Tunnel established with LocalConnector');
});
server.listen(4000, () => {
console.log('PublicRemoteConnector listening for tunnel on port 4000');
});
}
private listenOnPorts(): void {
2024-04-14 01:38:00 +00:00
// Example for port 80, adapt for port 443 similarly
// Note: TLS for the initial connection might not apply directly for HTTP/HTTPS traffic without additional setup
const options = {
key: plugins.fs.readFileSync('path/to/key.pem'),
cert: plugins.fs.readFileSync('path/to/cert.pem'),
};
plugins.tls.createServer(options, (socket: plugins.tls.TLSSocket) => {
console.log('Received connection, tunneling to LocalConnector');
if (this.tunnel) {
socket.pipe(this.tunnel).pipe(socket);
} else {
console.log('Tunnel to LocalConnector not established');
socket.end();
}
}).listen(80); // Repeat this block for any other ports you wish to listen on
2024-03-24 13:44:44 +00:00
}
}