-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtester.py
148 lines (129 loc) · 5.53 KB
/
tester.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
import threading
import sqlite3
from mcstatus import JavaServer
import colorama
import argparse
import random
from datetime import datetime
import sys
db = sqlite3.connect("database.db")
cursor = db.cursor()
colorama.init()
cursor.execute('''CREATE TABLE IF NOT EXISTS "servers" ( id INTEGER PRIMARY KEY AUTOINCREMENT, addr TEXT, port INTEGER, first_seen DATETIME, last_seen DATETIME);''')
db.commit()
cursor.execute("SELECT * FROM servers")
database = cursor.fetchall()
random.shuffle(database)
parser = argparse.ArgumentParser()
parser.add_argument('--minplayers', type=int, help='Minimum of players connected on the server')
parser.add_argument('--players', type=int, help='Players connected on the server')
parser.add_argument('--maxplayers', type=int, help='Maximum of players connected on the server')
parser.add_argument('--timeout', type=int, help='Timout (ms)')
parser.add_argument('--capacity', type=int, help="Server's player capacity")
parser.add_argument('--version', type=str, help="Server's version")
parser.add_argument('--threads', type=str, help="Threads (default = 300)")
args = parser.parse_args()
minplayers = args.minplayers
players = args.players
maxplayers = args.maxplayers
timeout = args.timeout
capacity = args.capacity
version = args.version
num_threads = args.threads
if not num_threads:
num_threads = 300
event = threading.Event()
results = []
def ping_server():
minplayers = args.minplayers
players = args.players
maxplayers = args.maxplayers
timeout = args.timeout
capacity = args.capacity
version = args.version
while True:
try :
if not database or event.is_set():
break
server = database.pop(0)
id = server[0]
addr = server[1]
port = server[2]
server = JavaServer.lookup(f"{addr}:{port}")
try:
status = server.status()
except:
continue
if not status:
continue
if minplayers and minplayers>=status.players.online:
continue
if players and players!=status.players.online:
continue
if maxplayers and maxplayers<=status.players.online:
continue
if timeout:
if timeout <= status.latency:
continue
else:
if 100 <= status.latency:
continue
if capacity and capacity != status.players.max:
continue
if version and not version in str(status.version.name):
continue
players = f"{status.players.online}/{status.players.max}"
ip = f"{addr}:{port}"
version=str(status.version.name)
if len(version) > 27:
version = f"{version[:27-3]}..."
latency = str(int(status.latency))+"ms"
description = status.description
description = description.replace("§1",colorama.Fore.BLUE)
description = description.replace("§2",colorama.Fore.GREEN)
description = description.replace("§3",colorama.Fore.CYAN)
description = description.replace("§4",colorama.Fore.RED)
description = description.replace("§5",colorama.Fore.MAGENTA)
description = description.replace("§6",colorama.Fore.YELLOW)
description = description.replace("§7",colorama.Fore.LIGHTBLACK_EX)
description = description.replace("§8",colorama.Fore.LIGHTBLACK_EX)
description = description.replace("§9",colorama.Fore.LIGHTBLUE_EX)
description = description.replace("§a",colorama.Fore.LIGHTGREEN_EX)
description = description.replace("§b",colorama.Fore.LIGHTCYAN_EX)
description = description.replace("§c",colorama.Fore.LIGHTRED_EX)
description = description.replace("§d",colorama.Fore.LIGHTMAGENTA_EX)
description = description.replace("§e",colorama.Fore.LIGHTYELLOW_EX)
description = description.replace("§f",colorama.Fore.WHITE)
description = description.replace("\n"," - ")
description = description.replace("§k","")
description = description.replace("§l",colorama.Style.BRIGHT)
description = description.replace("§m","")
description = description.replace("§n","\e[4m")
description = description.replace("§o","")
description = description.replace("§r",colorama.Style.RESET_ALL)
if event.is_set():
break
results.append([f"{colorama.Fore.LIGHTRED_EX}{players:10}{colorama.Fore.LIGHTBLUE_EX}{ip:22}{colorama.Fore.LIGHTYELLOW_EX}{version:30}{colorama.Fore.LIGHTGREEN_EX}{latency:6}{colorama.Style.RESET_ALL}{description}{colorama.Style.RESET_ALL}", id])
except:
pass
def Main():
try:
threads = []
for i in range(0, num_threads):
thread = threading.Thread(target=ping_server, args=[])
threads.append(thread)
thread.start()
while threading.active_count() != 1:
for result in results:
print(result[0])
cursor.execute(f"UPDATE servers SET last_seen = '{datetime.now()}' WHERE id = {result[1]}")
db.commit()
except:
event.set()
print("\nStopping...")
while threading.active_count() != 1:
pass
print("Complete")
sys.exit()
if __name__ == "__main__":
Main()