conv_maxmind-geolite2_to_ac.awk 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # convert Geolite2 lists for Assaultcube servers
  2. #
  3. # expects to process GeoLite2-Country-Locations-en.csv and GeoLite2-Country-Blocks-IPv4.csv (in that order)
  4. #
  5. # gawk -f conv_maxmind-geolite2_to_ac.awk GeoLite2-Country-Locations-en.csv GeoLite2-Country-Blocks-IPv4.csv > geoip.cfg
  6. #
  7. # note: the ISO 3166-1 alpha-2 list reserves XA..XZ for user-assigned code elements - we use xs, xp and xz here, see below
  8. #
  9. # download lists from http://dev.maxmind.com/geoip/geoip2/geolite2/ (please don't download too often: once a month should be enough)
  10. #
  11. # thanks to MaxMind:
  12. # This product includes GeoLite2 data created by MaxMind, available from http://www.maxmind.com
  13. #
  14. BEGIN {
  15. FS="," # expect csv
  16. getcc=0
  17. }
  18. # geoname_id,locale_code,continent_code,continent_name,country_iso_code,country_name
  19. $1=="geoname_id" && $5=="country_iso_code" {
  20. getcc=1 # use first line to switch mode
  21. next
  22. }
  23. # network,geoname_id,registered_country_geoname_id,represented_country_geoname_id,is_anonymous_proxy,is_satellite_provider
  24. $1=="network" && $2=="geoname_id" {
  25. getcc=0 # use first line to switch mode
  26. next
  27. }
  28. getcc==1 {
  29. if ( length($5)==2 )
  30. cctab[$1]=$5 # has country_iso_code
  31. else if ( length($3)==2 )
  32. cctab[$1]=tolower($3) # has continent_code (convert to lowercase to distinguish from country codes)
  33. else
  34. print "ERROR: no usable country code in line " $0 # should not occur
  35. next
  36. }
  37. getcc==0 {
  38. if ( $2=="" ) { # pick geoname_id over represented_country_geoname_id over registered_country_geoname_id
  39. if ( $4 == "" )
  40. cc=$3
  41. else
  42. cc=$4
  43. } else {
  44. cc=$2
  45. }
  46. if ( cc=="" && $6!=0 )
  47. print $1 " xs" # use XS to mark satellite providers with no other country affiliation
  48. else if ( cc=="" && $5!=0 )
  49. print $1 " xp" # use XP to mark anonymous proxies with no other country affiliation
  50. else if ( cc in cctab )
  51. print $1 " " cctab[cc]
  52. else
  53. print $1 " xz // " $0 " not found" # use XZ to mark table conversion errors
  54. next
  55. }