[serverup] Improve and add IPv6 support
parent
dc06198988
commit
582d437fb6
56
serverup.py
56
serverup.py
|
@ -7,31 +7,43 @@ Licensed under GNU General Public License v2.0
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
|
||||||
def check(address, port):
|
def check(host, port):
|
||||||
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
||||||
sock.settimeout(2.0)
|
|
||||||
try:
|
try:
|
||||||
buf = b"\x4f\x45\x74\x03\x00\x00\x00\x01"
|
ai = socket.getaddrinfo(host, port, proto=socket.IPPROTO_UDP)[0]
|
||||||
sock.sendto(buf, (address, port))
|
except socket.gaierror:
|
||||||
|
return None, "host did not resolve"
|
||||||
|
if all(c in "0123456789." for c in host) or ":" in host:
|
||||||
|
ipproto = "" # is obvious to the user
|
||||||
|
else:
|
||||||
|
ipproto = "IPv6" if ai[0] == socket.AF_INET6 else "IPv4"
|
||||||
|
|
||||||
|
sock = socket.socket(*ai[:3])
|
||||||
|
sock.settimeout(2.0)
|
||||||
|
sock.connect(ai[4])
|
||||||
|
try:
|
||||||
|
# ask for a peer id
|
||||||
|
sock.send(b"\x4f\x45\x74\x03\x00\x00\x00\x01")
|
||||||
start = time.time()
|
start = time.time()
|
||||||
data = sock.recv(1024)
|
data = sock.recv(1024)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return None, ipproto
|
||||||
end = time.time()
|
end = time.time()
|
||||||
|
# disconnect again
|
||||||
peer_id = data[12:14]
|
peer_id = data[12:14]
|
||||||
buf = b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03"
|
sock.send(b"\x4f\x45\x74\x03" + peer_id + b"\x00\x00\x03")
|
||||||
sock.sendto(buf, (address, port))
|
return end - start, ipproto
|
||||||
|
except socket.error:
|
||||||
|
return None, ""
|
||||||
|
finally:
|
||||||
sock.close()
|
sock.close()
|
||||||
return (end - start)
|
|
||||||
except (socket.gaierror, socket.error):
|
|
||||||
return
|
|
||||||
|
|
||||||
def serverup(phenny, input):
|
def serverup(phenny, input):
|
||||||
arg = input.group(2)
|
arg = input.group(2)
|
||||||
if not arg:
|
if not arg:
|
||||||
return phenny.reply("give me an address and port (optional)")
|
return phenny.reply("give me an address and (optionally) a port")
|
||||||
|
|
||||||
arg = arg.replace(":", " ")
|
if '.' in arg: # IPv4 or a domain name
|
||||||
|
arg = arg.replace(":", " ")
|
||||||
if ' ' in arg:
|
if ' ' in arg:
|
||||||
address, port = arg.split(' ')
|
address, port = arg.split(' ')
|
||||||
try:
|
try:
|
||||||
|
@ -42,17 +54,21 @@ def serverup(phenny, input):
|
||||||
address = arg
|
address = arg
|
||||||
port = 30000
|
port = 30000
|
||||||
|
|
||||||
if '.' not in address:
|
|
||||||
return phenny.reply("invalid address")
|
|
||||||
if port < 1024 and port >= 2**16:
|
if port < 1024 and port >= 2**16:
|
||||||
return phenny.reply("invalid port")
|
return phenny.reply("invalid port")
|
||||||
|
|
||||||
desc = "%s:%d" % (address, port)
|
if ":" in address:
|
||||||
result = check(address, port)
|
desc = "[%s]:%d" % (address, port)
|
||||||
if result is None:
|
|
||||||
phenny.say("%s seems to be down" % desc)
|
|
||||||
else:
|
else:
|
||||||
phenny.say("%s is up (%dms)" % (desc, result*1000))
|
desc = "%s:%d" % (address, port)
|
||||||
|
result, extra = check(address, port)
|
||||||
|
if result is None:
|
||||||
|
msg = "%s seems to be down" % desc
|
||||||
|
else:
|
||||||
|
msg = "%s is up (%dms)" % (desc, result*1000)
|
||||||
|
if extra != "":
|
||||||
|
msg += " (%s)" % extra
|
||||||
|
phenny.say(msg)
|
||||||
|
|
||||||
serverup.commands = ['up']
|
serverup.commands = ['up']
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue