Skip to content

Commit

Permalink
New ngrok domains and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jn-jairo committed Apr 12, 2023
1 parent 4c2466f commit 9ab8394
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 68 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG-3.x.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog

## [Unreleased](https://github.com/jn-jairo/laravel-ngrok/compare/v3.0.0...3.x)
## [Unreleased](https://github.com/jn-jairo/laravel-ngrok/compare/v3.0.1...3.x)

## [v3.0.1 (2023-04-12)](https://github.com/jn-jairo/laravel-ngrok/compare/v3.0.0...v3.0.1)

### Added
- New ngrok domains

## [v3.0.0 (2023-02-25)](https://github.com/jn-jairo/laravel-ngrok/compare/v2.0.4...v3.0.0)

Expand Down
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ test:
$(ARGS)

.PHONY: test-coverage
test-coverage: clear-coverage
test-coverage: clean-coverage
@$(call show_title,'TEST COVERAGE') \
XDEBUG_MODE=coverage \
php -d zend_extension=xdebug.so \
Expand Down Expand Up @@ -119,7 +119,7 @@ parallel-test:
--passthru="--do-not-cache-result"

.PHONY: parallel-test-coverage
parallel-test-coverage: clear-coverage
parallel-test-coverage: clean-coverage
@$(call show_title,'PARALLEL TEST COVERAGE') \
XDEBUG_MODE=coverage \
php -d zend_extension=xdebug.so \
Expand All @@ -132,7 +132,7 @@ parallel-test-coverage: clear-coverage
--coverage

.PHONY: infection-test
infection-test: clear-infection
infection-test: clean-infection
@$(call show_title,'INFECTION TEST') \
infection \
--threads=$(shell nproc) \
Expand Down Expand Up @@ -164,15 +164,15 @@ show-coverage:
show-infection:
@xdg-open build/infection/infection.html > /dev/null 2>&1

.PHONY: clear
clear: clear-coverage clear-infection
.PHONY: clean
clean: clean-coverage clean-infection

.PHONY: clear-coverage
clear-coverage:
.PHONY: clean-coverage
clean-coverage:
@$(call show_title,'CLEAR COVERAGE') \
(rm -r build/coverage > /dev/null 2>&1 || true)

.PHONY: clear-infection
clear-infection:
.PHONY: clean-infection
clean-infection:
@$(call show_title,'CLEAR INFECTION') \
(rm -r build/infection > /dev/null 2>&1 || true)
9 changes: 9 additions & 0 deletions php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@

