88import json
99from pydantic import BaseSettings
1010from flask_session import Session
11+ from Crypto .Cipher import AES
12+ from Crypto .Util .Padding import pad , unpad
13+ import hashlib
14+ import base64
1115
1216
1317class Settings (BaseSettings ):
@@ -30,13 +34,13 @@ class Settings(BaseSettings):
3034
3135app = Flask (__name__ )
3236app .config ['SECRET_KEY' ] = "test key"
33- app .config ['SESSION_TYPE' ] = 'redis'
34- app .config ['SESSION_PERMANENT' ] = False
35- app .config ['SESSION_USE_SIGNER' ] = True
36- app .config ['SESSION_REDIS' ] = redis .from_url (f'{ REDIS_IP } :{ REDIS_PORT } ' )
37- server_session = Session (app )
37+ # app.config['SESSION_TYPE'] = 'redis'
38+ # app.config['SESSION_PERMANENT'] = False
39+ # app.config['SESSION_USE_SIGNER'] = True
40+ # app.config['SESSION_REDIS'] = redis.from_url(f'{REDIS_IP}:{REDIS_PORT}')
41+ # server_session = Session(app)
3842socketio = SocketIO (app , message_queue = f'{ REDIS_IP } :{ REDIS_PORT } ' , cors_allowed_origins = "*" )
39-
43+ key = hashlib . pbkdf2_hmac ( hash_name = 'sha256' , password = b'pass123#' , salt = b'eyestalk' , iterations = 100 )
4044users_in_room = {}
4145rooms_sid = {}
4246names_sid = {}
@@ -75,20 +79,21 @@ def on_create_room(data):
7579 "name" : data ["userNickname" ]
7680 }
7781 print (session )
78-
82+
7983 # Spring 로직 추가 => 방 생성
8084 response = create_room_request (data , request .sid )
8185 print ("방 생성됨!!!!!!!!!!!!!!!!!" )
8286
8387 emit ("join-request" )
84-
88+
8589 # elasticsearch
8690 room_info = response .json ()
8791 user_nickname = str (data ["userNickname" ])
88-
92+
8993 if room_info ["roomEnterUser" ] == 0 :
9094 room_id = data ["roomName" ]
91- doc_create = {"des" : "create room" , "room_id" : room_id , "user_nickname" : user_nickname , "@timestamp" : utc_time ()}
95+ doc_create = {"des" : "create room" , "room_id" : room_id , "user_nickname" : user_nickname ,
96+ "@timestamp" : utc_time ()}
9297 es .index (index = index_name , doc_type = "log" , body = doc_create )
9398
9499
@@ -118,7 +123,8 @@ def on_join_room(data):
118123 ### elasticsearch
119124 if len (users_in_room ) > 0 :
120125 user_nickname = str (data ["userNickname" ])
121- doc_join = {"des" : "New member joined" , "room_id" : room_id , "user_nickname" : user_nickname , "sid" : sid , "@timestamp" : utc_time ()}
126+ doc_join = {"des" : "New member joined" , "room_id" : room_id , "user_nickname" : user_nickname , "sid" : sid ,
127+ "@timestamp" : utc_time ()}
122128 es .index (index = index_name , doc_type = "log" , body = doc_join )
123129 emit ("user-connect" , {"sid" : sid , "name" : display_name }, broadcast = True , include_self = False , room = room_id )
124130
@@ -232,7 +238,10 @@ def send_message(message):
232238 user_nickname = str (message ["sender" ])
233239 date = datetime .datetime .now ()
234240 now = date .strftime ('%m/%d/%y %H:%M:%S' )
235- doc_chatting = {"des" : "chatting" , "room_id" : room_id , "user_nickname" :user_nickname , "chatting message" : text , "@timestamp" : utc_time ()}
241+ # 채팅 암호화 추가
242+ doc_chatting = {"des" : "chatting" , "room_id" : room_id , "user_nickname" : user_nickname ,
243+ "chatting message" : encrypt (text ),
244+ "@timestamp" : utc_time ()}
236245 es .index (index = index_name , doc_type = "log" , body = doc_chatting )
237246
238247 data = {
@@ -297,6 +306,30 @@ def exit_room(socketID):
297306 return response
298307
299308
309+ def encrypt (data ):
310+ encrypt_data = {}
311+
312+ aes = AES .new (key , AES .MODE_ECB )
313+ block_size = 16
314+
315+ data = data .encode ('utf8' ) # bytes인코딩
316+ padded_value = pad (data , block_size ) # 블록 사이즈 맞추기(패딩)
317+
318+ encrypt_data = base64 .b64encode (aes .encrypt (padded_value )).decode ('utf8' )
319+
320+ return encrypt_data
321+
322+
323+ def decrypt (data ):
324+ aes = AES .new (key , AES .MODE_ECB )
325+
326+ block_size = 16
327+ decrypted_value = aes .decrypt (base64 .b64decode (data )) # 복호화
328+ unpadded_value = unpad (decrypted_value , block_size ) # 암호화 할 때 붙였던 pad 떼어내기
329+
330+ return unpadded_value .decode ('utf-8' )
331+
332+
300333if __name__ == '__main__' :
301334 socketio .run (app ,
302335 host = "0.0.0.0" ,
0 commit comments