Add socks5 and HTTP proxy, implement authorization in socks5 proxy, implement authorization in http proxy, simplify socket and connection classes

This commit is contained in:
Daniil Gentili 2018-04-18 16:11:59 +02:00
parent a0a9bf2889
commit 1e3ec54ce8
10 changed files with 267 additions and 91 deletions

1
.gitignore vendored
View File

@ -2,7 +2,6 @@ MadelineProtoPhar
JSON.sh
halloween
*.raw
SocksProxy.php
magnabroadcast.php
broadcast.php

View File

@ -65,7 +65,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
}
}
$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]]; //, 'connection_settings' => ['all' => ['test_mode' => true]]];
$settings = ['app_info' => ['api_id' => 6, 'api_hash' => 'eb06d4abfb49dc3eeb1aeb98ae0f581e'], 'updates' => ['handle_updates' => true]];;
$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings);

View File

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

2
docs

@ -1 +1 @@
Subproject commit 916411226dd921657305ee7c344c870f6800f479
Subproject commit b6152298e377fe696e67971d889833fa1f36da8e

View File

@ -18,7 +18,7 @@ class HttpProxy implements \danog\MadelineProto\Proxy
private $extra;
private $sock;
public function __construct($domain, $type, $protocol)
public function __construct(int $domain, int $type, int $protocol)
{
if (!in_array($domain, [AF_INET, AF_INET6])) {
throw new \danog\MadelineProto\Exception('Wrong protocol family provided');
@ -73,7 +73,7 @@ class HttpProxy implements \danog\MadelineProto\Proxy
public function select(array &$read, array &$write, array &$except, $tv_sec, $tv_usec = 0)
{
throw new \danog\MadelineProto\Exception('Not Implemented');
return $this->sock->select($read, $write, $except, $tv_sec, $tv_usec);
}
public function connect($address, $port = 0)
@ -86,7 +86,7 @@ class HttpProxy implements \danog\MadelineProto\Proxy
}
} catch (\danog\MadelineProto\Exception $e) {
}
$this->sock->write("CONNECT $address:$port HTTP/1.1\r\nHost: $address:$port\r\n\r\n");
$this->sock->write("CONNECT $address:$port HTTP/1.1\r\nHost: $address:$port\r\nAccept: */*\r\n".$this->getProxyAuthHeader()."Connection: keep-Alive\r\n\r\n");
$response = $this->read_http_payload();
if ($response['code'] !== 200) {
\danog\MadelineProto\Logger::log(trim($response['body']));
@ -98,35 +98,24 @@ class HttpProxy implements \danog\MadelineProto\Proxy
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 = $lastchar = $curchar = '';
while ($lastchar.$curchar !== "\r\n") {
$line .= $curchar;
$lastchar = $curchar;
$curchar = $this->sock->read(1);
}
return rtrim($line);
return $line;
}
public function read_http_payload()
{
$header = explode(' ', $this->read_http_line(), 3);
$protocol = $header[0];
$code = (int) $header[1];
$description = $header[2];
list($protocol, $code, $description) = explode(' ', $this->read_http_line(), 3);
list($protocol, $protocol_version) = explode('/', $protocol);
if ($protocol !== 'HTTP') throw new \danog\MadelineProto\Exception('Wrong protocol');
$code = (int) $code;
$headers = [];
while (strlen($current_header = $this->read_http_line())) {
$current_header = explode(':', $current_header, 2);
@ -135,26 +124,21 @@ class HttpProxy implements \danog\MadelineProto\Proxy
$read = '';
if (isset($headers['content-length'])) {
$read = $this->http_read((int) $headers['content-length']);
$read = $this->sock->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);
$read .= $this->sock->read($length);
$this->read_http_line();
} while ($length);
}*/
return ['protocol' => $protocol, 'code' => $code, 'description' => $description, 'body' => $read, 'headers' => $headers];
return ['protocol' => $protocol, 'protocol_version' => $protocol_version, '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;
return $this->sock->read($length, $flags);
}
public function write($buffer, $length = -1)
@ -182,8 +166,18 @@ class HttpProxy implements \danog\MadelineProto\Proxy
throw new \danog\MadelineProto\Exception('Not Implemented');
}
private function getProxyAuthHeader()
{
if (!isset($this->extra['user']) || !isset($this->extra['password'])) {
return '';
}
return 'Proxy-Authorization: Basic '.base64_encode($this->extra['user'].':'.$this->extra['password'])."\r\n";
}
public function getProxyHeaders()
{
return '';
}
public function getResource()

