OTP-Viewer/OtpViewer.py
Vincent Hanewinkel 193a05b4d5 add Icon
2025-11-26 11:12:46 +01:00

112 lines
3.0 KiB
Python

import pyotp, time
import tkinter as tk
from tkinter import simpledialog, messagebox
from dotenv import load_dotenv
import sys
import os
load_dotenv()
email = os.getenv("ACCOUND")
seed = os.getenv("OTP_SEED")
def resource_path(filename):
if hasattr(sys, 'frozen'): # PyInstaller-EXE
return os.path.join(sys._MEIPASS, filename)
return os.path.join(os.path.dirname(__file__), filename)
def set_env_value(key, value, path=".env"):
lines = []
found = False
try:
with open(path, "r") as f:
for line in f:
if line.startswith(f"{key}="):
lines.append(f"{key}={value}\n")
found = True
else:
lines.append(line)
except FileNotFoundError:
pass # Falls Datei noch nicht existiert
if not found:
lines.append(f"{key}={value}\n")
with open(path, "w") as f:
f.writelines(lines)
otp = pyotp.TOTP(seed)
INTERVAL = otp.interval # Standard = 30s
def seconds_left():
# Differenz bis zum nächsten 30-Sekunden-Zyklus
return INTERVAL - int(time.time()) % INTERVAL
root = tk.Tk()
root.title("OTP-Code")
label_email = tk.Label(
root,
text=f"Account: {email}",
font=("Arial", 20, "bold"),
fg='Black', # Textfarbe
)
label_email.pack(padx=20, pady=20)
label = tk.Label(
root,
text=f"Lade ...",
font=("Arial", 20, "bold"),
fg='Black', # Textfarbe
)
label.pack(padx=20, pady=20)
def update():
remaining = seconds_left()
global seed
if seed == 'Standard':
label.config(text=f"Gibt deinen Otp Seed ein",fg='black',)
root.after(250, update)
else:
if remaining > 10:
color = 'green'
elif remaining > 5:
color = "#e9ab00"
else:
color = 'red'
label.config(text=f"Code: {otp.now()} - Läuft ab in: {remaining:02d}s",fg=color,)
root.after(250, update)
def set_seed():
global email, seed, otp
new_account = simpledialog.askstring("Accountname eingeben", "Gib deinen Accountnamen ein:")
new_seed = simpledialog.askstring("Seed eingeben", "Gib den neuen OTP-Seed ein:")
if new_seed and new_account:
try:
otp = pyotp.TOTP(new_seed)
seed = new_seed
set_env_value("OTP_SEED", new_seed)
set_env_value("ACCOUND", new_account)
email = new_account
label_email.config(text=f"Account: {email}")
messagebox.showinfo("Erfolg", "Seed wurde erfolgreich geändert!")
except Exception as e:
messagebox.showerror("Fehler", f"Ungültiger Seed:\n{e}")
menubar = tk.Menu(root)
root.config(menu=menubar)
otp_menu = tk.Menu(menubar, tearoff=0)
otp_menu.add_command(label="Seed ändern", command=set_seed)
otp_menu.add_separator()
otp_menu.add_command(label="Beenden", command=root.destroy)
menubar.add_cascade(label="Optionen", menu=otp_menu)
root.iconbitmap(resource_path("otp-icon.ico"))
update()
root.mainloop()