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

// Augment the Node.js Socket type to include TLS-related properties
// This helps TypeScript understand properties that are dynamically added by Node.js
declare module 'net' {
  interface Socket {
    // TLS-related properties
    encrypted?: boolean;    // Indicates if the socket is encrypted (TLS/SSL)
    authorizationError?: Error; // Authentication error if TLS handshake failed
    
    // TLS-related methods
    getTLSVersion?(): string;  // Returns the TLS version (e.g., 'TLSv1.2', 'TLSv1.3')
    getPeerCertificate?(detailed?: boolean): any;  // Returns the peer's certificate
    getSession?(): Buffer;   // Returns the TLS session data
    
    // Connection tracking properties (used by HttpProxy)
    _connectionId?: string;   // Unique identifier for the connection
    _remoteIP?: string;       // Remote IP address
    _realRemoteIP?: string;   // Real remote IP (when proxied)
  }
}

// Export a utility function to check if a socket is a TLS socket
export function isTLSSocket(socket: plugins.net.Socket): boolean {
  return 'encrypted' in socket && !!socket.encrypted;
}

// Export a utility function to safely get the TLS version
export function getTLSVersion(socket: plugins.net.Socket): string | null {
  if (socket.getTLSVersion) {
    try {
      return socket.getTLSVersion();
    } catch (e) {
      return null;
    }
  }
  return null;
}