Dislocker/dislocker_client.py

160 lines
6 KiB
Python
Raw Normal View History

2024-06-06 19:40:41 +09:00
import os
import json
2024-06-10 13:27:21 +09:00
import tkinter.messagebox
2024-06-06 19:40:41 +09:00
import customtkinter
from winotify import Notification, audio
import keyboard
import subprocess
import requests
import hashlib
2024-06-10 13:27:21 +09:00
import string
import random
import tkinter
2024-06-06 19:40:41 +09:00
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
2024-06-10 13:27:21 +09:00
self.title(f"{app_name} | ロック中")
2024-06-06 19:40:41 +09:00
self.attributes('-fullscreen', True)
2024-06-08 22:38:53 +09:00
self.attributes('-topmost', True)
2024-06-06 19:40:41 +09:00
self.frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
self.frame.grid(row=0, column=0, sticky='nsew')
2024-06-06 19:40:41 +09:00
self.block_taskmgr()
self.block_key()
lock = Lock()
2024-06-06 19:40:41 +09:00
def exit(self):
self.unlock_taskmgr()
2024-06-06 19:40:41 +09:00
self.toast()
self.destroy()
def block_key(self):
block_keys = ['ctrl', 'alt', 'windows', 'shift', 'delete']
for i in block_keys:
keyboard.block_key(i)
def block_taskmgr(self):
block = subprocess.run(['reg', 'add', 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System', '/v', 'DisableTaskMgr', '/t', 'REG_DWORD', '/d', '1'])
print(block)
def unlock_taskmgr(self):
unlock = subprocess.run(['reg', 'delete', 'HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System', '/v', 'DisableTaskMgr', '/f'])
print(unlock)
2024-06-06 19:40:41 +09:00
def toast(self):
resource_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "resource")
success = Notification(
app_id='Dislocker',
title='ご協力ありがとうございます!',
msg='パスワード認証に成功しました。\n現在使われたパスワードは削除されます。',
icon=resource_path + r'\success.png'
)
success.set_audio(audio.Default, loop=False)
success.show()
def handler_close(self):
pass
2024-06-06 19:40:41 +09:00
class Lock(customtkinter.CTkToplevel):
def __init__(self):
super().__init__()
2024-06-10 13:27:21 +09:00
self.title(f'{app_name} | PC番号 {client_config["pc_number"]} | ロックされています')
self.geometry('400x200')
2024-06-06 19:40:41 +09:00
self.resizable(height=False, width=False)
self.attributes('-topmost', True)
2024-06-08 22:38:53 +09:00
self.grab_set()
2024-06-06 19:40:41 +09:00
self.lift()
self.protocol("WM_DELETE_WINDOW", self.handler_close)
2024-06-06 19:40:41 +09:00
self.msg_title_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
self.msg_title_frame.grid(row=0, column=0, sticky="nsew")
self.msg_title_1 = customtkinter.CTkLabel(self.msg_title_frame, text='ちょっと待って!!')
self.msg_title_1.grid(row=0, column=0, padx=10, pady=10)
self.msg_subtitle_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
self.msg_subtitle_frame.grid(row=1, column=0, sticky="nsew")
self.msg_subtitle_1 = customtkinter.CTkLabel(self.msg_subtitle_frame, text='サインインするには、パスワードが必要です。')
self.msg_subtitle_1.grid(row=0, column=0, padx=10, pady=10)
self.input_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
self.input_frame.grid(row=2, column=0, sticky="nsew")
self.password_entry = customtkinter.CTkEntry(self.input_frame, placeholder_text='パスワード', width=380, show='*')
2024-06-06 19:40:41 +09:00
self.password_entry.grid(row=0, column=0, padx=10, pady=10)
self.button_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color='transparent')
self.button_frame.grid(row=3, column=0, sticky="nsew")
self.signin_button = customtkinter.CTkButton(self.button_frame, text='サインイン', command=self.auth)
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()
2024-06-06 19:40:41 +09:00
def auth(self):
auth_url = client_config["auth_host_url"] + "/verify"
auth_json = {
2024-06-08 22:07:51 +09:00
"pc_number": int(client_config["pc_number"]),
"password": self.hash_genarate(str(self.password_entry.get()))
}
responce = requests.post(auth_url, json=auth_json)
if responce.status_code == 200:
2024-06-06 19:40:41 +09:00
self.destroy()
app.exit()
else:
self.msg_subtitle_1.configure(text='サインインするには、パスワードが必要です。\nパスワードが違います!')
def handler_close(self):
pass
def exit(self):
self.destroy()
2024-06-06 19:40:41 +09:00
2024-06-10 13:27:21 +09:00
def master_password_gen():
numbers = string.digits # (1)
password = ''.join(random.choice(numbers) for _ in range(10)) # (2)
password_hash = hashlib.md5(password.encode()).hexdigest()
result = {"password": password, "password_hash": password_hash}
return result
app_name = "Dislocker"
2024-06-06 19:40:41 +09:00
config_dir_path = "./config/"
client_config_path = config_dir_path + "client.json"
if not os.path.isfile(client_config_path):
2024-06-06 19:40:41 +09:00
if not os.path.isdir(config_dir_path):
os.mkdir(config_dir_path)
client_config = {
2024-06-10 13:27:21 +09:00
"initial": 0,
2024-06-08 22:07:51 +09:00
"auth_host_url": "http://localhost",
2024-06-10 13:27:21 +09:00
"pc_number": 1,
"master_password_hash": "",
2024-06-06 19:40:41 +09:00
}
with open(client_config_path, "w") as w:
json.dump(client_config, w, indent=4)
elif os.path.isfile(client_config_path):
with open(client_config_path, "r") as r:
client_config = json.load(r)
2024-06-10 13:27:21 +09:00
if client_config["initial"] == 0:
master_password = master_password_gen()
msgbox = tkinter.messagebox.showinfo(title=f"{app_name} | 初回起動を検出", message=f"初回起動のようです。\nマスターパスワードを記録しておいてください。\nこれ以降二度と表示されることはないでしょう。\n\n{master_password["password"]}\n\n")
client_config["master_password_hash"] = master_password["password_hash"]
with open(client_config_path, "w") as w:
json.dump(client_config, w, indent=4)
2024-06-06 19:40:41 +09:00
2024-06-10 13:27:21 +09:00
else:
if __name__ == '__main__':
app = App()
app.protocol("WM_DELETE_WINDOW", app.handler_close)
app.mainloop()