View File

@ -117,12 +117,30 @@ If not, see <http://www.gnu.org/licenses/>.
public function read(int $length, int $flags = 0)
{
return stream_get_contents($this->sock, $length);
$packet = '';
while (strlen($packet) < $length) {
$read = stream_get_contents($this->sock, $length - strlen($packet));
if ($read === false || strlen($read) === 0) throw new \danog\MadelineProto\NothingInTheSocketException();
$packet .= $read;
}
return $packet;
}
public function write(string $buffer, int $length = -1)
{
return $length === -1 ? fwrite($this->sock, $buffer) : fwrite($this->sock, $buffer, $length);
if ($length === -1) {
$length = strlen($buffer);
} else {
$buffer = substr($buffer, 0, $length);
}
$wrote = 0;
if (($wrote += fwrite($this->sock, $buffer, $length)) !== $length) {
while (($wrote += fwrite($this->sock, substr($buffer, $wrote), $length-$wrote)) !== $length) {
}
}
return $wrote;
}
public function send(string $data, int $length, int $flags)
@ -261,12 +279,30 @@ if (!extension_loaded('pthreads')) {
public function read(int $length, int $flags = 0)
{
return socket_read($this->sock, $length, $flags);
$packet = '';
while (strlen($packet) < $length) {
$read = socket_read($this->sock, $length - strlen($packet), $flags);
if ($read === false || strlen($read) === false) throw new \danog\MadelineProto\NothingInTheSocketException();
$packet .= $read;
}
return $packet;
}
public function write(string $buffer, int $length = -1)
{
return $length === -1 ? socket_write($this->sock, $buffer) : socket_write($this->sock, $buffer, $length);
if ($length === -1) {
$length = strlen($buffer);
} else {
$buffer = substr($buffer, 0, $length);
}
$wrote = 0;
if (($wrote += socket_write($this->sock, $buffer, $length)) !== $length) {
while (($wrote += socket_write($this->sock, substr($buffer, $wrote), $length-$wrote)) !== $length) {
}
}
return $wrote;
}
public function send(string $data, int $length, int $flags)

166
src/SocksProxy.php Normal file
View File

@ -0,0 +1,166 @@
<?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 SocksProxy implements \danog\MadelineProto\Proxy
{
private $domain;
private $type;
private $protocol;
private $extra;
private $sock;
public function __construct(int $domain, int $type, int $protocol)
{
if (!in_array($domain, [AF_INET, AF_INET6])) {
throw new \danog\MadelineProto\Exception('Wrong protocol family provided');
}
if (!in_array($type, [SOCK_STREAM, SOCK_DGRAM])) {
throw new \danog\MadelineProto\Exception('Wrong connection type provided');
}
if (!in_array($protocol, [getprotobyname('tcp'), getprotobyname('udp'), 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;
$name = $this->protocol === PHP_INT_MAX ? '\\FSocket' : '\\Socket';
$this->sock = new $name(strlen(@inet_pton($this->extra['address'])) !== 4 ? \AF_INET6 : \AF_INET, \SOCK_STREAM, $this->protocol);
}
public function setOption(int $level, int $name, $value) {
return $this->sock->setOption($level, $name, $value);
}
public function getOption(int $level, int $name) {
return $this->sock->getOption($level, $name);
}
public function setBlocking(bool $blocking) {
return $this->sock->setBlocking($blocking);
}
public function bind(string $address, int $port = 0) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function listen(int $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, int $tv_sec, int $tv_usec = 0) {
return $this->sock->select($read, $write, $except, $tv_sec, $tv_usec);
}
public function connect(string $address, int $port = 0) {
$this->sock->connect($this->extra['address'], $this->extra['port']);
$methods = chr(0);
if (isset($this->extra['username']) && isset($this->extra['password'])) {
$methods .= chr(2);
}
$this->sock->write(chr(5).chr(strlen($methods)).$methods);
$version = ord($this->sock->read(1));
$method = ord($this->sock->read(1));
if ($version !== 5) {
throw new \danog\MadelineProto\Exception("Wrong SOCKS5 version: $version");
}
if ($method === 2) {
$this->sock->write(chr(1).chr(strlen($this->extra['username'])).$this->extra['username'].chr(strlen($this->extra['password'])).$this->extra['password']);
$version = ord($this->sock->read(1));
if ($version !== 1) {
throw new \danog\MadelineProto\Exception("Wrong authorized SOCKS version: $version");
}
$result = ord($this->sock->read(1));
if ($result !== 0) {
throw new \danog\MadelineProto\Exception("Wrong authorization status: $version");
}
} else if ($method !== 0) {
throw new \danog\MadelineProto\Exception("Wrong method: $method");
}
$payload = pack("C3", 0x05, 0x01, 0x00);
try {
$ip = inet_pton($address);
$payload .= pack("C1", strlen($ip) === 4 ? 0x01 : 0x04).$ip;
} catch (\danog\MadelineProto\Exception $e) {
$payload .= pack("C2", 0x03, strlen($address)).$address;
}
$payload .= pack("n", $port);
$this->sock->write($payload);
$version = ord($this->sock->read(1));
if ($version !== 5) {
throw new \danog\MadelineProto\Exception("Wrong SOCKS5 version: $version");
}
$rep = ord($this->sock->read(1));
if ($rep !== 0) {
throw new \danog\MadelineProto\Exception("Wrong SOCKS5 rep: $rep");
}
$rsv = ord($this->sock->read(1));
if ($rsv !== 0) {
throw new \danog\MadelineProto\Exception("Wrong socks5 final RSV: $rsv");
}
switch (ord($this->sock->read(1))) {
case 1:
$ip = inet_ntop($this->sock->read(4));
break;
case 4:
$ip = inet_ntop($this->sock->read(16));
break;
case 3:
$ip = $this->sock->read(ord($this->sock->read(1)));
break;
}
$port = unpack("n", $this->sock->read(2))[1];
\danog\MadelineProto\Logger::log(['Connected to '.$ip.':'.$port.' via socks5']);
return true;
}
public function read(int $length, int $flags = 0) {
return $this->sock->read($length, $flags);
}
public function write(string $buffer, int $length = -1) {
return $this->sock->write($buffer, $length);
}
public function send(string $data, int $length, int $flags) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function close() {
$this->sock->close();
}
public function getPeerName(bool $port = true) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function getSockName(bool $port = true) {
throw new \danog\MadelineProto\Exception('Not Implemented');
}
public function getProxyHeaders() {
return '';
}
public function getResource() {
return $this->sock->getResource();
}
}

View File

@ -243,13 +243,7 @@ class Connection
case 'tcp_full':
case 'http':
case 'https':
$wrote = 0;
if (($wrote += $this->sock->write($what)) !== $length) {
while (($wrote += $this->sock->write(substr($what, $wrote))) !== $length) {
}
}
return $wrote;
return $this->sock->write($what);
case 'udp':
throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']);
default:
@ -262,31 +256,13 @@ class Connection
//\danog\MadelineProto\Logger::log("Asked to read $length", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
switch ($this->protocol) {
case 'obfuscated2':
$packet = '';
while (strlen($packet) < $length) {
$piece = $this->sock->read($length - strlen($packet));
if ($piece === false || strlen($piece) === 0) {
throw new \danog\MadelineProto\NothingInTheSocketException(\danog\MadelineProto\Lang::$current_lang['nothing_in_socket']);
}
$packet .= $piece;
}
return @$this->obfuscated['decryption']->encrypt($packet);
return @$this->obfuscated['decryption']->encrypt($this->sock->read($length));
case 'tcp_abridged':
case 'tcp_intermediate':
case 'tcp_full':
case 'http':
case 'https':
$packet = '';
while (strlen($packet) < $length) {
$piece = $this->sock->read($length - strlen($packet));
if ($piece === false || strlen($piece) === 0) {
throw new \danog\MadelineProto\NothingInTheSocketException(\danog\MadelineProto\Lang::$current_lang['nothing_in_socket']);
}
$packet .= $piece;
}
return $packet;
return $this->sock->read($length);
case 'udp':
throw new Exception(\danog\MadelineProto\Lang::$current_lang['protocol_not_implemented']);
default:
@ -384,20 +360,22 @@ class Connection
public function read_http_line()
{
$line = '';
while (($curchar = $this->read(1)) !== "\n") {
$line = $lastchar = $curchar = '';
while ($lastchar.$curchar !== "\r\n") {
$line .= $curchar;
$lastchar = $curchar;
$curchar = $this->sock->read(1);
}
return rtrim($line);
return $line;
}
public function read_http_payload()
{
$header = explode(' ', $this->read_http_line(), 3);
$protocol = $header[0];
$code = (int) $header[1];
$description = $header[2];
list($protocol, $code, $description) = explode(' ', $this->read_http_line(), 3);
list($protocol, $protocol_version) = explode('/', $protocol);
if ($protocol !== 'HTTP') throw new \danog\MadelineProto\Exception('Wrong protocol');
$code = (int) $code;
$headers = [];
while (strlen($current_header = $this->read_http_line())) {
$current_header = explode(':', $current_header, 2);
@ -406,16 +384,16 @@ class Connection
$read = '';
if (isset($headers['content-length'])) {
$read = $this->read((int) $headers['content-length']);
} elseif (isset($headers['transfer-encoding']) && $headers['transfer-encoding'] === 'chunked') {
$read = $this->sock->read((int) $headers['content-length']);
}/* elseif (isset($headers['transfer-encoding']) && $headers['transfer-encoding'] === 'chunked') {
do {
$length = hexdec($this->read_http_line());
$read .= $this->read($length);
$read .= $this->sock->read($length);
$this->read_http_line();
} while ($length);
}
}*/
return ['protocol' => $protocol, 'code' => $code, 'description' => $description, 'body' => $read, 'headers' => $headers];
return ['protocol' => $protocol, 'protocol_version' => $protocol_version, 'code' => $code, 'description' => $description, 'body' => $read, 'headers' => $headers];
}
public function getSocket()

View File

@ -100,7 +100,9 @@ class DataCenter
return true;
} catch (\danog\MadelineProto\Exception $e) {
\danog\MadelineProto\Logger::log("Connection failed: ".$e->getMessage(), \danog\MadelineProto\Logger::ERROR);
} catch (\danog\MadelineProto\NothingInTheSocketException $e) {
\danog\MadelineProto\Logger::log("Connection failed: read timeout", \danog\MadelineProto\Logger::ERROR);
}
if (isset($this->settings[$dc_config_number]['do_not_retry']) && $this->settings[$dc_config_number]['do_not_retry']) {
break;

View File

@ -15,33 +15,33 @@ namespace danog\MadelineProto;
interface Proxy
{
public function __construct($domain, $type, $protocol);
public function __construct(int $domain, int $type, int $protocol);
public function setOption($level, $name, $value);
public function setOption(int $level, int $name, $value);
public function getOption($level, $name);
public function getOption(int $level, int $name);
public function setBlocking($blocking);
public function setBlocking(bool $blocking);
public function bind($address, $port = 0);
public function bind(string $address, int $port = 0);
public function listen($backlog = 0);
public function listen(int $backlog = 0);
public function accept();
public function connect($address, $port = 0);
public function connect(string $address, int $port = 0);
public function read($length, $flags = 0);
public function read(int $length, int $flags = 0);
public function write($buffer, $length = -1);
public function write(string $buffer, int $length = -1);
public function send($data, $length, $flags);
public function send(string $data, int $length, int $flags);
public function close();
public function getPeerName($port = true);
public function getPeerName(bool $port = true);
public function getSockName($port = true);
public function getSockName(bool $port = true);
public function getProxyHeaders();