summaryrefslogtreecommitdiffstats
path: root/server.py
diff options
context:
space:
mode:
Diffstat (limited to 'server.py')
-rw-r--r--server.py36
1 files changed, 35 insertions, 1 deletions
diff --git a/server.py b/server.py
index ae7a921..45bc2fc 100644
--- a/server.py
+++ b/server.py
@@ -1,4 +1,4 @@
-import re, struct, socket, select, traceback
+import re, struct, socket, select, traceback, time
if not globals().get('skip_imports'):
import ssnet, helpers, hostwatch
import compat.ssubprocess as ssubprocess
@@ -106,6 +106,25 @@ class Hostwatch:
self.sock = None
+class DnsProxy(Handler):
+ def __init__(self, mux, chan, request):
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
+ Handler.__init__(self, [sock])
+ self.sock = sock
+ self.timeout = time.time()+30
+ self.mux = mux
+ self.chan = chan
+ self.sock.setsockopt(socket.SOL_IP, socket.IP_TTL, 42)
+ self.sock.connect((resolvconf_random_nameserver(), 53))
+ self.sock.send(request)
+
+ def callback(self):
+ data = self.sock.recv(4096)
+ debug2('DNS response: %d bytes\n' % len(data))
+ self.mux.send(self.chan, ssnet.CMD_DNS_RESPONSE, data)
+ self.ok = False
+
+
def main():
if helpers.verbose >= 1:
helpers.logprefix = ' s: '
@@ -165,6 +184,14 @@ def main():
handlers.append(Proxy(MuxWrapper(mux, channel), outwrap))
mux.new_channel = new_channel
+ dnshandlers = {}
+ def dns_req(channel, data):
+ debug2('Incoming DNS request.\n')
+ h = DnsProxy(mux, channel, data)
+ handlers.append(h)
+ dnshandlers[channel] = h
+ mux.got_dns_req = dns_req
+
while mux.ok:
if hw.pid:
assert(hw.pid > 0)
@@ -176,3 +203,10 @@ def main():
if latency_control:
mux.check_fullness()
mux.callback()
+
+ if dnshandlers:
+ now = time.time()
+ for channel,h in dnshandlers.items():
+ if h.timeout < now or not h.ok:
+ del dnshandlers[channel]
+ h.ok = False