import * as plugins from './plugins.js';

export class ConnectorPrivate {
  private targetHost: string;
  private targetPort: number;

  constructor(targetHost: string, targetPort: number = 4000) {
    this.targetHost = targetHost;
    this.targetPort = targetPort;
    this.connectToPublicRemoteConnector();
  }

  private connectToPublicRemoteConnector(): void {
    const options = {
      // 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
    };

    const tunnel = plugins.tls.connect(this.targetPort, options, () => {
      console.log('Connected to PublicRemoteConnector on port 4000');
    });

    tunnel.on('data', (data: Buffer) => {
      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
      });
    });
  }
}
