-
Notifications
You must be signed in to change notification settings - Fork 2
/
chininux-server
executable file
·90 lines (84 loc) · 3.38 KB
/
chininux-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env python2
#
# Copyright 2014 Claudio Pisa (clauz at ninux dot org)
#
# This file is part of chininux
#
# chininux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# chininux is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with chininux. If not, see <http://www.gnu.org/licenses/>.
#
from chininux import *
import settings
import threading
from SocketServer import BaseRequestHandler, ThreadingTCPServer
import socket
import time
import sys
class AddressRetriever(threading.Thread):
def __init__(self, refreshInterval=3600):
self.alock = threading.Lock()
# update one AddressDirectory while using the other
self.adirs = [AddressDirectory(settings.GIURLS), AddressDirectory(settings.GIURLS)]
self.currentadir = 0
self.refreshInterval = refreshInterval
self.ready = False
threading.Thread.__init__(self)
def run(self):
# refresh the records every refreshInterval seconds
while True:
# update one AddressDirectory while using the other
nextcurrentadir = (self.currentadir + 1) % 2
self.adirs[nextcurrentadir].refresh()
print "fresh!"
# switch the current AddressDirectory
self.alock.acquire()
self.currentadir = nextcurrentadir
self.ready = True
self.alock.release()
time.sleep(self.refreshInterval)
def search(self, query):
self.alock.acquire()
if self.ready:
r = self.adirs[self.currentadir].search(query)
else:
r = ["% Sorry, the whois server is not ready yet :(\n% Please try again in a few seconds."]
self.alock.release()
return r
class WhoisRequestHandler(BaseRequestHandler):
def handle(self):
query = self.request.recv(4096)
response = "\n".join([str(r) for r in self.server.ar.search(query)])
self.request.sendall(response)
class WhoisServer(ThreadingTCPServer):
address_family = socket.AF_INET6
def __init__(self, bindPort=43, bindAddress="::", refreshInterval = 3600, allow_reuse_address = True):
self.ar = AddressRetriever(refreshInterval=refreshInterval)
self.ar.daemon = True
self.ar.start()
ThreadingTCPServer.__init__(self, (bindAddress, bindPort), WhoisRequestHandler)
self.allow_reuse_address = allow_reuse_address
def server_bind(self):
# dual-stack IPv4-IPv6
self.socket.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
# from SocketServer.py
if self.allow_reuse_address:
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind(self.server_address)
self.server_address = self.socket.getsockname()
if __name__ == "__main__":
try:
whoisServer = WhoisServer(settings.bindPort, settings.bindAddress, settings.refreshInterval)
except Exception, e:
print e
sys.exit(1)
whoisServer.serve_forever()