import * as plugins from '../../plugins.js';
import { logger } from './logger.js';
import { ProxyProtocolParser as ProtocolParser } from '../../protocols/proxy/index.js';
/**
 * Parser for PROXY protocol v1 (text format)
 * Spec: https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
 *
 * This class now delegates to the protocol parser but adds
 * smartproxy-specific features like socket reading and logging
 */
export class ProxyProtocolParser {
    static { this.PROXY_V1_SIGNATURE = ProtocolParser.PROXY_V1_SIGNATURE; }
    static { this.MAX_HEADER_LENGTH = ProtocolParser.MAX_HEADER_LENGTH; }
    static { this.HEADER_TERMINATOR = ProtocolParser.HEADER_TERMINATOR; }
    /**
     * Parse PROXY protocol v1 header from buffer
     * Returns proxy info and remaining data after header
     */
    static parse(data) {
        // Delegate to protocol parser
        return ProtocolParser.parse(data);
    }
    /**
     * Generate PROXY protocol v1 header
     */
    static generate(info) {
        // Delegate to protocol parser
        return ProtocolParser.generate(info);
    }
    /**
     * Validate IP address format
     */
    static isValidIP(ip, protocol) {
        return ProtocolParser.isValidIP(ip, protocol);
    }
    /**
     * Attempt to read a complete PROXY protocol header from a socket
     * Returns null if no PROXY protocol detected or incomplete
     */
    static async readFromSocket(socket, timeout = 5000) {
        return new Promise((resolve) => {
            let buffer = Buffer.alloc(0);
            let resolved = false;
            const cleanup = () => {
                socket.removeListener('data', onData);
                socket.removeListener('error', onError);
                clearTimeout(timer);
            };
            const timer = setTimeout(() => {
                if (!resolved) {
                    resolved = true;
                    cleanup();
                    resolve({
                        proxyInfo: null,
                        remainingData: buffer
                    });
                }
            }, timeout);
            const onData = (chunk) => {
                buffer = Buffer.concat([buffer, chunk]);
                // Check if we have enough data
                if (!buffer.toString('ascii', 0, Math.min(6, buffer.length)).startsWith(this.PROXY_V1_SIGNATURE)) {
                    // Not PROXY protocol
                    resolved = true;
                    cleanup();
                    resolve({
                        proxyInfo: null,
                        remainingData: buffer
                    });
                    return;
                }
                // Try to parse
                try {
                    const result = this.parse(buffer);
                    if (result.proxyInfo) {
                        // Successfully parsed
                        resolved = true;
                        cleanup();
                        resolve(result);
                    }
                    else if (buffer.length > this.MAX_HEADER_LENGTH) {
                        // Header too long
                        resolved = true;
                        cleanup();
                        resolve({
                            proxyInfo: null,
                            remainingData: buffer
                        });
                    }
                    // Otherwise continue reading
                }
                catch (error) {
                    // Parse error
                    logger.log('error', `PROXY protocol parse error: ${error.message}`);
                    resolved = true;
                    cleanup();
                    resolve({
                        proxyInfo: null,
                        remainingData: buffer
                    });
                }
            };
            const onError = (error) => {
                logger.log('error', `Socket error while reading PROXY protocol: ${error.message}`);
                resolved = true;
                cleanup();
                resolve({
                    proxyInfo: null,
                    remainingData: buffer
                });
            };
            socket.on('data', onData);
            socket.on('error', onError);
        });
    }
}
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicHJveHktcHJvdG9jb2wuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi90cy9jb3JlL3V0aWxzL3Byb3h5LXByb3RvY29sLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sa0JBQWtCLENBQUM7QUFDNUMsT0FBTyxFQUFFLE1BQU0sRUFBRSxNQUFNLGFBQWEsQ0FBQztBQUNyQyxPQUFPLEVBQUUsbUJBQW1CLElBQUksY0FBYyxFQUEyQyxNQUFNLGdDQUFnQyxDQUFDO0FBS2hJOzs7Ozs7R0FNRztBQUNILE1BQU0sT0FBTyxtQkFBbUI7YUFDZCx1QkFBa0IsR0FBRyxjQUFjLENBQUMsa0JBQWtCLENBQUM7YUFDdkQsc0JBQWlCLEdBQUcsY0FBYyxDQUFDLGlCQUFpQixDQUFDO2FBQ3JELHNCQUFpQixHQUFHLGNBQWMsQ0FBQyxpQkFBaUIsQ0FBQztJQUVyRTs7O09BR0c7SUFDSCxNQUFNLENBQUMsS0FBSyxDQUFDLElBQVk7UUFDdkIsOEJBQThCO1FBQzlCLE9BQU8sY0F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