2013-01-07 10:00:44 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
2015-02-17 12:21:07 -08:00
|
|
|
serverup.py - Minetest server ping module
|
2014-11-20 13:20:28 -08:00
|
|
|
Copyright 2014, sfan5
|
2015-02-17 12:21:07 -08:00
|
|
|
Licensed under GNU General Public License v2.0
|
2013-01-07 10:00:44 -08:00
|
|
|
"""
|
|
|
|
|
|
|
|
import socket, time
|
|
|
|
|
|
|
|
def serverup(phenny, input):
|
|
|
|
arg = input.group(2)
|
|
|
|
if not arg:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("give me an address (and port if you want)")
|
2013-01-07 10:00:44 -08:00
|
|
|
if not '.' in arg:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid address")
|
2013-01-07 10:00:44 -08:00
|
|
|
if ':' in arg:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("use 'example.org 1337' instead of 'example.org:1337'")
|
2013-01-19 06:14:01 -08:00
|
|
|
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:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid port list")
|
2013-01-19 06:14:01 -08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
a = int(p.split('-')[0])
|
|
|
|
except:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid port: %s" % p.split('-')[0])
|
2013-01-19 06:14:01 -08:00
|
|
|
try:
|
|
|
|
b = int(p.split('-')[1]) + 1
|
|
|
|
except:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid port: %s" % p.split('-')[1])
|
2013-01-19 06:14:01 -08:00
|
|
|
for i in range(a, b):
|
|
|
|
ports.append(i)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
ports.append(int(p))
|
|
|
|
except:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid port: %s" % p)
|
2013-01-19 06:14:01 -08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
ports = [int(port)]
|
|
|
|
except:
|
2014-11-20 13:20:28 -08:00
|
|
|
return phenny.reply("invalid port: %s" % port)
|
2013-01-07 10:00:44 -08:00
|
|
|
else:
|
|
|
|
address = arg
|
2013-01-19 06:14:01 -08:00
|
|
|
ports = [30000]
|
2013-02-10 09:29:25 -08:00
|
|
|
if len(ports) != 1 and input.sender.startswith('#') and not (input.admin or input.owner):
|
2014-11-20 13:20:28 -08:00
|
|
|
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!")
|
2013-01-19 06:14:01 -08:00
|
|
|
for port in ports:
|
|
|
|
repres = address + ':' + str(port)
|
|
|
|
try:
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
sock.settimeout(2.5)
|
2014-07-22 04:34:01 -07:00
|
|
|
buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
|
2013-01-07 10:00:44 -08:00
|
|
|
sock.sendto(buf, (address, port))
|
2014-11-20 13:20:28 -08:00
|
|
|
start = time.time()
|
2013-01-19 06:14:01 -08:00
|
|
|
data, addr = sock.recvfrom(1000)
|
|
|
|
if data:
|
2014-11-20 13:20:28 -08:00
|
|
|
end = time.time()
|
2013-01-19 06:14:01 -08:00
|
|
|
peer_id = data[12:14]
|
2014-07-22 04:34:01 -07:00
|
|
|
buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
|
2013-01-19 06:14:01 -08:00
|
|
|
sock.sendto(buf, (address, port))
|
|
|
|
sock.close()
|
2014-11-20 13:20:28 -08:00
|
|
|
t = (end - start) * 1000
|
2013-05-27 08:06:47 -07:00
|
|
|
phenny.say("%s is up (%dms)" % (repres,t))
|
2013-01-19 06:14:01 -08:00
|
|
|
else:
|
2013-01-19 06:19:17 -08:00
|
|
|
phenny.say("%s seems to be down " % repres)
|
2013-01-19 06:14:01 -08:00
|
|
|
except:
|
2013-01-19 06:19:17 -08:00
|
|
|
phenny.say("%s seems to be down " % repres)
|
2013-01-07 10:00:44 -08:00
|
|
|
|
|
|
|
serverup.commands = ['up']
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-07-20 07:13:59 -07:00
|
|
|
print(__doc__)
|