forked from pR0Ps/miniboa-py3
/
udp2telnet.py
470 lines (444 loc) · 17.3 KB
/
udp2telnet.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
import threading
import datetime,time
import re
from miniboa import TelnetServer, colorize
import socket
import WSJTXClass
import logging
threads = {}
service = {
"1":"spot_server",
"2":"wsjtx_udp",
"3":"pydecoder_udp",
}
IDLE_TIMEOUT = 3600
CLIENT_LIST = []
DecoderArray = {}
ADMIN_PWD = "xxxxxx" #do not use "'=, in password, !!!!Character case insensitivity!!!
def pydecoder_udp(my_thread) :
"""
receive udp packet from my ft8-deocder
each message format is : 150430 -6 1.1 2724 ~ CQ BD4QA OM91
Identify each station is send a line before any message : Start Decode
"""
try:
mSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
mSocket.bind(("",5556))
except Exception as e:
logging.error("bind 5556 error {}".format(e))
else :
logging.debug("Listening for Plante UDP ({}) log on port {}. CTRL-C to break.".format(my_thread, "5556"))
try:
while True:
if my_thread not in SERVER_RUN :
logging.info("Restart pydecoder_udp thread by %s" % SERVER_RUN)
return
fileContent, (remoteHost, remotePort) = mSocket.recvfrom(10240)
DecoderStation = ("{}:{}:UDP".format(remoteHost, remotePort))
for i in fileContent.decode("utf-8").split("\n") :
if len(i) > 0:
decode_2(i, DecoderStation)
except Exception as e :
logging.info("UDP receive error (%s) %s" % (e, fileContent))
def decode_2(msg, Station):
if Station not in DecoderArray :
DecoderArray.update({Station : ""} )
logging.debug("New Station %s" % Station)
try:
if msg[:11] == "<parameters" :
msg = adif_spot(msg)
elif msg[:12] == "Start Decode" :
msg = msg.upper()
if DecoderArray[Station].split("@")[0] != msg.replace("START DECODE ", "") :
DecoderArray[Station] = msg.replace("START DECODE ", "") + "@" + datetime.datetime.now().strftime('%Y/%m/%d-%H:%M:%S')
for station in list(DecoderArray.keys()) :
if DecoderArray[station].split("@")[0] == msg.replace("START DECODE ", "") :
if station != Station :
del DecoderArray[station]
elif msg[:16] == "Redpitaya Decode" :
msg = msg.upper()
if DecoderArray[Station].split("@")[0] != msg.replace("REDPITAYA DECODE ", "") :
DecoderArray[Station] = msg.replace("REDPITAYA DECODE ", "") + "@" + datetime.datetime.now().strftime('%Y/%m/%d-%H:%M:%S')
for i in list(DecoderArray.keys()) :
if DecoderArray[i].split("@")[0] == msg.replace("REDPITAYA DECODE ", "") :
if i != Station :
del DecoderArray[i]
elif msg[:10] == "End Decode" :
return
else :
if DecoderArray[Station].split("@")[0][0:2] == "0-" :
rp_freq = msg[30:38]
msg = (msg.replace(rp_freq + " ", "") + " ")[0:51]
broadcast(msg + DecoderArray[Station].split("@")[0].replace("0-", rp_freq + "-") + '\n')
else :
broadcast(msg[:51] + DecoderArray[Station].split("@")[0] + '\n')
except :
logging.error("UDP packet error (%s) : %s" % (DecoderArray[Station],msg))
return
def adif_spot(adif_text): #No used function at this time
adif_data = {}
resutl = ""
"""
#https://github.com/ctjacobs/pyqso/blob/master/pyqso/adif.py
#https://n1mmwp.hamdocs.com/appendices/external-udp-broadcasts/
p1 = re.compile("(\<parameters\:[0-9]*\>)")
adif_text = re.sub(p1, "", adif_text)
p2 = re.compile("<(.*?):(\d*).*?>([^<]+)")
for item in p2.findall(adif_text) :
adif_data[item[0]]=item[2][:int(item[1])]
try:
Spot = adif_data['STATION_CALLSIGN'].upper() + ":" + " "
Freq = " " + str(float(adif_data['Freq'])*1000) + " "
Call = adif_data['Call'].upper() + " "
Note = adif_data['NOTES'] + " "
Time = adif_data['TIME_ON'][:4] + "Z "
Mode = adif_data['Mode'].upper() + " "
result = 'DX de ' + Spot[:10] + Freq[:-9] + Call[:10] + Note[:31] + Time[:6] + Mode[:5]
except :
result = ""
print("adif data error %s" % adif_data)
"""
return result
def wsjtx_udp(my_thread):
"""
receive message from JTDX or WSJTX program UDP packet
"""
try:
mSocket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
mSocket.bind(("",5555))
except Exception as e:
logging.error("bind 5555 error {}".format(e))
logging.debug("Listening for WSTJ-X UDP ({}) log on port {}. CTRL-C to break.".format(my_thread, "5555"))
while True:
if my_thread not in SERVER_RUN:
logging.info("Restart wsjtx_udp thread by %s" % SERVER_RUN)
return
fileContent, (remoteHost, remotePort) = mSocket.recvfrom(1024)
DecoderStation = ("{}:{}:JTDX".format(remoteHost, remotePort))
try:
# logging.info("{} send UDP '{}'".format(remoteHost, fileContent))
NewPacket = WSJTXClass.WSJTX_Packet(fileContent, 0)
NewPacket.Decode()
if NewPacket.PacketType == 0:
HeartbeatPacket = WSJTXClass.WSJTX_Heartbeat(fileContent, NewPacket.index)
HeartbeatPacket.Decode()
dataDecode = ("{} {} {}".format(HeartbeatPacket.MaximumSchema, HeartbeatPacket.Version ,HeartbeatPacket.Revision))
try:
logging.debug("Heartbeat : {} @ {}".format(dataDecode, DecoderArray[DecoderStation]))
except :
logging.debug("Heartbeat : {} @ ".format(dataDecode))
elif NewPacket.PacketType == 1:
StatusPacket = WSJTXClass.WSJTX_Status(fileContent, NewPacket.index)
StatusPacket.Decode()
dataDecode = ("{:08}-{}-{}".format(StatusPacket.Frequency, StatusPacket.Mode, StatusPacket.DECall))
#StatusPacket.Decoding
if DecoderStation not in DecoderArray :
DecoderArray.update({DecoderStation : ""} )
logging.debug("New Station %s" % DecoderStation)
if StatusPacket.Decoding and DecoderArray[DecoderStation].split("@")[0] != dataDecode:
DecoderArray[DecoderStation] = dataDecode + "@" + datetime.datetime.now().strftime('%Y/%m/%d-%H:%M:%S')
for station in list(DecoderArray.keys()) :
if DecoderArray[station].split("@")[0] == dataDecode :
if station != DecoderStation :
del DecoderArray[station]
logging.debug("Status : {} {} @ {}".format(dataDecode, StatusPacket.Decoding, DecoderArray[DecoderStation]))
# broadcast("Status : {} {} @ {}\n".format(dataDecode, StatusPacket.Decoding, DecoderArray[DecoderStation]))
elif NewPacket.PacketType == 2:
DecodePacket = WSJTXClass.WSJTX_Decode(fileContent, NewPacket.index)
DecodePacket.Decode()
# can use PyQt4.QtCore.QTime for this as well!
s = int( (DecodePacket.Time/1000) % 60 )
m = int( ((DecodePacket.Time/(1000*60) ) %60 ) )
h = int( ((DecodePacket.Time/(1000*60*60)) %24))
pkmsg = (DecodePacket.Message + ' ')[:26]
dataDecode = ("{:02}:{:02}:{:02} {:>3} {:4.1f} {:>4} {} {}".format(h,m,s,DecodePacket.snr,DecodePacket.DeltaTime,DecodePacket.DeltaFrequency,DecodePacket.Mode,pkmsg))
try :
logging.debug("Decode : {} @ {}".format(dataDecode, DecoderArray[DecoderStation]))
except :
logging.info("Decode : {} @".format(dataDecode))
# self.DecodeCount += 1
# now we need to send it to the UI
try:
msg = dataDecode + DecoderArray[DecoderStation].split("@")[0] + '\n'
except :
msg = dataDecode + ' @ ' + DecoderStation + '\n'
broadcast(msg)
elif NewPacket.PacketType == 3:
logging.debug("PacketType = 3")
elif NewPacket.PacketType == 5:
LoggedPacket = WSJTXClass.WSJTX_Logged(fileContent, NewPacket.index)
LoggedPacket.Decode()
logging.debug("LoggedPacket : {}".format(LoggedPacket))
except :
logging.error("WSJTX UDP error (%s): %s" % (DecoderArray[DecoderStation],fileContent))
def spot_server(my_thread):
"""
Create a telnet server with a port, address, a function to call with new connections
and one to call with lost connections.
"""
telnet_server = TelnetServer(
port=7300,
address='',
on_connect=on_connect,
on_disconnect=on_disconnect,
timeout = .05
)
logging.debug("Listening for SPOT ({}) connections on port {}. CTRL-C to break.".format(my_thread, telnet_server.port))
while 1:
if my_thread not in SERVER_RUN :
logging.info("Restart spot_server thread by %s" % SERVER_RUN)
return
telnet_server.poll()
kick_idle()
process_clients()
def on_connect(client):
"""
Sample on_connect function.
Handles new connections.
"""
logging.info("Opened connection to {}".format(client.addrport()))
#broadcast("{} joins the conversation.\n".format(client.addrport()))
CLIENT_LIST.append(client)
client.send("Welcome to the Spot Server, {}.\n".format(client.addrport()))
def on_disconnect(client):
"""
Sample on_disconnect function.
Handles lost connections.
"""
logging.info("Lost connection to {}".format(client.addrport()))
CLIENT_LIST.remove(client)
#broadcast("{} leaves the conversation.\n".format(client.addrport()))
client.send("73 de bd7nwr ft8 deocder server.")
def process_clients():
"""
Check each client, if client.cmd_ready == True then there is a line of
input available via client.get_command().
"""
for client in CLIENT_LIST:
if client.active :
# If the client sends input echo it to the chat room
if client.callsign == "" :
client.send("Please input your Callsign :\n")
client.callsign = "-"
elif client.filter == "" and client.callsign != "-" :
client.send("Please select band(Mhz) to monitor , example : fe=14\n")
client.filter = "-"
if client.cmd_ready :
chat(client)
def chat(client):
"""
Echo whatever client types to everyone.
"""
msg = client.get_command()
if msg is not None :
msg = msg.upper().replace(" ", "")
logging.info("{} says '{}'".format(client.addrport(), msg))
if len(msg.split())>0 :
command = msg.split("=")
if command[0] == "HELP" or command[0] == "?":
send_msg = "sa=ba7ib : filter only msg from station ba7ib\n"
send_msg += "fe=14 : filter only band is 14MHz\n"
send_msg += "ca=y : filter only callsign begin with y\n"
send_msg += "mo=ft8 : filter only mode is ft8\n"
send_msg += "cq : only cq call will display\n"
send_msg += "ck : check your filter\n"
send_msg += "nf : clear all filter\n"
send_msg += "nc : toggle color True/False\n"
send_msg += "stations : list all register report station\n"
send_msg += "clients : list all listen clients\n"
send_msg += "bye : disconnect\n"
client.send(colorize("^w" + send_msg))
elif command[0] in ['SA', 'FE', 'CA', "MO", "CQ", "DI"] :
if client.filter == "-" :
client.filter = msg.replace(" ", "")
else :
client.filter = client.filter + ", " + msg.replace(" ", "")
client.send(colorize("^w your filter is : {}\n".format( client.filter )))
elif command[0] == "NF" :
client.filter = ""
client.send(colorize("^wNo filters\n"))
elif command[0] == "NC" :
client.color = not client.color
client.send(colorize("^wSet Colorful %s\n" % client.color))
elif command[0] == "CK" :
client.send(colorize("^w{} filter is : {}\n".format( client.callsign, client.filter )))
elif command[0] == 'BYE': # bye = disconnect
client.active = False
elif command[0] == "THREADS" : # check threading status
send_msg = "Telnet Server Thread List :\n"
for i in threads :
send_msg += service[threads[i]] + "(" + threads[i] + ") is : " + str(i.isAlive()) + '\n'
client.send(colorize("^w" + send_msg))
elif command[0] == 'SHUTDONW': # shutdown == stop the server
if command[1] == ADMIN_PWD.upper() :
SERVER_RUN.append("STOP")
else :
client.send(colorize("^wAdmin Password Error\n".format(SERVER_RUN)))
elif command[0] == 'RESTART' : # restart == restart thread
if command[1] == ADMIN_PWD.upper() :
for i in command[2].split(",") :
if i in SERVER_RUN:
SERVER_RUN.remove(i)
client.send(colorize("^wwait 60s for {}\n".format(SERVER_RUN)))
else :
client.send(colorize("^wAdmin Password Error\n".format(SERVER_RUN)))
elif command[0] == 'CLIENTS' : # clients == list all clients
i = 0
send_msg = ""
for guest in CLIENT_LIST :
send_msg += "{} from {} filter {} ({}s)\n".format(guest.callsign, guest.addrport(), guest.filter, int(guest.duration()))
i += 1
send_msg += "Total clints : {}\n".format(i)
client.send(colorize("^w" + send_msg))
elif command[0] == 'KILL' : # kill == kick out client
if command[1] == ADMIN_PWD.upper() :
try:
for guest in CLIENT_LIST :
if guest.addrport() == command[2] :
guest.active = False
client.send("\n")
break
except :
logging.error("kickout command error %s" % msg)
else :
client.send(colorize("^wAdmin Password Error\n".format(SERVER_RUN)))
elif command[0] == 'STATIONS' : # stations == list all report stations
for station in list(DecoderArray.keys()) :
client.send(colorize("^w{} : {}\n".format(station, DecoderArray[station])))
if DecoderArray[station].strip() == "" :
del DecoderArray[station]
elif client.callsign == "" or client.callsign == "-" :
CallSign_reg = r'([A-Z]{1,2}|[0-9][A-Z])([0-9])([A-Z]{1,3})'
try:
if re.match(CallSign_reg, command[0] , re.I).span()[0] == 0 :
logging.info("{} logon the server".format(command[0]))
client.callsign = command[0]
client.send(colorize("^wWellcome {} logon @ {}\n".format(command[0], client.addrport())))
except :
client.send(colorize("^wCallsign Error. Please input again\n"))
else :
client.send(colorize("^wunknow command, try 'help' for all command\n"))
"""
for guest in CLIENT_LIST:
if guest != client:
#guest.send("{} says '{}'\n".format(client.addrport(), msg))
client.active = True
else:
guest.send("You say '{}'\n".format(msg))
"""
def kick_idle():
"""
Looks for idle clients and disconnects them by setting active to False.
"""
# Who hasn't been typing?
for client in CLIENT_LIST:
if client.idle() > IDLE_TIMEOUT and client.callsign == "-" :
logging.info("Kicking idle lobby client from {}".format(client.addrport()))
client.active = False
def calu_dis(tar_grid):
return int(pow((pow(abs(ord(tar_grid[:1])- ord("O")),2) + pow(abs(ord(tar_grid[1:2])-ord("L")),2)),0.5))
def broadcast(msg):
"""
Send msg to every client.
"""
colorful = ("^r", "^g", "^y", "^b", "^m", "^c", "^w")
detail_msg = msg.split()
msg_color=""
grid = detail_msg[-2]
DIS = 0
if re.match(r'^[a-z]{2}[0-9]{2}$', grid , re.I) and grid != "RR73" :
DIS = calu_dis(grid)
if detail_msg[5] == "CQ" :
msg_color = "^G"
if DIS > 7 :
msg_color = "^R"
elif DIS > 3 and DIS <= 7 :
msg_color = "^Y"
else :
char_sum = 0
for i in "-".join(detail_msg[-1].split("-")[2:]) :
char_sum += ord(i)
msg_color = colorful[int(char_sum % 7)]
for client in CLIENT_LIST:
if client.callsign != "" and client.callsign != "-" and client.filter != "" and client.filter != "-" :
sended = True
if len(client.filter) > 0 :
if client.filter.find("=*") == -1 :
for detail in client.filter.split(", ") :
filter = detail.split("=")
if filter[0] == "SA" :
if "-".join(detail_msg[-1].split("-")[2:]) != filter[1] :
sended = False
elif filter[0] == "FE" :
if detail_msg[-1].split("-")[0][0:2] != ("0" + filter[1])[-2:] :
sended = False
elif filter[0] == "CQ" :
if detail_msg[5] != "CQ" :
sended = False
elif filter[0] == "CA" :
if " ".join(detail_msg[5:-1]).find(filter[1]) == -1 :
sended = False
elif filter[0] == "MO" :
if detail_msg[-1].split("-")[1] != filter[1] :
sended = False
elif filter[0] == "DI" :
if DIS < int(filter[1]) :
sended = False
if not sended :
break
if sended :
if len(detail_msg[0]) == 6 :
if client.last_send != detail_msg[1] :
client.send(" .-.-. .-.-. .-.-.\n")
client.last_send = detail_msg[1]
else :
if client.last_send != detail_msg[0] :
client.send(" .-.-. .-.-. .-.-.\n")
client.last_send = detail_msg[0]
if client.color :
client.send(colorize(msg_color + msg))
else :
client.send(msg)
if __name__ == '__main__':
# Simple chat server to demonstrate connection handling via the
# async and telnet modules.
global SERVER_RUN
logging.basicConfig(level=logging.INFO,#控制台打印的日志级别
filename='/tmp/udp2telnet.log',
filemode='w',##模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志
#a是追加模式,默认如果不写的话,就是追加模式
format='%(asctime)s - [line:%(lineno)d] - %(levelname)s: %(message)s' #日志格式
)
SERVER_RUN =[]
for i in service:
if service[i] == "spot_server" :
telnet_thread = threading.Thread(target=spot_server, args=(i,))
elif service[i] == "wsjtx_udp" :
telnet_thread = threading.Thread(target=wsjtx_udp, args=(i,))
elif service[i] == "pydecoder_udp" :
telnet_thread = threading.Thread(target=pydecoder_udp, args=(i,))
threads.update({telnet_thread:i})
SERVER_RUN.append(i)
for i in threads:
# threads[i].setDaemon(True)
i.start()
# Server Loop
while "STOP" not in SERVER_RUN :
for i in threads:
if not i.isAlive() :
r = threads[i]
del threads[i]
logging.info("Retart {} thread @ {}".format(r,time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))))
if service[r] == "spot_server" :
telnet_thread = threading.Thread(target=spot_server, args=(r,))
elif service[r] == "wsjtx_udp" :
telnet_thread = threading.Thread(target=wsjtx_udp, args=(r,))
elif service[r] == "pydecoder_udp" :
telnet_thread = threading.Thread(target=pydecoder_udp, args=(r,))
threads.update({telnet_thread:r})
telnet_thread.start()
if r not in SERVER_RUN :
SERVER_RUN.append(r)
time.sleep(5)
logging.info("Server shutdown.")