From b9f3e1e223d2153965775359483d1afd2661e620 Mon Sep 17 00:00:00 2001 From: Sfan5 Date: Sat, 19 Jan 2013 15:14:01 +0100 Subject: [PATCH] [ServerUp] New Syntax, Bulk Checking, Limit for multiple Ports --- serverup.py | 78 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 23 deletions(-) diff --git a/serverup.py b/serverup.py index 0127b90..7c41a41 100755 --- a/serverup.py +++ b/serverup.py @@ -13,32 +13,64 @@ def serverup(phenny, input): if not '.' in arg: return phenny.reply("Invalid Address") if ':' in arg: - address = arg.split(':')[0] - try: - port = int(arg.split(':')[1]) - except: - return phenny.reply("Invalid Port") + phenny.reply("Note: Syntax changed please use 'example.org 1337' instead of 'example.org:1337'") + arg = arg.split(':')[0] + ' ' + arg.split(':')[1] + 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 - port = 30000 - try: - start = time.time() - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) - sock.settimeout(2.5) - buf = "\x4f\x45\x74\x03\x00\x00\x00\x01" - sock.sendto(buf, (address, port)) - data, addr = sock.recvfrom(1000) - if data: - peer_id = data[12:14] - buf = "\x4f\x45\x74\x03" + peer_id + "\x00\x00\x03" + ports = [30000] + if len(ports) > 4 and not (input.admin or input.owner): # Owner and Admins of the Bot can bypass the Limit + return phenny.reply("Too many Ports specified") + for port in ports: + repres = address + ':' + str(port) + try: + start = time.time() + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.settimeout(2.5) + buf = "\x4f\x45\x74\x03\x00\x00\x00\x01" sock.sendto(buf, (address, port)) - sock.close() - end = time.time() - phenny.reply("%s is up (%0.3fms)" % (arg,end-start)) - else: - phenny.reply("%s seems to be down " % arg) - except: - phenny.reply("%s seems to be down " % arg) + data, addr = sock.recvfrom(1000) + if data: + peer_id = data[12:14] + buf = "\x4f\x45\x74\x03" + peer_id + "\x00\x00\x03" + sock.sendto(buf, (address, port)) + sock.close() + end = time.time() + phenny.reply("%s is up (%0.3fms)" % (repres,end-start)) + else: + phenny.reply("%s seems to be down " % repres) + except: + phenny.reply("%s seems to be down " % repres)