Using a proxy

You can use a proxy with MadelineProto.

There are three ways to do this:

Socks5 proxy

No password:

$settings['connection_settings']['all']['proxy'] = '\SocksProxy';
$settings['connection_settings']['all']['proxy_extra'] = ['address' => $proxy_address, 'port' => $proxy_port];

$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);

With password:

$settings['connection_settings']['all']['proxy'] = '\SocksProxy';
$settings['connection_settings']['all']['proxy_extra'] = ['address' => $proxy_address, 'port' => $proxy_port, 'username' => 'user', 'password' => 'afnjasf'];

$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);

HTTP proxy

No password:

$settings['connection_settings']['all']['proxy'] = '\HttpProxy';
$settings['connection_settings']['all']['proxy_extra'] = ['address' => $proxy_address, 'port' => $proxy_port];

$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);

With password:

$settings['connection_settings']['all']['proxy'] = '\HttpProxy';
$settings['connection_settings']['all']['proxy_extra'] = ['address' => $proxy_address, 'port' => $proxy_port, 'username' => 'user', 'password' => 'afnjasf'];

$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);

Build your proxy

class MyProxy implements \danog\MadelineProto\Proxy
{
    //...
}
$MadelineProto->settings['connection_settings']['all']['proxy'] = '\MyProxy';

Simply create a class that implements the \danog\MadelineProto\Proxy interface, and enter its name in the settings.

Your proxy class MUST use the \Socket class for all TCP/UDP communications.

Your proxy class can also have a setExtra method that accepts an array as the first parameter, to pass the values provided in the proxy_extra setting.

The \Socket class has the following methods (all of the following methods must also be implemented by your proxy class):

public function __construct(int $domain, int $type, int $protocol);

Works exactly like the socket_connect function.

public function setOption(int $level, int $name, $value);

Works exactly like the socket_set_option function.

public function getOption(int $name, $value);

Works exactly like the socket_get_option function.

public function setBlocking(bool $blocking);

Works like the socket_block or socket_nonblock functions.

public function bind(string $address, [ int $port = 0 ]);

Works exactly like the socket_bind function.

public function listen([ int $backlog = 0 ]);

Works exactly like the socket_listen function.

public function accept();

Works exactly like the socket_accept function.

public function connect(string $address, [ int $port = 0 ]);

Works exactly like the socket_accept function.

public function read(int $length, [ int $flags = 0 ]);

Works exactly like the socket_read function.

public function write(string $buffer, [ int $length ]);

Works exactly like the socket_write function.

public function send(string $data, int $length, int $flags);

Works exactly like the socket_send function.

public function close();

Works exactly like the socket_close function.

public function getPeerName(bool $port = true);

Works like socket_getpeername: the difference is that it returns an array with the host and the port.

public function getSockName(bool $port = true);

Works like socket_getsockname: the difference is that it returns an array with the host and the port.

public function getProxyHeaders();

Can return additional HTTP headers to use when the HTTP protocol is being used.

public function getResource();

Returns the resource used for socket communication: should call $socket->getResource().

Next section