2024-06-08 19:18:35 +09:00
import psycopg2
import os
import json
2024-06-08 20:26:26 +09:00
from flask import Flask , request , jsonify , render_template
2024-08-25 23:28:36 +09:00
import uuid
import string
import random
2024-09-06 00:09:37 +09:00
import hashlib
2024-06-08 19:18:35 +09:00
config_dir_path = " ./config/ "
2024-07-20 20:10:18 +09:00
server_config_path = config_dir_path + " server.json "
2024-08-25 23:28:36 +09:00
onetime_config_path = config_dir_path + " onetime.json "
2024-07-20 20:10:18 +09:00
if not os . path . isfile ( server_config_path ) :
2024-06-08 19:18:35 +09:00
if not os . path . isdir ( config_dir_path ) :
os . mkdir ( config_dir_path )
2024-07-20 20:10:18 +09:00
server_config = {
" db " : {
" host " : " localhost " ,
" port " : " 5432 " ,
" db_name " : " dislocker " ,
" username " : " user " ,
" password " : " password "
} ,
" bot " : {
" token " : " TYPE HERE BOTS TOKEN KEY " ,
2024-08-10 18:47:16 +09:00
" activity " : {
" name " : " Dislocker " ,
" details " : " ロック中... " ,
" type " : " playing " ,
" state " : " ロック中... "
} ,
2024-07-20 20:10:18 +09:00
" log_channel_id " : " TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!) " ,
2024-08-10 18:47:16 +09:00
" config_channel_id " : " TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!) " ,
" config_public_channel_id " : " TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!) "
2024-07-20 20:10:18 +09:00
}
2024-06-08 19:18:35 +09:00
}
2024-07-20 20:10:18 +09:00
with open ( server_config_path , " w " ) as w :
json . dump ( server_config , w , indent = 4 )
elif os . path . isfile ( server_config_path ) :
with open ( server_config_path , " r " ) as r :
server_config = json . load ( r )
2024-06-08 19:18:35 +09:00
class Auth ( ) :
def __init__ ( self , host , db , port , user , password ) :
self . db = psycopg2 . connect ( f " host= { host } dbname= { db } port= { port } user= { user } password= { password } " )
2024-08-25 23:28:36 +09:00
def token_generate ( self , length ) :
letters = string . ascii_letters + string . digits
2024-09-06 00:09:37 +09:00
token = ' ' . join ( random . choice ( letters ) for _ in range ( length ) )
return token
def master_password_generate ( self , length ) :
characters = string . ascii_letters + string . digits + string . punctuation
master_password = ' ' . join ( random . choice ( characters ) for _ in range ( length ) )
return master_password
def hash_genarate ( self , source ) :
hashed = hashlib . sha256 ( source . encode ( ) )
return hashed . hexdigest ( )
2024-08-25 23:28:36 +09:00
def check ( self , * * kwargs ) :
2024-07-23 15:15:24 +09:00
try :
cursor = self . db . cursor ( )
2024-08-25 23:28:36 +09:00
pc_number = int ( kwargs [ " pc_number " ] )
2024-08-26 00:19:53 +09:00
pc_uuid = str ( kwargs [ " pc_uuid " ] )
2024-08-25 23:28:36 +09:00
pc_token = str ( kwargs [ " pc_token " ] )
2024-09-06 01:30:44 +09:00
device_list = kwargs [ " device_list " ]
keyboard_number = " own "
mouse_number = " own "
2024-08-25 23:28:36 +09:00
if " password_hash " in kwargs :
password_hash = str ( kwargs [ " password_hash " ] )
2024-08-26 00:51:31 +09:00
cursor . execute ( " SELECT * FROM pc_list WHERE pc_number = %s AND password_hash = %s AND pc_uuid = %s AND pc_token = %s " , ( pc_number , password_hash , pc_uuid , pc_token ) )
2024-08-25 23:28:36 +09:00
pc_info = cursor . fetchall ( )
2024-09-06 01:30:44 +09:00
if pc_info :
for device in device_list :
cursor . execute ( " SELECT * FROM keyboard_list WHERE device_id = %s " , ( device [ " device_id " ] , ) )
keyboard_record = cursor . fetchall ( )
if keyboard_record :
keyboard_number = int ( keyboard_record [ 0 ] [ 0 ] )
break
else :
pass
for device in device_list :
cursor . execute ( " SELECT * FROM mouse_list WHERE device_id = %s " , ( device [ " device_id " ] , ) )
mouse_record = cursor . fetchall ( )
if mouse_record :
mouse_number = int ( mouse_record [ 0 ] [ 0 ] )
break
else :
pass
return { " result " : 0 , " about " : " ok " , " output_dict " : { " keyboard_number " : keyboard_number , " mouse_number " : mouse_number } }
else :
return { " result " : 1 , " about " : " unregistered_pc " }
2024-08-25 23:28:36 +09:00
else :
2024-08-26 00:51:31 +09:00
cursor . execute ( " SELECT * FROM pc_list WHERE pc_number = %s AND pc_uuid = %s AND pc_token = %s " , ( pc_number , pc_uuid , pc_token ) )
2024-08-25 23:28:36 +09:00
pc_info = cursor . fetchall ( )
2024-09-06 01:30:44 +09:00
if pc_info :
return { " result " : 0 , " about " : " ok " }
else :
return { " result " : 1 , " about " : " unregistered_pc " }
2024-08-23 00:18:07 +09:00
2024-08-25 23:28:36 +09:00
except Exception as error :
print ( " PCの登録状況を調査中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
2024-07-23 15:15:24 +09:00
finally :
cursor . close ( )
2024-09-06 01:30:44 +09:00
def device_use_register ( self , * * kwargs ) :
try :
pc_number = int ( kwargs [ " pc_number " ] )
if " keyboard_number " in kwargs :
keyboard_number = int ( kwargs [ " keyboard_number " ] )
else :
keyboard_number = None
if " mouse_number " in kwargs :
mouse_number = int ( kwargs [ " mouse_number " ] )
else :
mouse_number = None
cursor = self . db . cursor ( )
cursor . execute ( " SELECT * FROM pc_list WHERE pc_number = %s " , ( pc_number , ) )
pc_list_record = cursor . fetchall ( )
pc_using_member_id = pc_list_record [ 0 ] [ 1 ]
if pc_using_member_id == None :
return { " result " : 1 , " about " : " not_used " }
else :
cursor . execute ( " SELECT * FROM pc_usage_history WHERE member_id = %s AND pc_number = %s ORDER BY id DESC LIMIT 1 " , ( pc_using_member_id , pc_number ) )
pc_usage_history_record = cursor . fetchall ( )
pc_usage_history_record_id = pc_usage_history_record [ 0 ] [ 0 ]
cursor . execute ( " UPDATE pc_usage_history SET keyboard_number = %s , mouse_number = %s WHERE id = %s " , ( keyboard_number , mouse_number , pc_usage_history_record_id ) )
cursor . execute ( " UPDATE keyboard_list SET using_member_id = %s WHERE keyboard_number = %s " , ( pc_using_member_id , keyboard_number ) )
cursor . execute ( " UPDATE mouse_list SET using_member_id = %s WHERE mouse_number = %s " , ( pc_using_member_id , mouse_number ) )
self . db . commit ( )
return { " result " : 0 , " about " : " ok " }
except Exception as error :
print ( " デバイスの使用登録中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
def device_register ( self , * * kwargs ) :
try :
cursor = self . db . cursor ( )
mode = kwargs [ " mode " ]
number = kwargs [ " number " ]
device_id = kwargs [ " device_id " ]
device_name = kwargs [ " device_name " ]
if mode == " keyboard " :
keyboard_number = int ( kwargs [ " number " ] )
cursor . execute ( " SELECT * FROM keyboard_list WHERE keyboard_number = %s " , ( keyboard_number , ) )
keyboard_record = cursor . fetchall ( )
if keyboard_record :
cursor . execute ( " UPDATE keyboard_list SET device_id = %s , device_name = %s WHERE keyboard_number = %s " , ( device_id , device_name , keyboard_number ) )
self . db . commit ( )
return { " result " : 0 , " about " : " ok " }
else :
cursor . execute ( " INSERT INTO keyboard_list (keyboard_number, device_id, device_name) VALUES ( %s , %s , %s ) " , ( keyboard_number , device_id , device_name ) )
return { " result " : 0 , " about " : " ok " }
elif mode == " mouse " :
mouse_number = int ( kwargs [ " number " ] )
cursor . execute ( " SELECT * FROM mouse_list WHERE mouse_number = %s " , ( mouse_number , ) )
mouse_record = cursor . fetchall ( )
if mouse_record :
cursor . execute ( " UPDATE mouse_list SET device_id = %s , device_name = %s WHERE mouse_number = %s " , ( device_id , device_name , mouse_number ) )
self . db . commit ( )
return { " result " : 0 , " about " : " ok " }
else :
cursor . execute ( " INSERT INTO mouse_list (mouse_number, device_id, device_name) VALUES ( %s , %s , %s ) " , ( mouse_number , device_id , device_name ) )
return { " result " : 0 , " about " : " ok " }
except Exception as error :
print ( " 停止処理中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
2024-06-08 19:18:35 +09:00
def delete ( self , pc_number ) :
2024-07-23 15:15:24 +09:00
try :
cursor = self . db . cursor ( )
cursor . execute ( " UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s " , ( pc_number , ) )
self . db . commit ( )
2024-08-23 00:18:07 +09:00
return { " result " : 0 , " about " : " ok " }
except Exception as error :
print ( " パスワードの削除中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
finally :
cursor . close ( )
def user_register_check ( self , * * kwargs ) :
try :
discord_user_id = str ( kwargs [ " discord_user_id " ] )
cursor = self . db . cursor ( )
cursor . execute ( " SELECT * FROM club_member WHERE discord_user_id = %s " , ( discord_user_id , ) )
user_record = cursor . fetchall ( )
#ユーザーデータが見つかった場合(登録済みの場合)
if user_record :
member_id = user_record [ 0 ] [ 0 ]
name = user_record [ 0 ] [ 1 ]
discord_user_name = user_record [ 0 ] [ 2 ]
return { " result " : 0 , " about " : " exist " , " user_info " : { " member_id " : member_id , " name " : name , " discord_user_name " : discord_user_name } }
#ユーザーデータがなかったら(未登録の場合)
else :
return { " result " : 1 , " about " : " user_data_not_found " }
except Exception as error :
print ( " ユーザーの登録状況を調査中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
2024-07-23 15:15:24 +09:00
finally :
cursor . close ( )
def stop ( self , * * kwargs ) :
2024-08-23 00:18:07 +09:00
# bot側のfstopを基に
2024-07-23 15:15:24 +09:00
try :
2024-08-23 00:18:07 +09:00
pc_number = kwargs [ " pc_number " ]
2024-07-23 15:15:24 +09:00
cursor = self . db . cursor ( )
cursor . execute ( " SELECT * FROM pc_list WHERE pc_number = %s " , ( pc_number , ) )
pc_list_record = cursor . fetchall ( )
2024-08-23 00:18:07 +09:00
pc_using_member_id = pc_list_record [ 0 ] [ 1 ]
pc_password_hash = pc_list_record [ 0 ] [ 2 ]
if pc_using_member_id == None :
return { " result " : 1 , " about " : " not_used " }
else :
cursor . execute ( " UPDATE pc_list SET using_member_id = NULL WHERE pc_number = %s " , ( pc_number , ) )
if pc_password_hash == None :
pass
else :
2024-07-23 15:15:24 +09:00
cursor . execute ( " UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s " , ( pc_number , ) )
2024-08-23 00:18:07 +09:00
cursor . execute ( " SELECT * FROM pc_usage_history WHERE member_id = %s AND pc_number = %s ORDER BY id DESC LIMIT 1 " , ( pc_using_member_id , pc_number ) )
2024-07-23 15:15:24 +09:00
pc_usage_history_record = cursor . fetchall ( )
2024-08-23 00:18:07 +09:00
pc_usage_history_record_id = pc_usage_history_record [ 0 ] [ 0 ]
keyboard_number = pc_usage_history_record [ 0 ] [ 3 ]
mouse_number = pc_usage_history_record [ 0 ] [ 4 ]
if keyboard_number == None :
pass
else :
# keyboard_listの使用中ユーザーを消す
cursor . execute ( " UPDATE keyboard_list SET using_member_id = NULL WHERE keyboard_number = %s " , ( keyboard_number , ) )
if mouse_number == None :
pass
else :
# mouse_listの使用中ユーザーを消す
2024-08-23 02:17:10 +09:00
cursor . execute ( " UPDATE mouse_list SET using_member_id = NULL WHERE mouse_number = %s " , ( mouse_number , ) )
2024-08-23 00:19:33 +09:00
cursor . execute ( " UPDATE pc_usage_history SET end_use_time = clock_timestamp() WHERE id = %s " , ( pc_usage_history_record_id , ) )
2024-07-23 15:15:24 +09:00
self . db . commit ( )
2024-08-23 00:18:07 +09:00
return { " result " : 0 , " about " : " ok " }
except Exception as error :
print ( " 停止処理中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
2024-07-23 15:15:24 +09:00
finally :
cursor . close ( )
2024-08-25 23:28:36 +09:00
def register ( self , * * kwargs ) :
try :
cursor = self . db . cursor ( )
pc_number = int ( kwargs [ " pc_number " ] )
2024-08-26 00:19:53 +09:00
pc_uuid = str ( kwargs [ " pc_uuid " ] )
2024-08-26 00:02:24 +09:00
cursor . execute ( " SELECT pc_uuid FROM pc_list WHERE pc_number = %s " , ( pc_number , ) )
2024-08-25 23:28:36 +09:00
pc_record = cursor . fetchall ( )
pc_record_uuid = pc_record [ 0 ] [ 0 ]
if pc_record_uuid == None :
pc_token = self . token_generate ( 36 )
2024-09-06 00:09:37 +09:00
master_password = self . master_password_generate ( 16 )
master_password_hash = self . hash_genarate ( master_password )
cursor . execute ( " UPDATE pc_list SET pc_uuid = %s , pc_token = %s , master_password = %s WHERE pc_number = %s " , ( pc_uuid , pc_token , master_password , pc_number ) )
2024-08-25 23:28:36 +09:00
self . db . commit ( )
2024-09-06 00:09:37 +09:00
return { " result " : 0 , " about " : " ok " , " output_dict " : { " pc_token " : pc_token , " master_password " : master_password , " master_password_hash " : master_password_hash } }
2024-08-26 00:16:53 +09:00
else :
return { " result " : 1 , " about " : " exist " }
2024-08-25 23:28:36 +09:00
except Exception as error :
print ( " 停止処理中にエラーが発生しました。 \n エラー内容 " )
print ( str ( error . __class__ . __name__ ) )
print ( str ( error . args ) )
print ( str ( error ) )
return { " result " : 1 , " about " : " error " }
finally :
cursor . close ( )
2024-06-08 19:18:35 +09:00
2024-06-08 20:26:26 +09:00
app = Flask ( __name__ , static_folder = " ./resource/ " )
2024-07-20 20:10:18 +09:00
auth = Auth ( server_config [ " db " ] [ " host " ] , server_config [ " db " ] [ " db_name " ] , server_config [ " db " ] [ " port " ] , server_config [ " db " ] [ " username " ] , server_config [ " db " ] [ " password " ] )
2024-06-08 19:18:35 +09:00
2024-08-25 23:28:36 +09:00
@app.route ( ' /register ' , methods = [ ' POST ' ] )
def register ( ) :
pc_number = int ( request . json . get ( ' pc_number ' ) )
pc_uuid = str ( request . json . get ( ' pc_uuid ' ) )
2024-08-25 23:58:53 +09:00
onetime_password = str ( request . json . get ( ' onetime ' ) )
2024-08-25 23:28:36 +09:00
if os . path . isfile ( onetime_config_path ) :
with open ( onetime_config_path , " r " ) as r :
onetime_config = json . load ( r )
2024-09-06 01:30:44 +09:00
if onetime_password == onetime_config [ " onetime " ] [ " pc_register " ] :
2024-08-25 23:28:36 +09:00
register_result = auth . register ( pc_number = pc_number , pc_uuid = pc_uuid )
pc_token = register_result [ " output_dict " ] [ " pc_token " ]
2024-09-06 00:09:37 +09:00
master_password = register_result [ " output_dict " ] [ " master_password " ]
master_password_hash = register_result [ " output_dict " ] [ " master_password_hash " ]
2024-09-06 01:30:44 +09:00
onetime_config [ " onetime " ] [ " pcregister " ] = None
with open ( onetime_config_path , " w " ) as w :
json . dump ( onetime_config , w , indent = 4 )
2024-09-06 00:09:37 +09:00
return jsonify ( { ' message ' : ' ok ' , ' pc_token ' : pc_token , ' master_password ' : master_password , ' master_password_hash ' : master_password_hash } ) , 200
2024-08-25 23:28:36 +09:00
else :
return jsonify ( { ' message ' : ' damedesu ' } ) , 401
else :
return jsonify ( { ' message ' : ' damedesu ' } ) , 401
2024-06-08 19:18:35 +09:00
@app.route ( ' /verify ' , methods = [ ' POST ' ] )
def verify ( ) :
pc_number = int ( request . json . get ( ' pc_number ' ) )
2024-08-25 23:28:36 +09:00
password_hash = request . json . get ( ' password ' )
pc_uuid = request . json . get ( ' pc_uuid ' )
pc_token = request . json . get ( ' pc_token ' )
2024-09-06 01:30:44 +09:00
devices = request . json . get ( ' devices ' )
2024-07-23 15:15:24 +09:00
print ( str ( pc_number ) + " の認証処理を開始... " )
2024-09-06 01:30:44 +09:00
pc_auth = auth . check ( pc_number = pc_number , password_hash = password_hash , pc_uuid = pc_uuid , pc_token = pc_token , device_list = devices )
2024-06-08 19:18:35 +09:00
2024-08-23 00:18:07 +09:00
if pc_auth [ " result " ] == 0 :
2024-06-08 19:18:35 +09:00
auth . delete ( pc_number )
2024-09-06 01:30:44 +09:00
auth . device_use_register ( pc_number = pc_number , keyboard_number = pc_auth [ " output_dict " ] [ " keyboard_number " ] , mouse_number = pc_auth [ " output_dict " ] [ " mouse_number " ] )
2024-07-23 15:15:24 +09:00
print ( str ( pc_number ) + " の認証処理は成功しました. " )
2024-06-08 22:07:30 +09:00
return jsonify ( { ' message ' : ' ok ' } ) , 200
2024-06-08 19:18:35 +09:00
else :
2024-07-23 15:15:24 +09:00
print ( str ( pc_number ) + " の認証処理は失敗しました. " )
2024-06-08 22:07:30 +09:00
return jsonify ( { ' message ' : ' damedesu ' } ) , 401
2024-06-08 19:18:35 +09:00
2024-07-23 15:15:24 +09:00
@app.route ( ' /stop ' , methods = [ ' POST ' ] )
def stop ( ) :
2024-08-25 23:28:36 +09:00
pc_number = int ( request . json . get ( ' pc_number ' ) )
2024-08-26 00:47:39 +09:00
pc_uuid = str ( request . json . get ( ' pc_uuid ' ) )
pc_token = str ( request . json . get ( ' pc_token ' ) )
2024-08-26 00:46:15 +09:00
print ( str ( pc_number ) + " の使用停止処理を開始... " )
2024-07-23 15:15:24 +09:00
2024-08-25 23:28:36 +09:00
pc_auth = auth . check ( pc_number = pc_number , pc_uuid = pc_uuid , pc_token = pc_token )
if pc_auth [ " result " ] == 0 :
pc_stop = auth . stop ( pc_number = pc_number )
if pc_stop [ " result " ] == 0 :
print ( str ( pc_number ) + " の使用停止処理は成功しました. " )
return jsonify ( { ' message ' : ' ok ' } ) , 200
else :
print ( str ( pc_number ) + " の使用停止処理は失敗しました. " )
return jsonify ( { ' message ' : ' error ' } ) , 500
else :
return jsonify ( { ' message ' : ' damedesu ' } ) , 401
2024-07-23 15:15:24 +09:00
2024-09-06 01:30:44 +09:00
@app.route ( ' /device_register ' , methods = [ ' POST ' ] )
def device_register ( ) :
onetime_password = str ( request . json . get ( ' onetime ' ) )
mode = str ( request . json . get ( ' mode ' ) )
number = int ( request . json . get ( ' number ' ) )
device_id = str ( request . json . get ( ' device_id ' ) )
device_name = str ( request . json . get ( ' device_name ' ) )
if os . path . isfile ( onetime_config_path ) :
with open ( onetime_config_path , " r " ) as r :
onetime_config = json . load ( r )
if onetime_password == onetime_config [ " onetime " ] [ " device_register " ] :
if mode == " keyboard " :
print ( " キーボードの登録処理を開始... " )
device_register = auth . device_register ( mode = " keyboard " , number = number , device_id = device_id , device_name = device_name )
if device_register [ " result " ] == 0 :
print ( f " キーボード { number } 番の登録処理は成功しました. " )
return jsonify ( { ' message ' : ' ok ' } ) , 200
else :
print ( f " キーボード { number } 番の登録処理は失敗しました. " )
return jsonify ( { ' message ' : ' error ' } ) , 500
elif mode == " mouse " :
print ( " マウスの登録処理を開始... " )
device_register = auth . device_register ( mode = " mouse " , number = number , device_id = device_id , device_name = device_name )
if device_register [ " result " ] == 0 :
print ( f " マウス { number } 番の登録処理は成功しました. " )
return jsonify ( { ' message ' : ' ok ' } ) , 200
else :
print ( f " マウス { number } 番の登録処理は失敗しました. " )
return jsonify ( { ' message ' : ' error ' } ) , 500
else :
return jsonify ( { ' message ' : ' damedesu ' } ) , 401
2024-06-08 19:18:35 +09:00
if __name__ == ' __main__ ' :
2024-08-10 18:21:35 +09:00
app . run ( host = " 0.0.0.0 " , port = 5000 , debug = False )