55import os
66import re
77import multiprocessing as mp
8+ from tqdm import tqdm
89from random import randint
910from multiprocessing .connection import _ConnectionBase
1011
2122
2223motdData = b'\x01 \x00 \x00 \x00 \x00 $\r \x12 \xd3 \x00 \xff \xff \x00 \xfe \xfe \xfe \xfe \xfd \xfd \xfd \xfd \x12 4Vx\n '
2324
24-
2525def getIpList (ip : str ):
2626 ipList = []
2727 if os .path .exists (TargetAddr ):
2828 with open (TargetAddr , "r" ) as file :
2929 for ip in file .readlines ():
30+ if len (ip ) < 3 :
31+ continue
3032 if "|" in ip :
3133 if ip .split (" | " )[2 ] not in ipList :
3234 ipList .append (ip .split (" | " )[2 ])
@@ -74,19 +76,21 @@ def sendPacket(portStart, portEnd, dstAddr):
7476
7577
7678def startThreads ():
77- global stopThread
79+ global stopThread , quietMode
7880 if "-" in portRange :
7981 portStart = int (portRange .split ("-" )[0 ])
8082 portEnd = int (portRange .split ("-" )[1 ])
8183 else :
8284 portStart = 0
8385 portEnd = 65535
8486 ipList = getIpList (TargetAddr )
87+ if quietMode :
88+ progressBar = tqdm (desc = "Scaning progress" ,total = len (ipList ), leave = True , unit = "IP" , unit_scale = False )
8589 for dstAddr in ipList :
8690 stopThread = False
87- print ( )
88- log (f"Scaning target: { dstAddr } " , info = "I" )
89- print ( )
91+ log ( quiet = quietMode )
92+ log (f"Scaning target: { dstAddr } " , info = "I" , quiet = quietMode )
93+ log ( quiet = quietMode )
9094 # sendPacket(portStart, portEnd, dstAddr)
9195 t1 = threading .Thread (target = sendPacket , args = (
9296 portStart , portEnd , dstAddr ), daemon = True )
@@ -97,49 +101,61 @@ def startThreads():
97101 if tmpServerCount == scanResult ['serverCount' ]:
98102 stopThread = True
99103 t1 .join ()
104+ if quietMode :
105+ progressBar .update (1 )
100106 # while threading.enumerate().__len__() != 2: # main and itself
101107 # time.sleep(1)
102108 # while mp.active_children():
103109 # time.sleep(1)
104110 mp .active_children ()[0 ].terminate ()
105- time .sleep (1 )
106- log (f"BE Server Count: { scanResult ['serverCount' ]} " , info = "I" )
107- log (f"BDS Count: { scanResult ['bdsCount' ]} " , info = "I" )
108- log (f"NK Count: { scanResult ['nkCount' ]} " , info = "I" )
109- log (f"Geyser Count: { scanResult ['geyserCount' ]} " , info = "I" )
110- log (f"Skipped Count: { scanResult ['skipped' ]} " , info = "I" )
111- log (f"Error Count: { scanResult ['error' ]} " , info = "I" )
112- log (f"Total Player Count: { scanResult ['totalPlayerCount' ]} " , info = "I" )
111+ # time.sleep(1)
112+ # print(scanResultList)
113+ with open (fileName , "w" ) as file :
114+ file .writelines (scanResultList )
115+ quietMode = False
116+ log (f"BE Server Count: { scanResult ['serverCount' ]} " , info = "I" , quiet = quietMode )
117+ log (f"BDS Count: { scanResult ['bdsCount' ]} " , info = "I" , quiet = quietMode )
118+ log (f"NK Count: { scanResult ['nkCount' ]} " , info = "I" , quiet = quietMode )
119+ log (f"Geyser Count: { scanResult ['geyserCount' ]} " , info = "I" , quiet = quietMode )
120+ log (f"Skipped Count: { scanResult ['skipped' ]} " , info = "I" , quiet = quietMode )
121+ log (f"Error Count: { scanResult ['error' ]} " , info = "I" , quiet = quietMode )
122+ log (f"Total Player Count: { scanResult ['totalPlayerCount' ]} " , info = "I" , quiet = quietMode )
113123 os ._exit (0 )
114124
115125
116- def recvPackets (socketSendRecv : socket .socket , verboseMode : str , fileName : str , scanResult : dict , pipe : _ConnectionBase ):
126+ def recvPackets (socketSendRecv : socket .socket , verboseMode : str , fileName : list , scanResult : dict , pipe : _ConnectionBase ):
127+ if verboseMode == "nn" :
128+ quietMode = True
129+ else :
130+ quietMode = False
131+ with open (fileName [1 ], "r" ) as file :
132+ scanResultList = file .readlines ()
117133 while True :
118134 try :
119135 data , addr = socketSendRecv .recvfrom (10240 )
120136 date = getTime ()
121137 if len (data ) <= 30 :
122138 scanResult ['skipped' ] += 1
123139 log (
124- f"data length <= 30, may not motd packet, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" )
140+ f"data length <= 30, may not motd packet, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" , quiet = quietMode )
125141 continue
126142 if b"MCPE" not in data :
127143 scanResult ['skipped' ] += 1
128144 log (
129- f"metadata \" MCPE\" not in packet, may not motd packet, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" )
145+ f"metadata \" MCPE\" not in packet, may not motd packet, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" , quiet = quietMode )
130146 continue
131147 if addr not in scanResult ['serverList' ]:
132148 scanResult ['serverList' ].append (addr )
133149 else :
134150 scanResult ['skipped' ] += 1
135151 log (
136- f"Duplicate server found, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" )
152+ f"Duplicate server found, skipped. Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" , quiet = quietMode )
137153 continue
138154 infos = []
139155 data1 = data .split (b"MCPE" )
140156 # log("----------------------------------------------------")
141157 # log(data1)
142- # log()
158+ # log(quiet=quietMode )
143159 infos_byte = data1 [1 ].split (b";" )
144160 for info in infos_byte :
145161 try :
@@ -148,29 +164,27 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: str,
148164 context = str (info )[2 :- 1 ]
149165 infos .append (context )
150166 # log(len(infos))
151- log (f"Motd: { infos [1 ]} " , info = "R" )
152- log (f"Versin: { infos [3 ]} /{ infos [2 ]} " , info = "R" )
153- log (f"Online: { infos [4 ]} /{ infos [5 ]} " , info = "R" )
167+ log (f"Motd: { infos [1 ]} " , info = "R" , quiet = quietMode )
168+ log (f"Versin: { infos [3 ]} /{ infos [2 ]} " , info = "R" , quiet = quietMode )
169+ log (f"Online: { infos [4 ]} /{ infos [5 ]} " , info = "R" , quiet = quietMode )
154170 try :
155- log (f"Map: { infos [7 ]} /{ infos [8 ]} " , info = "R" )
171+ log (f"Map: { infos [7 ]} /{ infos [8 ]} " , info = "R" , quiet = quietMode )
156172 except :
157- log (f"Map info is unavailable." , info = "R" )
173+ log (f"Map info is unavailable." , info = "R" , quiet = quietMode )
158174 try :
159- log (f"Port(v4/v6): { infos [10 ]} /{ infos [11 ]} " , info = "R" )
175+ log (f"Port(v4/v6): { infos [10 ]} /{ infos [11 ]} " , info = "R" , quiet = quietMode )
160176 except :
161- log (f"Port info is unavailable." , info = "R" )
162- log (f"Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" )
177+ log (f"Port info is unavailable." , info = "R" , quiet = quietMode )
178+ log (f"Source: { addr [0 ]} :{ addr [1 ]} " , info = "R" , quiet = quietMode )
163179 scanResult ['serverCount' ] += 1
164180 scanResult ['totalPlayerCount' ] += int (infos [4 ])
165- log (f"{ scanResult ['serverCount' ]} " , info = "C" )
166- log (f"{ scanResult ['totalPlayerCount' ]} " , info = "P" )
167- print ("" )
168- if fileName :
169- with open (fileName , "a+" ) as file :
170- file .write (
171- f"{ date } | { scanResult ['serverCount' ]} | { addr [0 ]} | { addr [1 ]} | { infos [1 ]} | { infos [3 ]} | { infos [4 ]} | { infos [5 ]} \n " )
181+ log (f"{ scanResult ['serverCount' ]} " , info = "C" , quiet = quietMode )
182+ log (f"{ scanResult ['totalPlayerCount' ]} " , info = "P" , quiet = quietMode )
183+ log (quiet = quietMode )
184+ if fileName [0 ]:
185+ scanResultList = saveResults (fileName , scanResult , addr , date , infos , scanResultList )
172186 if len (infos ) == 10 or len (infos ) == 6 :
173- nkCount += 1
187+ scanResult [ ' nkCount' ] += 1
174188 elif re .search (b"edicated" , data ):
175189 scanResult ['bdsCount' ] += 1
176190 elif re .search (b"nukkit" , data ):
@@ -180,13 +194,54 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: str,
180194 # socketSendRecv.close()
181195 except OSError as errorInfo :
182196 if verboseMode == "y" :
183- log (f"{ errorInfo } , skipped." , info = "R" )
197+ log (f"{ errorInfo } , skipped." , info = "R" , quiet = quietMode )
184198 scanResult ['error' ] += 1
185199 except Exception as errorInfo :
186- log (f"{ errorInfo } , skipped." , info = "R" )
200+ log (f"{ errorInfo } , skipped." , info = "R" , quiet = quietMode )
187201 scanResult ['error' ] += 1
188202 finally :
189- pipe .send (scanResult )
203+ pipe .send ((scanResult , scanResultList ))
204+
205+ def saveResults (fileName , scanResult , addr , date , infos , scanResultList ):
206+ formatedScanResult = f"{ date } | { scanResult ['serverCount' ]} | { addr [0 ]} | { addr [1 ]} | { infos [1 ]} | { infos [3 ]} | { infos [4 ]} | { infos [5 ]} "
207+ # scanResultListBackup = scanResultList
208+ with open (fileName [0 ], "r+" ) as file :
209+ # scanResultList = file.readlines()
210+ for index in range (len (scanResultList )):
211+ # print(scanResultList[index])
212+ # print(infos[1], scanResultList[index])
213+ if scanResultList [index ] == "\n \n " :
214+ del scanResultList [index ]
215+ # log("del line: ", scanResultList[index])
216+ return scanResultList
217+ if infos [1 ] in scanResultList [index ]:
218+ try :
219+ int (infos [1 ])
220+ infos [1 ][3 ]
221+ except :
222+ return scanResultList
223+ if addr [0 ] not in scanResultList [index ]:
224+ if fileName [0 ] == fileName [1 ]:
225+ with open ("updated.txt" , "a" ) as file :
226+ pervInfo = scanResultList [index ].split (" | " )
227+ file .write (f"{ formatedScanResult } Pervious: { pervInfo [2 ]} :{ pervInfo [3 ]} \n " )
228+ scanResultList [index ] = formatedScanResult + "\n "
229+ # if len(scanResultList) < len(scanResultListBackup):
230+ # scanResultList = scanResultListBackup
231+ # log(f"{len(scanResult)} < {len(scanResultListBackup)}!")
232+ # with open("less.txt", "w") as file:
233+ # file.writelines(scanResultList)
234+ # with open("more.txt", "w") as file:
235+ # file.writelines(scanResultListBackup)
236+ # os._exit(0)
237+ # if randint(1, 5) == 0:
238+ # with open(fileName, "w") as file:
239+ # file.writelines(scanResultList)
240+ return scanResultList
241+ scanResultList .append ("\n " + formatedScanResult )
242+ if len (scanResultList ) == 0 :
243+ scanResultList .append (formatedScanResult + "\n " )
244+ return scanResultList
190245
191246
192247if __name__ == "__main__" :
@@ -197,8 +252,11 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: str,
197252 except :
198253 TargetAddr = input ("Target IP: " )
199254 portRange = input ("Port range(like 1145-1919 and all): " )
200- verboseMode = input ("Show verbose info(y/n): " )
201-
255+ verboseMode = input ("Show verbose info(y/n/nn): " )
256+ if verboseMode == "nn" :
257+ quietMode = True
258+ else :
259+ quietMode = False
202260 try :
203261 timeout = int (sys .argv [4 ])
204262 except :
@@ -211,9 +269,9 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: str,
211269
212270 pipe1 , pipe2 = mp .Pipe ()
213271 p = mp .Process (target = recvPackets , args = (
214- socketSendRecv , verboseMode , fileName , scanResult , pipe2 ), daemon = True )
272+ socketSendRecv , verboseMode , [ fileName , TargetAddr ] , scanResult , pipe2 ), daemon = True )
215273 p .start ()
216274 t = threading .Thread (target = startThreads , daemon = True )
217275 t .start ()
218276 while mp .active_children ():
219- scanResult = pipe1 .recv ()
277+ scanResult , scanResultList = pipe1 .recv ()
0 commit comments