-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws-setup.sh
445 lines (394 loc) · 13.6 KB
/
aws-setup.sh
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#!/bin/bash
sudo apt-get update
sudo apt install -y python3-pip
pip3 install peewee
pip3 install stomp.py
pip3 install PyMySQL
sudo apt install -y default-jdk
cd /opt
sudo wget http://www.trieuvan.com/apache/activemq/5.15.8/apache-activemq-5.15.8-bin.tar.gz
sudo tar zxf apache-activemq-5.15.8-bin.tar.gz
sudo ln -s /opt/apache-activemq-5.15.8 activemq
sudo rm apache-activemq-5.15.8-bin.tar.gz
sudo /opt/activemq/bin/activemq start
cd /home/ubuntu/
cat > model.py << END
from peewee import *
db = MySQLDatabase("test_0105", host=os.getenv('MySQL_RDS'), port=3306, user=os.getenv('MySQL_USER'), passwd=os.getenv('MySQL_PASS'))
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField(unique=True)
password = CharField()
class Invitation(BaseModel):
inviter = ForeignKeyField(User, on_delete='CASCADE')
invitee = ForeignKeyField(User, on_delete='CASCADE')
class Friend(BaseModel):
user = ForeignKeyField(User, on_delete='CASCADE')
friend = ForeignKeyField(User, on_delete='CASCADE')
class Post(BaseModel):
user = ForeignKeyField(User, on_delete='CASCADE')
message = CharField()
class Follow(BaseModel):
follower = ForeignKeyField(User, on_delete='CASCADE')
followee = ForeignKeyField(User, on_delete='CASCADE')
class Token(BaseModel):
token = CharField(unique=True)
owner = ForeignKeyField(User, on_delete='CASCADE')
class Group(BaseModel):
group = CharField()
user = ForeignKeyField(User, on_delete='CASCADE')
class App_Server(BaseModel):
server = CharField()
user = ForeignKeyField(User, on_delete='CASCADE')
if __name__ == '__main__':
db.connect()
db.create_tables([User, Invitation, Friend, Post, Follow, Token, Group, App_Server])
END
cat > app_server.py << END
import os
import sys
import socket
from model import *
import json
import uuid
import stomp
class DBControl(object):
def __auth(func):
def validate_token(self, token=None, *args):
if token:
t = Token.get_or_none(Token.token == token)
if t:
return func(self, t, *args)
return {
'status': 1,
'message': 'Not login yet'
}
return validate_token
@__auth
def invite(self, token, username=None, *args):
if not username or args:
return {
'status': 1,
'message': 'Usage: invite <user> <id>'
}
if username == token.owner.username:
return {
'status': 1,
'message': 'You cannot invite yourself'
}
friend = User.get_or_none(User.username == username)
if friend:
res1 = Friend.get_or_none((Friend.user == token.owner) & (Friend.friend == friend))
res2 = Friend.get_or_none((Friend.friend == token.owner) & (Friend.user == friend))
if res1 or res2:
return {
'status': 1,
'message': '{} is already your friend'.format(username)
}
else:
invite1 = Invitation.get_or_none((Invitation.inviter == token.owner) & (Invitation.invitee == friend))
invite2 = Invitation.get_or_none((Invitation.inviter == friend) & (Invitation.invitee == token.owner))
if invite1:
return {
'status': 1,
'message': 'Already invited'
}
elif invite2:
return {
'status': 1,
'message': '{} has invited you'.format(username)
}
else:
Invitation.create(inviter=token.owner, invitee=friend)
return {
'status': 0,
'message': 'Success!'
}
else:
return {
'status': 1,
'message': '{} does not exist'.format(username)
}
pass
@__auth
def list_invite(self, token, *args):
if args:
return {
'status': 1,
'message': 'Usage: list-invite <user>'
}
res = Invitation.select().where(Invitation.invitee == token.owner)
invite = []
for r in res:
invite.append(r.inviter.username)
return {
'status': 0,
'invite': invite
}
@__auth
def accept_invite(self, token, username=None, *args):
if not username or args:
return {
'status': 1,
'message': 'Usage: accept-invite <user> <id>'
}
inviter = User.get_or_none(User.username == username)
invite = Invitation.get_or_none((Invitation.inviter == inviter) & (Invitation.invitee == token.owner))
if invite:
Friend.create(user=token.owner, friend=inviter)
invite.delete_instance()
return {
'status': 0,
'message': 'Success!',
'user': token.owner.username
}
else:
return {
'status': 1,
'message': '{} did not invite you'.format(username)
}
pass
@__auth
def list_friend(self, token, *args):
if args:
return {
'status': 1,
'message': 'Usage: list-friend <user>'
}
friends = Friend.select().where((Friend.user == token.owner) | (Friend.friend == token.owner))
res = []
for f in friends:
if f.user == token.owner:
res.append(f.friend.username)
else:
res.append(f.user.username)
return {
'status': 0,
'friend': res
}
@__auth
def post(self, token, *args):
if len(args) <= 0:
return {
'status': 1,
'message': 'Usage: post <user> <message>'
}
Post.create(user=token.owner, message=' '.join(args))
return {
'status': 0,
'message': 'Success!'
}
@__auth
def receive_post(self, token, *args):
if args:
return {
'status': 1,
'message': 'Usage: receive-post <user>'
}
res = Post.select().where(Post.user != token.owner).join(Friend, on=((Post.user == Friend.user) | (Post.user == Friend.friend))).where((Friend.user == token.owner) | (Friend.friend == token.owner))
post = []
for r in res:
post.append({
'id': r.user.username,
'message': r.message
})
return {
'status': 0,
'post': post
}
@__auth
def send(self, token, username=None, *args):
if len(args) <= 0:
return {
'status': 1,
'message': 'Usage: send <user> <friend> <message>'
}
friend = User.get_or_none(User.username == username)
if not friend:
return {
'status': 1,
'message': 'No such user exist'
}
check_friend = Friend.get_or_none((Friend.user == token.owner) & (Friend.friend == friend))
check_friend_ = Friend.get_or_none((Friend.friend == token.owner) & (Friend.user == friend))
if (not check_friend) and (not check_friend_):
return {
'status': 1,
'message': '{} is not your friend'.format(username)
}
check_online = Token.get_or_none(Token.owner == friend)
if not check_online:
return {
'status': 1,
'message': '{} is not online'.format(username)
}
message=' '.join(args)
conn = stomp.Connection([(os.getenv('ActiveMQ_Endpoint'), 61613)])
conn.start()
conn.connect()
add_header = {'sender': token.owner.username, 'sendee':username, 'group':''}
destination = '/queue/{}_2_{}'.format(token.owner.username, username)
conn.send(destination, body=message, header=add_header)
conn.disconnect()
return {
'status': 0,
'message': 'Success!'
}
@__auth
def create_group(self, token, groupname=None, *args):
if len(args) > 0 or groupname == None:
return {
'status': 1,
'message': 'Usage: create-group <user> <group>'
}
check_group = Group.get_or_none(Group.group == groupname)
if check_group:
return {
'status': 1,
'message': '{} already exist'.format(groupname)
}
Group.create(user=token.owner, group=groupname)
return {
'status': 0,
'message': 'Success!'
}
@__auth
def list_group(self, token, *args):
if len(args) > 0:
return {
'status': 1,
'message': 'Usage: list-group <user>'
}
groups_info = []
groups = Group.select(Group.group).distinct()
for group in groups:
groups_info.append(group.group)
print(group.group)
return {
'status': 0,
'groups': groups_info
}
@__auth
def list_joined(self, token, *args):
if len(args) > 0:
return {
'status': 1,
'message': 'Usage: list-joined <user>'
}
groups_info = []
groups = Group.select().where(Group.user == token.owner)
for group in groups:
groups_info.append(group.group)
print(group.group)
return {
'status': 0,
'groups': groups_info
}
@__auth
def join_group(self, token, groupname=None, *args):
if len(args) > 0:
return {
'status': 1,
'message': 'Usage: join-group <user> <group>'
}
groups = Group.select().where(Group.group == groupname)
if not groups:
return {
'status': 1,
'message': '{} does not exist'.format(groupname)
}
groups = Group.select().where(Group.user == token.owner)
for group in groups:
if group.group == groupname:
return {
'status': 1,
'message': 'Already a member of {}'.format(groupname)
}
Group.create(user=token.owner, group=groupname)
return {
'status': 0,
'message': 'Success!'
}
@__auth
def send_group(self, token, groupname=None, *args):
if len(args) <= 0:
return {
'status': 1,
'message': 'Usage: send-group <user> <group> <message>'
}
check_group = Group.get_or_none(Group.group == groupname)
if not check_group:
return {
'status': 1,
'message': 'No such group exist'
}
check_user_in_group = Group.select().where((Group.user == token.owner) & (Group.group == groupname))
#print(token.owner.username)
#print(check_user_in_group[0].user.username, check_user_in_group[0].group)
if not check_user_in_group:
return {
'status': 1,
'message': 'You are not the member of {}'.format(groupname)
}
message=' '.join(args)
conn = stomp.Connection([(os.getenv('ActiveMQ_Endpoint'), 61613)])
conn.start()
conn.connect()
add_header = {'sender': token.owner.username, 'sendee':'', 'group':groupname}
destination = '/topic/{}'.format(groupname)
conn.send(destination, body=message, header=add_header)
conn.disconnect()
return {
'status': 0,
'message': 'Success!'
}
class Server(object):
def __init__(self, ip, port):
try:
socket.inet_aton(ip)
if 0 < int(port) < 65535:
self.ip = ip
self.port = int(port)
else:
raise Exception('Port value should between 1~65535')
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.db = DBControl()
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
def run(self):
self.sock.bind((self.ip, self.port))
self.sock.listen(100)
socket.setdefaulttimeout(0.1)
while True:
try:
conn, addr = self.sock.accept()
with conn:
cmd = conn.recv(4096).decode()
resp = self.__process_command(cmd)
conn.send(resp.encode())
except Exception as e:
print(e, file=sys.stderr)
def __process_command(self, cmd):
command = cmd.split()
if len(command) > 0:
command_exec = getattr(self.db, command[0].replace('-', '_'), None)
print(command[0].replace('-', '_'))
if command_exec:
return json.dumps(command_exec(*command[1:]))
return self.__command_not_found(command[0])
def __command_not_found(self, cmd):
return json.dumps({
'status': 1,
'message': 'Unknown command {}'.format(cmd)
})
def launch_server(ip, port):
c = Server(ip, port)
c.run()
if __name__ == '__main__':
if sys.argv[1] and sys.argv[2]:
launch_server(sys.argv[1], sys.argv[2])
END
python3 app_server.py 0.0.0.0 8888