Skip to content

Commit 473fb07

Browse files
WillowSauceRWillowSauceR
authored andcommitted
use UTF-8 encoding to save file
1 parent 7580cea commit 473fb07

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

motd.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,8 @@ def sendPacket(ip, port):
1818
def recvPacket(sk_send):
1919
data, addr = sk_send.recvfrom(10240)
2020
infos = []
21-
data1 = data.split(b"MCPE")
22-
infos_byte = data1[1].split(b";")
23-
# log(data)
24-
# log(data1)
25-
# log(infos_byte)
21+
prefix, motd_info = data.split(b"MCPE")
22+
infos_byte = motd_info.split(b";")
2623
for info in infos_byte:
2724
try:
2825
context = info.decode()
@@ -41,6 +38,8 @@ def recvPacket(sk_send):
4138
except:
4239
log(f"Port info is unavailable.")
4340
log(f"Source: {addr[0]}:{addr[1]}")
41+
#infos = {"motd": infos[1], "version_id": infos[2], "version": infos[3], "online": infos[4], "max_player": infos[5],
42+
# "unique_id": infos[6], "map": infos[7], "gamemode": infos[8], "source_port_v4": infos[10], "source_port_v6": infos[11]}
4443

4544
sk_send.close()
4645

scan.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,18 @@
1515
localHostPort = randint(1024, 65535)
1616

1717
socketSendRecv = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
18-
socketSendRecv.bind((localHostIP, localHostPort))
18+
if sys.platform.startswith('win32'):
19+
socketSendRecv.bind((localHostIP, localHostPort))
1920

2021
scanResult = {"serverCount": 0, "bdsCount": 0, "nkCount": 0, "geyserCount": 0,
21-
"skipped": 0, "error": 0, "serverList": [], "totalPlayerCount": 0}
22+
"skipped": 0, "error": 0, "serverList": [], "totalPlayerCount": 0, "updated": 0, "ipUpdated": 0}
2223

2324
motdData = b'\x01\x00\x00\x00\x00$\r\x12\xd3\x00\xff\xff\x00\xfe\xfe\xfe\xfe\xfd\xfd\xfd\xfd\x124Vx\n'
2425

2526
def getIpList(ip: str):
2627
ipList = []
2728
if os.path.exists(TargetAddr):
28-
with open(TargetAddr, "r") as file:
29+
with open(TargetAddr, "r", encoding="utf-8") as file:
2930
for ip in file.readlines():
3031
if len(ip) < 3:
3132
continue
@@ -111,7 +112,7 @@ def startThreads():
111112
# time.sleep(1)
112113
# print(scanResultList)
113114
if os.path.exists(fileName):
114-
with open(fileName, "w") as file:
115+
with open(fileName, "w", encoding="utf-8") as file:
115116
file.writelines(scanResultList)
116117
quietMode = False
117118
print()
@@ -122,6 +123,8 @@ def startThreads():
122123
log(f"Skipped Count: {scanResult['skipped']}", info="I", quiet=quietMode)
123124
log(f"Error Count: {scanResult['error']}", info="I", quiet=quietMode)
124125
log(f"Total Player Count: {scanResult['totalPlayerCount']}", info="I", quiet=quietMode)
126+
log(f"Updated Count: {scanResult['updated']}", info="I", quiet=quietMode)
127+
log(f"IP Updated Count: {scanResult['ipUpdated']}", info="I", quiet=quietMode)
125128
os._exit(0)
126129

127130

@@ -131,7 +134,7 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: list,
131134
else:
132135
quietMode = False
133136
if os.path.exists(fileName[1]):
134-
with open(fileName[1], "r") as file:
137+
with open(fileName[1], "r", encoding="utf-8") as file:
135138
scanResultList = file.readlines()
136139
else:
137140
scanResultList = []
@@ -187,7 +190,7 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: list,
187190
log(f"{scanResult['totalPlayerCount']}", info="P", quiet=quietMode)
188191
log(quiet=quietMode)
189192
if fileName[0]:
190-
scanResultList = saveResults(fileName, scanResult, addr, date, infos, scanResultList)
193+
scanResultList, scanResult = saveResults(fileName, scanResult, addr, date, infos, scanResultList)
191194
if len(infos) == 10 or len(infos) == 6:
192195
scanResult['nkCount'] += 1
193196
elif re.search(b"edicated", data):
@@ -215,7 +218,7 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: list,
215218
def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
216219
formatedScanResult = f"{date} | {scanResult['serverCount']} | {addr[0]} | {addr[1]} | {infos[1]} | {infos[3]} | {infos[4]} | {infos[5]}"
217220
# scanResultListBackup = scanResultList
218-
with open(fileName[0], "r+") as file:
221+
with open(fileName[0], "r+", encoding="utf-8") as file:
219222
# scanResultList = file.readlines()
220223
for index in range(len(scanResultList)):
221224
# print(scanResultList[index])
@@ -231,9 +234,11 @@ def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
231234
except:
232235
return scanResultList
233236
if fileName[0] == fileName[1] and addr[0] not in scanResultList[index]:
234-
with open("updated.txt", "a") as file:
237+
with open("updated.txt", "a", encoding="utf-8") as file:
235238
pervInfo = scanResultList[index].split(" | ")
236239
file.write(f"{formatedScanResult} Pervious: {pervInfo[2]}:{pervInfo[3]}\n")
240+
scanResult['ipUpdated'] += 1
241+
scanResult['updated'] += 1
237242
scanResultList[index] = formatedScanResult + "\n"
238243
# if len(scanResultList) < len(scanResultListBackup):
239244
# scanResultList = scanResultListBackup
@@ -250,7 +255,7 @@ def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
250255
scanResultList.append("\n" + formatedScanResult)
251256
if len(scanResultList) == 0:
252257
scanResultList.append(formatedScanResult + "\n")
253-
return scanResultList
258+
return scanResultList, scanResult
254259

255260

256261
if __name__ == "__main__":
@@ -277,6 +282,7 @@ def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
277282
fileName = ""
278283

279284
pipe1, pipe2 = mp.Pipe()
285+
mp.set_start_method('spawn')
280286
p = mp.Process(target=recvPackets, args=(
281287
socketSendRecv, verboseMode, [fileName, TargetAddr], scanResult, pipe2), daemon=True)
282288
p.start()

send.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def sendPacket(target: str, port, payloadFile: str, loops, interval):
104104
scanProcess = False
105105
for i in range(loops):
106106
if ":" in target:
107-
with open(targetFile, "r") as file: #, encoding="utf-8"
107+
with open(targetFile, "r", encoding="utf-8") as file: #, encoding="utf-8"
108108
fileContent = file.readlines()
109109
contentCount = len(fileContent)
110110
for index in range(contentCount):

0 commit comments

Comments
 (0)