{"id":1158,"date":"2012-03-31T21:45:08","date_gmt":"2012-03-31T20:45:08","guid":{"rendered":"http:\/\/blog.guiguiabloc.fr\/?p=1158"},"modified":"2012-03-31T21:45:08","modified_gmt":"2012-03-31T20:45:08","slug":"geolocalisation-par-dns-avec-bind","status":"publish","type":"post","link":"http:\/\/blog.guiguiabloc.fr\/index.php\/2012\/03\/31\/geolocalisation-par-dns-avec-bind\/","title":{"rendered":"G\u00e9olocalisation par DNS avec Bind"},"content":{"rendered":"<p><a href=\"http:\/\/blog.guiguiabloc.fr\/wp-content\/2012\/03\/geo-targeting.jpg\"><img loading=\"lazy\" class=\"aligncenter size-medium wp-image-1159\" title=\"geo-targeting\" src=\"http:\/\/blog.guiguiabloc.fr\/wp-content\/2012\/03\/geo-targeting-300x200.jpg\" alt=\"\" width=\"300\" height=\"200\" srcset=\"http:\/\/blog.guiguiabloc.fr\/wp-content\/2012\/03\/geo-targeting-300x200.jpg 300w, http:\/\/blog.guiguiabloc.fr\/wp-content\/2012\/03\/geo-targeting.jpg 355w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Un petit billet rapide suite \u00e0 la r\u00e9ception en Alpha test d&rsquo;un serveur OVH au Canada \ud83d\ude42<\/p>\n<p>Alors voil\u00e0, vous avez votre serveur en Europe et un autre aux USA (je simplifie, hein, ca peut \u00eatre votre ferme de serveurs dans le Larzac et l&rsquo;autre dans le Wisconsin).<\/p>\n<p>Si vous avez d\u00e9cid\u00e9 d&rsquo;offrir le m\u00eame service au monde entier, il serait peut-\u00eatre int\u00e9ressant de rediriger vos visiteurs am\u00e9ricains sur votre serveur aux US et vos visiteurs europ\u00e9ens sur celui en\u00a0Europe, question de latence.<\/p>\n<p>Cette technique, employ\u00e9e depuis tr\u00e8s longtemps par les \u00ab\u00a0acc\u00e9l\u00e9rateurs de contenus\u00a0\u00bb comme<a title=\"http:\/\/www.akamai.com\/\" href=\"http:\/\/www.akamai.com\/\" target=\"_blank\"> Akama\u00ef<\/a> , a \u00e9t\u00e9 reprise par les sites \u00e0 gros trafic comme Google, Yahoo et consorts.<\/p>\n<p>Comment on met cela en place ?<\/p>\n<p>D&rsquo;abord je consid\u00e8re que vous avez 1 serveur DNS qui tourne sur chacun de vos serveurs (1 aux US, 1 en Europe).<\/p>\n<p>Bien \u00e9videmment, on d\u00e9finira ses deux serveurs en <a title=\"http:\/\/fr.wikipedia.org\/wiki\/Domain_Name_System\" href=\"http:\/\/fr.wikipedia.org\/wiki\/Domain_Name_System\" target=\"_blank\">NS du domaine<\/a> que l&rsquo;on veut g\u00e9olocaliser.<\/p>\n<p>Pour r\u00e9ussir ce tour de force de g\u00e9olocalisation, il vous faut une base des adresses IPs mondiales et les pays dans lesquels elles sont attribu\u00e9es.<\/p>\n<p>L&rsquo;un des site sp\u00e9cialis\u00e9 dans ce domaine est <a title=\"http:\/\/www.maxmind.com\/\" href=\"http:\/\/www.maxmind.com\/\" target=\"_blank\">Maxmind<\/a><\/p>\n<p>Et bonus, ce site offre en t\u00e9l\u00e9chargement une base de ses IPs :<\/p>\n<p><a title=\"http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoIPCountryCSV.zip\" href=\"http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoIPCountryCSV.zip\" target=\"_blank\">http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoIPCountryCSV.zip<\/a><\/p>\n<p>C&rsquo;est un fichier CSV au contenu plut\u00f4t impressionnant qu&rsquo;il va falloir rendre compr\u00e9hensible par votre serveur DNS.<\/p>\n<p>Heureusement, Mark, sur son site<a title=\"http:\/\/phix.me\/geodns\/\" href=\"http:\/\/phix.me\/geodns\/\" target=\"_blank\"> Phix.me<\/a>, vous propose un script Bash pour faire cela rapidement.<\/p>\n<p>.<\/p>\n<pre lang=\"bash\">#!\/bin\/bash\r\n\r\n[ -f GeoIPCountryCSV.zip ] || wget -T 5 -t 1 http:\/\/geolite.maxmind.com\/download\/geoip\/database\/GeoIPCountryCSV.zip\r\n\r\necho -n \"Creating CBE (Country,Begin,End) CSV file...\"\r\nunzip -p GeoIPCountryCSV.zip GeoIPCountryWhois.csv | awk -F \\\" '{print $10\",\"$6\",\"$8}' &gt; cbe.csv\r\necho -ne \"DONE\\nGenerating BIND GeoIP.acl file...\"\r\n\r\n(for c in $(awk -F , '{print $1}' cbe.csv | sort -u)\r\ndo\r\n  echo \"acl \\\"$c\\\" {\"\r\n  grep \"^$c,\" cbe.csv | awk -F , 'function s(b,e,l,m,n) {l = int(log(e-b+1)\/log(2)); m = 2^32-2^l; n = and(m,e); if (n == and(m,b)) printf \"\\t%u.%u.%u.%u\/%u;\\n\",b\/2^24%256,b\/2^16%256,b\/2^8%256,b%256,32-l; else {s(b,n-1); s(n,e)}} s($2,$3)'\r\n  echo -e \"};\\n\"\r\ndone) &gt; GeoIP.acl\r\nrm -f cbe.csv\r\necho \"DONE\"\r\nexit 0<\/pre>\n<p>Vous avez maintenant un fichier geo.acl compr\u00e9hensible par BIND.<\/p>\n<p>On se modifie BIND pour servir un fichier d&rsquo;enregistrement diff\u00e9rent suivant le pays d&rsquo;origine du visiteur :<\/p>\n<pre lang=\"text\">named.conf :\r\ninclude \"\/opt\/bind\/GeoIP.acl\";<\/pre>\n<p>Et pour traiter les visiteurs, nous allons utiliser le syst\u00e8me \u00ab\u00a0View\u00a0\u00bb de BIND qui nous permet de servir des r\u00e9ponses diff\u00e9rentes suivant l&rsquo;ip du demandeur.<\/p>\n<pre lang=\"text\">view \"north_america\" {\r\n  match-clients { US; CA; MX; };\r\n  zone \"guiguiabloc.fr\" {\r\n       type master;\r\n       file \"\/etc\/bind\/guiguiabloc.us\";\r\n       allow-transfer { ipdnsslave; };\r\n       allow-query { any; };\r\n};\r\n};\r\n\r\nview \"south_america\" {\r\n  match-clients { AR; CL; BR; PY; PE; EC; CO; VE; BO; UY; };\r\n  recursion no;\r\nzone \"guiguiabloc.fr\" {\r\n       type master;\r\n       file \"\/etc\/bind\/guiguiabloc.us\";\r\n       allow-transfer { ipdnsslave; };\r\n       allow-query { any; };\r\n\r\n};\r\n};\r\n\r\nview \"other\" {\r\n  match-clients { any; };\r\nzone \"guiguiabloc.fr\" {\r\n       type master;\r\n       file \"\/etc\/bind\/guiguiabloc.fr\";\r\n       allow-transfer { ipdnsslave; };\r\n       allow-query { any; };\r\n};<\/pre>\n<p>Et voila, on relance BIND et vous devriez d\u00e9sormais avoir une r\u00e9ponse diff\u00e9rente suivant le pays depuis lequel vous \u00eates (ou pas :p)<\/p>\n<p>En esp\u00e9rant que cela vous soit utile \ud83d\ude00<\/p>\n<p>Tout cela bien expliqu\u00e9 dans la langue de <a title=\"http:\/\/fr.wikipedia.org\/wiki\/William_Shakespeare\" href=\"http:\/\/fr.wikipedia.org\/wiki\/William_Shakespeare\" target=\"_blank\">Shakespeare<\/a> (ou de <a title=\"http:\/\/fr.wikipedia.org\/wiki\/John_C._Holmes\" href=\"http:\/\/fr.wikipedia.org\/wiki\/John_C._Holmes\" target=\"_blank\">John Holmes<\/a>, suivant vos centres d&rsquo;int\u00e9r\u00eats&#8230; ) :<\/p>\n<p><a title=\"http:\/\/phix.me\/geodns\/ \" href=\"http:\/\/phix.me\/geodns\/ \" target=\"_blank\">http:\/\/phix.me\/geodns\/ <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Un petit billet rapide suite \u00e0 la r\u00e9ception en Alpha test d&rsquo;un serveur OVH au Canada \ud83d\ude42 Alors voil\u00e0, vous avez votre serveur en Europe et un autre aux USA (je simplifie, hein, ca peut \u00eatre votre ferme de serveurs &hellip; <a href=\"http:\/\/blog.guiguiabloc.fr\/index.php\/2012\/03\/31\/geolocalisation-par-dns-avec-bind\/\">Read More <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7,3],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/posts\/1158"}],"collection":[{"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/comments?post=1158"}],"version-history":[{"count":3,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/posts\/1158\/revisions"}],"predecessor-version":[{"id":1162,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/posts\/1158\/revisions\/1162"}],"wp:attachment":[{"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/media?parent=1158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/categories?post=1158"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.guiguiabloc.fr\/index.php\/wp-json\/wp\/v2\/tags?post=1158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}