forked from mhco0/PyCRP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdns.py
More file actions
49 lines (38 loc) · 1.07 KB
/
dns.py
File metadata and controls
49 lines (38 loc) · 1.07 KB
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
import socket
import sys
from get_ip import *
server_mapping = {}
def get_command(data):
return data.decode().split(' ',1)
pass
def add_server(server_alias,ip_address):
server_mapping[server_alias] = ip_address
pass
def find_server(server_alias):
if server_alias in server_mapping:
return server_mapping[server_alias]
else:
return "NOT FOUND"
pass
def main():
dnsHost = get_ip()
dnsPort = 8080
dnsSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dnsSocket.bind((dnsHost,dnsPort))
print ("DNS STARTED")
print ("\tDNS PORT: {}".format(dnsPort))
print (get_ip())
while True:
data, client_address = dnsSocket.recvfrom(1024)
print ("\tDATA: {}".format(get_command(data)))
command, server_alias = get_command(data)
print("Command:", command)
if command == 'ADD':
add_server(server_alias,client_address[0])
print (server_mapping)
elif command == 'FIND':
result = find_server(server_alias)
print ("server found: ip_address is {}".format(result))
dnsSocket.sendto(str(result).encode(), client_address)
if __name__ == "__main__" :
main()