summaryrefslogtreecommitdiff
path: root/server/piztor/ptp_send.py
blob: b82004114412815de6ca48bc875fb7d8bdd81e4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
from struct import *
import socket, logging
from select import select
from time import sleep

FORMAT = "%(asctime)-15s %(message)s"
logging.basicConfig(format = FORMAT)
logger = logging.getLogger('piztor_server')
logger.setLevel(logging.INFO)

def get_hex(data):
    return "".join([hex(ord(c))[2:].zfill(2) for c in data])

class _SectionSize:
    LENGTH = 4
    OPT_ID = 1
    STATUS = 1
    USER_ID = 4
    USER_TOKEN = 32
    GROUP_ID = 2
    ENTRY_CNT = 4
    LATITUDE = 8
    LONGITUDE = 8
    LOCATION_ENTRY = USER_ID + LATITUDE + LONGITUDE
    PADDING = 1

host = "localhost" #"localhost"
port = 2222

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 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 += username
    data += chr(0)
    data += pack("!dd", lat, lng)
    return 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)
    data += username
    data += chr(0)
    data += pack("!BB", comp_id, sec_id)
    return 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)
    data += username
    data += chr(0)
    data += pack("!L", uid)
    return 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 += username
    data += chr(0)
    return 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 += username
    data += chr(0)
    return 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 += username
    data += chr(0)
    data += mesg
    data += chr(0)
    return data

def send(data):
    received = bytes()
    try:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((host, port))
        sock.sendall(data)
        while True:
            rd, wr, err = select([sock], [], [], 10)
            if rd:
                buff = sock.recv(4096)
                if len(buff) == 0:
                    break
                received += buff
            else:
                break
    finally:
        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)
        if pl != len(resp):
            logger.error("User authentication: incorrect packet length")
        print "status: " + str(status)
        print "uid: " + str(uid)
        print "token: " + get_hex(token)
    except error:
        logger.error("User authentication: can not parse the response")

    return uid, token

def update_location(token, username, lat, lng):
    resp = send(gen_update_location(token, username, lat, lng)) 
    print get_hex(resp)
    try:
        pl, optcode, status = unpack("!LBB", resp[:6])
        if pl != len(resp):
            logger.error("Request location: incorrect packet length")
        print "status: " + str(status)
    except error:
        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))
    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
    except error:
        logger.error("Request user info: can not parse the response")

def logout(token, username):
    resp = send(gen_logout(token, username))
    try:
        pl, optcode, status = unpack("!LBB", resp)
        if pl != len(resp):
            logger.error("Logout: incorrect packet length")
        print "status: " + str(status)
    except error:
        logger.error("Logout: can not parse the response")

def send_text_mesg(token, username, mesg):
    resp = send(gen_send_text_mesg(token, username, mesg))
    try:
        pl, optcode, status = unpack("!LBB", resp)
        if pl != len(resp):
            logger.error("Send text mesg: incorrect packet length")
        print "status: " + str(status)
    except error:
        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.recv(4096)
    
    length = -1
    while True:
        received = bytes()
        while True:
            if len(received) > 4:
                length, optcode = unpack("!LB", received[:5])

            if len(received) == length:
                break
            rd, wr, err = select([sock], [], [])
            if rd:
                buff = sock.recv(4096)
                if len(buff) == 0:
                    break
                received += buff
            else:
                break
        print len(received)
        pl, optcode, fingerprint = unpack("!LB32s", received[:37])
        mesg = received[37:-1]
        logger.info("Received a push: %s", mesg)
        sock.sendall(pack("!LB32s", 37, 0x00, fingerprint))