MadelineProtoDocs/docs/docs/PROXY.md

180 lines
5.4 KiB
Markdown
Raw Normal View History

2018-04-11 14:17:45 +02:00
---
title: Using a proxy
description: You can use a proxy with MadelineProto.
image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
---
2018-04-01 13:19:25 +02:00
# Using a proxy
You can use a proxy with MadelineProto.
2019-06-01 14:03:34 +02:00
* [How to set a proxy](#how-to-set-a-proxy)
* [Multiple proxies with automatic switch](#multiple-proxies-with-automatic-switch)
* [Use pre-built MTProxy](#mtproxy)
2018-04-18 16:12:15 +02:00
* [Use pre-built Socks5 proxy](#socks5-proxy)
* [Use pre-built HTTP proxy](#http-proxy)
* [Build your own proxy](#build-your-proxy)
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
## How to set a proxy
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
In order to set a proxy for MadelineProto, you have to modify the [settings](SETTINGS.html).
Legacy proxy names are allowed (`\HttpProxy`, `\SocksProxy`), but not recommended.
The recommended way of getting proxy names to set in the settings is through the `getName` method of the new [proxy interface](#build-your-proxy).
2018-04-01 13:19:25 +02:00
```php
2019-06-01 14:03:34 +02:00
use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream;
use danog\MadelineProto\Stream\Proxy\HttpProxy;
use danog\MadelineProto\Stream\Proxy\SocksProxy;
use my\Custom\Namespace\MyProxy;
$settings['connection_settings']['all']['proxy'] = ObfuscatedStream::getName();
// ...
$settings['connection_settings']['all']['proxy'] = HttpProxy::getName();
// ...
$settings['connection_settings']['all']['proxy'] = SocksProxy::getName();
// ...
$settings['connection_settings']['all']['proxy'] = MyProxy::getName();
2018-04-18 16:12:15 +02:00
```
2019-06-01 14:03:34 +02:00
You can also set multiple proxies:
2018-04-18 16:12:15 +02:00
2019-06-01 14:03:34 +02:00
## Multiple proxies with automatic switch
2018-04-18 16:12:15 +02:00
2019-06-01 14:03:34 +02:00
To set multiple proxies, and let MadelineProto choose the best one, simply assign arrays of proxy and proxy extras to the appropriate settings.
You can also use [iterable objects](https://www.php.net/manual/en/class.iterator.php) instead of arrays of proxy settings to dynamically change the proxies used without reloading the settings.
2018-04-18 16:12:15 +02:00
2019-06-01 14:03:34 +02:00
```php
use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream;
use danog\MadelineProto\Stream\Proxy\HttpProxy;
use danog\MadelineProto\Stream\Proxy\SocksProxy;
use my\Custom\Namespace\MyProxy;
$settings['connection_settings']['all']['proxy'] = [
ObfuscatedStream::getName(),
ObfuscatedStream::getName(),
HttpProxy::getName(),
SocksProxy::getName(),
MyProxy::getName()
];
$settings['connection_settings']['all']['proxy_extra'] = [
[
'address' => 'magtg.com',
'port' => 443,
'secret' => 'dd.....'
],
[
'address' => '1.2.3.4',
'port' => 443,
'secret' => 'dd.....'
],
[
'address' => '0.0.0.0',
'port' => 80,
'username' => 'user',
'password' => 'pass'
],
[
'address' => '0.0.0.0',
'port' => 242,
'username' => 'user',
'password' => 'pass'
],
[
'address' => '0.0.0.0',
'port' => 242,
'custom' => 'args',
],
];
2018-04-18 16:12:15 +02:00
```
2019-06-01 14:03:34 +02:00
## MTProxy
2018-04-18 16:12:15 +02:00
```php
2019-06-01 14:03:34 +02:00
use danog\MadelineProto\Stream\MTProtoTransport\ObfuscatedStream;
$settings['connection_settings']['all']['proxy'] = ObfuscatedStream::getName();
$settings['connection_settings']['all']['proxy_extra'] = [
'address' => '0.0.0.0',
'port' => 443,
'secret' => 'dd.....'
];
2018-04-18 16:12:15 +02:00
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
2019-06-01 14:03:34 +02:00
Both normal and `dd` secrets are supported, the transport is changed automatically by MadelineProto.
2018-04-18 16:12:15 +02:00
2019-06-01 14:03:34 +02:00
## Socks5 proxy
No password:
2018-04-18 16:12:15 +02:00
```php
2019-06-01 14:03:34 +02:00
use danog\MadelineProto\Stream\Proxy\SocksProxy;
$settings['connection_settings']['all']['proxy'] = SocksProxy::getName();
$settings['connection_settings']['all']['proxy_extra'] = [
'address' => '0.0.0.0',
'port' => 2343,
];
2018-04-18 16:12:15 +02:00
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
2019-06-01 14:03:34 +02:00
With password:
2018-04-01 13:19:25 +02:00
```php
2019-06-01 14:03:34 +02:00
use danog\MadelineProto\Stream\Proxy\SocksProxy;
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$settings['connection_settings']['all']['proxy'] = SocksProxy::getName();
$settings['connection_settings']['all']['proxy_extra'] = [
'address' => '0.0.0.0',
'port' => 2343,
'username' => 'username',
'password' => 'password',
];
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
## HTTP proxy
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
No password:
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
```php
use danog\MadelineProto\Stream\Proxy\HttpProxy;
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$settings['connection_settings']['all']['proxy'] = HttpProxy::getName();
$settings['connection_settings']['all']['proxy_extra'] = [
'address' => '0.0.0.0',
'port' => 2343,
];
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
With password:
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
```php
use danog\MadelineProto\Stream\Proxy\HttpProxy;
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$settings['connection_settings']['all']['proxy'] = HttpProxy::getName();
$settings['connection_settings']['all']['proxy_extra'] = [
'address' => '0.0.0.0',
'port' => 2343,
'username' => 'username',
'password' => 'password',
];
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
$MadelineProto = new \danog\MadelineProto\API('session.madeline', $settings);
```
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
## Build your proxy
2018-04-01 13:19:25 +02:00
2019-06-01 14:03:34 +02:00
In order to build your proxy, you have to use the new MadelineProto stream API to create your own proxy class.
Feel free to study [existing proxies](https://github.com/danog/MadelineProto/tree/master/src/danog/MadelineProto/Stream/Proxy) and the [stream API](https://github.com/danog/MadelineProto/tree/master/src/danog/MadelineProto/Stream): it's well structured and well documented, so feel free to read the code.
Don't forget to add support for [TLS connections](https://github.com/danog/MadelineProto/blob/master/src/danog/MadelineProto/Stream/Proxy/SocksProxy.php#L141) as well!
2018-04-01 13:19:25 +02:00
2019-03-08 14:35:09 +01:00
<a href="https://docs.madelineproto.xyz/docs/USING_METHODS.html">Next section</a>