Giter Club home page Giter Club logo

ip2location / ip2location-apache Goto Github PK

View Code? Open in Web Editor NEW
7.0 5.0 3.0 38 KB

This is a IP2Location Apache Module that enables the user to find the country, region, city, latitude, longitude, zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation, usage type, address type and IAB category by IP address or hostname originates from.

Home Page: http://www.ip2location.com

License: GNU General Public License v2.0

C 72.61% PHP 27.39%
ip2location geolocation ip-database ip-geolocation apache ip2location-apache ip-address-geolocation ip-lookup

ip2location-apache's Introduction

IP2Location Apache Module

This is a IP2Location Apache Module that enables the user to find the country, region or state, district, city, latitude and longitude, ZIP/Postal code, time zone, Internet Service Provider (ISP) or company name, domain name, net speed, area code, weather station code, weather station name, mobile country code (MCC), mobile network code (MNC) and carrier brand, elevation, usage type, address type, IAB category and ASN and category by IP address or hostname originates from. The library reads the geo location information from IP2Location BIN data file.

Supported IPv4 and IPv6 address.

For more details, please visit: https://www.ip2location.com/developers/apache

Requirements

  1. IP2Location C API library (download from https://github.com/chrislim2888/IP2Location-C-Library)
  2. Apache 2.0x
  3. GNU make or any compatible make utility.

Installation

Linux Build (Debian)

  1. Create a new folder called ip2location.

  2. Install the development libraries in Debian.

    apt install apache2-dev
    
  3. Download IP2Location C library from here into the ip2location folder.

  4. Decompress C library.

    unzip master.zip && rm master.zip
    
  5. Get into working directory.

    cd IP2Location-C-Library-master
    
  6. Compile and install IP2Location-C-Library.

    autoreconf -i -v --force
    ./configure
    make
    make install
    

    Note: You may need to run ldconfig to refresh the shared libraries, if needed.

  7. Download IP2Location Apache Module into the ip2location folder.

  8. Decompress IP2Location Apache Module

    unzip master.zip && rm master.zip
    
  9. Get into IP2Location Apache Module directory.

    cd ip2location-apache-master
    
  10. Compile the IP2Location Apache Module.

apxs2 -i -a -L ../IP2Location-C-Library-master/libIP2Location/ -I ../IP2Location-C-Library-master/libIP2Location/ -l IP2Location -c mod_ip2location.c
  1. Run the below command and make sure you see IP2Location_module (shared) entry.
apache2ctl -M
  1. Add following lines into /etc/apache2/apache2.conf
<IfModule mod_ip2location.c>
	IP2LocationEnable On
	IP2LocationDetectProxy <On|Off>
	IP2LocationSetmode ALL
	IP2LocationDBFile <ip2location_binary_db_file_with_fully_qualified_path>
</IfModule>

Windows Build

  1. Create a new folder called ip2location.

  2. Download IP2Location C library from here into the ip2location folder.

  3. Decompress C library using 7-zip or other compression tools.

  4. Get into working directory.

    cd IP2Location-C-Library-master
    
  5. Start compilation.

    nmake /f Makefile.win
    
  6. Download and decompress Apache module into the ip2location folder.

  7. Navigate to ip2location-apache-master folder.

  8. Edit Makefile.win to change the path for C library.

    IP2LOCATION_CSRC_PATH = C:/ip2location/IP2Location-C-Library-master/libIP2Location
    IP2LOCATION_CLIB_PATH = C:/ip2location/IP2Location-C-Library-master/libIP2Location
    APACHE_INSTALL_PATH = "C:/Program Files/Apache Software Foundation/Apachex.x"
    
  9. Start compilation.

    nmake /f Makefile.win
    
  10. Copy IP2Location_apache.dll generated to the Apache modules directory.

  11. Add following lines into httpd.conf

    <IfModule mod_ip2location.c>
    	IP2LocationEnable <ON|OFF>
    	IP2LocationDetectProxy <ON|OFF>
    	IP2LocationSetmode ALL
    	IP2LocationDBFile <ip2location_binary_db_file_with_fully_qualified_path>
    </IfModule>
    

Apache Configuration

  1. To load IP2Location module in Apache, add the following lines in httpd.conf.

    LoadModule IP2Location_module FULL_PATH_TO_IP2LOCATION_MODULE
    
    <IfModule mod_ip2location.c>
    IP2LocationEnable On 
    # ENV will set server variables
    # NOTES will set apache notes 
    # ALL will set both
    IP2LocationSetmode ALL
    IP2LocationDBFile PATH_TO_IP2LOCATION_BIN_DATABASE
    IP2LocationDetectProxy On
    </IfModule>
    

    Reference:

    Name Value Description
    IP2LocationEnable On|Off Enable or disable IP2Location module.
    IP2LocationSetmode ENV|NOTES|ALL Choose where to display the geolocation variables. ENV = Server variables, NOTES = Apache notes, ALL = Both
    IP2LocationDBFile Path The full path to a IP2Location BIN database
    IP2LocationDetectProxy On|Off If you website is hosted behind a reverse proxy server, turn this on to detect the correct IP address
  2. Restart Apache server to take effect of the changes

Notes If you are getting startup errors where Apache cannot locate the module files, please set your environment path as below:

$ LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/apache2/modules/
$ export LD_LIBRARY_PATH

Testing

PHP Testing

From internet browser, load mod_ip2location_test.php

Apache Rewrite Testing

  1. Add below lines to your domain configuration file (apaches/sites-enabled) or .htaccess:

    RewriteEngine On
    RewriteCond %{ENV:IP2LOCATION_COUNTRY_SHORT} ^US$
    RewriteRule ^(.*)$ https://www.google.com [L]
    
  2. This will redirect all IP address from United States to https://www.google.com

Sample BIN Databases

IPv4 BIN vs IPv6 BIN

  • Use the IPv4 BIN file if you just need to query IPv4 addresses.
  • Use the IPv6 BIN file if you need to query BOTH IPv4 and IPv6 addresses.

Support

Email: [email protected].
URL: https://www.ip2location.com

ip2location-apache's People

Contributors

chrislim2888 avatar ip2location avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ip2location-apache's Issues

very weird results on IP2LOCATION_COUNTRY_SHORT logging/filtering

When using this (tested both on a recent Ubuntu 18.04 or 20.04 LTS) with some load, it produces very weird results on IP2LOCATION_COUNTRY_SHORT. I would expect only short country codes to be logged, but here's what I get - and therefore cannot really use in a production enviroment to restrict access.

Steps I've done and how to reproduce it:

  • set up a Ubuntu server with apache2, ssl, maybe phpmyadmin for quick testing to produce "load"
  • download, compile and install https://github.com/ip2location/IP2Location-C-Library/
  • download, compile and install https://github.com/ip2location/ip2location-apache/
  • download a recent IP2LOCATION-LITE-DB1.BIN e.g. to /usr/share/ip2location/IP2LOCATION-LITE-DB1.BIN
  • enable an apache conf as per README.md
  • enable logging in an enabled site by LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\" \"%{IP2LOCATION_COUNTRY_SHORT}e\"" location and CustomLog ${APACHE_LOG_DIR}/location.log location
  • fiddle around in your phpmyadmin
  • parse resulting location.log with cat location.log | awk '{print $NF}' | sort -u

Below samples are unfiltered

Sample result (all access to /phpmyadmin from same sourced in Austria so one would expect only single line "AT"):

"-"
""
a"
"AT"
"AU"
Faso\x02BG\bBulgaria\x02BH\x07Bahrain\x02BI\x07B"
Firefox/78.0"
Herzegovina\x02BB\bBarbados\x02BD\nBangladesh\x02BE\x07Belgium\x02BF\x0cB"
[email protected])"
Islands"
Islands\x02AZ\nAzerbaijan\x02BA\x16"
Islands\x02AZ\nAzerbaijan\x02BA\x16Bo"
"T\x07"
"\x02"
"\x1a)\xd4\x02"
"\x1e"
"\xa0\xa5\x1e"
"\xbe\x02"

