Skip to content

Commit

Permalink
Merge pull request #12 from nutgram/fix-safemode
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasss93 authored Sep 9, 2023
2 parents 2f8eee4 + 4d321cb commit 984513d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"require": {
"php": "^8.2",
"nunomaduro/termwind": "^1.15",
"nutgram/nutgram": "^4.2.0"
"nutgram/nutgram": "^4.6.0"
},
"require-dev": {
"illuminate/testing": "^9.0|^10.0",
Expand Down
1 change: 1 addition & 0 deletions src/Console/HookSetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function handle(Nutgram $bot): int
url: $url,
ip_address: $ip_address,
max_connections: $max_connections,
secret_token: config('nutgram.safe_mode', false) ? md5(config('app.key')) : null
);

$this->info("Bot webhook set with url: $url");
Expand Down
11 changes: 8 additions & 3 deletions src/NutgramServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public function register()
logger: $app->get(LoggerInterface::class)->channel(config('nutgram.log_channel', 'null')),
localPathTransformer: config('nutgram.config.local_path_transformer'),
pollingTimeout: config('nutgram.config.polling.timeout', Configuration::DEFAULT_POLLING_TIMEOUT),
pollingAllowedUpdates: config('nutgram.config.polling.allowed_updates', Configuration::DEFAULT_ALLOWED_UPDATES),
pollingAllowedUpdates: config('nutgram.config.polling.allowed_updates',
Configuration::DEFAULT_ALLOWED_UPDATES),
pollingLimit: config('nutgram.config.polling.limit', Configuration::DEFAULT_POLLING_LIMIT),
enableHttp2: config('nutgram.config.enable_http2', Configuration::DEFAULT_ENABLE_HTTP2),
);
Expand All @@ -56,9 +57,13 @@ public function register()
$bot->setRunningMode(Polling::class);
} else {
$webhook = LaravelWebhook::class;

if (config('nutgram.safe_mode', false)) {
// take into account the trust proxy Laravel configuration
$webhook = new LaravelWebhook(fn () => $app->make('request')?->ip());
$webhook = new LaravelWebhook(
getToken: fn () => request()?->header('X-Telegram-Bot-Api-Secret-Token'),
secretToken: md5(config('app.key'))
);
$webhook->setSafeMode(true);
}

$bot->setRunningMode($webhook);
Expand Down
22 changes: 21 additions & 1 deletion tests/Commands/HookSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,27 @@
test('nutgram:hook:set sets the bot webhook', function () {
$this->mock(Nutgram::class, function (MockInterface $mock) {
$mock->shouldReceive('setWebhook')
->with('https://foo.bar/hook', null, null, 50)
->with('https://foo.bar/hook', null, null, 50, null, null, null)
->andReturn(0);
});

$this->artisan(HookSetCommand::class, ['url' => 'https://foo.bar/hook'])
->expectsOutputToContain('Bot webhook set with url: https://foo.bar/hook')
->assertSuccessful();
});

test('nutgram:hook:set sets the bot webhook + safe mode', function () {
$appKey = 'test-key';
$hashedAppKey = md5($appKey);

config([
'app.key' => $appKey,
'nutgram.safe_mode' => true,
]);

$this->mock(Nutgram::class, function (MockInterface $mock) use ($hashedAppKey) {
$mock->shouldReceive('setWebhook')
->with('https://foo.bar/hook', null, null, 50, null, null, $hashedAppKey)
->andReturn(0);
});

Expand Down

0 comments on commit 984513d

Please sign in to comment.