Skip to content
This repository has been archived by the owner on Mar 16, 2024. It is now read-only.

Commit

Permalink
Fix domain validation bug + add one feature test
Browse files Browse the repository at this point in the history
  • Loading branch information
paramtamtam committed Aug 6, 2017
1 parent 16b6004 commit e816c5a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

Более подробно о нем можно прочитать по [этой ссылке (хабр)][habr].

Крайне рекомендую использовать `php` версии 7 и выше по соображениям производительности.

### Установка

Для развертывания приложения достаточно выполнить в терминале:
Expand Down
40 changes: 26 additions & 14 deletions app/Services/HostsParser/HostsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,28 @@ public function addExcludedHosts($hosts_names)
*/
public function isValidHostname($hostname)
{
//static $regexp = '((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*'
// . '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))';
if (is_string($hostname) && ! empty($hostname)) {
if (mb_strpos($hostname, ' ') === false) { // It's faster
//if ((bool) preg_match('/^' . $regexp . '$/', $hostname)) {
return true;
static $regexp = '((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*'
. '([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9]))';

static $stack = [];

$hostname = (string) $hostname;

if (isset($stack[$hostname])) {
return $stack[$hostname];
} else {
if (is_string($hostname) && ! empty($hostname)) {
if ((bool) preg_match('/^' . $regexp . '$/', $hostname)) {
$stack[$hostname] = true;

return $stack[$hostname];
}
}

$stack[$hostname] = false;
}

return false;
return $stack[$hostname];
}

/**
Expand Down Expand Up @@ -408,14 +420,14 @@ public function render($limit = 0, $entry_comment = 'ADBlock', $format = 'router

$entry_comment = str_replace(' ', '', $entry_comment);
foreach ($hosts as $host) {
// $result .= sprintf(
// "add address=%s name=%s comment=%s\n",
// $this->redirect_to,
// $host,
// $entry_comment
// );
$result .= sprintf(
"add address=%s name=%s comment=%s\n",
$this->redirect_to,
$host,
$entry_comment
);
// It's faster:
$result .= 'add address=' . $this->redirect_to . ' name=' . $host . ' comment=' . $entry_comment . "\n";
//$result .= 'add address=' . $this->redirect_to . ' name=' . $host . ' comment=' . $entry_comment . "\n";
}
}

Expand Down
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@
]
},
"scripts": {
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
]
"post-install-cmd": [
"php -r 'list($s, $t) = [\"./.env.example\", \"./.env\"]; file_exists($t) || copy($s, $t);'"
],
"post-update-cmd": [
"php -r 'list($s, $t) = [\"./.env.example\", \"./.env\"]; file_exists($t) || copy($s, $t);'"
],
"test": "./vendor/bin/phpunit --no-coverage"
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
2 changes: 1 addition & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
return [

// Версия
'version' => '2.0.1',
'version' => '2.0.2',

/*
|--------------------------------------------------------------------------
Expand Down
26 changes: 26 additions & 0 deletions nginx.conf.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
server {
listen 80;
server_name your.domain.com;
root /path_to/mikrotik-hosts-parser/public;
access_log off;
error_log off;

index index.php index.html index.htm;

location / {
try_files $uri $uri/ /index.php$is_args$args;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
access_log off;
log_not_found off;
expires 1M;
}

location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php;
}

location ~ /\.ht {return 444;}
}
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./app</directory>
<directory suffix="Test.php">./app</directory>
</whitelist>
</filter>
<php>
Expand Down
19 changes: 17 additions & 2 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@
class ExampleTest extends TestCase
{
/**
* A basic test example.
* Test of index page.
*
* @return void
*/
public function testExample()
public function testIndexPage()
{
$response = $this->get('/');

$response->assertResponseStatus(200);
}

/**
* Test of script source page.
*
* @return void
*/
public function testScriptSourcePage()
{
$response = $this->get('/script/source?format=routeros&version=2.0.2&redirect_to=127.0.0.2&limit=666&sources_urls=https%3A%2F%2Fcdn.rawgit.com%2Ftarampampam%2Fstatic%2Fmaster%2Fhosts%2Fblock_shit.txt&excluded_hosts=localhost');

$response->assertResponseOk();
$this->assertContains('Script generated', $response->response->getContent());

$response->assertResponseStatus(200);
}
}

0 comments on commit e816c5a

Please sign in to comment.