[Minetest] Add IPv6 support and more descriptive error messages to up command

master
ShadowNinja 2015-02-25 00:09:14 -06:00
parent ef65b6c310
commit dd6261ffea
1 changed files with 20 additions and 10 deletions

View File

@ -39,17 +39,27 @@ class Minetest(callbacks.Plugin):
results = [] results = []
threads = [] threads = []
for port in ports: for port in ports:
try:
info = socket.getaddrinfo(address, port,
type=socket.SOCK_DGRAM,
proto=socket.SOL_UDP)[0]
except Exception as e:
irc.reply("Resolving %s failed: %s" %
(address, str(e)))
return
th = threading.Thread(name="ParallelServerUpThread-" + str(port), th = threading.Thread(name="ParallelServerUpThread-" + str(port),
target=self.parallelServerUp, target=self.parallelServerUp,
args=(resultQueue, address, port)) args=(resultQueue, info, port))
th.start() th.start()
threads.append(th) threads.append(th)
for th in threads: for th in threads:
th.join() # Wait for all threads to finish th.join() # Wait for all threads to finish
for i in range(0, resultQueue.qsize()): for i in range(0, resultQueue.qsize()):
info = resultQueue.get_nowait() info = resultQueue.get_nowait()
if info[1] is None: if isinstance(info[1], Exception):
results.append("port %d has an error" % (info[0],)) results.append("port %d errored: %s" %
(info[0], str(info[1])))
continue continue
msg = "port %d is " % (info[0],) msg = "port %d is " % (info[0],)
if info[1]: if info[1]:
@ -173,14 +183,14 @@ class Minetest(callbacks.Plugin):
"port": lambda self, server_list, arg: self.filterServersByNum(server_list, arg, "port", int) "port": lambda self, server_list, arg: self.filterServersByNum(server_list, arg, "port", int)
} }
def parallelServerUp(self, queue, address, port): def parallelServerUp(self, queue, info, port):
queue.put([port, self.serverUp(address, port)]) queue.put([port, self.serverUp(info)])
def serverUp(self, address, port): def serverUp(self, info):
try: try:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock = socket.socket(info[0], info[1], info[2])
sock.settimeout(2.5) sock.settimeout(2.5)
sock.connect((address, port)) sock.connect(info[4])
buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01" buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
sock.send(buf) sock.send(buf)
start = time.time() start = time.time()
@ -195,8 +205,8 @@ class Minetest(callbacks.Plugin):
return end - start return end - start
except socket.timeout: except socket.timeout:
return False return False
except: except Exception as e:
return None return e
def getPorts(self, port, irc): def getPorts(self, port, irc):
if '-' in port or ',' in port: if '-' in port or ',' in port: