Skip to content

Commit 2d58679

Browse files
authored
Merge pull request #16 from noplanman/custom_ips
Custom valid IPs
2 parents 2587d47 + d99aaa1 commit 2d58679

File tree

6 files changed

+90
-18
lines changed

6 files changed

+90
-18
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
33

44
## [Unreleased]
5+
### Added
6+
- Ability to define custom valid IPs to access webhook.
57

68
## [0.43.0] - 2017-04-17
79
### Added

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ Here is a list of available extra parameters:
157157
| --------- | ----------- |
158158
| validate_request | Only allow webhook access from valid Telegram API IPs. |
159159
| *bool* | *default is `true`* |
160+
| valid_ips | When using `validate_request`, also allow these IPs (single, CIDR, wildcard, range). |
161+
| *array* | *e.g.* `['1.2.3.4', '192.168.1.0/24', '10/8', '5.6.*', '1.1.1.1-2.2.2.2']` |
160162
| webhook | URL to the manager PHP file used for setting up the Webhook. |
161163
| *string* | *e.g.* `'https://example.com/manager.php'` |
162164
| certificate | Path to a self-signed certificate (if necessary). |

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@
1717
"role": "Developer"
1818
}
1919
],
20+
"repositories": [
21+
{
22+
"type": "vcs",
23+
"url": "https://github.com/noplanman/Utils-IP-Tools"
24+
}
25+
],
2026
"require": {
2127
"php": "^7.0",
22-
"longman/telegram-bot": "^0.43"
28+
"longman/telegram-bot": "^0.43",
29+
"allty/utils-ip": "dev-master"
2330
},
2431
"require-dev": {
2532
"jakub-onderka/php-parallel-lint": "^0.9.2",

composer.lock

Lines changed: 60 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BotManager.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace NPM\TelegramBotManager;
1212

13+
use Allty\Utils\IpTools;
1314
use Longman\TelegramBot\Entities;
1415
use Longman\TelegramBot\Request;
1516
use Longman\TelegramBot\Telegram;
@@ -20,14 +21,9 @@
2021
class BotManager
2122
{
2223
/**
23-
* @var string Telegram post servers lower IP limit
24+
* @var string Telegram post servers IP range
2425
*/
25-
const TELEGRAM_IP_LOWER = '149.154.167.197';
26-
27-
/**
28-
* @var string Telegram post servers upper IP limit
29-
*/
30-
const TELEGRAM_IP_UPPER = '149.154.167.233';
26+
const TELEGRAM_IP_RANGE = '149.154.167.197-149.154.167.233';
3127

3228
/**
3329
* @var string The output for testing, instead of echoing
@@ -470,17 +466,22 @@ public function isValidRequest(): bool
470466

471467
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
472468
foreach (['HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR'] as $key) {
473-
$addr = $_SERVER[$key] ?? null;
474-
if (filter_var($addr, FILTER_VALIDATE_IP)) {
475-
$ip = $addr;
469+
if (filter_var($_SERVER[$key] ?? null, FILTER_VALIDATE_IP)) {
470+
$ip = $_SERVER[$key];
476471
break;
477472
}
478473
}
479474

480-
$lower_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_LOWER));
481-
$upper_dec = (float) sprintf('%u', ip2long(self::TELEGRAM_IP_UPPER));
482-
$ip_dec = (float) sprintf('%u', ip2long($ip));
475+
$valid_ips = array_merge(
476+
[self::TELEGRAM_IP_RANGE],
477+
(array) $this->params->getBotParam('valid_ips', [])
478+
);
479+
foreach ($valid_ips as $valid_ip) {
480+
if (IpTools::ipInRange($ip, $valid_ip)) {
481+
return true;
482+
}
483+
}
483484

484-
return $ip_dec >= $lower_dec && $ip_dec <= $upper_dec;
485+
return false;
485486
}
486487
}

src/Params.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class Params
3838
*/
3939
private static $valid_extra_bot_params = [
4040
'validate_request',
41+
'valid_ips',
4142
'webhook',
4243
'certificate',
4344
'max_connections',
@@ -72,7 +73,8 @@ class Params
7273
* api_key (string) Telegram Bot API key
7374
* bot_username (string) Telegram Bot username
7475
* secret (string) Secret string to validate calls
75-
* validate_request (bool) Only allow webhook access from valid Telegram API IPs
76+
* validate_request (bool) Only allow webhook access from valid Telegram API IPs and defined valid_ips
77+
* valid_ips (array) Any IPs, besides Telegram API IPs, that are allowed to access the webhook
7678
* webhook (string) URI of the webhook
7779
* certificate (string) Path to the self-signed certificate
7880
* max_connections (int) Maximum allowed simultaneous HTTPS connections to the webhook

0 commit comments

Comments
 (0)