. * * @link https://www.librenms.org * * @copyright 2022 Tony Murray * @author Tony Murray */ namespace LibreNMS\Validations; use Illuminate\Support\Str; use LibreNMS\Config; use LibreNMS\Validator; class WebServer extends BaseValidation { /** @var string */ private $http_regex = '#(http://([^:/]+|\[[a-fA-F\d:]+:[a-fA-F\d:]+]))(?::80)?/#'; /** @var string */ private $https_regex = '#(https://([^:/]+|\[[a-fA-F\d:]+:[a-fA-F\d:]+]))(?::443)?/#'; /** @var string */ private $host_regex = '#://([^/:\[]+|\[[a-fA-F\d:]+:[a-fA-F\d:]+])#'; /** * @inheritDoc */ public function validate(Validator $validator): void { if (! app()->runningInConsole()) { $url = $this->removeStandardPorts(request()->url()); $expected = $this->removeStandardPorts(Str::finish(Config::get('base_url'), '/') . 'validate/results'); if ($url !== $expected) { preg_match($this->host_regex, $url, $actual_host_match); preg_match($this->host_regex, $expected, $expected_host_match); $actual_host = $actual_host_match[1] ?? ''; $expected_host = $expected_host_match[1] ?? "parse failure ($expected)"; if ($actual_host != $expected_host) { $nginx = Str::startsWith(request()->server->get('SERVER_SOFTWARE'), 'nginx'); $server_name = $nginx ? 'server_name' : 'ServerName'; $fix = $nginx ? "server_name $actual_host;" : "ServerName $actual_host"; $validator->fail("$server_name is set incorrectly for your webserver, update your webserver config. $actual_host $expected_host", $fix); } else { $correct_base = str_replace('validate/results', '', $url); $validator->fail('base_url is not set correctly', "lnms config:set base_url $correct_base"); } } if (request()->secure() && ! \config('session.secure')) { $validator->fail('Secure session cookies are not enabled', 'Set SESSION_SECURE_COOKIE=true and run lnms config:cache'); } } } public function isDefault(): bool { return ! app()->runningInConsole(); } private function removeStandardPorts(string $url): string { return preg_replace($this->http_regex, '$1/', preg_replace($this->https_regex, '$1/', $url)); } }