-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzeerover-pipe.py
executable file
·141 lines (111 loc) · 3.55 KB
/
zeerover-pipe.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python
import socket
import select
import sys
import os
import fileinput
import threading
import traceback
import resolver
import json
import logging
import logging.handlers
DATA_TEMPLATE = 'DATA\t%s\t%s\t%s\t%s\t%s\t%s\n'
TTL = '5'
HOSTNAME_BASE = None
#sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
def read_conffile(filename):
try:
with open(filename,'r') as fh:
conf = json.load(fh)
return conf
except Exception, e:
raise Exception('Error reading config file: %s' % filename, e)
def process(data, resolver):
line = data.strip()
if line == 'HELO\t1' or line == 'HELO\t2' or line == 'HELO\t3':
logger.info('Got %s. Responding OK.' % line)
sys.stdout.write('OK\t\n')
sys.stdout.flush()
return
chunks = line.split('\t')
qname = chunks[1]
qclass = chunks[2]
qtype = chunks[3]
qid = chunks[4]
remote_ip = chunks[5]
local_ip = None
edns_subnet = None
if len(chunks) >= 7: #at least atlas pipe protocol version 2
local_ip = chunks[6]
if len(chunks) == 8:
edns_subnet = chunks[7]
"""
Generate response and respond
"""
hostname = qname.lower()
if hostname[-1] == '.':
hostname = hostname[:-1]
if hostname.endswith(HOSTNAME_BASE):
if qtype == 'ANY' or qtype == 'A' or qtype == 'AAAA':
dest = resolver.resolve(hostname)
if not is_ip(dest):
qtype = 'CNAME'
else:
qtype = 'A'
response = DATA_TEMPLATE % (qname, qclass, qtype, TTL, qid, dest)
sys.stdout.write(response)
if qtype == 'ANY' or qtype == 'SOA':
response = DATA_TEMPLATE % (qname, qclass, 'SOA', TTL, qid, 'ns1.m.ripeatlasdns.net\troot.ripeatlasdns.net\t2008080300\t1800\t3600\t604800\t3600')
sys.stdout.write(response)
else:
logger.error('FAIL for %s' % line)
sys.stdout.write('FAIL\n')
sys.stdout.flush()
return
sys.stdout.write('END\n')
sys.stdout.flush()
def is_ip(dest_str):
try:
socket.inet_aton(dest_str)
return True
except socket.error:
#try IPv6
try:
socket.inet_pton(socket.AF_INET6, dest_str)
return True
except socket.error:
return False
if __name__ == '__main__':
if len(sys.argv) != 2:
sys.stderr.write('usage: config.json\n')
sys.exit(1)
loglevel = logging.DEBUG
logger = logging.getLogger()
logger.setLevel(loglevel)
formatter = logging.Formatter('%(process)d %(levelname)s %(message)s')
handler = logging.handlers.SysLogHandler(address = '/dev/log')
handler.setLevel(loglevel)
handler.setFormatter(formatter)
logger.addHandler(handler)
config = read_conffile(sys.argv[1])
logger.debug('Finished reading configuration\n')
#logging_config = config['logging']
#dictConfig(logging_config)
#socket_file = config['socket-file']
HOSTNAME_BASE = config['hostname-base']
resolver = resolver.Resolver(config)
try:
while True:
line = sys.stdin.readline()
logger.debug('Got line %s' % line)
try:
process(line, resolver)
except:
#traceback.print_exc(file=sys.stderr)
logger.error(traceback.format_exc())
break
except:
#traceback.print_exc(file=sys.stderr)
logger.error(traceback.format_exc())
logger.info('Exiting')