-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathfoghornd.tac
executable file
·87 lines (68 loc) · 2.53 KB
/
foghornd.tac
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
# You can run this .tac file directly with:
# twistd -ny service.tac
"""
This .tac file starts the foghorn DNS proxying daemon
according to the settings in foghornd/settings.json
"""
import netifaces
from twisted.application import service, internet
from twisted.names import dns, cache
from twisted.web import server as webserver
from foghornd import Foghorn, FoghornSettings, Cache
from foghornd.foghornrpc import FoghornXMLRPC
from foghornd.foghorndnsserverfactory import FoghornDNSServerFactory
class Main(object):
"""Load settings and start the server"""
foghorn = None
settings = None
def __init__(self):
self.settings = FoghornSettings()
self.foghorn = Foghorn(self.settings)
self.foghornrpc = FoghornXMLRPC()
self.foghornrpc.foghorn = self.foghorn
self.foghornrpc.allowNone = 1
def foghord_service():
"""Return a service suitable for creating an application object."""
# create a resource to serve static files
foghorn = Main()
servers = []
fcache = Cache.Cache()
fcache.foghorn = foghorn.foghorn
factory = FoghornDNSServerFactory(
caches=[fcache],
clients=[foghorn.foghorn]
)
factory.noisy = False
addresses = []
try:
addresses = foghorn.settings.listen
except AttributeError:
pass
if not addresses:
for iface in netifaces.interfaces():
try:
for addr in netifaces.ifaddresses(iface)[netifaces.AF_INET]:
addresses.append(addr["addr"])
except KeyError:
# No address for this interface
pass
for listen in addresses:
udp_protocol = dns.DNSDatagramProtocol(controller=factory)
udp_protocol.noisy = False
udp_server = internet.UDPServer(foghorn.settings.dns_port, udp_protocol, interface=listen)
udp_server.noisy = False
servers.append(udp_server)
tcp_server = internet.TCPServer(foghorn.settings.dns_port, factory, interface=listen)
servers.append(tcp_server)
#xml_server = internet.TCPServer(7080, webserver.Site(foghorn.foghornrpc))
rpcsite = webserver.Site(foghorn.foghornrpc)
rpcsite.noisy = False
xml_server = internet.UNIXServer("foghornd.sock", rpcsite)
servers.append(xml_server)
return servers
# This is the required part of the .tac file to start
# the service as a daemon
application = service.Application("foghorn")
# attach the service to its parent application
for item in foghord_service():
item.setServiceParent(application)