-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdnshttp.py
159 lines (132 loc) · 4.84 KB
/
dnshttp.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import cgi
import json
import logging
import subprocess
import socket
server_ip = socket.gethostbyname(socket.gethostname())
port_number = 8088
logfile = '/var/log/dnshttp.log'
formatter = logging.Formatter(
'%(asctime)s [%(levelname)s] %(module)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
fh = logging.FileHandler(logfile)
fh.setFormatter(formatter)
logging.getLogger().addHandler(fh)
logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger(__name__)
def shell_cmd(cmd):
p = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = p.communicate()
return p.returncode, out.rstrip(), err.rstrip()
class DNSArecord():
def __init__(self):
self.dns_file = '/etc/dnsmasq.d/hadoop'
self.dns_records_host = {}
self.dns_records_ip = {}
self.get_all_dns()
def get_all_dns(self):
with open(self.dns_file, 'r') as f:
content = f.readlines()
for item in content:
other, host, ip = item.split('/')
ip = ip.strip()
self.dns_records_host.update({str(host): str(ip)})
self.dns_records_ip.update({str(ip): str(host)})
return self.dns_records_host
def add_host(self, record):
host = record.get('host', None)
ip = record.get('ip', None)
if host is None or host in self.dns_records_host:
return
self.dns_records_host.update({str(host): str(ip)})
self.dns_records_ip.update({str(ip): str(host)})
self.write_dns_file()
def del_host(self, record):
host = record.get('host', None)
ip = record.get('ip', None)
if host is None or host not in self.dns_records_host:
return
self.dns_records_host.pop(str(host), None)
self.dns_records_ip.pop(str(ip), None)
self.write_dns_file()
def write_dns_file(self):
address = []
for host, ip in self.dns_records_host.iteritems():
address.append('address=/' + str(host) + '/' + str(ip))
with open(self.dns_file, 'w+') as f:
f.write('\n'.join(address))
dns_a_record = DNSArecord()
class Myhander(BaseHTTPRequestHandler):
cmd = 'systemctl restart dnsmasq'
def do_GET(self):
message = {
'code': 200,
'message': 'thanks',
'data': dns_a_record.get_all_dns()
}
self.send_header('Content-Type', 'application/json')
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(message))
return
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
post_data = {}
for field in form.keys():
post_data.update({field: form[field].value})
logger.info(self.client_address)
logger.info(json.dumps(post_data))
message = {'code': 200, 'message': 'sucess', 'data': ''}
try:
dns_a_record.add_host(post_data)
returncode, out, err = shell_cmd(self.cmd)
logger.info('{0} returncode {1} , out {2} , err {3}'.format(
self.cmd, returncode, out, err))
except Exception as e:
message = {'code': 500, 'message': str(e), 'data': ''}
logger.error(str(e))
self.send_header('Content-Type', 'application/json')
self.send_response(200)
self.end_headers()
self.wfile.write(message)
return
def do_DELETE(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
post_data = {}
for field in form.keys():
post_data.update({field: form[field].value})
logger.info(self.client_address)
logger.info(json.dumps(post_data))
message = {'code': 200, 'message': 'sucess', 'data': ''}
try:
dns_a_record.del_host(post_data)
returncode, out, err = shell_cmd(self.cmd)
logger.info('{0} returncode {1} , out {2} , err {3}'.format(
self.cmd, returncode, out, err))
except Exception as e:
message = {'code': 500, 'message': str(e), 'data': ''}
logger.error(str(e))
self.send_header('Content-Type', 'application/json')
self.send_response(200)
self.end_headers()
self.wfile.write(message)
return
try:
server = HTTPServer((server_ip, port_number), Myhander)
server.serve_forever()
except Exception as e:
logger.error(str(e))