-
Notifications
You must be signed in to change notification settings - Fork 0
/
padlock_TCP_server_V2.4.py
245 lines (240 loc) · 6.38 KB
/
padlock_TCP_server_V2.4.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
from threading import Thread
import time
import socket
from multiprocessing.pool import ThreadPool
import sys
from command_extractor import *
from Send_device_data import *
import struct
global plat_M_SN
sv=sys.argv[0]
sv=sv[sv.find('_')+1:]
sv=sv[:sv.find('.py')]
pool = ThreadPool(processes=1)
timeout=30
def handle_client(c, addr):
global plat_M_SN
print ('connection from : '+addr[0])
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[+] get connection from "+addr[0]+" Server version "+sv)
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\t\n")
f.close()
st=time.time()
while True and time.time()-st<timeout:
l=[]
Ll=[]
n=0
st2=time.time()
while n<2 and time.time()-st2<20:
lll=c.recv(1)
if len(lll)==0:
return
val=int.from_bytes(lll,byteorder="big")
#Ll+=lll
ll=val#ord(lll)
#print('{:02x}'.format(ll),end=' ')
if ll==0x7e:
n+=1
l.append(ll)
Ll.append(hex(ll)[2:])
#print()
#print(' '.join(Ll))
if n<2:continue
#print(l)
log=time.strftime("command_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[!] get command from "+addr[0])
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\t\n")
for i in l:
f.write('{:02x} '.format(i))
f.write("\t\n")
f.close()
#l=ut8_hex(l)
l2=list(map(lambda x:hex(x)[2:].rjust(2,'0'),l))
#print(l)
if (check_command(l)):
h,d=get_data(l)
if not(h):
return
if h['Terminal_id'] not in sr_list:
sr_list.append(h['Terminal_id'])
R=0
for hi in range(h['number_message']):
#print("send data")
#print()
#print(d[hi]['Terminal_ID'])
r=send_data(d[hi],'http://www.monitor-rwc.ml//post.php')
print("response : {}".format(r))
R=R|int(r[-1])
print(d[0])
print(sr_list)
#print(R)
m=[0x80,0x01,0x00,0x05,]+h['Terminal_id']+[plat_M_SN//256,plat_M_SN%256]+h['message_sn']+h['message_id']
code=0
for i in m:
code^=i
M=[0x7e]+m+[int(r[-1])]+[code]+[0x7e]
M2=list(map(lambda x:hex(x)[2:].rjust(2,'0'),M))
print(' '.join(M2))
print('--------------------------------')
for ik in M:
#MM+=chr(ik)
val = bytes([ik])
c.send(val)
#c.sendall(MM.encode('UTF-8'))
plat_M_SN=(plat_M_SN+1)%65536
print('test Terminal Query')
#m=[0x03,0x10,0x00,0x04,]+h['Terminal_id']+[plat_M_SN//256,plat_M_SN%256]+[0x01,0x24,0x01,0x00]
m=[3, 18, 0, 49]+h['Terminal_id']+[plat_M_SN//256,plat_M_SN%256]+[48, 36, 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, 38, 39, 40, 41, 42, 43, 51, 53, 54, 55, 58, 59]
code=0
for i in m:
code^=i
M=[0x7e]+m+[code]+[0x7e]
print(M)
time.sleep(10)
M2=list(map(lambda x:hex(x)[2:].rjust(2,'0'),M))
print(M2)
for ik in M:
#MM+=chr(ik)
val = bytes([ik])
c.send(val)
plat_M_SN=(plat_M_SN+1)%65536
print("get_command back from :",addr[0])
l=[]
Ll=[]
n=0
st2=time.time()
while n<2 and time.time()-st2<20:
lll=c.recv(1)
if len(lll)==0:
return
val=int.from_bytes(lll,byteorder="big")
#Ll+=lll
ll=val#ord(lll)
#print('{:02x}'.format(ll),end=' ')
if ll==0x7e:
n+=1
l.append(ll)
Ll.append(hex(ll)[2:])
print()
print(' '.join(Ll))
elif Ll==['7e', '6c', '5a', '31', '32', '33', '46', '67', '66', '31', '32', '6e', '6e', '7a', '6f', '65', '31', '32', '34', '40', '7a', '65', '72', '21', '7e']:
print("shut_down_request")
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[!] shut down request "+addr[0])
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\t\n")
f.close()
m='YES_Shut_down'
for mi in m:
val = bytes([ord(mi)])
c.send(val)
#c.send('YES_Shut_down'.encode('UTF-8'))
return "Shut"
else:
m='~ERROR~'
for mi in m:
val = bytes([mi])
c.send(val)
time.sleep(1)
for ci in c_list:
if ci==c:
ci[0].close()
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[-] close connection timeout "+addr[0])
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\t\n")
f.close()
def main(s):
host = socket.gethostbyname(socket.gethostname()) # Get local machine name
port = 5132 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
global plat_M_SN
plat_M_SN=0
print("Server "+sv+" started : "+host)
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("[+] Server "+sv+" started : "+host)
f.write("\t\n")
f.write("\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\n")
f.close()
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept()
c_list.append((c,addr))
print("client connected")
for i in c_list:
print(i[1])
print("________________")
try:
mn=pool.apply_async(handle_client,(c, addr))
#mn=Thread(target=handle_client,args=(c, addr,)).start()
if mn.get()=='Shut':
return "Shut"
except Exception as e:
print(e,"main error")
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[!] connection problem client "+addr[0])
f.write("\t\n")
f.write("\t\t"+str(e))
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\n")
f.close()
for ci in c_list:
if ci==c:
ci[0].close()
s.close() # Close the connection
def timing():
time.sleep(60)
#soc.close()
sys.exit()
if __name__=='__main__':
#Thread(target=timing).start()
while True:
try:
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create a socket object
#soc.settimeout(1)
c_list=[]
sr_list=[]
r=main(soc)
if r=='Shut':
print("shut_dows_server")
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[-] shut down server")
f.write("\t\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\t\n")
for ci in c_list:
print("close connection "+ci[1][0])
f.write("\t\t[-] close connection "+ci[1][0])
f.write("\t\n")
ci[0].close()
f.close()
soc.close()
break
except Exception as e:
print(e,"socket error")
log=time.strftime("log_%d_%h_%Y.txt")
f=open(log,'a')
f.write("\t[!] connection problem server")
f.write("\n")
f.write("\t\t"+str(e))
f.write("\n")
f.write("\t\t"+time.strftime("%H:%M:%S - %d/%h/%Y"))
f.write("\n")
f.close()
soc.close()
time.sleep(20)