Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
kyle95wm committed Mar 5, 2018
1 parent ebd3cb5 commit 7fdf418
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 95 deletions.
84 changes: 21 additions & 63 deletions gamespy/gs_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,9 @@ def initialize_database(self):
tx.nonquery("CREATE TABLE IF NOT EXISTS gamestat_profile (profileid INT, dindex TEXT, ptype TEXT, data TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS gameinfo (profileid INT, dindex TEXT, ptype TEXT, data TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS nas_logins (userid TEXT, authtoken TEXT, data TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS ap_banned (bssid TEXT, timestamp INT(11), reason TEXT, ubtime INT(11))")
tx.nonquery("CREATE TABLE IF NOT EXISTS ip_banned (ipaddr TEXT, timestamp INT(11), reason TEXT, ubtime INT(11))")
tx.nonquery("CREATE TABLE IF NOT EXISTS console_macadr_banned (macadr TEXT, timestamp INT(11), reason TEXT, ubtime INT(11))")
tx.nonquery("CREATE TABLE IF NOT EXISTS console_csnum_banned (csnum TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS console_cfc_banned (cfc TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS pending (macadr TEXT, csnum TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS registered (macadr TEXT, csnum TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS banned (banned_id TEXT, timestamp INT(11), reason TEXT, ubtime INT(11), type TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS consoles (macadr TEXT, csnum TEXT, platform TEXT, abuse INT, enabled INT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS allowed_games (gamecd TEXT)")
tx.nonquery("CREATE TABLE IF NOT EXISTS profile_banned (gsbrcd TEXT, timestamp INT(11), reason TEXT, ubtime INT(11))")

# Create some indexes for performance.
tx.nonquery("CREATE UNIQUE INDEX IF NOT EXISTS gamestatprofile_triple on gamestat_profile(profileid,dindex,ptype)")
Expand Down Expand Up @@ -410,90 +404,65 @@ def get_nas_login_from_userid(self, userid):
def is_ap_banned(self,postdata):
if 'bssid' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM ap_banned WHERE bssid = ? AND ubtime > ?",(postdata['bssid'],time.time(),))
row = tx.queryone("SELECT COUNT(*) FROM banned WHERE banned_id = ? AND ubtime > ? AND type = 'ap'",(postdata['bssid'],time.time(),))
return int(row[0]) > 0

def is_ip_banned(self,postdata):
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM ip_banned WHERE ipaddr = ? AND ubtime > ?",(postdata['ipaddr'],time.time(),))
row = tx.queryone("SELECT COUNT(*) FROM banned WHERE banned_id = ? AND ubtime > ? AND type = 'ip'",(postdata['ipaddr'],time.time(),))
return int(row[0]) > 0

def is_console_macadr_banned(self,postdata):
if 'macadr' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM console_macadr_banned WHERE macadr = ? AND ubtime > ?",(postdata['macadr'],time.time(),))
row = tx.queryone("SELECT COUNT(*) FROM banned WHERE banned_id = ? AND ubtime > ? and type = 'console'",(postdata['macadr'],time.time(),))
return int(row[0]) > 0
else:
return False

def is_console_csnum_banned(self,postdata):
def console_register(self,postdata):
if 'csnum' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM console_csnum_banned WHERE csnum = ?",(postdata['csnum'],))
return int(row[0]) > 0
else:
return False

def is_console_cfc_banned(self,postdata):
if 'cfc' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM console_cfc_banned WHERE cfc = ?",(postdata['cfc'],))
return int(row[0]) > 0
else:
return False

def pending(self,postdata):
if 'csnum' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM pending WHERE macadr = ? and csnum = ?",(postdata['macadr'], postdata['csnum']))
#return int(row[0]) > 0
row = tx.queryone("SELECT COUNT(*) FROM consoles WHERE macadr = ? and platform = 'wii'",(postdata['macadr'],))
result = int(row[0])
if result == 0:
tx.nonquery("INSERT INTO pending (macadr, csnum) VALUES (?,?)", (postdata['macadr'], postdata['csnum']))
# Comment out the line below to disable auto activation of consoles
tx.nonquery("INSERT INTO registered (macadr, csnum) VALUES (?,?)", (postdata['macadr'], postdata['csnum']))
# Change the 1 to a 0 to require manual console activation
tx.nonquery("INSERT INTO consoles (macadr, csnum, platform, enabled, abuse) VALUES (?,?,'wii','1','0')", (postdata['macadr'], postdata['csnum']))
return result > 0
else:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM pending WHERE macadr = ?",(postdata['macadr'],))
#return int(row[0]) > 0
row = tx.queryone("SELECT COUNT(*) FROM consoles WHERE macadr = ? and platform = 'other'",(postdata['macadr'],))
result = int(row[0])
if result == 0:
tx.nonquery("INSERT INTO pending (macadr) VALUES (?)", (postdata['macadr'],))
# Comment out the line below to disable auto activation of consoles
tx.nonquery("INSERT INTO registered (macadr) VALUES (?)", (postdata['macadr'],))
# Change the 1 to a 0 to require manual console activation
tx.nonquery("INSERT INTO consoles (macadr, platform, enabled, abuse) VALUES (?,'other','1','0')", (postdata['macadr'],))
return result > 0

