summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-30 13:54:37 +0800
committerTeddy <[email protected]>2013-08-30 13:54:37 +0800
commit1f9ffa4a59a5f84cbbfadee4d5407b82b8224b5f (patch)
tree4a0ccca2ee5a046b7b962c6485e8da735a79d53b /server
parentbb359726671504297c5b48badc24bc7975d490ca (diff)
...
Diffstat (limited to 'server')
-rw-r--r--server/piztor/mesg_receiver.py6
-rw-r--r--server/piztor/mesg_sender.py8
-rw-r--r--server/piztor/prober.py20
-rw-r--r--server/piztor/ptp_send.py150
-rw-r--r--server/piztor/server.py32
5 files changed, 75 insertions, 141 deletions
diff --git a/server/piztor/mesg_receiver.py b/server/piztor/mesg_receiver.py
index d6fbf35..5f84d43 100644
--- a/server/piztor/mesg_receiver.py
+++ b/server/piztor/mesg_receiver.py
@@ -4,8 +4,8 @@ from time import sleep
from sys import argv
from ptp_send import *
-username = "a"
-password = "a"
+username = "hello"
+password = "world"
#username = "1234567890123456789012"
#password = "world12345678901234567890"
@@ -16,5 +16,5 @@ if len(argv) == 3:
username = argv[1]
password = argv[2]
-uid, token = user_auth(username, password)
+token = user_auth(username, password)
open_push_tunnel(token, username)
diff --git a/server/piztor/mesg_sender.py b/server/piztor/mesg_sender.py
index 96a3a9d..41f2858 100644
--- a/server/piztor/mesg_sender.py
+++ b/server/piztor/mesg_sender.py
@@ -4,8 +4,8 @@ from time import sleep
from sys import argv
from ptp_send import *
-username = "a"
-password = "a"
+username = "hello"
+password = "world"
mesg = "niu x push!"
#username = "1234567890123456789012"
#password = "world12345678901234567890"
@@ -19,10 +19,10 @@ if len(argv) == 4:
mesg = argv[3]
-uid, token = user_auth(username, password)
+token = user_auth(username, password)
send_text_mesg(token, username, mesg)
send_text_mesg(token, username, "a")
update_location(token, username, 1.2345, 2.468)
update_location(token, username, 1.2345, 2.468)
send_text_mesg(token, username, "the last")
-#logout(token, username)
+logout(token, username)
diff --git a/server/piztor/prober.py b/server/piztor/prober.py
index 00208cd..623089f 100644
--- a/server/piztor/prober.py
+++ b/server/piztor/prober.py
@@ -4,8 +4,8 @@ from time import sleep
from sys import argv
from ptp_send import *
-username = "a"
-password = "a"
+username = "haha"
+password = "haha"
#username = "1234567890123456789012"
#password = "world12345678901234567890"
@@ -17,16 +17,8 @@ if len(argv) == 3:
password = argv[2]
-for i in xrange(10):
+token = user_auth(username, password)
- uid, token = user_auth(username, password)
- update_location(token, username, random(), random())
-
- comp_id, sec_id, sex = request_user_info(token, username, uid)
- if comp_id:
- request_location(token, username, comp_id, sec_id)
- request_location(token, username, comp_id, 0xff)
-
- logout(token, username)
-
- sleep(10)
+update_sub(token, username,
+ [(23, 15), (23, 14)])
+user_info(token, username, 23, 15)
diff --git a/server/piztor/ptp_send.py b/server/piztor/ptp_send.py
index 745eeb5..d8f783f 100644
--- a/server/piztor/ptp_send.py
+++ b/server/piztor/ptp_send.py
@@ -27,95 +27,57 @@ class _SectionSize:
host = "localhost" #"localhost"
port = 2223
-def gen_auth(username, password):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- len(username) + \
- _SectionSize.PADDING + \
- len(password) + \
- _SectionSize.PADDING
-
- data = pack("!LB", length, 0x00)
- data += username
- data += "\0"
- data += password
- data += "\0"
- return data
-
-def gen_auth_head_length(token, username):
- return _SectionSize.USER_TOKEN + \
- len(username) + \
- _SectionSize.PADDING
+def pack_data(optcode, data):
+ return pack("!LB", _SectionSize.LENGTH + \
+ _SectionSize.OPT_ID + \
+ len(data), optcode) + data
+def gen_auth(username, password):
+ return pack_data(0x00, username + chr(0) + \
+ password + chr(0))
def gen_update_location(token, username, lat, lng):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username) + \
- _SectionSize.LATITUDE + \
- _SectionSize.LONGITUDE
-
- data = pack("!LB32s", length, 0x01, token)
+ data = pack("!32s", token)
data += username
data += chr(0)
data += pack("!dd", lat, lng)
- return data
+ return pack_data(0x01, data)
-def gen_request_location(token, username, comp_id, sec_id):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username) + \
- _SectionSize.GROUP_ID
-
- data = pack("!LB32s", length, 0x02, token)
+def gen_user_info(token, username, gid):
+ data = pack("!32s", token)
data += username
data += chr(0)
- data += pack("!BB", comp_id, sec_id)
- return data
-
+ data += pack("!H", gid)
+ return pack_data(0x02, data)
-def gen_request_user_info(token, username, uid):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username) + \
- _SectionSize.USER_ID
-
- data = pack("!LB32s", length, 0x03, token)
+def gen_update_sub(token, username, sub):
+ data = pack("!32s", token)
data += username
data += chr(0)
- data += pack("!L", uid)
- return data
+ for gid in sub:
+ data += pack("!H", gid)
+ data += chr(0)
+ return pack_data(0x03, data)
def gen_logout(token, username):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username)
- data = pack("!LB32s", length, 0x04, token)
+ data = pack("!32s", token)
data += username
data += chr(0)
- return data
+ return pack_data(0x04, data)
def gen_open_push_tunnel(token, username):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username)
- data = pack("!LB32s", length, 0x05, token)
+ data = pack("!32s", token)
data += username
data += chr(0)
- return data
+ return pack_data(0x05, data)
def gen_send_text_mesg(token, username, mesg):
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username) + \
- len(mesg) + \
- _SectionSize.PADDING
- data = pack("!LB32s", length, 0x06, token)
+ data = pack("!32s", token)
data += username
data += chr(0)
data += mesg
data += chr(0)
- return data
+ return pack_data(0x06, data)
def send(data):
received = bytes()
@@ -136,33 +98,19 @@ def send(data):
sock.close()
return received
-def request_location(token, username, comp_id, sec_id):
- resp = send(gen_request_location(token, username, comp_id, sec_id))
- try:
- pl, optcode, status = unpack("!LBB", resp[:6])
- if pl != len(resp):
- logger.error("Request location: incorrect packet length")
- idx = 6
- while idx < pl:
- uid, lat, lng = unpack("!Ldd", resp[idx:idx + 20])
- idx += 20
- print (uid, lat, lng)
- except error:
- logger.error("Request location: can not parse the response")
-
def user_auth(username, password):
resp = send(gen_auth(username, password))
try:
- pl, optcode, status, uid, token = unpack("!LBBL32s", resp)
+ pl, optcode, status, token = unpack("!LBB32s", resp[:38])
if pl != len(resp):
logger.error("User authentication: incorrect packet length")
print "status: " + str(status)
- print "uid: " + str(uid)
print "token: " + get_hex(token)
+ print get_hex(resp[38:])
except error:
logger.error("User authentication: can not parse the response")
- return uid, token
+ return token
def update_location(token, username, lat, lng):
resp = send(gen_update_location(token, username, lat, lng))
@@ -176,27 +124,25 @@ def update_location(token, username, lat, lng):
logger.error("Request location: can not parse the response")
-def request_user_info(token, username, uid):
- resp = send(gen_request_user_info(token, username, uid))
+def user_info(token, username, comp_no, sec_no):
+ resp = send(gen_user_info(token, username, comp_no * 256 + sec_no))
+ try:
+ pl, optcode, status = unpack("!LBB", resp[:6])
+ if pl != len(resp):
+ logger.error("Request location: incorrect packet length")
+ print get_hex(resp[6:])
+ except error:
+ logger.error("Request location: can not parse the response")
+
+
+def update_sub(token, username, sub):
+ sub = map(lambda t: t[0] * 256 + t[1], sub)
+ resp = send(gen_update_sub(token, username, sub))
try:
pl, optcode, status = unpack("!LBB", resp[:6])
if pl != len(resp):
logger.error("Request user info: incorrect packet length")
-
- idx = 6
- comp_id = None
- sec_id = None
- sex = None
- while idx < pl:
- info_key, = unpack("!B", resp[idx:idx + 1])
- idx += 1
- if info_key == 0x00:
- comp_id, sec_id = unpack("!BB", resp[idx:idx + 2])
- idx += 2
- elif info_key == 0x01:
- sex, = unpack("!B", resp[idx:idx + 1])
- idx += 1
- return comp_id, sec_id, sex
+ print "status: " + str(status)
except error:
logger.error("Request user info: can not parse the response")
@@ -221,17 +167,9 @@ def send_text_mesg(token, username, mesg):
logger.error("Send text mesg: can not parse the response")
def open_push_tunnel(token, username):
-
- length = _SectionSize.LENGTH + \
- _SectionSize.OPT_ID + \
- gen_auth_head_length(token, username)
- data = pack("!LB32s", length, 0x05, token)
- data += username
- data += chr(0)
-
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
- sock.sendall(data)
+ sock.sendall(gen_open_push_tunnel(token, username))
print get_hex(sock.recv(6))
while True:
diff --git a/server/piztor/server.py b/server/piztor/server.py
index 8362e0f..6f45ad8 100644
--- a/server/piztor/server.py
+++ b/server/piztor/server.py
@@ -46,7 +46,7 @@ _HEADER_SIZE = _SectionSize.LENGTH + \
_SectionSize.OPT_ID
_MAX_TEXT_MESG_SIZE = 1024
-
+_MAX_SUB_LIST_SIZE = 10
_MAX_PENDING_PUSH = 100
class _OptCode:
@@ -224,7 +224,7 @@ class RequestHandler(object):
@classmethod
def pack_user_entry(cls, user):
buff = bytes()
- for entry_code in _code_map:
+ for entry_code in cls._code_map:
buff += cls.pack_info_entry(user, entry_code)
buff += chr(0)
return buff
@@ -244,7 +244,8 @@ class RequestHandler(object):
end = len(data) - 1
while idx < end:
res.append(struct.unpack("!H",
- data[idx:idx + _SectionSize.GROUP_ID]))
+ data[idx:idx + _SectionSize.GROUP_ID])[0])
+ idx += _SectionSize.GROUP_ID
return res
@@ -257,7 +258,7 @@ class UserAuthHandler(RequestHandler):
_SectionSize.PADDING
_failed_response = \
- lambda : self.pack(struct.pack("!B", _StatusCode.failure))
+ lambda self: self.pack(struct.pack("!B", _StatusCode.failure))
def handle(self, tr_data, conn):
@@ -373,7 +374,7 @@ class UserInfoHandler(RequestHandler):
_optcode = _OptCode.user_info
_max_tr_data_size = _MAX_AUTH_HEAD_SIZE + \
- _SectionSize.USER_ID
+ _SectionSize.GROUP_ID
_failed_response = \
lambda : self.pack(struct.pack("!B", _StatusCode.failure))
@@ -404,9 +405,9 @@ class UserInfoHandler(RequestHandler):
u = uauth.user
grp = self.session.query(UserModel) \
- .filter(UserModel.sec_id == gid)
+ .filter(UserModel.sec_id == gid).all()
grp += self.session.query(UserModel) \
- .filter(UserModel.comp_id == gid)
+ .filter(UserModel.comp_id == gid).all()
reply = struct.pack("!B", _StatusCode.sucess)
for user in grp:
@@ -417,10 +418,10 @@ class UpdateSubscription(RequestHandler):
_optcode = _OptCode.update_subscription
_max_tr_data_size = _MAX_AUTH_HEAD_SIZE + \
- _SectionSize.LATITUDE + \
- _SectionSize.LONGITUDE
+ _SectionSize.GROUP_ID * _MAX_SUB_LIST_SIZE + \
+ _SectionSize.PADDING
- def _find_or_create_group(self, gid, session):
+ def _find_or_create_group(self, gid):
q = self.session.query(GroupInfo).filter(GroupInfo.id == gid)
entry = q.first()
if not entry:
@@ -430,19 +431,22 @@ class UpdateSubscription(RequestHandler):
def handle(self, tr_data, conn):
self.check_size(tr_data)
+ print get_hex(tr_data)
logger.info("Reading update subscription data...")
try:
token, = struct.unpack("!32s", tr_data[:32])
+ print "a"
username, tail = RequestHandler.trunc_padding(tr_data[32:])
+ print "b"
if username is None:
raise struct.error
sub_list = RequestHandler.unpack_sub_list(tail)
except struct.error:
- raise BadReqError("Location update: Malformed request body")
+ raise BadReqError("Update Subscription: 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 update subscription with "
+ "(token = {0}, username = {1}, grps = {2})"\
+ .format(get_hex(token), username, str(sub_list)))
uauth = RequestHandler.get_uauth(token, username, self.session)
# Authentication failure