Sample result on new Ubuntu 20 server without any content, used phpmyadmin and left staling some hours (mainly bot access therefore):

""
"-"
-
-"
'A"
Africa\x02ZM\x06Zambia\x02ZW\bZimbabwe\x05'Adan\x06'Ajlun\x06'Ajma
"Akita"
apskritis\tVinh"
'Asi
"AT"
AT
AT"
Balq
Balq"
"BE"
Beach\x06Veroia\vVerolanuova\x05
connection)"
Firefox/78.0"
"gailhenc\tVillejuif\tVillejust\nVillelaure\x0cVillemandeur\x0cVillemoirieu\x0cVillemontais\x10Villemoustaussou\x07Ville"
Hills\x06Vestby\bVestbygd\x06Vestec\tVestmanna\x0eVestmannaeyjar\x07Vestnes\bVeszprem\x06"
"NL"
NL
of)\x02IS\x07Iceland\x02IT\x05Italy\x02JE\x06Jersey\x02JM\x07Jamaica\x02JO\x06Jordan\x02JP\x05Japan\x02KE\x05Kenya\x02KG\nKy
Safari/537.36"
Thuan\x10Binshan"
"US"
"\x04\x14\n"
"{\x06"
{\x06
"\xb7\x84"
\xb7\x84
\xb7\x84"

Sample result on U20 with only access from single 192.168.x.x client without quotes in LogFormat:

-
AD
Emirates\x02AF\vAfg
Firefox/78.0"
US
\x02
\x14
\x14\v
\x1b\x02
\x1e
\x9c
\xa1\x1e
)\xd4\x02

