Compare commits

..

4 commits

4 changed files with 88 additions and 25 deletions

1
.gitignore vendored
View file

@ -162,3 +162,4 @@ cython_debug/
config/ config/
db/ db/
test.py

View file

@ -38,6 +38,7 @@ class Bot(discord.Client):
if msg_split[1].isdigit() and msg_split[2].isdigit(): if msg_split[1].isdigit() and msg_split[2].isdigit():
if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1: if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1:
password = self.password_generate(4) password = self.password_generate(4)
password_hash = self.hash_genarate(password)
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
member_info = cursor.fetchall() member_info = cursor.fetchall()
@ -48,7 +49,7 @@ class Bot(discord.Client):
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1])) cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password, msg_split[1])) cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit() self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
@ -61,7 +62,7 @@ class Bot(discord.Client):
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1])) cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password, msg_split[1])) cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit() self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
@ -75,6 +76,7 @@ class Bot(discord.Client):
if msg_split[1].isdigit() and msg_split[2].isdigit(): if msg_split[1].isdigit() and msg_split[2].isdigit():
if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1: if int(msg_split[1]) <= 10 and int(msg_split[1]) >= 1:
password = self.password_generate(4) password = self.password_generate(4)
password_hash = self.hash_genarate(password)
cursor = self.db.cursor() cursor = self.db.cursor()
cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),)) cursor.execute("SELECT * FROM club_member WHERE discord_userid = %s", (str(message.author.id),))
member_info = cursor.fetchall() member_info = cursor.fetchall()
@ -85,7 +87,7 @@ class Bot(discord.Client):
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1])) cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password, msg_split[1])) cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit() self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
@ -97,7 +99,7 @@ class Bot(discord.Client):
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1])) cursor.execute("UPDATE pc_list SET using_user_id = %s WHERE pc_number = %s", (member_info[0][0], msg_split[1]))
self.db.commit() self.db.commit()
cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password, msg_split[1])) cursor.execute("UPDATE pc_list SET password_hash = %s WHERE pc_number = %s", (password_hash, msg_split[1]))
self.db.commit() self.db.commit()
await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n") await message.channel.send(f"使用が開始されました。\nパスワード | {password}\nPC番号 | {msg_split[1]}\nデバイス番号 | {msg_split[2]}\n")
await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています') await self.get_channel(bot_config["log_channel_id"]).send(f'{member_info[0][2]}がPC{msg_split[1]}を使用しています')

59
dislocker_auth.py Normal file
View file

@ -0,0 +1,59 @@
import psycopg2
import os
import json
from flask import Flask, request, jsonify
config_dir_path = "./config/"
db_config_path = config_dir_path + "db.json"
if not os.path.isfile(db_config_path):
if not os.path.isdir(config_dir_path):
os.mkdir(config_dir_path)
db_config = {
"host": "localhost",
"db": "dislocker",
"username": "user",
"password": "example_pass",
"port": "5432"
}
with open(db_config_path, "w") as w:
json.dump(db_config, w, indent=4)
elif os.path.isfile(db_config_path):
with open(db_config_path, "r") as r:
db_config = json.load(r)
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):
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
def delete(self, pc_number):
cursor = self.db.cursor()
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (pc_number,))
self.db.commit()
app = Flask(__name__)
auth = Auth(db_config["host"], db_config["db"], db_config["port"], db_config["username"], db_config["password"])
@app.route('/verify', methods=['POST'])
def verify():
pc_number = int(request.json.get('pc_number'))
password = request.json.get('password')
if auth.check(pc_number, password) == 0:
auth.delete(pc_number)
return jsonify({'message': 'ok'}), 200
else:
return jsonify({'message': 'damedesu'}), 401
if __name__ == '__main__':
app.run()

View file

@ -1,10 +1,11 @@
import psycopg2
import os import os
import json import json
import customtkinter import customtkinter
from winotify import Notification, audio from winotify import Notification, audio
import keyboard import keyboard
import subprocess import subprocess
import requests
import hashlib
class App(customtkinter.CTk): class App(customtkinter.CTk):
def __init__(self): def __init__(self):
@ -89,15 +90,18 @@ class Lock(customtkinter.CTkToplevel):
self.signin_button = customtkinter.CTkButton(self.button_frame, text='サインイン', command=self.auth) self.signin_button = customtkinter.CTkButton(self.button_frame, text='サインイン', command=self.auth)
self.signin_button.grid(row=0, column=0, padx=10, pady=10) self.signin_button.grid(row=0, column=0, padx=10, pady=10)
def hash_genarate(self, source):
hashed = hashlib.md5(source.encode())
return hashed.hexdigest()
def auth(self): def auth(self):
self.db = psycopg2.connect(f"host={db_config["host"]} dbname={db_config["db"]} port={db_config["port"]} user={db_config["username"]} password={db_config["password"]}") auth_url = client_config["auth_host_url"] + "/verify"
cursor = self.db.cursor() auth_json = {
cursor.execute("SELECT * FROM pc_list WHERE pc_number = %s", (10,)) "pc_number": client_config["pc_number"],
pc_info = cursor.fetchall() "password": self.hash_genarate(self.password_entry.get())
auth_password = str(self.password_entry.get()) }
if pc_info[0][2] == auth_password: responce = requests.post(auth_url, json=auth_json)
cursor.execute("UPDATE pc_list SET password_hash = NULL WHERE pc_number = %s", (10,)) if responce.status_code == 200:
self.db.commit()
self.destroy() self.destroy()
app.exit() app.exit()
else: else:
@ -111,22 +115,19 @@ class Lock(customtkinter.CTkToplevel):
config_dir_path = "./config/" config_dir_path = "./config/"
db_config_path = config_dir_path + "db.json" client_config_path = config_dir_path + "client.json"
if not os.path.isfile(db_config_path): if not os.path.isfile(client_config_path):
if not os.path.isdir(config_dir_path): if not os.path.isdir(config_dir_path):
os.mkdir(config_dir_path) os.mkdir(config_dir_path)
db_config = { client_config = {
"host": "localhost", "auth_host_url": "localhost",
"db": "dislocker", "pc_number": ""
"username": "user",
"password": "example_pass",
"port": "5432"
} }
with open(db_config_path, "w") as w: with open(client_config_path, "w") as w:
json.dump(db_config, w, indent=4) json.dump(client_config, w, indent=4)
elif os.path.isfile(db_config_path): elif os.path.isfile(client_config_path):
with open(db_config_path, "r") as r: with open(client_config_path, "r") as r:
db_config = json.load(r) db_config = json.load(r)
if __name__ == '__main__': if __name__ == '__main__':