geoIP script for AC 1.2
#1
GeoIP script shows name of country, from where connected chosen player.
To use it, paste below code to config/autoexec.cfg in profile and unpack .txt files to that folder from this file:
geoip_GeoLite2_AC_attachments_20191224.zip

Usage: /geoip CN , where CN is client number, for example: /geoip 4
/geoipall - show countries of the all players

Notes: database has ~2,5 MB (it isn't added to saved.cfg), script checks only first 3 octets of IP, it uses binary search algorithm, database is from https://dev.maxmind.com/geoip/geoip2/geolite2/

// geoip script v. 4.2 by grenadier - date: 28.10.2018

// execute database of IPs
// this product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com
exec config/geoip_ipnum.txt
exec config/geoip_abbr.txt
exec config/geoip_countrylist.txt

alias geoip [
 if (= (isclient $arg1) 1) [
   if (!= (player $arg1 ip) (player1 ip)) [
     tmp_first_byte = (at (strreplace (player $arg1 ip) "." " ") 0)
     tmp_second_byte = (at (strreplace (player $arg1 ip) "." " ") 1)
     tmp_third_byte = (at (strreplace (player $arg1 ip) "." " ") 2)
     tmp_ipnum = ( + ( + ( * 65536 (tmp_first_byte) ) ( * 256 (tmp_second_byte) ) ) (tmp_third_byte) )
     
     //take IP of server for players with private IP
     if (|| (|| (&& (>= (tmp_ipnum) 655360) (<= (tmp_ipnum) 720895)) (&& (>= (tmp_ipnum) 11276288) (<= (tmp_ipnum) 11280383))) (&& (>= (tmp_ipnum) 12625920) (<= (tmp_ipnum) 12626175))) [              
       tmp_first_byte = (at (strreplace (curserver 1) "." " ") 0)
       tmp_second_byte = (at (strreplace (curserver 1) "." " ") 1)
       tmp_third_byte = (at (strreplace (curserver 1) "." " ") 2)
       tmp_ipnum = ( + ( + ( * 65536 (tmp_first_byte) ) ( * 256 (tmp_second_byte) ) ) (tmp_third_byte) )
echo 1
     ]

     i = 0
     j = 0

     persistidents 0
     alias tmp_geoip_ipnum [ getalias (concatword tmp_geoip_ipnum_ $tmp_first_byte) ]
     alias tmp_geoip_abbr [ getalias (concatword tmp_geoip_abbr_ $tmp_first_byte) ]
     persistidents 1
     
     alias tmp_length [ listlen (tmp_geoip_ipnum) ]
     alias tmp_search [ at (tmp_geoip_ipnum) (- $tmp_IP_position 1) ] //-1, because CubeScript starts from 0
     alias tmp_search_plus [ at (tmp_geoip_ipnum) $tmp_IP_position ]      

     //binary search algorithm
     while [&& (>= $i 0) (<= $j 19)][
       ++ j
       tmp_IP_position = (div (+ $i (tmp_length)) 2)  
       if (&& (>= $tmp_ipnum (tmp_search_plus)) (!= (- $tmp_IP_position $i) -1) )[ //find last country
         i = (+ $tmp_IP_position 1)
       ][
         if (< $tmp_ipnum (tmp_search))[
           tmp_length = (- $tmp_IP_position 1)        
         ][
           alias tmp_abbr (at (tmp_geoip_abbr) (- $tmp_IP_position 1)) //find country abbreviation                

           if (<= (strlen $tmp_abbr) 2)[ //if in this IP range is only 1 country
             tmp_player_country = (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist $tmp_abbr) 1) ) //find country name from abbreviation
           ][  //if in this IP range is more than 1 country
             if (= (strlen $tmp_abbr) 4)[
               tmp_player_country = ( concatword (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist (substr $tmp_abbr 0 2)) 1)) " or " (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist (substr $tmp_abbr 2 2)) 1)) )
             ][
               if (<= (strlen $tmp_abbr) 6)[
                 tmp_player_country = ( concatword (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist (substr $tmp_abbr 0 2)) 1)) " or " (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist (substr $tmp_abbr 2 2)) 1)) " or " (at $tmp_geoip_countrylist (+ (findlist $tmp_geoip_countrylist (substr $tmp_abbr 4 2)) 1)) )
               ][
                 tmp_player_country = (concatword "one of " (div (strlen $tmp_abbr) 2) " countries")
               ]
             ]
           ]          
           break          
         ]
       ]
     ] //end of binary search algorithm

     if (= $j 20) [tmp_player_country = "nowhere"]
     if (= $arg2 1) [tmp_player_ip = ""][tmp_player_ip = (concatword (c Y) "(" (player $arg1 ip) ")")]
     echo (c Y) (player $arg1 name) (c Y) (concatword "(" $arg1 ")") (c Y) "connected from" (c Y) $tmp_player_country $tmp_player_ip
   ][
     echo "geoIP info: this client number" $arg1 "is your number"
   ]
 ][
   echo "geoIP info: this client number" $arg1 "isn't valid"
 ]
]

alias "geoipall" [ loop i (curplayers) [if (&& (= (isclient $i) 1) (!= $i (player1 cn))) [ geoip $i $arg1]] ]

Note: if you want to use the last version of abandoned GeoLite database from https://dev.maxmind.com/geoip/legacy/geolite/ instead of newer GeoLite2 database, please use the attachments from the following package: geoip_AC_attachments_2018_03_27.zip
Thanks given by: D3M0NW0LF


Messages In This Thread
geoIP script for AC 1.2 - by grenadier - 25 Aug 18, 05:08PM
RE: geoIP script for AC 1.2 - by grenadier - 25 Aug 18, 05:12PM
RE: geoIP script for AC 1.2 - by D3M0NW0LF - 26 Aug 18, 03:23AM
RE: geoIP script for AC 1.2 - by grenadier - 26 Aug 18, 09:55AM
RE: geoIP script for AC 1.2 - by grenadier - 28 Oct 18, 01:57PM
RE: geoIP script for AC 1.2 - by grenadier - 01 Feb 20, 08:47PM