mod_ip2location is not picking up Client IP as set by mod_remoteip

I have a situation where my services are behind a proxy. I utilize mod_remoteip to overwrite Client-IP with the one passed via a custom header which is set by said proxy. I'm confident that part works as I can see IP passed via that header as I can see it reflected in my app logs and in apache logs (logged as %a).

I was thinking to modify mod_ip2location to pick up that custom header but unfortunately mod_remoteip discards it the moment IP is extracted.

Using X-Forwarded-For will not work for us as it would allow end user to manipulate business logic by modifying the value of X-Forwarded-For. Custom header we use cannot be manipulated by the end user. Besides, and this might be a topic for a separate issue, the way proxy works, X-Forwarded-For often contains 2+ comma separated IPs which seems to be breaking mod_ip2location logic, resulting in INVALID V4 ADDRESS.

Apache not loading after enable IP2Location Module

Hi,
I follow all the steps mentioned in the readme.
I'm using ubuntu 16.04 LTS version.
Can any one help me in finding solution for below error.?

apache2: Syntax error on line 140 of /etc/apache2/apache2.conf: Syntax error on line 1 of /etc/apache2/mods-enabled/IP2Location.load: Cannot load /usr/lib/apache2/modules/mod_ip2location.so into server: /usr/lib/apache2/modules/mod_ip2location.so: undefined symbol: IP2Location_open
Action 'configtest' failed.

Error: HTTPD.CONF SYNTAX CHECK RESULTS

Hi, I am using Apache 2.4.46 with Interworx control panel. I wanted to install IP2Location module and after installation of IP2Location Apache module I get this error:

httpd: Syntax error on line 101 of /etc/httpd/conf/httpd.conf: Cannot load /usr/lib64/httpd/modules/mod_ip2location.so into server: libIP2Location.so: cannot open shared object file: No such file or directory

More info on line 101:
101. LoadModule IP2Location_module /usr/lib64/httpd/modules/mod_ip2location.so
102. #
103. Include conf.modules.d/*.conf

I used sudo find / -name mod_ip2location.so to find the path I got this:
/root/IP2Location-Apache-Module-master/.libs/mod_ip2location.so
/usr/lib64/httpd/modules/mod_ip2location.so

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.