Skip to content

Commit

Permalink
implement apache mod_maxminddb and peeringdb.com API
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianhegge committed Nov 19, 2019
1 parent 3cb3341 commit 1f2728b
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 27 deletions.
37 changes: 37 additions & 0 deletions README-install-apache-mod-maxmind.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
```bash
apt install git apache2-dev build-essential libmaxminddb-dev
cd /usr/local/src/
git clone https://github.com/maxmind/mod_maxminddb.git
cd mod_maxminddb
./bootstrap
./configure
make
make install
```

```bash
/etc/apache2/mods-enabled/maxminddb.conf:

MaxMindDBEnable On
MaxMindDBFile MM_COUNTRY_DB /usr/local/share/maxminddb/GeoLite2-Country.mmdb
MaxMindDBFile MM_CITY_DB /usr/local/share/maxminddb/GeoLite2-City.mmdb
MaxMindDBFile MM_ASN_DB /usr/local/share/maxminddb/GeoLite2-ASN.mmdb


MaxMindDBEnv MM_COUNTRY_CODE_COUNTRY_DB MM_COUNTRY_DB/country/iso_code
MaxMindDBNetworkEnv MM_COUNTRY_DB MM_COUNTRY_NETWORK


MaxMindDBEnv MM_COUNTRY_CODE_CITY_DB MM_CITY_DB/country/iso_code
MaxMindDBEnv MM_COUNTRY_NAME MM_CITY_DB/country/names/de
MaxMindDBEnv MM_CITY_NAME MM_CITY_DB/city/names/de
MaxMindDBEnv MM_REGION_CODE MM_CITY_DB/subdivisions/0/iso_code
MaxMindDBEnv MM_LONGITUDE MM_CITY_DB/location/longitude
MaxMindDBEnv MM_LATITUDE MM_CITY_DB/location/latitude
MaxMindDBNetworkEnv MM_CITY_DB MM_CITY_DB_NETWORK


MaxMindDBEnv MM_ASN MM_ASN_DB/autonomous_system_number
MaxMindDBEnv MM_AS_ORG MM_ASN_DB/autonomous_system_organization
MaxMindDBNetworkEnv MM_ASN_DB MM_ASN_DB_NETWORK
```
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ A small tool to test several IP settings with the help of [ip-api.com](http://ip
## Requirements

* webserver with:
* PHP
* PHP Curl extension
* PHP Browscap extension [website](https://browscap.org) / [download lite version](https://browscap.org/stream?q=Lite_PHP_BrowsCapINI)
* PHP
* PHP Curl extension
* PHP Browscap extension [website](https://browscap.org) / [download lite version](https://browscap.org/stream?q=Lite_PHP_BrowsCapINI)
* Apache Module MaxMind DB [GitHub](https://github.com/maxmind/mod_maxminddb) / [README](README-install-apache-mod-maxmind.md)
* one webserver vhost with:
* production-domain (for example DOMAIN.de), reachable via IPv4 and IPv6 (with an A Record and an AAAA Record)
* ipv4 subdomain (ipv4.DOMAIN.de), reachable only via IPv4 (only an A Record)
Expand All @@ -22,7 +23,8 @@ A small tool to test several IP settings with the help of [ip-api.com](http://ip
* [jQuery 3.3.1 min](https://code.jquery.com/jquery-3.3.1.min.js) in `/jquery`
* [Bootstrap 4.0](https://getbootstrap.com/docs/4.0/getting-started/download/) in `/bootstrap`
* [flag-icon-css project](https://github.com/lipis/flag-icon-css/archive/master.zip) in `/flags` (the content of `/flags/4x3`)
* the service of [ip-api.com](http://ip-api.com)
* the API service of [ip-api.com](http://ip-api.com)
* the API service of [peeringdb.com](https://www.peeringdb.com/apidocs/)

## License
MIT
35 changes: 31 additions & 4 deletions geo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,38 @@

if(isset($_GET['ip'])){
$ip = $_GET['ip'];
} else {
$ip = $_SERVER['REMOTE_ADDR'];
$response = get_web_page('http://ip-api.com/json/'.$ip.'?fields=19187&lang=de');
print_r($response);
}
else {
$asn = getenv('MM_ASN');

$asn_response = get_web_page('https://peeringdb.com/api/net?asn='.$asn);
$asn_response_obj = json_decode($asn_response);
$asn_org_id = $asn_response_obj->data[0]->org_id;
$org_response = get_web_page('https://www.peeringdb.com/api/org/'.$asn_org_id);
$org_response_obj = json_decode($org_response);
//print_r($org_response);

$ret_obj['status'] = 'success';
$ret_obj['ip_network'] = getenv('MM_ASN_DB_NETWORK');
$ret_obj['country'] = getenv('MM_COUNTRY_NAME');
$ret_obj['country_code'] = getenv('MM_COUNTRY_CODE_CITY_DB');
$ret_obj['city'] = $org_response_obj->data[0]->city;
$ret_obj['zip'] = $org_response_obj->data[0]->zipcode;
$ret_obj['lat'] = (float)getenv('MM_LATITUDE');
$ret_obj['lon'] = (float)getenv('MM_LONGITUDE');
$ret_obj['isp'] = getenv('MM_AS_ORG');
$ret_obj['isp_website'] = $org_response_obj->data[0]->website;
$ret_obj['street'] = $org_response_obj->data[0]->address1;
$ret_obj['state'] = $org_response_obj->data[0]->state;
$ret_obj['as_number'] = $asn;
$ret_obj['as_name'] = $asn_response_obj->data[0]->irr_as_set;
$ret_obj['as_organisation'] = $org_response_obj->data[0]->name;

print_r(json_encode($ret_obj));
}

$response = get_web_page('http://ip-api.com/json/'.$ip.'?fields=19187&lang=de');
print_r($response);

function get_web_page($url) {
$options = array(
Expand All @@ -34,3 +60,4 @@ function get_web_page($url) {
return $content;
}
?>

14 changes: 11 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
<th scope="row">Hostname:</th>
<td id="content-ipv4-hostname"><img src="loader.gif" width="16" height="16" id="loader-ipv4-hostname"></td>
</tr>
<tr>
<th scope="row">IP-Netz:</th>
<td id="content-ipv4-ip-network"><img src="loader.gif" width="16" height="16" id="loader-ipv4-ip"></td>
</tr>
<tr>
<th scope="row">ISP:</th>
<td id="content-ipv4-isp"><img src="loader.gif" width="16" height="16" id="loader-ipv4-isp"></td>
Expand All @@ -121,7 +125,7 @@
</tr>
<tr>
<th scope="row">Adresse:</th>
<td id="content-ipv4-country"><img src="loader.gif" width="16" height="16" id="loader-ipv4-country"></td>
<td id="content-ipv4-address"><img src="loader.gif" width="16" height="16" id="loader-ipv4-address"></td>
</tr>
</tbody>
</table>
Expand All @@ -146,6 +150,10 @@
<th scope="row">Hostname:</th>
<td id="content-ipv6-hostname"><img src="loader.gif" width="16" height="16" id="loader-ipv6-hostname"></td>
</tr>
<tr>
<th scope="row">IP-Netz:</th>
<td id="content-ipv6-ip-network"><img src="loader.gif" width="16" height="16" id="loader-ipv6-ip"></td>
</tr>
<tr>
<th scope="row">ISP:</th>
<td id="content-ipv6-isp"><img src="loader.gif" width="16" height="16" id="loader-ipv6-isp"></td>
Expand All @@ -156,7 +164,7 @@
</tr>
<tr>
<th scope="row">Adresse:</th>
<td id="content-ipv6-country"><img src="loader.gif" width="16" height="16" id="loader-ipv6-country"></td>
<td id="content-ipv6-address"><img src="loader.gif" width="16" height="16" id="loader-ipv6-address"></td>
</tr>
</tbody>
</table>
Expand All @@ -168,7 +176,7 @@
<div class="col-sm-12 col-md-12 col-lg-12">
<div class="card mt-4">
<div class="card-header card-header-no-border text-center">
&copy; Sebastian Hegge &middot; <a href="https://heg.ge/impressum" target="_blank">Impressum & Datenschutz</a> &middot; <a href="https://github.com/sebastianhegge/iptest" target="_blank">Source on GitHub</a> &middot; v1.7
&copy; Sebastian Hegge &middot; <a href="https://heg.ge/impressum" target="_blank">Impressum & Datenschutz</a> &middot; <a href="https://github.com/sebastianhegge/iptest" target="_blank">Source on GitHub</a> &middot; v2.0
</div>
</div>
</div>
Expand Down
95 changes: 79 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,61 @@ function translate_link_type(link_type){
}
}

function format_isp(data){
isp = '';
if(present(data.isp_website)){
isp = '<a href="' + data.isp_website + '" target="_blank">' + data.isp + '</a>';
}
else{
isp = data.isp;
}
return isp;
}

function format_as(data){
as = '';
if(present(data.as_number)){
as += data.as_number;
}
if(present(data.as_name)){
if(present(data.as_number)){
as += '<br/>';
}
as += data.as_name;
}
if(present(data.as_number)){
if(present(data.as_number) || present(data.as_name)){
as += '<br/>';
}
as += data.as_organisation;
}
return as;
}

function format_address(data){
address = '';
if(present(data.street)){
address += data.street + '<br/>';
}
if(present(data.zip)){
address += data.zip;
}
if(present(data.city)){
if(present(data.zip)){
address += ' ';
}
address += data.city + '<br/>';
}
if(present(data.country)){
address += data.country + ' <img src="flags/' + data.country_code.toLowerCase() + '.svg" width="20" height="16" style="margin-bottom: 2px;">';
}
return address;
}

function present(val){
return !(val === undefined || val == null || val.length <= 0)
}

$(document).ready(function(){
$.ajax({
url: location.protocol + '//ipv4.' + location.host + '/ip.php',
Expand All @@ -49,14 +104,15 @@ $(document).ready(function(){
success: function(data){
$('#content-ipv4-ip').text(data);
get_ipv4_host();
get_ipv4_data(data);
get_ipv4_data();
},
error: function(error){
$('#content-ipv4-ip').text('-');
$('#content-ipv4-hostname').text('-');
$('#content-ipv4-ip-network').text('-');
$('#content-ipv4-isp').text('-');
$('#content-ipv4-as').text('-');
$('#content-ipv4-country').text('-');
$('#content-ipv4-address').text('-');
}
});

Expand All @@ -67,14 +123,15 @@ $(document).ready(function(){
success: function(data){
$('#content-ipv6-ip').html(data.replace(/:/g, ':<wbr>'));
get_ipv6_host();
get_ipv6_data(data);
get_ipv6_data();
},
error: function(error){
$('#content-ipv6-ip').text('-');
$('#content-ipv6-hostname').text('-');
$('#content-ipv6-ip-network').text('-');
$('#content-ipv6-isp').text('-');
$('#content-ipv6-as').text('-');
$('#content-ipv6-country').text('-');
$('#content-ipv6-address').text('-');
}
});

Expand Down Expand Up @@ -120,45 +177,51 @@ $(document).ready(function(){
});
};

function get_ipv4_data(ip) {
function get_ipv4_data() {
$.ajax({
url: location.protocol + '//' + location.host + '/geo.php?ip=' + ip,
url: location.protocol + '//ipv4.' + location.host + '/geo.php',
type: 'GET',
crossDomain: true,
success: function(data){
if (data && data.status && data.status == 'success') {
$('#content-ipv4-isp').text(data.isp);
$('#content-ipv4-as').text(data.as);
$('#content-ipv4-country').html(data.zip + ' ' + data.city + ', ' + data.country + ' <img src="flags/' + data.countryCode.toLowerCase() + '.svg" width="20" height="16" style="margin-bottom: 2px;">');
$('#content-ipv4-ip-network').text(data.ip_network);
$('#content-ipv4-isp').html(format_isp(data));
$('#content-ipv4-as').html(format_as(data));
$('#content-ipv4-address').html(format_address(data));
/* $('#content-ipv4-country').html(data.zip + ' ' + data.city + ', ' + data.country + ' <img src="flags/' + data.country_code.toLowerCase() + '.svg" width="20" height="16" style="margin-bottom: 2px;">'); */
$('#map-ipv4').addClass('map-height');
init_map_ipv4(data.lat, data.lon);
}
else {
$('#content-ipv4-ip-network').text('-');
$('#content-ipv4-isp').text('-');
$('#content-ipv4-as').text('-');
$('#content-ipv4-country').text('-');
$('#content-ipv4-address').text('-');
}
}
});
};

function get_ipv6_data(ip) {
$.ajax({
url: location.protocol + '//' + location.host + '/geo.php?ip=' + ip,
url: location.protocol + '//ipv6.' + location.host + '/geo.php',
type: 'GET',
crossDomain: true,
success: function(data){
if (data && data.status && data.status == 'success') {
$('#content-ipv6-isp').text(data.isp);
$('#content-ipv6-as').text(data.as);
$('#content-ipv6-country').html(data.zip + ' ' + data.city + ', ' + data.country + ' <img src="flags/' + data.countryCode.toLowerCase() + '.svg" width="20" height="16" style="margin-bottom: 2px;">');
$('#content-ipv6-ip-network').text(data.ip_network);
$('#content-ipv6-isp').html(format_isp(data));
$('#content-ipv6-as').html(format_as(data));
$('#content-ipv6-address').html(format_address(data));
/* $('#content-ipv6-country').html(data.zip + ' ' + data.city + ', ' + data.country + ' <img src="flags/' + data.country_code.toLowerCase() + '.svg" width="20" height="16" style="margin-bottom: 2px;">'); */
$('#map-ipv6').addClass('map-height');
init_map_ipv6(data.lat, data.lon);
}
else {
$('#content-ipv6-ip-network').text('-');
$('#content-ipv6-isp').text('-');
$('#content-ipv6-as').text('-');
$('#content-ipv6-country').text('-');
$('#content-ipv6-address').text('-');
}
}
});
Expand Down Expand Up @@ -247,7 +310,7 @@ $(document).ready(function(){

var addrs = [];
function updateDisplay(newAddr) {
if (newAddr != '0.0.0.0' && addrs.indexOf(newAddr) == -1 && newAddr.indexOf('-') == -1) {
if (newAddr != '0.0.0.0' && newAddr.indexOf('-') == -1 && addrs.indexOf(newAddr) == -1) {
addrs.push(newAddr);
}
$('#content-local-ip').text(addrs.join(', ') || '-');
Expand Down
47 changes: 47 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
print('<pre>');

print("\n\nMM_COUNTRY_CODE_COUNTRY_DB:\n");
print_r(getenv('MM_COUNTRY_CODE_COUNTRY_DB'));

print("\n\nMM_COUNTRY_NETWORK:\n");
print_r(getenv('MM_COUNTRY_NETWORK'));

print("\n\n\n");

print("\n\nMM_COUNTRY_CODE_CITY_DB:\n");
print_r(getenv('MM_COUNTRY_CODE_CITY_DB'));

print("\n\nMM_COUNTRY_NAME:\n");
print_r(getenv('MM_COUNTRY_NAME'));

print("\n\nMM_CITY_NAME:\n");
print_r(getenv('MM_CITY_NAME'));

print("\n\nMM_LONGITUDE:\n");
print_r(getenv('MM_LONGITUDE'));

print("\n\nMM_LATITUDE:\n");
print_r(getenv('MM_LATITUDE'));

print("\n\nMM_CITY_DB_NETWORK:\n");
print_r(getenv('MM_CITY_DB_NETWORK'));

print("\n\n\n");

print("\n\nMM_ASN:\n");
print_r(getenv('MM_ASN'));

print("\n\nMM_AS_ORG:\n");
print_r(getenv('MM_AS_ORG'));

print("\n\nMM_ASN_DB_NETWORK:\n");
print_r(getenv('MM_ASN_DB_NETWORK'));

print("\n\n\n");

print("\n\nMM_REGION_CODE:\n");
print_r(getenv('MM_REGION_CODE'));

print('</pre>');
?>

0 comments on commit 1f2728b

Please sign in to comment.