/
i_udp.py
151 lines (133 loc) · 5.65 KB
/
i_udp.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
#!/usr/bin/env python3
#/***************************************************************************//**
# @file i_udp.py
#
# @author Black-Blade
# @brief i_udp.py
# @date 13.01.2021
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see https://tools.ietf.org/html/rfc1035
#*******************************************************************************/
import socket
from socket import AF_INET, SOCK_STREAM, SO_REUSEADDR, SOL_SOCKET, SHUT_RDWR
from _thread import start_new_thread
# IMPORT MY STANDRT CLASS
from log import logging
from config import Config
if __name__ == "__main__":
quit()
class Input_UDP:
#/*******************************************************************************
# @author Black-Blade
# @brief Constructor of Input_UDP
# @date 10.03.2021
# @param switch(pointer),geoip(pointer),[UDPSERVER(String),UDPPORT(INT)]
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see
# *******************************************************************************/
def __init__(self,switch,geoip,udpserver=None):
logging.debug ("")
self._switch =switch
self._geoip=geoip
if udpserver is None:
self._listen_addr = Config.I_DOTSERVER
self._listen_port = Config.I_UDPPORT
else:
server,port =udpserver
self._listen_addr = server
self._listen_port = port
self._buffersize =1024
self._conterrequests=0
self._conterrequest=0
self._countererror =0
#/*******************************************************************************
# @author Black-Blade
# @brief Deconstructor of Input_UDP
# @date 06.03.2021
# @param
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see
# *******************************************************************************/
def __del__(self):
logging.debug ("")
#/*******************************************************************************
# @author Black-Blade
# @brief Init of Input_UDP
# @date 06.03.2021
# @param
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see
# *******************************************************************************/
def init(self):
logging.debug ("")
start_new_thread(self._init_thread,())
#/*******************************************************************************
# @author Black-Blade
# @brief Init the thread of Input_UDP
# @date 06.03.2021
# @param
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see https://tools.ietf.org/html/rfc1035
# *******************************************************************************/
def _init_thread(self):
logging.debug ("")
with socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) as sock:
sock.bind((self._listen_addr, self._listen_port))
logging.info ("UDP input start from :"+str( self._listen_addr)+":"+str( self._listen_port))
while True:
try:
msg,conn = sock.recvfrom(self._buffersize)
start_new_thread(self._decoder_thread,(sock,conn, msg))
except OSError as err:
logging.error("OS error: {0}".format(err))
#/*******************************************************************************
# @author Black-Blade
# @brief Read the DNS rquest of extern
# @date 10.03.2021
# @param conn,addr
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see https://tools.ietf.org/html/rfc1035
# *******************************************************************************/
def _decoder_thread(self,sock,conn, txdata):
try:
logging.debug ("")
host, port = conn
if self._geoip is None:
ok= True
text= "NO GEOIP"
else:
ok,text = self._geoip(host)
if ok == True:
self._conterrequests=self._conterrequests+1
self._conterrequest=self._conterrequest+1
data =self._switch(txdata)
if data is not None:
isblock,rxdata,blockname,dname = data
sock.sendto(rxdata , conn)
logging.info("IP : "+str(host)+":"+str(port)+" :"+ text)
if isblock==True:
logging.info("Domain : "+str(dname)+ " is block true : blockname " + str(blockname))
else:
logging.info("Domain : "+str(dname)+ " is block false ")
else:
logging.info("IP : "+str(host)+": "+ text)
except OSError as err:
logging.error("OS error: {0}".format(err))
self._countererror=self._countererror +1
self._conterrequest=self._conterrequest-1
#/*******************************************************************************
# @author Black-Blade
# @brief Logs REQUESTS
# @date 08.03.2021
# @param
# @return
# @version 0.0.1 Doxygen style eingebaut und erstellen dieser File
# @see
# *******************************************************************************/
def logs(self):
logging.info("REQUEST : "+str(self._conterrequest) +": REQUESTS : "+str(self._conterrequests)+": ERRORS : "+str(self._countererror))