1- // Fetches and converts MaxMind lite databases
1+ // ============================================================================
2+ // GeoIP Database Updater
3+ // Fetches and converts MaxMind GeoLite2 databases
4+ // ============================================================================
25
36'use strict' ;
47
8+ // ============================================================================
9+ // Dependencies
10+ // ============================================================================
11+
512const { name, version } = require ( '../package.json' ) ;
613const UserAgent = `Mozilla/5.0 (compatible; ${ name } /${ version } ; +https://github.com/sefinek/geoip-lite2)` ;
714
@@ -19,6 +26,10 @@ const AdmZip = require('adm-zip');
1926const utils = require ( '../lib/utils.js' ) ;
2027const { Address6, Address4 } = require ( 'ip-address' ) ;
2128
29+ // ============================================================================
30+ // Configuration
31+ // ============================================================================
32+
2233const args = process . argv . slice ( 2 ) ;
2334let license_key = args . find ( arg => arg . match ( / ^ l i c e n s e _ k e y = [ a - z A - Z 0 - 9 ] + / ) !== null ) ;
2435if ( typeof license_key === 'undefined' && typeof process . env . LICENSE_KEY !== 'undefined' ) {
@@ -67,6 +78,10 @@ const databases = [{
6778 dest : [ 'geoip-city-names.dat' , 'geoip-city.dat' , 'geoip-city6.dat' ] ,
6879} ] ;
6980
81+ // ============================================================================
82+ // Utility Functions
83+ // ============================================================================
84+
7085function mkdir ( dirName ) {
7186 const dir = path . dirname ( dirName ) ;
7287 if ( ! fs . existsSync ( dir ) ) fs . mkdirSync ( dir ) ;
@@ -118,6 +133,10 @@ function CSVtoArray(text) {
118133 return a ;
119134}
120135
136+ // ============================================================================
137+ // HTTP Configuration
138+ // ============================================================================
139+
121140function getHTTPOptions ( downloadUrl ) {
122141 const parsedUrl = new URL ( downloadUrl ) ;
123142 const options = {
@@ -140,6 +159,10 @@ function getHTTPOptions(downloadUrl) {
140159 return options ;
141160}
142161
162+ // ============================================================================
163+ // Database Download Functions
164+ // ============================================================================
165+
143166function check ( database , cb ) {
144167 if ( args . indexOf ( 'force' ) !== - 1 ) {
145168 // We are forcing database upgrade,
@@ -292,6 +315,10 @@ function processLookupCountry(src, cb) {
292315 } ) ;
293316}
294317
318+ // ============================================================================
319+ // Data Processing Functions
320+ // ============================================================================
321+
295322async function processCountryData ( src , dest ) {
296323 let lines = 0 ;
297324 const dataFile = path . join ( dataPath , dest ) ;
@@ -538,6 +565,10 @@ function processCityDataNames(src, dest, cb) {
538565 rl . on ( 'close' , cb ) ;
539566}
540567
568+ // ============================================================================
569+ // Main Processing Dispatcher
570+ // ============================================================================
571+
541572function processData ( database , cb ) {
542573 if ( database . skip ) return cb ( null , database ) ;
543574
@@ -573,6 +604,10 @@ function processData(database, cb) {
573604 }
574605}
575606
607+ // ============================================================================
608+ // Checksum Management
609+ // ============================================================================
610+
576611function updateChecksum ( database , cb ) {
577612 if ( database . skip || ! database . checkValue ) return cb ( ) ; // Don't need to update checksums because it was not fetched or did not change
578613
@@ -582,6 +617,10 @@ function updateChecksum(database, cb) {
582617 } ) ;
583618}
584619
620+ // ============================================================================
621+ // Main Execution Flow
622+ // ============================================================================
623+
585624if ( ! license_key ) {
586625 console . error ( 'ERROR: Missing license_key' ) ;
587626 process . exit ( 1 ) ;
0 commit comments