Announcement

Collapse
No announcement yet.

Resolve IP to get ISO 3166 Country code

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Resolve IP to get ISO 3166 Country code

    I'm trying to replace an IP text string with an icon by resolving the country code, it needs to be quick as there is a good few to do so does anyone know what functions I use for this?

  • #2
    Originally posted by Shrek View Post
    I'm trying to replace an IP text string with an icon by resolving the country code, it needs to be quick as there is a good few to do so does anyone know what functions I use for this?
    I already made a plugin for this in the past to get the country but you have to use a online server so what I would do is get all the ips you want to convert convert them all at once then pass them back to ams it should take about 5 secs tops but sometimes it will take up to the time out if your using the default ams http.

    but getting the country code means you have to deal with a service that has a upto date IP table for each country a few also let you have the older DB to host your own but there out of date from what I saw in the past.
    Plugins or Sources MokoX
    BunnyHop 2021 Here

    Comment


    • #3
      Cheers mate, I can log previous resolves in a table to save time and time out requests and use a blank icon but I figured there may be a Microsoft function for this that would save on the AMS http?

      Comment


      • #4
        Originally posted by Shrek View Post
        Cheers mate, I can log previous resolves in a table to save time and time out requests and use a blank icon but I figured there may be a Microsoft function for this that would save on the AMS http?
        I might be very wrong but I don't think there is, you could create your own basic DB about it and download that to the app and then use the local DB this would be much faster.
        Plugins or Sources MokoX
        BunnyHop 2021 Here

        Comment


        • #5
          Thanks, so I found a free csv of country codes and it resembles this:

          Code:
          "1.0.0.0","1.0.0.255","16777216","16777471","AU","Australia"
          "1.0.1.0","1.0.3.255","16777472","16778239","CN","China"
          "1.0.4.0","1.0.7.255","16778240","16779263","AU","Australia"
          "1.0.8.0","1.0.15.255","16779264","16781311","CN","China"
          "1.0.16.0","1.0.31.255","16781312","16785407","JP","Japan"
          "1.0.32.0","1.0.63.255","16785408","16793599","CN","China"
          "1.0.64.0","1.0.127.255","16793600","16809983","JP","Japan"
          "1.0.128.0","1.0.255.255","16809984","16842751","TH","Thailand"
          what do I do with it lol?

          The thing is quite big at 6.1MB but I'm thinking converted to a MemoryEx LH file it will comfortably be under 1MB.

          Comment


          • #6
            OK so I got it working online with this and LuLanes so it's responsive but the online check is limited to 1000 checks per day so I would like to use the CSV first then if that fails check online, they give you some code here:

            Code:
            address = '174.36.207.186'
            
            ( o1, o2, o3, o4 ) = address.split('.')
            
            integer_ip =   ( 16777216 * o1 )
                         + (    65536 * o2 )
                         + (      256 * o3 )
                         +              o4
            for dealing with the IP to check but how does that relate to the CSV?

            Comment


            • #7
              Originally posted by Shrek View Post
              OK so I got it working online with this and LuLanes so it's responsive but the online check is limited to 1000 checks per day so I would like to use the CSV first then if that fails check online, they give you some code here:

              Code:
              address = '174.36.207.186'
              
              ( o1, o2, o3, o4 ) = address.split('.')
              
              integer_ip =   ( 16777216 * o1 )
                           + (    65536 * o2 )
                           + (      256 * o3 )
                           +              o4
              for dealing with the IP to check but how does that relate to the CSV?
              I used maxmind and another one at the time I little busy right now with sorting baby out but
              http://www.youtube.com/watch?v=Dpak7ggGHDQ
              and I don't think I have the source for it but I know its about as a plugin somwhere
              Plugins or Sources MokoX
              BunnyHop 2021 Here

              Comment


              • #8
                Hi

                first of all , there is no algorithm to convert an ip to country
                you sould use a databse to check country ,some organizations share this kind of databases for free , and usually in csv format

                here is one of them , commonly used by developers

                if you open that CSV file you will see a block of data in each line
                Code:
                "16777216","17367039","AU","AUS","AUSTRALIA"
                "16777216" and "17367039" specifying an IP range From - To
                to convert an IP to decimal value there is a simple calculation
                Code:
                IPNo = 16777216*ip_part_1 + 65536*ip_part_2 + 256*ip_part_3 + ip_part_4
                once you converted IP address to decimal value ,you should compare
                each line of CSV file against this decimal value to find whether IPNo between range of a country

                for example
                Code:
                from = 16777216
                to = 17367039
                if(IPNo >= tonumber(from) and  IPNo <= tonumber(to)) then
                -- found it
                end
                i attached a sample project
                Note : you must download ip-country database from the link above and put ip-to-country.csv to AutoPlay\Docs folder

                you can also convert this file to a database , but Lua's io functions are fast enough to process CSV file line by line
                Attached Files
                amsplugins.com Is Closed.

                Facebook Page

                Comment


                • #9
                  ^^ wow, thanks :yes

                  Comment


                  • #10
                    So I'm trying to speed this up a little and make it a bit smaller, changing the csv to look like this:

                    Code:
                    "16777216","17367039","AU"
                    "17367040","17432575","MY"
                    "17432576","17498111","AU"
                    "17498112","17563647","KR"
                    reduces it from 5.37 MB to 3.40 MB then I figured I could use MemoryEx or require to load it from a table but it seem Lua has some arbitrary limit on script sizes so it's a 60000 limit of lines per script that means anything bigger can't get passed require or the syntax checker on MemoryEx or AMS so what's that all about?

                    What I've done now is use the io.open method to put the csv into a table and then comparing the key with the start position gives an almost instant result from a for loop but somethings odd about the memory the app uses, prior to io.open the application uses 3.6MB of computer RAM then doing the io.open to put things in a table it goes to 34.0MB then with a collectgarbage() it drops to 28.8MB so how can it consume around 25MB of memory for a table from a file that is only 3.4MB?

                    Comment


                    • #11
                      I've worked with these CSV's before in PureBasic, and I always first convert them to a binary filetype. String versions of the numeric IP addresses take up much more space than a DWORD in a binary file. I'll try to get you an example later today or tomorrow.
                      Bas Groothedde
                      Imagine Programming :: Blog

                      AMS8 Plugins
                      IMXLH Compiler

                      Comment


                      • #12
                        My plugin WAS on here http://software77.net/geoip-software.htm but seems its no longer there, it downloaded there CSV that did take a little while but once you have it it works fast, then it showed the country, they had ip range, country code and then the written name of country at the time so was kinda easy at the time to do
                        Plugins or Sources MokoX
                        BunnyHop 2021 Here

                        Comment


                        • #13
                          I have found my example of IP Nation so I will try get it uploaded tonight for you.
                          Plugins or Sources MokoX
                          BunnyHop 2021 Here

                          Comment


                          • #14
                            Here it is.

                            IPNation Example.zip
                            Plugins or Sources MokoX
                            BunnyHop 2021 Here

                            Comment


                            • #15
                              Cheers for that :yes


                              So whats the best way to make a table thats going to have in excess of 100,000 entries in that it should have the smallest possible memory footprint? IP mentioned binary but I'm not sure where to begin with that.

                              Comment

                              Working...
                              X