-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessage.py
171 lines (139 loc) · 3.97 KB
/
Message.py
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
import json
SEPARATOR = '\n'
SEPARATOR1 = ' '
MAX_MSG_SIZE = 65536
MAX_TIMESTAMP_GAP = 10
class MessageType(object):
INIT = 'INIT'
AUTH_START = 'AUTH_START'
AUTH_END = 'AUTH_END'
LIST_USERS = 'LIST_USERS'
GET_USER_INFO = 'GET_USER_INFO'
CONN_USER_START = 'CONN_USER_START'
CONN_USER_RES = 'CONN_USER_RES'
CONN_USER_END = 'CONN_USER_END'
DIS_CONN = 'DIS_CONN'
TEXT_MSG = 'PLAIN_MSG'
LOGOUT = 'LOGOUT'
RES_FOR_INVALID_REQ = 'RES_FOR_INVALID_REQ'
RES_FOR_VALID_REQ = 'RES_FOR_VALID_REQ'
def loads(msg):
json_obj = json.loads(msg)
tpe = json_obj['type']
data = json_obj['data']
return tpe, data
def dumps(tpe, data=''):
msg = dict()
msg['type'] = tpe
msg['data'] = data
return json.dumps(msg)
class AuthStartMsg(object):
def __init__(self,
user_name,
password,
rsa_pub_key,
dh_pub_key,
ip,
port,
c1_nonce):
self.user_name = user_name
self.password = password
self.rsa_pub_key = rsa_pub_key
self.dh_pub_key = dh_pub_key
self.ip = ip
self.port = port
self.c1_nonce = c1_nonce
class AuthStartRes(object):
def __init__(self,
dh_pub_key,
c1_nonce,
c2_nonce):
self.dh_pub_key = dh_pub_key
self.c1_nonce = c1_nonce
self.c2_nonce = c2_nonce
class UserListRes(object):
def __init__(self,
user_names,
timestamp=None):
self.user_names = user_names
self.timestamp = timestamp
class UserInfoRes(object):
def __init__(self,
ip,
port,
sec_key,
ticket,
ticket_signature,
pub_key,
timestamp=None):
self.ip = ip
self.port = port
self.sec_key = sec_key
self.ticket = ticket
self.ticket_signature = ticket_signature
self.pub_key = pub_key
self.timestamp = timestamp
class ConnStartMsg(object):
def __init__(self,
user_name,
ip,
port,
pub_key,
ticket,
ticket_signature,
c3_nonce,
timestamp):
self.user_name = user_name
self.ip = ip
self.port = port
self.pub_key = pub_key
self.ticket = ticket
self.ticket_signature = ticket_signature
self.c3_nonce = c3_nonce
self.timestamp = timestamp
class ConnBackMsg(object):
def __init__(self,
user_name,
iv,
encrypted_c3_nonce,
c4_nonce,
timestamp):
self.user_name = user_name
self.iv = iv
self.encrypted_c3_nonce = encrypted_c3_nonce
self.c4_nonce = c4_nonce
self.timestamp = timestamp
class ConnEndMsg(object):
def __init__(self,
user_name,
iv,
encrypted_c4_nonce,
timestamp):
self.user_name = user_name
self.iv = iv
self.encrypted_c4_nonce = encrypted_c4_nonce
self.timestamp = timestamp
class TextMsg(object):
def __init__(self,
user_name,
iv,
encrypted_msg,
msg_signature,
timestamp):
self.user_name = user_name
self.iv = iv
self.encrypted_msg = encrypted_msg
self.msg_signature = msg_signature
self.timestamp = timestamp
class DisconnMsg(object):
def __init__(self,
user_name,
timestamp):
self.user_name = user_name
self.timestamp = timestamp
class LogoutRes(object):
def __init__(self,
result,
timestamp=None):
self.result = result
self.timestamp = timestamp