fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 webtrees development team |
||
| 6 | * This program is free software: you can redistribute it and/or modify |
||
| 7 | * it under the terms of the GNU General Public License as published by |
||
| 8 | * the Free Software Foundation, either version 3 of the License, or |
||
| 9 | * (at your option) any later version. |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * You should have received a copy of the GNU General Public License |
||
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
| 16 | */ |
||
| 17 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Services; |
||
| 21 | |||
| 22 | use Throwable; |
||
| 23 | |||
| 24 | use function fclose; |
||
| 25 | use function fsockopen; |
||
| 26 | use function fwrite; |
||
| 27 | use function is_resource; |
||
| 28 | use function preg_match_all; |
||
| 29 | use function sprintf; |
||
| 30 | use function stream_get_contents; |
||
| 31 | use function stream_set_timeout; |
||
| 32 | |||
| 33 | class NetworkService |
||
| 34 | { |
||
| 35 | private const array WHOIS_HOSTS = ['whois.radb.net', 'whois.ripe.net']; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 36 | private const string WHOIS_QUERY_FORMAT = "-i origin %s\r\n"; |
||
| 37 | private const int WHOIS_TIMEOUT_SECONDS = 5; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @return list<string> |
||
| 41 | */ |
||
| 42 | public function findIpRangesForAsn(string $asn): array |
||
| 43 | { |
||
| 44 | $query = sprintf(self::WHOIS_QUERY_FORMAT, $asn); |
||
| 45 | |||
| 46 | foreach (self::WHOIS_HOSTS as $host) { |
||
| 47 | try { |
||
| 48 | $stream = fsockopen(hostname: $host, port: 43, timeout: self::WHOIS_TIMEOUT_SECONDS); |
||
| 49 | |||
| 50 | stream_set_timeout(stream: $stream, seconds: self::WHOIS_TIMEOUT_SECONDS); |
||
| 51 | |||
| 52 | fwrite(stream: $stream, data: $query); |
||
| 53 | |||
| 54 | $text = stream_get_contents(stream: $stream); |
||
| 55 | } catch (Throwable) { |
||
| 56 | continue; |
||
| 57 | } finally { |
||
| 58 | if (isset($stream) && is_resource(value: $stream)) { |
||
| 59 | fclose(stream: $stream); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | preg_match_all(pattern: '/\nroute6?:[ \t]*([0-9a-f.:]+\/[0-9]+)/i', subject: $text, matches: $matches); |
||
| 64 | |||
| 65 | if ($matches[1] !== []) { |
||
| 66 | return $matches[1]; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | return []; |
||
| 71 | } |
||
| 72 | } |
||
| 73 |