Dislocker/dislocker_auth.py

125 lines
4.6 KiB
Python
Raw Normal View History

2024-06-08 19:18:35 +09:00
import psycopg2
import os
import json
from flask import Flask, request, jsonify, render_template
2024-06-08 19:18:35 +09:00
config_dir_path = "./config/"
server_config_path = config_dir_path + "server.json"
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)
server_config = {
"db": {
"host": "localhost",
"port": "5432",
"db_name": "dislocker",
"username": "user",
"password": "password"
},
"bot": {
"token": "TYPE HERE BOTS TOKEN KEY",
"activity": {
"name": "Dislocker",
"details": "ロック中...",
"type": "playing",
"state": "ロック中..."
},
"log_channel_id" : "TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!)",
"config_channel_id": "TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!)",
"config_public_channel_id": "TYPE HERE CHANNEL ID (YOU MUST USE INT !!!!)"
}
2024-06-08 19:18:35 +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}")
def check(self, pc_number, password):
2024-07-23 15:15:24 +09:00
try:
cursor = self.db.cursor()
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s AND password_hash = %s", (pc_number, password))
pc_info = cursor.fetchall()
if not pc_info:
return 1
else:
return 0
finally:
cursor.close()
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()
finally:
cursor.close()
def stop(self, **kwargs):
try:
pc_number = int(kwargs["pc_number"])
cursor = self.db.cursor()
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (pc_number,))
pc_list_record = cursor.fetchall()
if not pc_list_record[0][1] == None:
cursor.execute("UPDATE pc_list SET using_user_id = NULL WHERE pc_number = %s", (pc_number,))
if not pc_list_record[0][2] == None:
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_number,))
cursor.execute("SELECT * FROM pc_usage_history WHERE member_id = %s AND pc_number = %s ORDER BY id DESC LIMIT 1", (pc_list_record[0][1], pc_number))
pc_usage_history_record = cursor.fetchall()
cursor.execute("UPDATE pc_usage_history SET end_use_time = current_timestamp WHERE id = %s", (pc_usage_history_record[0][0],))
self.db.commit()
result = {"result": "ok"}
else:
result = {"result": "not_used"}
except:
result = {"result": "error"}
finally:
cursor.close()
return result
2024-06-08 19:18:35 +09:00
app = Flask(__name__, static_folder="./resource/")
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
@app.route('/verify', methods=['POST'])
def verify():
pc_number = int(request.json.get('pc_number'))
password = request.json.get('password')
2024-07-23 15:15:24 +09:00
print(str(pc_number) + "の認証処理を開始...")
2024-06-08 19:18:35 +09:00
if auth.check(pc_number, password) == 0:
auth.delete(pc_number)
2024-07-23 15:15:24 +09:00
print(str(pc_number) + "の認証処理は成功しました.")
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) + "の認証処理は失敗しました.")
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():
pc_number = int(request.json.get('pc_number'))
print(str(pc_number) + "の使用停止処理を開始...")
pc_stop = auth.stop(pc_number=pc_number)
if pc_stop["result"] == "ok":
print(str(pc_number) + "の使用停止処理は成功しました.")
return jsonify({'message': 'ok'}), 200
else:
print(str(pc_number) + "の使用停止処理は失敗しました.")
return jsonify({'message': 'error'}), 500
2024-06-08 19:18:35 +09:00
if __name__ == '__main__':
app.run(host="0.0.0.0", port=5000, debug=False)