return (new PhpCsFixer\Config())->setRules([
'no_unused_imports' => true,
'trailing_comma_in_multiline' => [
'after_heredoc' => true,
'elements' => [
'arguments',
'arrays',
'match',
'parameters',
],
],
])->setFinder($finder);
2 changes: 1 addition & 1 deletion src/NgrokProcessBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function buildProcess(
string $hostHeader = '',
string $port = '80',
string $host = '',
array $extra = []
array $extra = [],
): Process {
$command = ['ngrok', 'http', '--log', 'stdout'];

Expand Down
21 changes: 20 additions & 1 deletion src/NgrokServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@

class NgrokServiceProvider extends ServiceProvider
{
/**
* Ngrok valid domains.
*
* @var array<int, string>
*/
public const NGROK_DOMAINS = [
'ngrok.io',
'ngrok-free.app',
'ngrok-free.dev',
'ngrok.app',
'ngrok.dev',
];

/**
* Bootstrap the application services.
*
Expand Down Expand Up @@ -118,6 +131,12 @@ private function extractOriginalHost(Request $request): string
*/
private function isNgrokHost(string $host): bool
{
return (bool) preg_match('/^[\.\-a-z0-9]+\.ngrok\.io$/i', $host);
$domains = collect(self::NGROK_DOMAINS)
->map(fn($domain) => preg_quote($domain, '/'))
->join('|');

$regex = '/^[\.\-a-z0-9]+\.(?:' . $domains . ')$/i';

return (bool) preg_match($regex, $host);
}
}
4 changes: 2 additions & 2 deletions src/NgrokWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function getTunnels(): array
$response = json_decode(
$this->getHttpClient()->request(
'GET',
$this->getUrl() . '/api/tunnels'
$this->getUrl() . '/api/tunnels',
)->getBody(),
true
true,
);

if ($response !== false && isset($response['tunnels']) && ! empty($response['tunnels'])) {
Expand Down
12 changes: 6 additions & 6 deletions tests/NgrokCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
array $config,
array $params,
array $expectedArguments,
array $expectedOutputs
array $expectedOutputs,
) {
config($config);

Expand All @@ -92,11 +92,11 @@

$tunnels = [
[
'public_url' => 'http://0000-0000.ngrok.io',
'public_url' => 'http://0000-0000.ngrok-free.app',
'config' => ['addr' => $host . ':' . $port],
],
[
'public_url' => 'https://0000-0000.ngrok.io',
'public_url' => 'https://0000-0000.ngrok-free.app',
'config' => ['addr' => $host . ':' . $port],
],
];
Expand Down Expand Up @@ -154,7 +154,7 @@
array $config,
array $params,
array $expectedArguments,
array $expectedOutputs
array $expectedOutputs,
) {
config($config);

Expand All @@ -163,11 +163,11 @@

$tunnels = [
[
'public_url' => 'http://0000-0000.ngrok.io',
'public_url' => 'http://0000-0000.ngrok-free.app',
'config' => ['addr' => $host . ':' . $port],
],
[
'public_url' => 'https://0000-0000.ngrok.io',
'public_url' => 'https://0000-0000.ngrok-free.app',
'config' => ['addr' => $host . ':' . $port],
],
];
Expand Down
2 changes: 1 addition & 1 deletion tests/NgrokProcessBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

it('can build process', function (
array $args,
string $command
string $command,
) {
$processBuilder = new NgrokProcessBuilder(__DIR__);
$process = $processBuilder->buildProcess(...$args);
Expand Down
101 changes: 59 additions & 42 deletions tests/NgrokServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,51 @@

uses(ProphecyTrait::class);

$datasetValidNgrokUrl = [
'http_ngrok_2' => [
'http',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.ngrok.io',
],
],
'http_ngrok_3' => [
'http',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.ngrok.io',
],
],
'https_ngrok_2' => [
'https',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.ngrok.io',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
'https_ngrok_3' => [
'https',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.ngrok.io',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
];
$datasetValidNgrokUrl = (function () {
$dataset = [];

$domains = [
'legacy' => 'ngrok.io',
'free_app' => 'ngrok-free.app',
'free_dev' => 'ngrok-free.dev',
'paid_app' => 'ngrok.app',
'paid_dev' => 'ngrok.dev',
];

foreach ($domains as $label => $domain) {
$dataset[$label . '_http_ngrok_2'] = [
'http',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.ngrok-free.app',
],
];

$dataset[$label . '_http_ngrok_3'] = [
'http',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.ngrok-free.app',
],
];

$dataset[$label . '_https_ngrok_2'] = [
'https',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.ngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
];

$dataset[$label . '_https_ngrok_3'] = [
'https',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.ngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
];
}

return $dataset;
})();

$datasetInvalidNgrokUrl = [
'http_empty' => [
Expand All @@ -59,13 +76,13 @@
'http_ngrok_2_domain' => [
'http',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.notngrok.io',
'HTTP_X_ORIGINAL_HOST' => '0000-0000.notngrok-free.app',
],
],
'http_ngrok_2_subdomain' => [
'http',
[
'HTTP_X_ORIGINAL_HOST' => '0000_0000.ngrok.io',
'HTTP_X_ORIGINAL_HOST' => '0000_0000.ngrok-free.app',
],
],
'http_ngrok_3_domain_top_level' => [
Expand All @@ -77,13 +94,13 @@
'http_ngrok_3_domain' => [
'http',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.notngrok.io',
'HTTP_X_FORWARDED_HOST' => '0000-0000.notngrok-free.app',
],
],
'http_ngrok_3_subdomain' => [
'http',
[
'HTTP_X_FORWARDED_HOST' => '0000_0000.ngrok.io',
'HTTP_X_FORWARDED_HOST' => '0000_0000.ngrok-free.app',
],
],
'https_ngrok_2_domain_top_level' => [
Expand All @@ -96,14 +113,14 @@
'https_ngrok_2_domain' => [
'https',
[
'HTTP_X_ORIGINAL_HOST' => '0000-0000.notngrok.io',
'HTTP_X_ORIGINAL_HOST' => '0000-0000.notngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
'https_ngrok_2_subdomain' => [
'https',
[
'HTTP_X_ORIGINAL_HOST' => '0000_0000.ngrok.io',
'HTTP_X_ORIGINAL_HOST' => '0000_0000.ngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
Expand All @@ -117,14 +134,14 @@
'https_ngrok_3_domain' => [
'https',
[
'HTTP_X_FORWARDED_HOST' => '0000-0000.notngrok.io',
'HTTP_X_FORWARDED_HOST' => '0000-0000.notngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
'https_ngrok_3_subdomain' => [
'https',
[
'HTTP_X_FORWARDED_HOST' => '0000_0000.ngrok.io',
'HTTP_X_FORWARDED_HOST' => '0000_0000.ngrok-free.app',
'HTTP_X_FORWARDED_PROTO' => 'https',
],
],
Expand Down Expand Up @@ -160,7 +177,7 @@

it('does not setup invalid ngrok url', function (
string $scheme,
array $headers
array $headers,
) {
/**
* @var \Prophecy\Prophecy\ObjectProphecy<\Illuminate\Routing\UrlGenerator> $urlGenerator
Expand Down Expand Up @@ -195,19 +212,19 @@

it('setup valid ngrok url', function (
string $scheme,
array $headers
array $headers,
) {
/**
* @var \Prophecy\Prophecy\ObjectProphecy<\Illuminate\Routing\UrlGenerator> $urlGenerator
*/
$urlGenerator = prophesize(UrlGenerator::class);
$urlGenerator->forceScheme($scheme)->shouldBeCalled();
$urlGenerator->forceRootUrl($scheme . '://0000-0000.ngrok.io')->shouldBeCalled();
$urlGenerator->forceRootUrl($scheme . '://0000-0000.ngrok-free.app')->shouldBeCalled();
$urlGenerator->to(
'foo',
\Prophecy\Argument::any(),
\Prophecy\Argument::any()
)->willReturn($scheme . '://0000-0000.ngrok.io/foo')->shouldBeCalled();
\Prophecy\Argument::any(),
)->willReturn($scheme . '://0000-0000.ngrok-free.app/foo')->shouldBeCalled();

$request = Request::create(
$scheme . '://example.com/foo',
Expand All @@ -230,5 +247,5 @@
$serviceProvider->boot();

expect(Paginator::resolveCurrentPath())
->toBe($scheme . '://0000-0000.ngrok.io/foo');
->toBe($scheme . '://0000-0000.ngrok-free.app/foo');
})->with($datasetValidNgrokUrl);
Loading

0 comments on commit 9ab8394

Please sign in to comment.