Skip to content

ezomeno/geo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 

Repository files navigation

GeoLite (for client-side)

This is free and unencumbered software released into the public domain.

Demo, enjoy!

Import library

<script src="geoLite.js" defer></script>
<script src="geoLite.datav5-20251025.js" defer></script>

As always, data file is loaded after geoLite.js along with attribute defer to allow page to render.
Alternatively see geoLite.html.

Important

Due geoLite needing to "decompress" data it can take half a second upto a second of delay before user can interact with the page.

Features (basics)

  • Fast lookup (~900k op/sec)
  • Country code and name (even emoji)
  • Continent code and name
  • ASO ("ISP" related)

Usage examples

Lookup (works locally)

Internet connection is not required after database is loaded - it works locally.

var info = geoLite.lookup(ipAddr.value);
ipaResult.innerHTML = (
	"Country: " + info.CN + " " + info.emoji + " " + info.CC + "\r\n" +
	"Continent: " + info.contName + " (" + info.contCode + ")\r\n" +
	"ASO: " + info.ASO
);

Fetch my IP (internet required)

Please avoid using this feature even though it works.
Can fail if using ipv6 (VPN/Tor or similar).

// it will attempt to fetch ip from multiple sources until one is retrieved
geoLite.fetchPubIPv4(function(arr, xhr, url, elapsed) {
  console.log("Result", arr, "from", u, "; elapsed:", elapsed, "[ms]");
});

Fetch my IP (config properties)

Note: geoLite already has these as default.

// code should be executed before fetchPubIPv4 is called.
// if using IPv6 then fetch will FAIL to parse
geoLite.ipFetchTimeout = 7000; // 7000 is default
geoLite.ipFetchMaxTimeout = 12000; // 12000 is default
geoLite.ipFetchURLs = ["https://icanhazip.com", "https://httpbin.org/ip"];

GZipped size?

(fyi: MiB = 1024 * 1024)
Database 20251025 is 3.31MiB (3481164 bytes; CSV size: 20.32750MiB)
Database 20241017 is 3.06MiB (3204748 bytes; CSV size: 18.45380MiB)
JS Databases: Link

Build own database.js and/or CSV

Requirements:

  1. NodeJS or BunJS or anything that runs nodejs code.
  2. Install dependency: mmdb-reader (BunJS auto installs it?)
  3. Downloaded file devtools/geoLite-dev.js

Steps:

  1. Download both ASN and City mmdb from anywhere.
  2. Ensure to rename it in this format: GeoLite2-City-{YYYYMMDD}.mmdb
    • Example: GeoLite2-ASN-20230107.mmdb
  3. Create folders mmdb and csv in the same folder of the file devtools/geoLite-dev.js
  4. Put .mmdb files in folder mmdb
    • Example 1: mmdb/GeoLite2-ASN-20230107.mmdb (for ASN)
    • Example 2: mmdb/GeoLite2-City-20230107.mmdb (for City)
  5. Edit file devtools/geoLite-dev.js
    1. (All below is about variable geoLiteConfig)
    2. Set defaultDbDate use this format YYYYMMDD (example: 20250930)
    3. Set scriptAct to 2 to build csv and data file.
  6. Save and Run the script
    node geoLite-dev
    
  7. Wait, usually can take upto an hour or more.
  8. Make sure to reset scriptAct to 0 once done.
  9. Enjoy (data is built in same dir as geoLite-dev.js)

Build more Detailed CSV?

Set geoLiteConfig.scriptAct=4 in devtools/geoLite-dev.js

Example output:

IPStart,IPCount,CountryCode,CountryName,ContinentCode,ContinentName,ASO
16777216,256,AU,Australia,OC,Oceania,CLOUDFLARENET
16777472,768,CN,China,AS,Asia,
...

Future plans

None at all, unless someone suggests public domain licensed library that can be used to further compress data file.

About

Client-side GeoIP. No bloat/libs. Basic features: country, continent and ASO.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published