2010-12-02 19:28:06 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright(C) 2010 Simon Howard
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
|
|
# 02111-1307, USA.
|
|
|
|
#
|
|
|
|
#
|
|
|
|
# Test script for querying the master server.
|
|
|
|
#
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
from __future__ import division, generators, unicode_literals, print_function
|
|
|
|
|
2010-12-02 19:28:06 +00:00
|
|
|
import socket
|
|
|
|
import sys
|
|
|
|
import struct
|
2019-02-10 02:24:40 +00:00
|
|
|
import json
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2019-02-10 02:53:56 +00:00
|
|
|
NET_PACKET_TYPE_NAT_HOLE_PUNCH = 16
|
|
|
|
|
2010-12-02 19:28:06 +00:00
|
|
|
NET_MASTER_PACKET_TYPE_ADD = 0
|
|
|
|
NET_MASTER_PACKET_TYPE_ADD_RESPONSE = 1
|
|
|
|
NET_MASTER_PACKET_TYPE_QUERY = 2
|
|
|
|
NET_MASTER_PACKET_TYPE_QUERY_RESPONSE = 3
|
2010-12-05 00:46:17 +00:00
|
|
|
NET_MASTER_PACKET_TYPE_GET_METADATA = 4
|
|
|
|
NET_MASTER_PACKET_TYPE_GET_METADATA_RESPONSE = 5
|
2012-08-04 01:25:20 +00:00
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_START = 6
|
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_START_RESPONSE = 7
|
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_END = 8
|
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_END_RESPONSE = 9
|
2019-02-10 02:53:56 +00:00
|
|
|
NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH = 10
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
UDP_PORT = 2342
|
|
|
|
|
|
|
|
def parse_address(addr_str):
|
|
|
|
if ":" in addr_str:
|
|
|
|
addr_str, port = addr_str.split(":", 1)
|
|
|
|
port = int(port)
|
|
|
|
else:
|
|
|
|
port = UDP_PORT
|
|
|
|
|
|
|
|
return (socket.gethostbyname(addr_str), port)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
def send_message(sock, addr, message_type, payload=None):
|
|
|
|
header = struct.pack(">h", message_type)
|
|
|
|
packet = header
|
|
|
|
|
|
|
|
if payload is not None:
|
|
|
|
packet += payload
|
|
|
|
|
|
|
|
sock.sendto(packet, addr)
|
|
|
|
|
|
|
|
def get_response(sock, addr, message_type):
|
|
|
|
""" Wait for a response of the specified type to be received. """
|
|
|
|
|
|
|
|
while True:
|
2012-08-04 16:10:03 +00:00
|
|
|
packet, remote_addr = sock.recvfrom(1400)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
if remote_addr == addr:
|
|
|
|
type, = struct.unpack(">h", packet[0:2])
|
|
|
|
|
|
|
|
if type != message_type:
|
|
|
|
raise Exception("Wrong type of packet received: %i != %i" %
|
|
|
|
(type, message_type))
|
|
|
|
|
|
|
|
return packet[2:]
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Rxed from %s, expected %s" % (remote_addr, addr))
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
def read_string(packet):
|
|
|
|
terminator = struct.pack("b", 0)
|
|
|
|
strlen = packet.index(terminator)
|
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
result, = struct.unpack("%ss" % strlen, packet[0:strlen])
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
return packet[strlen + 1:], result
|
|
|
|
|
|
|
|
def add_to_master(addr_str):
|
|
|
|
""" Add self to master at specified IP address. """
|
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
addr = parse_address(addr_str)
|
2010-12-02 19:28:06 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# Send request
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Sending request to master at %s" % str(addr))
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
send_message(sock, addr, NET_MASTER_PACKET_TYPE_ADD)
|
|
|
|
|
|
|
|
# Wait for response.
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Waiting for response...")
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
response = get_response(sock, addr, NET_MASTER_PACKET_TYPE_ADD_RESPONSE)
|
|
|
|
|
|
|
|
success, = struct.unpack(">h", response)
|
|
|
|
|
|
|
|
if not success:
|
|
|
|
raise Exception("Address not successfully added to master.")
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Address added to master.")
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
def decode_string_list(packet):
|
|
|
|
""" Decode binary data containing NUL-terminated strings. """
|
|
|
|
|
|
|
|
strings = []
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
while len(packet) > 0:
|
2010-12-05 00:46:17 +00:00
|
|
|
packet, string = read_string(packet)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
strings.append(string)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
return strings
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
def query_master(addr_str):
|
|
|
|
""" Query a master server for its list of server IP addresses. """
|
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
addr = parse_address(addr_str)
|
2010-12-02 19:28:06 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# Send request
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Sending query to master at %s" % str(addr))
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
send_message(sock, addr, NET_MASTER_PACKET_TYPE_QUERY)
|
|
|
|
|
|
|
|
# Receive response
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Waiting for response...")
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
response = get_response(sock, addr, NET_MASTER_PACKET_TYPE_QUERY_RESPONSE)
|
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
servers = decode_string_list(response)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("%i servers" % len(servers))
|
2010-12-02 19:28:06 +00:00
|
|
|
|
|
|
|
for s in servers:
|
2019-02-10 02:24:40 +00:00
|
|
|
print("\t%s" % s)
|
2010-12-02 19:28:06 +00:00
|
|
|
|
2010-12-05 00:46:17 +00:00
|
|
|
def get_metadata(addr_str):
|
|
|
|
""" Query a master server for metadata about its servers. """
|
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
addr = parse_address(addr_str)
|
2010-12-05 00:46:17 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# Send request
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Sending metadata query to master at %s" % str(addr))
|
2010-12-05 00:46:17 +00:00
|
|
|
|
|
|
|
send_message(sock, addr, NET_MASTER_PACKET_TYPE_GET_METADATA)
|
|
|
|
|
|
|
|
# Receive response
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Waiting for response...")
|
2010-12-05 00:46:17 +00:00
|
|
|
|
2012-08-04 01:25:20 +00:00
|
|
|
response = get_response(sock, addr,
|
|
|
|
NET_MASTER_PACKET_TYPE_GET_METADATA_RESPONSE)
|
2010-12-05 00:46:17 +00:00
|
|
|
|
|
|
|
servers = decode_string_list(response)
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("%i servers" % len(servers))
|
2010-12-05 00:46:17 +00:00
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
for server in servers:
|
|
|
|
metadata = json.loads(server)
|
|
|
|
print("\tServer: %s:%i" % (metadata["address"], metadata["port"]))
|
|
|
|
print("\t\tAge: %i seconds" % metadata["age"])
|
|
|
|
print("\t\tName: %s" % metadata["name"])
|
|
|
|
print("\t\tVersion: %s" % metadata["version"])
|
|
|
|
print("\t\tMax. players: %i" % metadata["max_players"])
|
2010-12-05 00:46:17 +00:00
|
|
|
|
2012-08-04 01:25:20 +00:00
|
|
|
def sign_start(addr_str):
|
|
|
|
""" Request a signed start message from the master. """
|
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
addr = parse_address(addr_str)
|
2012-08-04 01:25:20 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# Send request
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Sending signed start request to master at %s" % str(addr_str))
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
send_message(sock, addr, NET_MASTER_PACKET_TYPE_SIGN_START)
|
|
|
|
|
|
|
|
# Receive response
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Waiting for response...")
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
response = get_response(sock, addr,
|
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_START_RESPONSE)
|
2012-08-04 02:02:13 +00:00
|
|
|
nonce = response[0:16]
|
|
|
|
signature = response[16:]
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Binary nonce: %s" % ("".join("%02x" % x for x in nonce)))
|
|
|
|
print(signature)
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
def sign_end(addr_str):
|
|
|
|
""" Request a signed end message from the server. """
|
|
|
|
|
2019-02-10 02:33:41 +00:00
|
|
|
addr = parse_address(addr_str)
|
2012-08-04 01:25:20 +00:00
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Paste the start message, then type ^D")
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
start_message = sys.stdin.read()
|
|
|
|
fake_sha1 = "3ijAI83u8A(*j98jf3jf"
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Sending signed end request to master at %s" % str(addr_str))
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
send_message(sock, addr, NET_MASTER_PACKET_TYPE_SIGN_END,
|
|
|
|
payload=(fake_sha1 + start_message))
|
|
|
|
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Waiting for response...")
|
2012-08-04 01:25:20 +00:00
|
|
|
|
|
|
|
response = get_response(sock, addr,
|
|
|
|
NET_MASTER_PACKET_TYPE_SIGN_END_RESPONSE)
|
2019-02-10 02:24:40 +00:00
|
|
|
print(response)
|
2012-08-04 01:25:20 +00:00
|
|
|
|
2019-02-10 02:53:56 +00:00
|
|
|
def hole_punch(master_addr_str, server_addr_str):
|
|
|
|
"""Send a NAT hole punch request to the master server."""
|
|
|
|
|
|
|
|
master_addr = parse_address(master_addr_str)
|
|
|
|
server_addr = parse_address(server_addr_str)
|
|
|
|
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
|
|
|
|
# Send request
|
|
|
|
print("Sending hole punch request to master at %s" % str(master_addr))
|
|
|
|
packet = server_addr_str.encode("utf8") + b"\x00"
|
|
|
|
send_message(sock, master_addr, NET_MASTER_PACKET_TYPE_NAT_HOLE_PUNCH,
|
|
|
|
packet)
|
|
|
|
|
|
|
|
# Wait for response.
|
|
|
|
print("Waiting for response...")
|
|
|
|
response = get_response(sock, server_addr,
|
|
|
|
NET_PACKET_TYPE_NAT_HOLE_PUNCH)
|
|
|
|
|
|
|
|
print("Got hole punch request from server via master server.")
|
|
|
|
|
|
|
|
|
2012-08-04 01:25:20 +00:00
|
|
|
commands = [
|
|
|
|
("query", query_master),
|
|
|
|
("add", add_to_master),
|
|
|
|
("get-metadata", get_metadata),
|
|
|
|
("sign-start", sign_start),
|
|
|
|
("sign-end", sign_end),
|
2019-02-10 02:53:56 +00:00
|
|
|
("hole-punch", hole_punch),
|
2012-08-04 01:25:20 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for name, callback in commands:
|
|
|
|
if len(sys.argv) > 2 and name == sys.argv[1]:
|
2019-02-10 02:53:56 +00:00
|
|
|
callback(*sys.argv[2:])
|
2012-08-04 01:25:20 +00:00
|
|
|
break
|
2010-12-02 19:28:06 +00:00
|
|
|
else:
|
2019-02-10 02:24:40 +00:00
|
|
|
print("Usage:")
|
|
|
|
print("chocolate-master-test.py query <address>")
|
|
|
|
print("chocolate-master-test.py add <address>")
|
|
|
|
print("chocolate-master-test.py get-metadata <address>")
|
|
|
|
print("chocolate-master-test.py sign-start <address>")
|
|
|
|
print("chocolate-master-test.py sign-end <address>")
|
2019-02-10 02:53:56 +00:00
|
|
|
print("chocolate-master-test.py hole-punch <master> <server>")
|
2010-12-02 19:28:06 +00:00
|
|
|
|