Help fix log error please (PHP Warning: stream_socket_client(): unable to connect to tcp)

mphp

New member
Hi everyone ツ. I have an annoying error in my php logs thats constant and annoying. Basically developer is out of business and their server is down so anytime the extension tries to connect to them I get log errors...

Can someone help me modify their PHP code to exclude this error? I am so appreciative of any help you guys can provide. Beginner in PHP and really don't want to break the extension if I try to modify the code myself...

PHP Warning: stream_socket_client(): unable to connect to tcp://46.101.32.100:443 (Connection timed out) in /lib/browser.php on line 489

This is the code from line 489:
Code:
$this->sock = stream_socket_client("tcp://$this->addr:$this->port", $errno, $errorMessage, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);

FULL CODE:
Code:
    public function connect() {
        $reuseKey = implode(':', array($this->addr, $this->port));
        if (isset(self::$connections[$reuseKey])) {
            foreach (self::$connections[$reuseKey] as $sock) {
                if (!in_array($sock, NitroBrowser::$free_connections)) continue;

                $this->sock = $sock;
                if ($this->isConnectionValid()) {// check if the connection is still alive
                    return;
                } else {
                    $this->disconnect(); // Remove the inactive connection
                }
            }
        }

        if (stripos(ini_get('disable_functions'), 'stream_socket_client') !== FALSE) {
            throw new RuntimeException("stream_socket_client is disabled."); 
        }

        $ctxOptions = array(
            "ssl" => array(
                "verify_peer" => false,
                "verify_peer_name" => false,
                "allow_self_signed" => true,
                "SNI_enabled" => true,
                "peer_name" => $this->host
            )
        );

        if (version_compare(PHP_VERSION, '5.6.0', '<')) {
            $ctxOptions["ssl"]["SNI_server_name"] = $this->host;
        }

        $ctx = stream_context_create($ctxOptions);

        $errno = $errorMessage = NULL;
        $connectionStartTime = microtime(true);

        $this->sock = stream_socket_client("tcp://$this->addr:$this->port", $errno, $errorMessage, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);

        $this->initial_connection = microtime(true) - $connectionStartTime;

        if($this->sock === false) {
            throw new SocketOpenException('Unable to open socket to: ' . $this->host .' on port ' . $this->port);
        }

        if ($this->connection_reuse) {
            if (!isset(self::$connections[$reuseKey])) {
                self::$connections[$reuseKey] = array();
            }
            self::$connections[$reuseKey][] = $this->sock;
        }

        NitroBrowser::$secure_connections[(int)$this->sock] = false;
    }
 
To handle the connection timeout error and exclude it from generating warnings in your PHP logs, you can wrap the stream_socket_client call in a try-catch block to catch the exception that is thrown when a connection timeout occurs

PHP:
public function connect() {
    $reuseKey = implode(':', array($this->addr, $this->port));
    if (isset(self::$connections[$reuseKey])) {
        foreach (self::$connections[$reuseKey] as $sock) {
            if (!in_array($sock, NitroBrowser::$free_connections)) continue;

            $this->sock = $sock;
            if ($this->isConnectionValid()) {// check if the connection is still alive
                return;
            } else {
                $this->disconnect(); // Remove the inactive connection
            }
        }
    }

    if (stripos(ini_get('disable_functions'), 'stream_socket_client') !== FALSE) {
        throw new RuntimeException("stream_socket_client is disabled.");
    }

    $ctxOptions = array(
        "ssl" => array(
            "verify_peer" => false,
            "verify_peer_name" => false,
            "allow_self_signed" => true,
            "SNI_enabled" => true,
            "peer_name" => $this->host
        )
    );

    if (version_compare(PHP_VERSION, '5.6.0', '<')) {
        $ctxOptions["ssl"]["SNI_server_name"] = $this->host;
    }

    $ctx = stream_context_create($ctxOptions);

    try {
        $errno = $errorMessage = NULL;
        $connectionStartTime = microtime(true);

        $this->sock = @stream_socket_client("tcp://$this->addr:$this->port", $errno, $errorMessage, $this->timeout, STREAM_CLIENT_CONNECT, $ctx);

        if ($this->sock === false) {
            throw new SocketOpenException('Unable to open socket to: ' . $this->host .' on port ' . $this->port);
        }

        $this->initial_connection = microtime(true) - $connectionStartTime;

        if ($this->connection_reuse) {
            if (!isset(self::$connections[$reuseKey])) {
                self::$connections[$reuseKey] = array();
            }
            self::$connections[$reuseKey][] = $this->sock;
        }

        NitroBrowser::$secure_connections[(int)$this->sock] = false;
    } catch (Exception $e) {
        // Handle the exception here (e.g., log it) if needed
        // You can add custom error handling or just leave it empty to suppress the error
    }
}

In this modification, I've wrapped the stream_socket_client call in a try-catch block. If a connection timeout occurs, it will catch the exception thrown and handle it appropriately. By using @ symbol before stream_socket_client, we suppress the warning from being displayed. However, it's important to handle the exception appropriately within the catch block, either by logging it or implementing custom error handling logic.

Best Regard
Danish Hafeez | QA Assistant
ICTInnovations
 
Thanks bud. Just gave that a try, although it does hide the timeout error.. it ended up with a lot more errors

Code:
2024-05-10 11:38:16 - PHP Notice:  Undefined offset: 0 in lib/browser.php on line 929
2024-05-10 11:38:16 - PHP Warning:  stream_set_blocking() expects parameter 1 to be resource, boolean given in lib/browser.php on line 530
2024-05-10 11:38:16 - PHP Warning:  stream_socket_enable_crypto() expects parameter 1 to be resource, boolean given in lib/browser.php on line 545
2024-05-10 11:38:16 - PHP Warning:  stream_set_blocking() expects parameter 1 to be resource, boolean given in lib/browser.php on line 546
 
Back
Top