Browse Source

update_geoip tool now grabs ASN data as well

Currently unused, but not for long
spaghetti 8 years ago
parent
commit
b12e756084
1 changed files with 85 additions and 27 deletions
  1. 85
    27
      sections/tools/development/update_geoip.php

+ 85
- 27
sections/tools/development/update_geoip.php View File

2
 ini_set('memory_limit', '1G');
2
 ini_set('memory_limit', '1G');
3
 set_time_limit(0);
3
 set_time_limit(0);
4
 
4
 
5
+$LIMIT = 1000;
6
+
5
 if (!check_perms('site_debug')) {
7
 if (!check_perms('site_debug')) {
6
   error(403);
8
   error(403);
7
 }
9
 }
8
 
10
 
9
 View::show_header();
11
 View::show_header();
10
 chdir('/tmp');
12
 chdir('/tmp');
11
-//requires wget, unzip, gunzip commands to be installed
13
+// requires wget, unzip, gunzip commands to be installed
14
+
15
+// Country section
12
 shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip');
16
 shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPCountryCSV.zip');
13
 shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz');
17
 shell_exec('wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.csv.gz');
14
 shell_exec('unzip GeoIPCountryCSV.zip');
18
 shell_exec('unzip GeoIPCountryCSV.zip');
15
 shell_exec('gunzip GeoIPv6.csv.gz');
19
 shell_exec('gunzip GeoIPv6.csv.gz');
16
 shell_exec('cut -d , -f 3-5 GeoIPCountryWhois.csv > GeoIPCountry.csv');
20
 shell_exec('cut -d , -f 3-5 GeoIPCountryWhois.csv > GeoIPCountry.csv');
17
 shell_exec('cut -d , -f 3-5 GeoIPv6.csv | tr -d " " >> GeoIPCountry.csv');
21
 shell_exec('cut -d , -f 3-5 GeoIPv6.csv | tr -d " " >> GeoIPCountry.csv');
18
-shell_exec('rm GeoIPCountryCSV.zip GeoIPv6.csv.gz GeoIPCountryWhois.csv GeoIPv6.csv');
19
 
22
 
