diff --git a/gamespy/gs_database.py b/gamespy/gs_database.py index c592d21..d6ae555 100644 --- a/gamespy/gs_database.py +++ b/gamespy/gs_database.py @@ -29,15 +29,15 @@ class GamespyDatabase(object): # user id's will be ints, or all passwords will be ints, etc, despite not seeing any # evidence yet to say otherwise as far as Nintendo DS games go. q = "CREATE TABLE users (profileid INT, userid TEXT, password TEXT, gsbrcd TEXT, email TEXT, uniquenick TEXT, pid TEXT, lon TEXT, lat TEXT, loc TEXT, firstname TEXT, lastname TEXT, stat TEXT, partnerid TEXT, console INT, csnum TEXT, cfc TEXT, bssid TEXT, devname TEXT, birth TEXT, sig TEXT)" - logger.log(logging.DEBUG, q) + logger.log(-1, q) c.execute(q) q = "CREATE TABLE sessions (session TEXT, profileid INT)" - logger.log(logging.DEBUG, q) + logger.log(-1, q) c.execute(q) q = "CREATE TABLE buddies (userProfileId INT, buddyProfileId INT, time INT, status INT, notified INT)" - logger.log(logging.DEBUG, q) + logger.log(-1, q) c.execute(q) self.conn.commit() @@ -51,7 +51,7 @@ class GamespyDatabase(object): def get_next_free_profileid(self): # TODO: Make profile ids start at 1 for each game? q = "SELECT max(profileid) FROM users" - logger.log(logging.DEBUG, q) + logger.log(-1, q) c = self.conn.cursor() c.execute(q) @@ -69,7 +69,7 @@ class GamespyDatabase(object): def check_user_exists(self, userid, gsbrcd): q = "SELECT * FROM users WHERE userid = ? and gsbrcd = ?" q2 = q.replace("?", "%s") % (userid, gsbrcd) - logger.log(logging.DEBUG, q) + logger.log(-1, q) c = self.conn.cursor() c.execute(q, [userid, gsbrcd]) @@ -86,7 +86,7 @@ class GamespyDatabase(object): def check_profile_exists(self, profileid): q = "SELECT * FROM users WHERE profileid = ?" q2 = q.replace("?", "%s") % (profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [profileid]) @@ -105,7 +105,7 @@ class GamespyDatabase(object): if profileid != 0: q = "SELECT * FROM users WHERE profileid = ?" q2 = q.replace("?", "%s") % (profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [profileid]) @@ -118,7 +118,7 @@ class GamespyDatabase(object): def perform_login(self, userid, password, gsbrcd): q = "SELECT * FROM users WHERE userid = ? and gsbrcd = ?" q2 = q.replace("?", "%s") % (userid, gsbrcd) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [userid, gsbrcd]) @@ -161,7 +161,7 @@ class GamespyDatabase(object): q = "INSERT INTO users VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)" q2 = q.replace("?", "%s") % (profileid, str(userid), password, gsbrcd, email, uniquenick, pid, lon, lat, loc, firstname, lastname, stat, partnerid, console, csnum, cfc, bssid, devname, birth, sig) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [profileid, str(userid), password, gsbrcd, email, uniquenick, pid, lon, lat, loc, firstname, lastname, stat, partnerid, console, csnum, cfc, bssid, devname, birth, sig]) @@ -175,7 +175,7 @@ class GamespyDatabase(object): c = self.conn.cursor() q = "SELECT * FROM users" - logger.log(logging.DEBUG, q) + logger.log(-1, q) users = [] for row in c.execute(q): @@ -190,7 +190,7 @@ class GamespyDatabase(object): # FIXME: Possible security issue due to embedding an unsanitized string directly into the statement. q = "UPDATE users SET %s = %s WHERE profileid = %s" q2 = q % (field[0], field[1], profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q % (field[0]), [field[1], profileid]) @@ -201,7 +201,7 @@ class GamespyDatabase(object): def get_profileid_from_session_key(self, session_key): q = "SELECT profileid FROM sessions WHERE session = ?" q2 = q.replace("?", "%s") % (session_key) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [session_key]) @@ -222,7 +222,7 @@ class GamespyDatabase(object): if profileid != 0: q = "SELECT profileid FROM sessions WHERE session = ?" q2 = q.replace("?", "%s") % (session_key) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [session_key]) @@ -237,7 +237,7 @@ class GamespyDatabase(object): q = "SELECT session FROM sessions WHERE session = ?" q2 = q.replace("?", "%s") % (session_key) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() for r in c.execute(q, [session_key]): @@ -248,7 +248,7 @@ class GamespyDatabase(object): def delete_session(self, profileid): q = "DELETE FROM sessions WHERE profileid = ?" q2 = q.replace("?", "%s") % (profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [profileid]) @@ -266,7 +266,7 @@ class GamespyDatabase(object): q ="INSERT INTO sessions VALUES (?, ?)" q2 = q.replace("?", "%s") % (session_key, profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [session_key, profileid]) @@ -281,12 +281,12 @@ class GamespyDatabase(object): if profileid != None: q = "SELECT * FROM sessions WHERE profileid = ?" q2 = q.replace("?", "%s") % (profileid) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) r = c.execute(q, [profileid]) else: q = "SELECT * FROM sessions" - logger.log(logging.DEBUG, q) + logger.log(-1, q) r = c.execute(q) @@ -299,7 +299,7 @@ class GamespyDatabase(object): def add_buddy(self, userProfileId, buddyProfileId): q = "INSERT INTO buddies VALUES (?, ?, ?, ?, ?)" q2 = q.replace("?", "%s") % (userProfileId, buddyProfileId, now, 0, 0) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() now = int(time.time()) @@ -309,7 +309,7 @@ class GamespyDatabase(object): def auth_buddy(self, userProfileId, buddyProfileId): q = "UPDATE buddies SET status = ? WHERE userProfileId = ? AND buddyProfileId = ?" q2 = q.replace("?", "%s") % (1, userProfileId, buddyProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [1, userProfileId, buddyProfileId]) # 1 will mean authorized @@ -320,7 +320,7 @@ class GamespyDatabase(object): if userProfileId != 0 and buddyProfileId != 0: q = "SELECT * FROM buddies WHERE userProfileId = ? AND buddyProfileId = ?" q2 = q.replace("?", "%s") % (userProfileId, buddyProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [userProfileId, buddyProfileId]) @@ -332,7 +332,7 @@ class GamespyDatabase(object): def delete_buddy(self, userProfileId, buddyProfileId): q = "DELETE FROM buddies WHERE userProfileId = ? AND buddyProfileId = ?" q2 = q.replace("?", "%s") % (userProfileId, buddyProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [userProfileId, buddyProfileId]) @@ -341,7 +341,7 @@ class GamespyDatabase(object): def get_buddy_list(self, userProfileId): q = "SELECT * FROM buddies WHERE userProfileId = ?" q2 = q.replace("?", "%s") % (userProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() @@ -354,7 +354,7 @@ class GamespyDatabase(object): def get_pending_buddy_requests(self, userProfileId): q = "SELECT * FROM buddies WHERE buddyProfileId = ? AND status = 0" q2 = q.replace("?", "%s") % (userProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() @@ -367,7 +367,7 @@ class GamespyDatabase(object): def buddy_need_auth_message(self, userProfileId): q = "SELECT * FROM buddies WHERE buddyProfileId = ? AND status = 1 AND notified = 0" q2 = q.replace("?", "%s") % (userProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() @@ -380,7 +380,7 @@ class GamespyDatabase(object): def buddy_sent_auth_message(self, userProfileId, buddyProfileId): q = "UPDATE buddies SET notified = ? WHERE userProfileId = ? AND buddyProfileId = ?" q2 = q.replace("?", "%s") % (1, userProfileId, buddyProfileId) - logger.log(logging.DEBUG, q2) + logger.log(-1, q2) c = self.conn.cursor() c.execute(q, [1, userProfileId, buddyProfileId]) # 1 will mean that the player has been sent the " diff --git a/gamespy_natneg_server.py b/gamespy_natneg_server.py index f199c6e..c4cb996 100644 --- a/gamespy_natneg_server.py +++ b/gamespy_natneg_server.py @@ -26,7 +26,7 @@ class GameSpyNatNegServer(object): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.bind(address) - logger.log(logging.DEBUG, "Server is now listening on %s:%s..." % (address[0], address[1])) + logger.log(logging.INFO, "Server is now listening on %s:%s..." % (address[0], address[1])) while 1: recv_data, addr = s.recvfrom(2048) diff --git a/gamespy_player_search_server.py b/gamespy_player_search_server.py index 0a12004..76aa780 100644 --- a/gamespy_player_search_server.py +++ b/gamespy_player_search_server.py @@ -17,12 +17,14 @@ logger_name = "GameSpyPlayerSearchServer" logger_filename = "gamespy_player_search_server.log" logger = utils.create_logger(logger_name, logger_filename, -1, logger_output_to_console, logger_output_to_file) +address = ("0.0.0.0", 29901) + class GameSpyPlayerSearchServer(object): def __init__(self): pass def start(self): - endpoint_search = serverFromString(reactor, "tcp:29901") + endpoint_search = serverFromString(reactor, "tcp:%d:interface=%s" % (address[1], address[0])) conn_search = endpoint_search.listen(PlayerSearchFactory()) try: @@ -34,7 +36,7 @@ class GameSpyPlayerSearchServer(object): class PlayerSearchFactory(Factory): def __init__(self): - logger.log(logging.INFO, "Now listening for player search connections...") + logger.log(logging.INFO, "Now listening for player search connections on %s:%d...", address[0], address[1]) def buildProtocol(self, address): return PlayerSearch(address) @@ -82,7 +84,7 @@ class PlayerSearch(LineReceiver): opids = data_parsed['opids'].split('|') if len(opids) != numopids: - print "Unexpected number of opids, got %d, expected %d." % (len(opids), numopids) + self.log(logging.ERROR, "Unexpected number of opids, got %d, expected %d." % (len(opids), numopids)) # Return all uniquenicks despite any unexpected/missing opids for opid in opids: diff --git a/gamespy_profile_server.py b/gamespy_profile_server.py index 9465951..41e73da 100644 --- a/gamespy_profile_server.py +++ b/gamespy_profile_server.py @@ -20,12 +20,13 @@ logger_name = "GameSpyProfileServer" logger_filename = "gamespy_profile_server.log" logger = utils.create_logger(logger_name, logger_filename, -1, logger_output_to_console, logger_output_to_file) +address = ("0.0.0.0", 29900) class GameSpyProfileServer(object): def __init__(self): pass def start(self): - endpoint = serverFromString(reactor, "tcp:29900") + endpoint = serverFromString(reactor, "tcp:%d:interface=%s" % (address[1], address[0])) conn = endpoint.listen(PlayerFactory()) try: @@ -37,7 +38,7 @@ class GameSpyProfileServer(object): class PlayerFactory(Factory): def __init__(self): # Instead of storing the sessions in the database, it might make more sense to store them in the PlayerFactory. - logger.log(logging.INFO, "Now listening for connections...") + logger.log(logging.INFO, "Now listening for connections on %s:%d...", address[0], address[1]) self.sessions = {} def buildProtocol(self, address): @@ -78,7 +79,7 @@ class PlayerSession(LineReceiver): return ipaddress def connectionMade(self): - self.log(logging.DEBUG, "Received connection from %s:%d" % (self.address.host, self.address.port)) + self.log(logging.INFO, "Received connection from %s:%d" % (self.address.host, self.address.port)) # Create new session id self.session = "" @@ -98,11 +99,11 @@ class PlayerSession(LineReceiver): self.transport.write(bytes(msg)) def connectionLost(self, reason): - self.log(logging.DEBUG, "Client disconnected") + self.log(logging.INFO, "Client disconnected") if self.session in self.sessions: del self.sessions[self.session] - self.log(logging.DEBUG, "Deleted session %d" % self.sessions) + self.log(logging.INFO, "Deleted session %d" % self.sessions) def rawDataReceived(self, data): self.log(logging.DEBUG, "RESPONSE: '%s'..." % data) @@ -138,7 +139,7 @@ class PlayerSession(LineReceiver): self.perform_authadd(data_parsed) else: # Maybe write unknown commands to a separate file later so new data can be collected more easily? - self.log(logging.DEBUG, "Found unknown command, don't know how to handle '%s'." % data_parsed['__cmd__']) + self.log(logging.ERROR, "Found unknown command, don't know how to handle '%s'." % data_parsed['__cmd__']) def perform_login(self, data_parsed): authtoken_parsed = gs_utils.parse_authtoken(data_parsed['authtoken']) @@ -197,7 +198,7 @@ class PlayerSession(LineReceiver): # Verify the client's response valid_response = gs_utils.generate_response(self.challenge, authtoken_parsed['challenge'], data_parsed['challenge'], data_parsed['authtoken']) if data_parsed['response'] != valid_response: - self.log(logging.DEBUG, "ERROR: Got invalid response. Got %s, expected %s" % (data_parsed['response'], valid_response)) + self.log(logging.ERROR, "ERROR: Got invalid response. Got %s, expected %s" % (data_parsed['response'], valid_response)) proof = gs_utils.generate_proof(self.challenge, authtoken_parsed['challenge'], data_parsed['challenge'], data_parsed['authtoken']) @@ -209,7 +210,7 @@ class PlayerSession(LineReceiver): if profileid == None: # Handle case where the user is invalid - print "Invalid password" + self.log(logging.ERROR, "Invalid password") if profileid != None: # Successfully logged in or created account, continue creating session. @@ -247,7 +248,7 @@ class PlayerSession(LineReceiver): self.get_status_from_friends() def perform_logout(self, data_parsed): - print "Session %s has logged off" % (data_parsed['sesskey']) + self.log(logging.INFO, "Session %s has logged off" % (data_parsed['sesskey'])) self.db.delete_session(data_parsed['sesskey']) def perform_getprofile(self, data_parsed): @@ -256,6 +257,8 @@ class PlayerSession(LineReceiver): self.profileid = int(profile['profileid']) # Wii example: \pi\\profileid\474888031\nick\5pde5vhn1WR9E2g1t533\userid\442778352\email\5pde5vhn1WR9E2g1t533@nds\sig\b126556e5ee62d4da9629dfad0f6b2a8\uniquenick\5pde5vhn1WR9E2g1t533\pid\11\lon\0.000000\lat\0.000000\loc\\id\2\final\ + sig = utils.generate_random_hex_str(32) + msg_d = [] msg_d.append(('__cmd__', "pi")) msg_d.append(('__cmd_val__', "")) @@ -263,7 +266,7 @@ class PlayerSession(LineReceiver): msg_d.append(('nick', profile['uniquenick'])) msg_d.append(('userid', profile['userid'])) msg_d.append(('email', profile['email'])) - msg_d.append(('sig', profile['sig'])) + msg_d.append(('sig', sig)) msg_d.append(('uniquenick', profile['uniquenick'])) msg_d.append(('pid', profile['pid'])) @@ -437,9 +440,9 @@ class PlayerSession(LineReceiver): buddies = self.db.get_pending_buddy_requests(self.profileid) profile = self.db.get_profile_from_profileid(self.profileid) + sig = utils.generate_random_hex_str(32) msg = "\r\n\r\n" - if "sig" in profile: - msg += "|signed|" + profile['sig'] + msg += "|signed|" + sig for buddy in buddies: msg_d = [] diff --git a/gamespy_qr_server.py b/gamespy_qr_server.py index 8a54003..12ae1f8 100644 --- a/gamespy_qr_server.py +++ b/gamespy_qr_server.py @@ -55,7 +55,7 @@ class GameSpyQRServer(object): self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) self.socket.bind(address) - logger.log(logging.DEBUG, "Server is now listening on %s:%s..." % (address[0], address[1])) + logger.log(logging.INFO, "Server is now listening on %s:%s..." % (address[0], address[1])) self.wait_loop() @@ -225,16 +225,16 @@ class GameSpyQRServer(object): elif recv_data[0] == '\x04': # Add Error - self.log(logging.DEBUG, address, "NOT IMPLEMENTED! Received add error from %s:%s... %s" % (address[0], address[1], recv_data[5:])) + self.log(logging.WARNING, address, "NOT IMPLEMENTED! Received add error from %s:%s... %s" % (address[0], address[1], recv_data[5:])) elif recv_data[0] == '\x05': # Echo Response - self.log(logging.DEBUG, address, "NOT IMPLEMENTED! Received echo response from %s:%s... %s" % (address[0], address[1], recv_data[5:])) + self.log(logging.WARNING, address, "NOT IMPLEMENTED! Received echo response from %s:%s... %s" % (address[0], address[1], recv_data[5:])) elif recv_data[0] == '\x06': # Client Message - self.log(logging.DEBUG, address, "NOT IMPLEMENTED! Received echo from %s:%s... %s" % (address[0], address[1], recv_data[5:])) + self.log(logging.WARNING, address, "NOT IMPLEMENTED! Received echo from %s:%s... %s" % (address[0], address[1], recv_data[5:])) elif recv_data[0] == '\x07': # Client Message Ack - self.log(logging.DEBUG, address, "NOT IMPLEMENTED! Received client message ack from %s:%s... %s" % (address[0], address[1], recv_data[5:])) + self.log(logging.WARNING, address, "NOT IMPLEMENTED! Received client message ack from %s:%s... %s" % (address[0], address[1], recv_data[5:])) elif recv_data[0] == '\x08': # Keep Alive self.log(logging.DEBUG, address, "Received keep alive from %s:%s..." % (address[0], address[1])) @@ -246,8 +246,8 @@ class GameSpyQRServer(object): elif recv_data[0] == '\x0a': # Client Registered # Only sent to client, never received? - self.log(logging.DEBUG, address, "NOT IMPLEMENTED! Received client registered from %s:%s... %s" % (address[0], address[1], recv_data[5:])) + self.log(logging.WARNING, address, "NOT IMPLEMENTED! Received client registered from %s:%s... %s" % (address[0], address[1], recv_data[5:])) else: - self.log(logging.DEBUG, address, "Unknown request from %s:%s:" % (address[0], address[1])) + self.log(logging.ERROR, address, "Unknown request from %s:%s:" % (address[0], address[1])) self.log(logging.DEBUG, address, utils.pretty_print_hex(recv_data)) diff --git a/gamespy_server_browser_server.py b/gamespy_server_browser_server.py index b20a16c..b46d649 100644 --- a/gamespy_server_browser_server.py +++ b/gamespy_server_browser_server.py @@ -41,12 +41,13 @@ GameSpyServerDatabase.register("modify_server_list") GameSpyServerDatabase.register("find_servers") GameSpyServerDatabase.register("find_server_by_address") +address = ("0.0.0.0", 28910) class GameSpyServerBrowserServer(object): def __init__(self): pass def start(self): - endpoint = serverFromString(reactor, "tcp:28910") + endpoint = serverFromString(reactor, "tcp:%d:interface=%s" % (address[1], address[0])) conn = endpoint.listen(SessionFactory()) try: @@ -59,7 +60,7 @@ class GameSpyServerBrowserServer(object): class SessionFactory(Factory): def __init__(self): - logger.log(logging.DEBUG, "Now listening for connections...") + logger.log(logging.INFO, "Now listening for connections on %s:%d...", address[0], address[1]) self.secret_key_list = gs_utils.generate_secret_keys("gslist.cfg") def buildProtocol(self, address):