1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 04:59:00 +01:00

Support altervista.org out of the box

This commit is contained in:
Daniil Gentili 2018-03-21 20:29:20 +00:00
parent 209dde1a89
commit 8ee71afccb
5 changed files with 177 additions and 5 deletions

View File

@ -44,7 +44,8 @@
"src/Volatile.php",
"src/Thread.php",
"src/Worker.php",
"src/Pool.php"
"src/Pool.php",
"src/HttpProxy.php"
]
}
}

160
src/HttpProxy.php Normal file
View File

@ -0,0 +1,160 @@
<?php
/*
Copyright 2016-2017 Daniil Gentili
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
class HttpProxy implements \danog\MadelineProto\Proxy
{
private $domain;
private $type;
private $protocol;
private $extra;
private $sock;
public function __construct($domain, $type, $protocol) {
if (!in_array($domain, [AF_INET, AF_INET6])) {
throw new \danog\MadelineProto\Exception('Wrong protocol family provided');
}
if (!in_array($type, [SOCK_STREAM])) {
throw new \danog\MadelineProto\Exception('Wrong connection type provided');
}
if (!in_array($protocol, [getprotobyname('tcp'), PHP_INT_MAX])) {
throw new \danog\MadelineProto\Exception('Wrong protocol provided');
}
$this->domain = $domain;
$this->type = $type;
$this->protocol = $protocol;
}
public function setExtra(array $extra = []) {
$this->extra = $extra;
$this->sock = new \Socket(strlen(@inet_pton($this->extra['address'])) !== 4 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, $this->protocol);
}
public function setOption($level, $name, $value) {
return $this->sock->setOption($level, $name, $value);
}
public function getOption($level, $name) {
return $this->sock->getOption($level, $name);
}
public function setBlocking($blocking) {
return $this->sock->setBlocking($blocking);
}
public function bind($address, $port = 0) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function listen($backlog = 0) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function accept() {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function select(array &$read, array &$write, array &$except, $tv_sec, $tv_usec = 0) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function connect($address, $port = 0) {
$this->sock->connect($this->extra['address'], $this->extra['port']);
try {
if (strlen(inet_pton($address)) !== 4) {
$address = '['.$address.']';
}
} catch (\danog\MadelineProto\Exception $e) {
}
$this->sock->write("CONNECT $address:$port HTTP/1.1\r\nHost: $address:$port\r\n\r\n");
$response = $this->read_http_payload();
if ($response['code'] !== 200) {
\danog\MadelineProto\Logger::log(trim($response['body']));
throw new \danog\MadelineProto\Exception($response['description'], $response['code']);
}
\danog\MadelineProto\Logger::log('Connected to '.$address.':'.$port.' via http');
return true;
}
private function http_read($length) {
$packet = '';
while (strlen($packet) < $length) {
$packet .= $this->sock->read($length - strlen($packet));
if ($packet === false || strlen($packet) === 0) {
throw new \danog\MadelineProto\NothingInTheSocketException(\danog\MadelineProto\Lang::$current_lang['nothing_in_socket']);
}
}
return $packet;
}
public function read_http_line()
{
$line = '';
while (($curchar = $this->http_read(1)) !== "\n") {
$line .= $curchar;
}
return rtrim($line);
}
public function read_http_payload()
{
$header = explode(' ', $this->read_http_line(), 3);
$protocol = $header[0];
$code = (int) $header[1];
$description = $header[2];
$headers = [];
while (strlen($current_header = $this->read_http_line())) {
$current_header = explode(':', $current_header, 2);
$headers[strtolower($current_header[0])] = trim($current_header[1]);
}
$read = '';
if (isset($headers['content-length'])) {
$read = $this->http_read((int) $headers['content-length']);
}/* elseif (isset($headers['transfer-encoding']) && $headers['transfer-encoding'] === 'chunked') {
do {
$length = hexdec($this->read_http_line());
$read .= $this->http_read($length);
$this->read_http_line();
} while ($length);
}*/
return ['protocol' => $protocol, 'code' => $code, 'description' => $description, 'body' => $read, 'headers' => $headers];
}
public function read($length, $flags = 0) {
$read = $this->sock->read($length, $flags);
if ($read === 0) {
throw new \danog\MadelineProto\Exception('pls reconnect');
}
return $read;
}
public function write($buffer, $length = -1) {
return $this->sock->write($buffer, $length);
}
public function send($data, $length, $flags) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function close() {
$this->sock->close();
}
public function getPeerName($port = true) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function getSockName($port = true) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function getProxyHeaders() {
}
}

View File

@ -151,7 +151,7 @@ class Connection
if (strpos($this->protocol, 'https') === 0 && $proxy === '\\Socket') {
$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')));
$this->sock = new $proxy($ipv6 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, strpos($this->protocol, 'https') === 0 ? PHP_INT_MAX : getprotobyname('tcp'));
if ($has_proxy) {
if ($this->extra !== []) {
$this->sock->setExtra($this->extra);

View File

@ -116,6 +116,16 @@ class DataCenter
$this->settings[$dc_config_number]['ipv6'] = !$this->settings[$dc_config_number]['ipv6'];
\danog\MadelineProto\Logger::log('Connection failed, retrying connection without the proxy with '.($this->settings[$dc_config_number]['ipv6'] ? 'ipv6' : 'ipv4').'...', \danog\MadelineProto\Logger::WARNING);
continue;
case 3:
$this->settings[$dc_config_number]['proxy'] = '\\HttpProxy';
$this->settings[$dc_config_number]['proxy_extra'] = ['address' => 'localhost', 'port' => 80];
$this->settings[$dc_config_number]['ipv6'] = !$this->settings[$dc_config_number]['ipv6'];
\danog\MadelineProto\Logger::log('Connection failed, retrying connection with localhost HTTP proxy with '.($this->settings[$dc_config_number]['ipv6'] ? 'ipv6' : 'ipv4').'...', \danog\MadelineProto\Logger::WARNING);
continue;
case 4:
$this->settings[$dc_config_number]['ipv6'] = !$this->settings[$dc_config_number]['ipv6'];
\danog\MadelineProto\Logger::log('Connection failed, retrying connection with localhost HTTP proxy with '.($this->settings[$dc_config_number]['ipv6'] ? 'ipv6' : 'ipv4').'...', \danog\MadelineProto\Logger::WARNING);
continue;
default:
throw new \danog\MadelineProto\Exception("Could not connect to DC $dc_number");
}

View File

@ -54,7 +54,7 @@ class MTProto
/*
const V = 71;
*/
const V = 97;
const V = 98;
const NOT_LOGGED_IN = 0;
const WAITING_CODE = 1;
const WAITING_SIGNUP = -1;
@ -369,6 +369,7 @@ class MTProto
if (isset(Lang::$lang[$lang_code])) {
Lang::$current_lang = &Lang::$lang[$lang_code];
}
$altervista = isset($_SERVER['SERVER_ADMIN']) && strpos($_SERVER['SERVER_ADMIN'], 'altervista.org');
// Set default settings
$default_settings = ['authorization' => [
// Authorization settings
@ -443,9 +444,9 @@ class MTProto
// decides whether to use ipv6, ipv6 attribute of API attribute of API class contains autodetected boolean
'timeout' => 2,
// timeout for sockets
'proxy' => '\\Socket',
'proxy' => $altervista ? '\\HttpProxy' : '\\Socket',
// The proxy class to use
'proxy_extra' => [],
'proxy_extra' => $altervista ? ['address' => 'localhost', 'port' => 80] : [],
// Extra parameters to pass to the proxy class using setExtra
'pfs' => extension_loaded('gmp'),
],