20
-if (($Blocks = file("GeoIPCountry.csv", FILE_IGNORE_NEW_LINES)) === false) {
21
-  echo 'Error';
23
+if (($fd = fopen('GeoIPCountry.csv', 'r')) !== false) {
24
+  $DB->query("TRUNCATE TABLE geoip_country");
25
+  $Values = array();
26
+  $Count = 0;
27
+  while (($Data = fgetcsv($fd)) !== false) {
28
+    list($StartIP, $EndIP, $CountryID) = $Data;
29
+    $Values[] = "($StartIP, $EndIP, '$CountryID')";
30
+    $Count++;
31
+    if ($Count % $LIMIT == 0) {
32
+      $DB->query("
33
+        INSERT INTO geoip_country (StartIP, EndIP, Code)
34
+        VALUES ".implode(', ', $Values));
35
+      $Values = array();
36
+    }
37
+  }
38
+  if (count($Values) > 0) {
39
+    $DB->query("
40
+      INSERT INTO geoip_country (StartIP, EndIP, Code)
41
+      VALUES ".implode(', ', $Values));
42
+  }
43
+  echo 'GeoIP_Country: There are '.($Count+count($Values)).' entries <br />';
44
+} else {
45
+  echo 'Country Error';
22
 }
46
 }
47
+shell_exec('rm GeoIPCountryCSV.zip GeoIPv6.csv.gz GeoIPCountryWhois.csv GeoIPv6.csv GeoIPCountry.csv');
23
 
48
 
24
-echo 'There are '.count($Blocks).' blocks';
25
-echo '<br />';
26
-
27
-//Because the old code reading a 2mil line database had splitting, we're just gonna keep using it with this much more reasonable 140k line db
28
-$SplitOn = 1000;
29
-$DB->query("TRUNCATE TABLE geoip_country");
30
-
31
-$Values = array();
32
-foreach ($Blocks as $Index => $Block) {
33
-  list($StartIP, $EndIP, $CountryID) = explode(",", $Block);
34
-  $StartIP = trim($StartIP, '"');
35
-  $EndIP = trim($EndIP, '"');
36
-  $CountryID = trim($CountryID, '"');
37
-  $Values[] = "('$StartIP', '$EndIP', '".$CountryID."')";
38
-  if ($Index % $SplitOn == 0) {
39
-    $DB->query('
40
-      INSERT INTO geoip_country (StartIP, EndIP, Code)
41
-      VALUES '.implode(', ', $Values));
42
-    $Values = array();
49
+// ASN (v4) section
50
+shell_exec('wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2.zip');
51
+shell_exec('unzip GeoIPASNum2.zip');
52
+
53
+if (($fd = fopen('GeoIPASNum2.csv', 'r')) !== false) {
54
+  $DB->query("TRUNCATE TABLE geoip_asn");
55
+  $Values = array();
56
+  $Count = 0;
57
+  while (($Data = fgetcsv($fd)) !== false) {
58
+    list($StartIP, $EndIP, $ASN) = $Data;
59
+    $ASN = substr($ASN, 2, strpos($ASN, ' ') ? strpos($ASN, ' ')-2 : strlen($ASN)-2);
60
+    $Values[] = "(INET6_ATON(INET_NTOA($StartIP)), INET6_ATON(INET_NTOA($EndIP)), $ASN)";
61
+    $Count++;
62
+    if ($Count % $LIMIT == 0) {
63
+      $DB->query("
64
+        INSERT INTO geoip_asn (StartIP, EndIP, ASN)
65
+        VALUES ".implode(', ', $Values));
66
+      $Values = array();
67
+    }
68
+  }
69
+  if (count($Values) > 0) {
70
+    $DB->query("
71
+      INSERT INTO geoip_asn (StartIP, EndIP, ASN)
72
+      VALUES ".implode(', ', $Values));
43
   }
73
   }
74
+  echo 'GeoIP_ASN (v4): There are '.($Count+count($Values)).' entries <br />';
75
+} else {
76
+  echo 'ASNv4 Error';
44
 }
77
 }
78
+shell_exec('rm GeoIPASNum2.zip GeoIPASNum2.csv');
79
+
80
+// ASN (v6) section
81
+shell_exec('wget http://download.maxmind.com/download/geoip/database/asnum/GeoIPASNum2v6.zip');
82
+shell_exec('unzip GeoIPASNum2v6.zip');
45
 
83
 
46
-if (count($Values) > 0) {
47
-  $DB->query("
48
-    INSERT INTO geoip_country (StartIP, EndIP, Code)
49
-    VALUES ".implode(', ', $Values));
84
+if (($fd = fopen('GeoIPASNum2v6.csv', 'r')) !== false) {
85
+  $Values = array();
86
+  $Count = 0;
87
+  while (($Data = fgetcsv($fd)) !== false) {
88
+    list($ASN, $StartIP, $EndIP) = $Data;
89
+    $ASN = substr($ASN, 2, strpos($ASN, ' ') ? strpos($ASN, ' ')-2 : strlen($ASN)-2);
90
+    $Values[] = "(INET6_ATON('$StartIP'), INET6_ATON('$EndIP'), $ASN)";
91
+    $Count++;
92
+    if ($Count % $LIMIT == 0) {
93
+      $DB->query("
94
+        INSERT INTO geoip_asn (StartIP, EndIP, ASN)
95
+        VALUES ".implode(', ', $Values));
96
+      $Values = array();
97
+    }
98
+  }
99
+  if (count($Values) > 0) {
100
+    $DB->query("
101
+      INSERT INTO geoip_asn (StartIP, EndIP, ASN)
102
+      VALUES ".implode(', ', $Values));
103
+  }
104
+  echo 'GeoIP_ASN (v6): There are '.($Count+count($Values)).' entries <br />';
105
+} else {
106
+  echo 'ASNv6 Error';
50
 }
107
 }
108
+shell_exec('rm GeoIPASNum2v6.zip GeoIPASNum2v6.tmp GeoIPASNum2v6.csv');
51
 
109
 
52
 View::show_footer();
110
 View::show_footer();

Loading…
Cancel
Save