mirror of
https://github.com/danog/TelegramApiServer.git
synced 2024-11-26 20:04:45 +01:00
Docker network whitelist
This commit is contained in:
parent
09cfaf4d70
commit
4754105e32
@ -1,4 +1,4 @@
|
|||||||
version: '3'
|
version: '3.5'
|
||||||
services:
|
services:
|
||||||
telegram-api-server:
|
telegram-api-server:
|
||||||
build: ./
|
build: ./
|
||||||
@ -29,3 +29,6 @@ services:
|
|||||||
- ./.mysql:/var/lib/mysql
|
- ./.mysql:/var/lib/mysql
|
||||||
environment:
|
environment:
|
||||||
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
|
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
|
||||||
|
networks:
|
||||||
|
default:
|
||||||
|
name: telegram-api-server
|
@ -13,14 +13,12 @@ use function Amp\call;
|
|||||||
class Authorization implements Middleware
|
class Authorization implements Middleware
|
||||||
{
|
{
|
||||||
private array $ipWhitelist;
|
private array $ipWhitelist;
|
||||||
|
private int $selfIp;
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->ipWhitelist = (array) Config::getInstance()->get('api.ip_whitelist', []);
|
$this->ipWhitelist = (array) Config::getInstance()->get('api.ip_whitelist', []);
|
||||||
//Add self ip for docker.
|
$this->selfIp = ip2long(getHostByName(php_uname('n')));
|
||||||
if (\count($this->ipWhitelist) > 0) {
|
|
||||||
$this->ipWhitelist[] = getHostByName(php_uname('n'));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleRequest(Request $request, RequestHandler $next): Promise {
|
public function handleRequest(Request $request, RequestHandler $next): Promise {
|
||||||
@ -39,6 +37,14 @@ class Authorization implements Middleware
|
|||||||
|
|
||||||
private function isIpAllowed(string $host): bool
|
private function isIpAllowed(string $host): bool
|
||||||
{
|
{
|
||||||
|
global $options;
|
||||||
|
if ($options['docker']) {
|
||||||
|
$isSameNetwork = abs(ip2long($host) - $this->selfIp) < 10;
|
||||||
|
if ($isSameNetwork) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ($this->ipWhitelist && !in_array($host, $this->ipWhitelist, true)) {
|
if ($this->ipWhitelist && !in_array($host, $this->ipWhitelist, true)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user