diff --git a/src/HTTPProxy.php b/src/HTTPProxy.php index 36f0c891..2884aa74 100644 --- a/src/HTTPProxy.php +++ b/src/HTTPProxy.php @@ -2,7 +2,6 @@ class HTTPProxy implements \danog\MadelineProto\Proxy { - private $sock; private $protocol; private $timeout = ['sec' => 0, 'usec' => 0]; @@ -29,7 +28,7 @@ class HTTPProxy implements \danog\MadelineProto\Proxy { if ($this->sock !== null) { fclose($this->sock); - $this->sock = NULL; + $this->sock = null; } } @@ -64,13 +63,13 @@ class HTTPProxy implements \danog\MadelineProto\Proxy if (isset($this->options['host']) && isset($this->options['port']) && true === $this->use_connect) { if ($this->domain === AF_INET6 && strpos($address, ':') !== false) { - $address = '[' . $address . ']'; + $address = '['.$address.']'; } - fwrite($this->sock, "CONNECT " . $address . ":" . $port . " HTTP/1.1\r\n" . - "Accept: */*\r\n" . - "Host: " . $address . ":" . $port . "\r\n" . - $this->getProxyAuthHeader() . - "connection: keep-Alive\r\n" . + fwrite($this->sock, 'CONNECT '.$address.':'.$port." HTTP/1.1\r\n". + "Accept: */*\r\n". + 'Host: '.$address.':'.$port."\r\n". + $this->getProxyAuthHeader(). + "connection: keep-Alive\r\n". "\r\n"); $response = ''; @@ -79,39 +78,43 @@ class HTTPProxy implements \danog\MadelineProto\Proxy $status = $status || (strpos($line, 'HTTP') !== false); if ($status) { $response .= $line; - if (!chop($line)) + if (!rtrim($line)) { break; + } } } - if (substr($response, 0, 13) !== "HTTP/1.1 200 ") + if (substr($response, 0, 13) !== 'HTTP/1.1 200 ') { return false; + } } if (true === $this->use_ssl) { - $modes = array( + $modes = [ STREAM_CRYPTO_METHOD_TLS_CLIENT, STREAM_CRYPTO_METHOD_SSLv3_CLIENT, STREAM_CRYPTO_METHOD_SSLv23_CLIENT, - STREAM_CRYPTO_METHOD_SSLv2_CLIENT - ); + STREAM_CRYPTO_METHOD_SSLv2_CLIENT, + ]; - $contextOptions = array( - 'ssl' => array( - 'verify_peer' => false, + $contextOptions = [ + 'ssl' => [ + 'verify_peer' => false, 'verify_peer_name' => false, - ), - ); + ], + ]; stream_context_set_option($this->sock, $contextOptions); $success = false; - foreach ($modes as $mode) - { + foreach ($modes as $mode) { $success = stream_socket_enable_crypto($this->sock, true, $mode); - if ($success) + if ($success) { return true; + } } + return false; } + return true; } @@ -162,6 +165,7 @@ class HTTPProxy implements \danog\MadelineProto\Proxy return true; } + throw new \danog\MadelineProto\Exception('Not supported'); } @@ -175,7 +179,8 @@ class HTTPProxy implements \danog\MadelineProto\Proxy if (!isset($this->options['user']) || !isset($this->options['pass'])) { return ''; } - return "Proxy-Authorization: Basic " . base64_encode($this->options['user'] . ":" . $this->options['pass']) . "\r\n"; + + return 'Proxy-Authorization: Basic '.base64_encode($this->options['user'].':'.$this->options['pass'])."\r\n"; } public function getProxyHeaders() @@ -187,5 +192,4 @@ class HTTPProxy implements \danog\MadelineProto\Proxy { $this->options = $extra; } - } diff --git a/src/Socket.php b/src/Socket.php index 6e27bdd9..a024c4c5 100644 --- a/src/Socket.php +++ b/src/Socket.php @@ -116,13 +116,12 @@ If not, see . { throw new \danog\MadelineProto\Exception('Not supported'); } - + public function setExtra(array $extra = []) { - } - public function getProxyHeaders() + public function getProxyHeaders() { return ''; } @@ -236,7 +235,7 @@ if (!extension_loaded('pthreads')) { return $port ? ['host' => $address, 'port' => $port] : ['host' => $address]; } - public function getProxyHeaders() + public function getProxyHeaders() { return ''; } diff --git a/src/danog/MadelineProto/Connection.php b/src/danog/MadelineProto/Connection.php index b88ac11c..2954c9e8 100644 --- a/src/danog/MadelineProto/Connection.php +++ b/src/danog/MadelineProto/Connection.php @@ -152,12 +152,12 @@ class Connection $proxy = '\\FSocket'; } $this->sock = new $proxy($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, strpos($this->protocol, 'https') === 0 ? PHP_INT_MAX : ($has_proxy ? PHP_INT_MAX - 1 : getprotobyname('tcp'))); - if ($has_proxy ) { + if ($has_proxy) { if ($this->extra !== []) { $this->sock->setExtra($this->extra); } if ($this->protocol === 'http') { - $this->parsed['path'] = $this->parsed['scheme'] . '://' . $this->parsed['host'] . + $this->parsed['path'] = $this->parsed['scheme'].'://'.$this->parsed['host']. $this->parsed['path']; $port = 80; } elseif ($this->protocol === 'https') { @@ -361,7 +361,7 @@ class Connection break; case 'http': case 'https': - $this->write('POST '.$this->parsed['path']." HTTP/1.1\r\nHost: ".$this->parsed['host'].':'.$this->port."\r\n" . $this->sock->getProxyHeaders() . "Content-Type: application/x-www-form-urlencoded\r\nConnection: keep-alive\r\nKeep-Alive: timeout=100000, max=10000000\r\nContent-Length: ".strlen($message)."\r\n\r\n".$message); + $this->write('POST '.$this->parsed['path']." HTTP/1.1\r\nHost: ".$this->parsed['host'].':'.$this->port."\r\n".$this->sock->getProxyHeaders()."Content-Type: application/x-www-form-urlencoded\r\nConnection: keep-alive\r\nKeep-Alive: timeout=100000, max=10000000\r\nContent-Length: ".strlen($message)."\r\n\r\n".$message); break; case 'udp': throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']); diff --git a/src/danog/MadelineProto/Proxy.php b/src/danog/MadelineProto/Proxy.php index a3ab66a6..1a5e0916 100644 --- a/src/danog/MadelineProto/Proxy.php +++ b/src/danog/MadelineProto/Proxy.php @@ -44,7 +44,7 @@ interface Proxy public function getPeerName($port = true); public function getSockName($port = true); - + public function getProxyHeaders(); public function setExtra(array $extra = []); diff --git a/tests/testing-proxy.php b/tests/testing-proxy.php index e2daaab0..72a26a89 100644 --- a/tests/testing-proxy.php +++ b/tests/testing-proxy.php @@ -34,42 +34,38 @@ if (file_exists('web_data.php')) { require_once 'web_data.php'; } - /** - * Set default context proxy for file_get_contents - * + * Set default context proxy for file_get_contents. */ - -$default_opts = array( - 'http' => array( - 'proxy' => "tcp://host:port", +$default_opts = [ + 'http' => [ + 'proxy' => 'tcp://host:port', 'request_fulluri' => true, /* Only if your proxy requires authentication */ - 'header' => array( - "Proxy-Authorization: Basic " . base64_encode("proxy_user:proxy_pass") - ) - ), - 'https' => array( - 'proxy' => "tcp://host:port", + 'header' => [ + 'Proxy-Authorization: Basic '.base64_encode('proxy_user:proxy_pass'), + ], + ], + 'https' => [ + 'proxy' => 'tcp://host:port', 'request_fulluri' => true, /* Only if your proxy requires authentication */ - 'header' => array( - "Proxy-Authorization: Basic " . base64_encode("proxy_user:proxy_pass") - ) - ), - 'tls' => array( - 'proxy' => "tcp://host:port", + 'header' => [ + 'Proxy-Authorization: Basic '.base64_encode('proxy_user:proxy_pass'), + ], + ], + 'tls' => [ + 'proxy' => 'tcp://host:port', 'request_fulluri' => true, /* Only if your proxy requires authentication */ - 'header' => array( - "Proxy-Authorization: Basic " . base64_encode("proxy_user:proxy_pass") - ) - ) -); + 'header' => [ + 'Proxy-Authorization: Basic '.base64_encode('proxy_user:proxy_pass'), + ], + ], +]; $default = stream_context_set_default($default_opts); - echo 'Deserializing MadelineProto from testing.madeline...'.PHP_EOL; $MadelineProto = false;