-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (66 loc) · 2.3 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import sys
import time
import subprocess
import webbrowser
import pystray
from PIL import Image, ImageDraw
SU_PASSWORD = 'bob'
class CommandError(Exception):
pass
def create_ui():
warp_checkbox = pystray.MenuItem(
'Warpified', on_clicked, checked=lambda item: state)
quit_button = pystray.MenuItem('Quit warpy', on_clicked)
help_button = pystray.MenuItem('Get help', on_clicked)
menu = pystray.Menu(
warp_checkbox,
quit_button,
help_button
)
icon = pystray.Icon('Warpy', load_image('icon.png'), menu=menu)
return icon
def load_image(path):
image = Image.open(path)
return image
def on_clicked(icon, item):
print(f"[+] '{item.text}' button pressed")
if item.text == "Quit warpy":
print("[-] Quitting...")
ui.stop()
sys.exit(0)
elif item.text == "Warpified":
global state
command = f'sudo -S wg-quick {"down" if state else "up"} wgcf-profile'.split()
try:
print(f"[*] Running command {command}")
print(f'[*] Providing sudo with password "{SU_PASSWORD}"')
process = subprocess.run(command, input=bytes(
f'{SU_PASSWORD}\n', 'utf-8'), capture_output=True)
if process.returncode != 0:
raise(CommandError(
f"Command returned code {process.returncode} and message {process.stderr}"))
except Exception as e:
print("[!] An error occured while running command:")
print(f"[!] {type(e)}\n{str(e)}")
else:
print(
f"[*] Successfully {'activated' if not state else 'deactivated'} Warp")
state = not item.checked
print(f"[*] Toggling check")
elif item.text == "Get help":
webbrowser.open("https://github.com/TheBdouilleur/warpy/issues")
print("[*] Opened issue page on default web browser.")
process = subprocess.run(['wgcf', 'trace'], capture_output=True)
state = b"warp=on" in process.stdout
print("[+] Starting...")
print(f"[*] Warp is {'on' if state else 'off'}")
ui = create_ui()
try:
ui.run()
except KeyboardInterrupt:
print("[!] Aborted by user.")
ui.stop()
sys.exit("aborted")
except Exception as e:
print("[!] An error occured while running mainloop:")
print(f"[!] {type(e)}\n{str(e)}")