diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/piztor/import.py | 16 | ||||
-rw-r--r-- | server/piztor/model.py | 36 | ||||
-rw-r--r-- | server/piztor/prober.py | 72 | ||||
-rw-r--r-- | server/piztor/server.py | 51 |
4 files changed, 92 insertions, 83 deletions
diff --git a/server/piztor/import.py b/server/piztor/import.py index c91aae9..5b23299 100644 --- a/server/piztor/import.py +++ b/server/piztor/import.py @@ -5,10 +5,11 @@ from model import * path = "root:helloworld@localhost/piztor" class UserData: - def __init__(self, username, password, gid, sex): + def __init__(self, username, password, comp_id, sec_id, sex): self.username = username self.password = password - self.gid = gid + self.comp_id = comp_id + self.sec_id = sec_id self.sex = sex def create_database(): @@ -21,7 +22,10 @@ def import_user_data(data): Session = sessionmaker(bind = engine) session = Session() for user in data: - um = UserModel(username = user.username, gid = user.gid, sex = user.sex) + um = UserModel(username = user.username, + comp_id = user.comp_id, + sec_id = user.sec_id, + sex = user.sex) um.auth = UserAuth(user.password) um.location = LocationInfo(lat = 0, lng = 0) session.add(um) @@ -39,7 +43,11 @@ if __name__ == '__main__': while True: line = f.readline().split() if len(line) == 0: break - data.append(UserData(line[0], line[1], line[2], line[3])) + data.append(UserData(username = line[0], + password = line[1], + comp_id = line[2], + sec_id = line[3], + sex = line[4])) create_database() import_user_data(data) diff --git a/server/piztor/model.py b/server/piztor/model.py index 961f67f..41b9a88 100644 --- a/server/piztor/model.py +++ b/server/piztor/model.py @@ -1,4 +1,5 @@ -from sqlalchemy import Column, Integer, String, Float, ForeignKey, LargeBinary, Boolean +from sqlalchemy import Column, Integer, String, Float, ForeignKey, Boolean +from sqlalchemy.dialects.mysql import BLOB, TINYINT from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship, backref @@ -9,6 +10,11 @@ _TOKEN_LEN = 16 MAX_USERNAME_SIZE = 20 MAX_PASSWORD_SIZE = 20 +_table_typical_settings = { + 'mysql_engine' : 'InnoDB', + 'mysql_charset' : 'utf8', + 'mysql_auto_increment' : '1'} + class _TableName: # avoid typoes UserModel = 'users' LocationInfo = 'location_info' @@ -16,26 +22,20 @@ class _TableName: # avoid typoes class UserModel(Base): __tablename__ = _TableName.UserModel - __table_args__ = { - 'mysql_engine' : 'InnoDB', - 'mysql_charset' : 'utf8', - 'mysql_auto_increment' : '1'} - + __table_args__ = _table_typical_settings id = Column(Integer, primary_key = True) - gid = Column(Integer, nullable = False) + sec_id = Column(TINYINT) + comp_id = Column(TINYINT) username = Column(String(MAX_USERNAME_SIZE), unique = True, nullable = False) sex = Column(Boolean, nullable = False) location = None auth = None + sec = None class LocationInfo(Base): - __table_args__ = { - 'mysql_engine' : 'InnoDB', - 'mysql_charset' : 'utf8', - 'mysql_auto_increment' : '1'} - __tablename__ = _TableName.LocationInfo + __table_args__ = _table_typical_settings uid = Column(Integer, ForeignKey(_TableName.UserModel + '.id'), primary_key = True) @@ -60,17 +60,13 @@ def _random_binary_string(length): return urandom(length) class UserAuth(Base): - __table_args__ = { - 'mysql_engine' : 'InnoDB', - 'mysql_charset' : 'utf8', - 'mysql_auto_increment' : '1'} - __tablename__ = _TableName.UserAuth + __table_args__ = _table_typical_settings uid = Column(Integer, ForeignKey(_TableName.UserModel + '.id'), primary_key = True) - password = Column(LargeBinary) - salt = Column(LargeBinary) - token = Column(LargeBinary) + password = Column(BLOB) + salt = Column(BLOB) + token = Column(BLOB) user = relationship("UserModel", uselist = False, backref = backref("auth", uselist = False, diff --git a/server/piztor/prober.py b/server/piztor/prober.py index 613d667..daf63b9 100644 --- a/server/piztor/prober.py +++ b/server/piztor/prober.py @@ -77,7 +77,6 @@ username = "hello" password = "world" #username = "1234567890123456789012" #password = "world12345678901234567890" -gid = 1 failed_cnt = 0 if len(argv) == 2: @@ -96,11 +95,11 @@ for i in xrange(10): failed_cnt += 1 continue if pl != len(resp): print "God!" -# print "size: " + str((pl, len(resp))) -# print "opt: " + str(optcode) -# print "status: " + str(status) -# print "uid: " + str(uid) -# print "token: " + get_hex(token) + print "size: " + str((pl, len(resp))) + print "opt: " + str(optcode) + print "status: " + str(status) + print "uid: " + str(uid) + print "token: " + get_hex(token) resp = send(gen_update_location(token, username, random(), random())) try: @@ -108,51 +107,52 @@ for i in xrange(10): except: print "fuck2" if pl != len(resp): print "God!" -# print "size: " + str((pl, len(resp))) -# print "opt: " + str(optcode) -# print "status: " + str(status) - - resp = send(gen_request_location(token, username, gid)) - try: - pl, optcode, status = unpack("!LBB", resp[:6]) - except: - print "fuck3" - if pl != len(resp): print "God!" -# print "size: " + str((pl, len(resp))) - idx = 6 -# print "length: " + str(len(resp[6:])) - try: - while idx < pl: -# print len(resp[idx:idx + 20]) - uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20]) - idx += 20 - except: - print "fuck4" -# print (uid, lat, lng) - + print "size: " + str((pl, len(resp))) + print "opt: " + str(optcode) + print "status: " + str(status) + resp = send(gen_request_user_info(token, username, uid)) try: pl, optcode, status = unpack("!LBB", resp[:6]) except: print "fuck5" if pl != len(resp): print "God!" -# print "size: " + str((pl, len(resp))) + print "size: " + str((pl, len(resp))) idx = 6 try: while idx < pl: info_key, = unpack("!B", resp[idx:idx + 1]) idx += 1 -# print info_key if info_key == 0x00: - info_value, = unpack("!L", resp[idx:idx + 4]) + gid, = unpack("!L", resp[idx:idx + 4]) idx += 4 + print "gid: {}".format(str(gid)) elif info_key == 0x01: - info_value, = unpack("!B", resp[idx:idx + 1]) + sex, = unpack("!B", resp[idx:idx + 1]) idx += 1 -# print (info_key, info_value) + print "sex: {}".format(str(sex)) except: print "fuck6" + + resp = send(gen_request_location(token, username, gid)) + try: + pl, optcode, status = unpack("!LBB", resp[:6]) + except: + print "fuck3" + if pl != len(resp): print "God!" + print "size: " + str((pl, len(resp))) + idx = 6 + print "length: " + str(len(resp[6:])) + try: + while idx < pl: + print len(resp[idx:idx + 20]) + uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20]) + idx += 20 + print (uid, lat, lng) + except: + print "fuck4" + resp = send(gen_logout(token, username)) try: @@ -160,9 +160,9 @@ for i in xrange(10): except: print "fuck7" if pl != len(resp): print "God!" -# print "size: " + str((pl, len(resp))) -# print "opt: " + str(optcode) -# print "status: " + str(status) + print "size: " + str((pl, len(resp))) + print "opt: " + str(optcode) + print "status: " + str(status) sleep(10) print failed_cnt diff --git a/server/piztor/server.py b/server/piztor/server.py index 18c2e2d..f58cd2d 100644 --- a/server/piztor/server.py +++ b/server/piztor/server.py @@ -3,7 +3,7 @@ from twisted.internet.protocol import Factory from twisted.internet.endpoints import TCP4ServerEndpoint from twisted.protocols.policies import TimeoutMixin -from sqlalchemy import create_engine +from sqlalchemy import create_engine, and_ from sqlalchemy.orm import sessionmaker from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound @@ -232,13 +232,13 @@ class LocationInfoHandler(RequestHandler): username, tail = RequestHandler.trunc_padding(tr_data[32:]) if username is None: raise struct.error - gid, = struct.unpack("!L", tail) + a, b, comp_id, sec_id = struct.unpack("!BBBB", tail) except struct.error: raise BadReqError("Location request: Malformed request body") -# logger.info("Trying to request locatin with " \ -# "(token = {0}, gid = {1})" \ -# .format(get_hex(token), gid)) + logger.info("Trying to request locatin with " \ + "(token = {0}, comp_id = {1}, sec_id = {2})" \ + .format(get_hex(token), comp_id, sec_id)) uauth = RequestHandler.get_uauth(token, username, self.session) # Auth failure @@ -248,7 +248,13 @@ class LocationInfoHandler(RequestHandler): _OptCode.location_info, _StatusCode.failure) - ulist = self.session.query(UserModel).filter(UserModel.gid == gid).all() + if sec_id == 0xff: # All members in the company + ulist = self.session.query(UserModel) \ + .filter(UserModel.comp_id == comp_id).all() + else: + ulist = self.session.query(UserModel) \ + .filter(and_(UserModel.comp_id == comp_id, + UserModel.sec_id == sec_id)).all() reply = struct.pack( "!LBB", self._response_size(len(ulist)), @@ -261,11 +267,11 @@ class LocationInfoHandler(RequestHandler): return reply -def pack_int(val): - return struct.pack("!L", val) +def pack_gid(user): + return struct.pack("!BBBB", 0, 0, user.comp_id, user.sec_id) -def pack_bool(val): - return struct.pack("!B", 0x01 if val else 0x00) +def pack_sex(user): + return struct.pack("!B", 0x01 if user.sex else 0x00) class UserInfoHandler(RequestHandler): @@ -283,15 +289,14 @@ class UserInfoHandler(RequestHandler): _OptCode.user_info, _StatusCode.failure) - _code_map = {0x00 : ('gid', pack_int), - 0x01 : ('sex', pack_bool)} + _code_map = {0x00 : pack_gid, + 0x01 : pack_sex} @classmethod def pack_entry(cls, user, entry_code): - attr, pack_method = cls._code_map[entry_code] + pack_method = cls._code_map[entry_code] info_key = entry_code - info_value = getattr(user, attr) - return struct.pack("!B", info_key) + pack_method(info_value) + return struct.pack("!B", info_key) + pack_method(user) def handle(self, tr_data): self.check_size(tr_data) @@ -305,9 +310,9 @@ class UserInfoHandler(RequestHandler): except struct.error: raise BadReqError("User info request: Malformed request body") -# logger.info("Trying to request locatin with " \ -# "(token = {0}, uid = {1})" \ -# .format(get_hex(token), uid)) + logger.info("Trying to request locatin with " \ + "(token = {0}, uid = {1})" \ + .format(get_hex(token), uid)) uauth = RequestHandler.get_uauth(token, username, self.session) # Auth failure @@ -354,10 +359,10 @@ class UserLogoutHandler(RequestHandler): except struct.error: raise BadReqError("User logout: Malformed request body") -# logger.info("Trying to update location with " -# "(token = {0}, username = {1}, lat = {2}, lng = {3})"\ -# .format(get_hex(token), username, lat, lng)) -# + logger.info("Trying to logout with " + "(token = {0}, username = {1})"\ + .format(get_hex(token), username)) + uauth = RequestHandler.get_uauth(token, username, self.session) # Authentication failure if uauth is None: @@ -422,7 +427,7 @@ class PTP(Protocol, TimeoutMixin): if len(self.buff) == self.length: h = PTP.handlers[self.optcode]() reply = h.handle(self.buff[5:]) -# logger.info("Wrote: %s", get_hex(reply)) + logger.info("Wrote: %s", get_hex(reply)) self.transport.write(reply) self.transport.loseConnection() elif len(self.buff) > self.length: |