[serverup] simplify

master
sfan5 2018-11-05 17:49:02 +01:00
parent 4ad3d24507
commit a39a538d80
1 changed files with 43 additions and 64 deletions

View File

@ -1,79 +1,58 @@
#!/usr/bin/env python #!/usr/bin/env python3
""" """
serverup.py - Minetest server ping module serverup.py - Minetest server ping module
Copyright 2014, sfan5 Copyright 2018, sfan5
Licensed under GNU General Public License v2.0 Licensed under GNU General Public License v2.0
""" """
import socket
import time
import socket, time def check(address, port):
def serverup(phenny, input):
arg = input.group(2)
if not arg:
return phenny.reply("give me an address (and port if you want)")
if not '.' in arg:
return phenny.reply("invalid address")
if ':' in arg:
return phenny.reply("use 'example.org 1337' instead of 'example.org:1337'")
if ' ' in arg:
address = arg.split(' ')[0]
port = arg.split(' ')[1]
if '-' in port or ',' in port:
ports = []
ports_ = port.split(',')
for p in ports_:
if '-' in p:
if len(p.split('-')) != 2:
return phenny.reply("invalid port list")
else:
try:
a = int(p.split('-')[0])
except:
return phenny.reply("invalid port: %s" % p.split('-')[0])
try:
b = int(p.split('-')[1]) + 1
except:
return phenny.reply("invalid port: %s" % p.split('-')[1])
for i in range(a, b):
ports.append(i)
else:
try:
ports.append(int(p))
except:
return phenny.reply("invalid port: %s" % p)
else:
try:
ports = [int(port)]
except:
return phenny.reply("invalid port: %s" % port)
else:
address = arg
ports = [30000]
if len(ports) != 1 and input.sender.startswith('#') and not (input.admin or input.owner):
return phenny.reply("to check multiple ports please use private chat")
if len(ports) > 6 and not (input.admin or input.owner): # owner and admins of the bot can bypass the limit
return phenny.reply("ow, too many ports!")
for port in ports:
repres = address + ':' + str(port)
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(2.5) sock.settimeout(2.0)
try:
buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01" buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
sock.sendto(buf, (address, port)) sock.sendto(buf, (address, port))
start = time.time() start = time.time()
data, addr = sock.recvfrom(1000) data = sock.recv(1024)
if data: if not data:
return
end = time.time() end = time.time()
peer_id = data[12:14] peer_id = data[12:14]
buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03" buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
sock.sendto(buf, (address, port)) sock.sendto(buf, (address, port))
sock.close() sock.close()
t = (end - start) * 1000 return (end - start)
phenny.say("%s is up (%dms)" % (repres,t)) except (socket.gaierror, socket.error):
return
def serverup(phenny, input):
arg = input.group(2)
if not arg:
return phenny.reply("give me an address and port (optional)")
arg = arg.replace(":", " ")
if ' ' in arg:
address, port = arg.split(' ')
try:
port = int(port)
except ValueError:
return phenny.reply("invalid port")
else: else:
phenny.say("%s seems to be down " % repres) address = arg
except: port = 30000
phenny.say("%s seems to be down " % repres)
if '.' not in address:
return phenny.reply("invalid address")
if port < 1024 and port >= 2**16:
return phenny.reply("invalid port")
desc = "%s:%d" % (address, port)
result = check(address, port)
if result is None:
phenny.say("%s seems to be down" % desc)
else:
phenny.say("%s is up (%dms)" % (desc, result*1000))
serverup.commands = ['up'] serverup.commands = ['up']