Skip to content

Commit 7f7d9ec

Browse files
authored
Merge pull request #12144 from nanaya/asn-backup-url
Add alternative url for iptoasn database source
2 parents 508b2ba + f0518b9 commit 7f7d9ec

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

app/Libraries/Ip2AsnUpdater.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,32 @@
1212

1313
class Ip2AsnUpdater
1414
{
15+
public static function fetch(Ip $version): string
16+
{
17+
static $baseUrls = [
18+
'https://iptoasn.com/data',
19+
'https://assets.ppy.sh',
20+
];
21+
22+
foreach ($baseUrls as $baseUrl) {
23+
try {
24+
$gz = file_get_contents("{$baseUrl}/ip2asn-{$version->value}.tsv.gz");
25+
26+
if ($gz !== false) {
27+
break;
28+
}
29+
} catch (\Throwable) {
30+
// retry
31+
}
32+
}
33+
34+
if (!isset($gz) || !is_string($gz)) {
35+
throw new \Exception('failed downloading source database');
36+
}
37+
38+
return gzdecode($gz);
39+
}
40+
1541
public static function getDbPath(Ip $version): string
1642
{
1743
return database_path("ip2asn/{$version->value}.tsv");
@@ -60,7 +86,7 @@ private function update(Ip $version, callable $logger): void
6086

6187
if ($newDb) {
6288
$logger('Db file is outdated. Downloading');
63-
$tsv = gzdecode(file_get_contents("https://iptoasn.com/data/ip2asn-{$version->value}.tsv.gz"));
89+
$tsv = static::fetch($version);
6490
} else {
6591
$tsv = file_get_contents($dbPath);
6692
}

0 commit comments

Comments
 (0)