From 023dd3b74cf659fda61a9ad02c20d340253dad2c Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 28 Jun 2014 10:56:26 +0200
Subject: [PATCH] resolve shard names to IPs

---
 authserv/app_nginx.py | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/authserv/app_nginx.py b/authserv/app_nginx.py
index 0286a7a..9661823 100644
--- a/authserv/app_nginx.py
+++ b/authserv/app_nginx.py
@@ -1,3 +1,5 @@
+import socket
+import threading
 from flask import Flask, request, abort, make_response
 from authserv.app_common import do_auth
 
@@ -5,6 +7,16 @@ app = Flask(__name__)
 
 _default_port_map = {'imap': 143, 'pop3': 110}
 
+_dns_cache = {}
+_dns_cache_lock = threading.Lock()
+
+def _shard_to_ip(shard):
+    hostname = '%s-vpn' % shard
+    with _dns_cache_lock:
+        if hostname not in _dns_cache:
+            _dns_cache[hostname] = socket.gethostbyname(hostname)
+        return _dns_cache[hostname]
+
 
 @app.route('/', methods=('GET',))
 def do_nginx_http_auth():
@@ -32,7 +44,7 @@ def do_nginx_http_auth():
     response = make_response('')
     if auth_status == 'OK':
         response.headers['Auth-Status'] = 'OK'
-        response.headers['Auth-Server'] = shard
+        response.headers['Auth-Server'] = _shard_to_ip(shard)
         response.headers['Auth-Port'] = str(
             app.config.get('NGINX_AUTH_PORT_MAP', _default_port_map)[protocol])
     else:
-- 
GitLab