def registered(self,postdata):
if 'csnum' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM registered WHERE macadr = ? and csnum = ?",(postdata['macadr'], postdata['csnum']))
return int(row[0]) > 0
else:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM registered WHERE macadr = ?",(postdata['macadr'],))

def pending_console(self,postdata):
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM consoles WHERE macadr = ? and enabled = 0",(postdata['macadr'],))
return int(row[0]) > 0

def allowed_games(self,postdata):
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM allowed_games WHERE gamecd = ?",(postdata['gamecd'][:3],))
return int(row[0]) > 0

def is_profile_banned(self,postdata):
if 'gsbrcd' in postdata:
with Transaction (self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM profile_banned WHERE gsbrcd = ? AND ubtime > ?",(postdata['gsbrcd'],time.time(),))
row = tx.queryone("SELECT COUNT(*) FROM banned WHERE banned_id = ? AND ubtime > ? AND type = 'profile'",(postdata['gsbrcd'],time.time(),))
return int(row[0]) > 0
else:
return False

def console_abuse(self,postdata): # ONLY FOR WII CONSOLES
if 'csnum' in postdata:
with Transaction(self.conn) as tx:
row = tx.queryone("SELECT COUNT(*) FROM pending WHERE csnum = ?",(postdata['csnum'],))
row = tx.queryone("SELECT COUNT(*) FROM registered WHERE csnum = ?",(postdata['csnum'],))
#return int(row[0]) > 0
row = tx.queryone("SELECT COUNT(*) FROM consoles WHERE csnum = ?",(postdata['csnum'],))
result = int(row[0])
if result > 2:
tx.nonquery("UPDATE consoles SET abuse = 1 WHERE csnum = ?", (postdata['csnum'],))
return True
else:
return False
Expand All @@ -504,17 +473,6 @@ def console_abuse(self,postdata): # ONLY FOR WII CONSOLES
def valid_mac(self,postdata):
return len(postdata["macadr"]) == 12

# def mkw_specific(self,postdata):
# if 'bssid' not in postdata:
# with Transaction(self.conn) as tx:
# row = tx.queryone("SELECT * FROM allowed_games WHERE gamecd = ?",(postdata['gamecd'][:3],))
# result = (row[0])
# if result == "RMC":
# if 'csnum' and 'cfc' in postdata:
# return True
# else:
# return False


def get_next_available_userid(self):
with Transaction(self.conn) as tx:
Expand Down
37 changes: 5 additions & 32 deletions nas_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,6 @@ def do_POST(self):
"retry": "1",
"reason": "User's console is banned."
}
elif self.server.db.is_console_cfc_banned(post):
logger.log(logging.DEBUG, "login denied for banned console"+str(post))
ret = {
"datetime": time.strftime("%Y%m%d%H%M%S"),
"returncd": "3915",
"locator": "gamespy.com",
"retry": "1",
"reason": "User's console is banned."
}
elif self.server.db.is_console_csnum_banned(post):
logger.log(logging.DEBUG, "login denied for banned console"+str(post))
ret = {
"datetime": time.strftime("%Y%m%d%H%M%S"),
"returncd": "3915",
"locator": "gamespy.com",
"retry": "1",
"reason": "User's console is banned."
}
elif self.server.db.console_abuse(post):
logger.log(logging.DEBUG, "Login denied - Console is banned due to abuse of identifiers"+str(post))
ret = {
Expand All @@ -188,19 +170,18 @@ def do_POST(self):
"locator": "gamespy.com",
"retry": "1",
}
elif not self.server.db.pending(post):
logger.log(logging.DEBUG, "Login denied - Unknown console"+str(post))
elif not self.server.db.console_register(post):
ret = {
"datetime": time.strftime("%Y%m%d%H%M%S"),
"returncd": "3921",
"returncd": "2222",
"locator": "gamespy.com",
"retry": "1",
}
elif not self.server.db.registered(post):
logger.log(logging.DEBUG, "Login denied - console pending"+str(post))
elif self.server.db.pending_console(post):
logger.log(logging.DEBUG, "Console pending manual activation"+str(post))
ret = {
"datetime": time.strftime("%Y%m%d%H%M%S"),
"returncd": "3888",
"returncd": "3000",
"locator": "gamespy.com",
"retry": "1",
}
Expand All @@ -212,14 +193,6 @@ def do_POST(self):
"locator": "gamespy.com",
"retry": "1",
}
# elif not self.server.db.mkw_specific(post):
# logger.log(logging.DEBUG, "Login denied for Mario Kart Wii - incomplete identifiers!"+str(post))
# ret = {
# "datetime": time.strftime("%Y%m%d%H%M%S"),
# "returncd": "2222",
# "locator": "gamespy.com",
# "retry": "1",
# }
elif not self.server.db.valid_mac(post):
logger.log(logging.DEBUG, "Login denied because MAC is not a valid length"+str(post))
ret = {
Expand Down

0 comments on commit 7fdf418

Please sign in to comment.