-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.py
169 lines (139 loc) · 6.65 KB
/
core.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
import telnetlib
import unicodedata
import re
import json
import mysql.connector
import sys
import os
# Local imports
import webhook_generator
import eas_codes_converter
try:
with open("settings.json", "r") as configfile:
configdata = json.load(configfile)
os.system('cls' if os.name == 'nt' else 'clear')
print("\nsettings.json loaded successfully!\n")
print("""
/$$$$$$$ /$$ /$$ /$$$$$$$ /$$$$$$$$ /$$$$$$
| $$__ $$|__/ |__/| $$__ $$| $$_____/ /$$__ $$
| $$ \ $$ /$$ /$$$$$$ /$$| $$ \ $$| $$ | $$ \__/
| $$ | $$| $$ /$$__ $$| $$| $$ | $$| $$$$$ | $$
| $$ | $$| $$| $$ \ $$| $$| $$ | $$| $$__/ | $$
| $$ | $$| $$| $$ | $$| $$| $$ | $$| $$ | $$ $$
| $$$$$$$/| $$| $$$$$$$| $$| $$$$$$$/| $$$$$$$$| $$$$$$/
|_______/ |__/ \____ $$|__/|_______/ |________/ \______/
/$$ \ $$
| $$$$$$/
\______/
""")
print('\n')
print("DigiDEC v2 by trevor229 | https://github.com/trevor229")
print("\nSAGE EAS Endec Serial/Telnet Logging Software with Discord Webhook integration")
print('\n')
except FileNotFoundError:
print("settings.json not found! Did you reanme the example file?")
# Serial Server IP here
HOST=configdata['telnet']['ip']
# Remote port here. 10001 and 10002 for UDS200
PORT=configdata['telnet']['port']
# Load enable variables
RXWH_ENABLE = configdata['webhook']['enable_rx_alerts']
TXWH_ENABLE = configdata['webhook']['enable_tx_alerts']
tn = telnetlib.Telnet()
try:
tn.open(HOST, port=PORT)
print("Connected to telnet server!")
except:
print("Failed to connect to telnet server. Is the connection already in use?")
exit()
# Use unicodedata library to remove unicode characters from provided text (ie \r, \n)
def remove_control_characters(s):
return "".join(ch for ch in s if unicodedata.category(ch)[0]!="C")
# Remove <ENDECSTART> and <ENDECEND> from provided string
def remove_header_footer(txt):
return txt.removeprefix("<ENDECSTART>").removesuffix("<ENDECEND>")
def sentAlert(rawdata):
MAIN_TEXT = re.findall(r'(?:A Broadcast|The National|The Civil|A Primary Entry)[^\)]+.', rawdata)
LOCAL = re.findall(r'^.*\d\d:\d\d:\d\d',"".join(rawdata))
ZCZC_TEXT = re.findall(r'ZCZC.\S+', rawdata)
ZCZC_TEXT = re.split(r'\b[-+]',"".join(ZCZC_TEXT))
COMBINED = [LOCAL,MAIN_TEXT,ZCZC_TEXT]
# Take our 3 letter event code and reference its full name
EVENT = eas_codes_converter.SAME2txt(ZCZC_TEXT[2])
try:
# Create new connection. Happens every time the sentAlert function is called
dbase= mysql.connector.connect(
host = configdata['mysql']['server_ip'],
user = configdata['mysql']['user'],
passwd = configdata['mysql']['pass']
)
cursorObject = dbase.cursor()
cursorObject.execute("USE test_tx_data")
SQL = "INSERT INTO alerts (EVENT_TXT,EVENT_CODE,DESCR,ZCZC_STR,TYPE) VALUES (%s,%s,%s,%s,%s)" # Note: no quotes
data = (EVENT[0],ZCZC_TEXT[2],''.join(MAIN_TEXT),'-'.join(ZCZC_TEXT),EVENT[2])
cursorObject.execute(SQL,data)
print("alert logged to SQL db!\n")
# Important to commit our data! Wont be in the fucking db otherwise.... ask me how I realized that.
dbase.commit()
# Close our connection and cursor to avoid leaving open a connection and timing out/wasting resources.
dbase.close()
except Exception as e:
print('\n')
print(e)
# POST output to Discord webhook if enabled
if TXWH_ENABLE:
webhook_generator.generateLocalAlertEmbed(COMBINED)
def rxAlert(rawdata):
MAIN_TEXT = re.findall(r'(?:A Broadcast|The National|The Civil|A Primary Entry)[^\)]+.', rawdata)
FILTER_MATCHED = re.findall(r'(?<=Matched Filter )(.*)(?=A Broadcast|The National|The Civil|A Primary Entry)', "".join(rawdata))
MON_NUM = re.findall(r'(?<=on monitor )(..)(?=Matched Filter)', "".join(rawdata))
LOCAL = re.findall(r'^.*\d\d:\d\d:\d\d',"".join(rawdata))
ZCZC_TEXT = re.findall(r'ZCZC.\S+', rawdata)
ZCZC_TEXT = re.split(r'\b[-+]',"".join(ZCZC_TEXT))
EVENT = eas_codes_converter.SAME2txt(ZCZC_TEXT[2])
# Remove last null item in list of ZCZC_TEXT
del ZCZC_TEXT[-1]
COMBINED = [LOCAL,FILTER_MATCHED,MON_NUM,MAIN_TEXT,ZCZC_TEXT]
try:
# Create new connection. Happens every time the sentAlert function is called
dbase= mysql.connector.connect(
host = configdata['mysql']['server_ip'],
user = configdata['mysql']['user'],
passwd = configdata['mysql']['pass']
)
cursorObject = dbase.cursor()
cursorObject.execute("USE test_rx_data")
SQL = "INSERT INTO alerts (EVENT_TXT,EVENT_CODE,FILTER,MON,DESCR,ZCZC_STR,TYPE) VALUES (%s,%s,%s,%s,%s,%s,%s)" # Note: no quotes
data = (EVENT[0],ZCZC_TEXT[2],''.join(FILTER_MATCHED),''.join(MON_NUM),''.join(MAIN_TEXT),'-'.join(ZCZC_TEXT),EVENT[2])
cursorObject.execute(SQL,data)
print("alert logged to SQL db!\n")
# Important to commit our data! Wont be in the fucking db otherwise.... ask me how I realized that.
dbase.commit()
# Close our connection and cursor to avoid leaving open a connection and timing out/wasting resources.
dbase.close()
except Exception as e:
print('\n')
print(e)
# POST output to Discord webhook if enabled
if RXWH_ENABLE:
webhook_generator.generateRXAlertEmbed(COMBINED)
while True:
# telnetlib read stream until ENDECEND is recieved then continue on
output = tn.read_until(b'<ENDECEND>')
# Decode bytes-like object into usable ascii string
NICERDATA = output.decode("ascii")
# Remove all newline and other control characters from decoded output
NICERDATA = remove_control_characters(NICERDATA)
NICERDATA = remove_header_footer(NICERDATA)
if NICERDATA.startswith("Local") or NICERDATA.startswith("Old"):
print("\nLocal or Old Alert Sent/Relayed! \n")
sentAlert(NICERDATA)
elif NICERDATA.startswith("Alert Received"):
print("\nAlert Received on Input! \n")
rxAlert(NICERDATA)
elif NICERDATA.startswith("Alert sent"):
print("\nAlert Relayed by Endec!\n")
#rxAlert(NICERDATA)
else:
print("ERROR: Formatted alert data did not match final check!")
print(NICERDATA)