forked from LethalEthan/Bluetooth-Unlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bluetooth-Unlock.py
267 lines (254 loc) · 9.63 KB
/
Bluetooth-Unlock.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#!/usr/bin/env python3
#Imports python modules
import sys
import os
import shutil
import argparse
import subprocess
import time
import getpass
import configparser
try:
import bluetooth
from bluetooth import *
import bluetooth._bluetooth as bt
except:
print("Cannot import the bluetooth modules!")
print("Please run install.sh!")
sys.exit(1)
#Detects python version
if (sys.version_info > (3, 0)):
print("Python 3 has been detected you may continue\n")
else:
sys.exit("Python 2 has been detected please run in Python3!")
#Things for imports
parser = argparse.ArgumentParser()
config = configparser.ConfigParser()
from distutils.spawn import find_executable
#Reads configuration file
config.read("config.ini")
#Variables
USER = getpass.getuser() #For the thank you message
GET_DEVADDR = 1 #For setup when no config is found
SELECT_ENV = 1 #For setup when no config is found
#Checks for a new version
if config.has_option("VERSION", "version"):
VERSION = config.get("VERSION", "version")
else:
sys.exit("VERSION/CONFIG NOT FOUND!")
UPDATE = input("Would you like to check for an update? [Y/N] ")
UPDATE = UPDATE.upper()
if UPDATE == "Y":
print("Downloading Update.ini from GitHub...")
os.system("wget -q -O Update.ini https://lethalethan.github.io/Bluetooth-Unlock/downloads/update.ini")
print("Done!\n")
config.read("Update.ini")
if config.has_option("NOTICES", "notices"):
NOTICES = config.get("NOTICES", "notices")
else:
NOTICES = "DOWNLOAD FAILED!"
print("Cannot find any notices, Download may have failed!")
if config.has_option("NEWVERSION", "newversion"):
NEWVERSION = config.get("NEWVERSION", "newversion")
else:
NEWVERSION = 0
print("Cannot find new version, Download may have failed!")
print(NOTICES,"\n")
if config.has_option("NEWVERSION", "newversion"):
if NEWVERSION > VERSION:
config.clear()
print("New version found:", NEWVERSION)
UPDATE_C = input("Would you like to Download updates? [Y/N] ")
UPDATE_C = UPDATE_C.upper()
if UPDATE_C == "Y":
print("Downloading Update from GitHub...")
os.system("wget -q -O Update.tar.gz https://lethalethan.github.io/Bluetooth-Unlock/downloads/zip/Bluetooth-Unlock-BU-V1-Stable.zip")
print("Downloaded!, please extract new files in current directory!")
else:
print("Not downloading update")
elif NEWVERSION < VERSION:
config.clear()
print("Version installed is higher than the version specified in update config!")
elif NEWVERSION == VERSION:
config.clear()
print("Latest version installed!")
else:
config.clear()
print("If you see this message then config has no version information!")
elif UPDATE == "N":
print("\nNot checking for updates!")
else:
print("Unknown response!")
config.read("config.ini")
time.sleep(2)
#Loads the options from the config and loads them into a local variable
#Loads Desktop Environment option
if config.has_option("DESKTOP", "env"):
print("\nDesktop Environment found, using:")
SELECT_ENV = 0
ENV = config.get("DESKTOP", "env")
print(ENV)
#Loads Device Address
if config.has_option("DEVADDR", "devaddr"):
print("\nDevice Address found, using:")
GET_DEVADDR = 0
DEVADDR = config.get("DEVADDR", "devaddr")
print(DEVADDR)
time.sleep(3)
#User can see what Desktop Environments are available (not all are detected)
if SELECT_ENV == 1:
AVAILDE = input("\nWould you like to see the available desktop environments? [Y/N] ")
AVAILDE = AVAILDE.upper()
if AVAILDE == "Y":
gnome_screensaver = {'GNOME': 'gnome-screensaver'}
for gnome_env in gnome_screensaver:
if find_executable(gnome_screensaver[gnome_env]):
print("GNOME has been found")
else:
print("GNOME has not found")
mate_screensaver = {'MATE': 'mate-screensaver-command'}
for mate_env in mate_screensaver:
if find_executable(mate_screensaver[mate_env]):
print("MATE has been found")
else:
print("MATE has not been found")
x_screensaver = {'XSCREENSAVER': 'xscreensaver'}
for x_env in x_screensaver:
if find_executable(x_screensaver[x_env]):
print("XSCREENSAVER has been found")
else:
print("XSCREENSAVER has not been found")
print("")
elif AVAILDE == "N":
print("Not displaying the available desktop environments")
else: ("Uknown response")
#Select Desktop Environment menu
if SELECT_ENV == 1:
#Detects if these desktop evironments are available
ENV = input("""Please Enter your Desktop Environment can be:
"LOGINCTL" (Recommended) (Don't use sudo)
"KDE" (Doesn"t work on older versions)
"GNOME"
"XSCREENSAVER"
"MATE"
"CINNAMON"
""")
ENV = ENV.upper()
if ENV == "LOGINCTL":
print(ENV,"has been selected\n")
elif ENV == "KDE":
print(ENV,"has been selected\n")
elif ENV == "GNOME":
print(ENV,"has been selected\n")
elif ENV == "XSCREENSAVER":
print(ENV,"has been selected\n")
elif ENV == "MATE":
print(ENV,"has been selected\n")
elif ENV == "CINNAMON":
print(ENV,"has been selected\n")
else:
sys.exit("Unidentified Environment exiting")
config["DESKTOP"] = {"ENV": (ENV)}
with open("config.ini", "w") as configfile:
config.write(configfile)
print("\nBluetooth-Unlock is a free open-source project and always will be")
print("I would like you to consider donating to allow me to do other projects")
print("The donation links are on the website: https://lethalethan.github.io/Bluetooth-Unlock/")
print("Thanks :)\n")
#Debug mode prints extra information of what's going on
DEBUG = input("Would you like to activate debug mode? [Y/N] ")
DEBUG = DEBUG.upper()
if DEBUG == "Y":
print("DEBUG is active")
elif DEBUG == "N":
print("DEBUG is not active")
else:
sys.exit("Unknown option")
#Code containing thank you message and device detection
print("")
print("Thank you for using Bluetooth-Unlock",VERSION ,USER ,"\n")
if GET_DEVADDR == 1:
print("Searching for nearby devices...\n")
nearby = discover_devices(lookup_names = True)
print(nearby,"\n")
DEVADDR = input("Enter Bluetooth Address of the device (e.g AA:BB:CC:DD:EE:FF): ")#Asks for bluetooth device address
config["DEVADDR"] = {"DEVADDR": (DEVADDR)}
with open("config.ini", "w") as configfile:
config.write(configfile)
elif GET_DEVADDR == 0:
print("Device Address is", DEVADDR)
time.sleep(2)
#Prints information
if DEBUG == "Y":
print("\nThis is the sections found in config.ini")
print(config.sections())
print("Desktop Environment is", ENV)
print("Device Address is",DEVADDR,"\n")
time.sleep(4)
else: time.sleep(1)
print("\nThank you to these contributors: jose1711, maaudrana, blackPanther OS")
print("jose1711 has improved the code of this project")
print("maaudrana is making a logo for this project")
print("blackPanther OS has improved/optimized the code of this project")
print("Thanks to all of them :)\n")
time.sleep(4)
#Variables for Main code
CHECKINTERVAL = 3 # device pinged at this interval (seconds) when screen is unlocked min:3
CHECKREPEAT = 2 # device must be unreachable this many times to lock
mode = "unlocked"
#Main code for Bluetooth-Unlock
#Return code 0 is when the command has ran successfully
#Return code 1 is when the command has failed to reach the device
print("Bluetooth-Unlock is now active!")
while True:
tries = 0
while tries < CHECKREPEAT:
#Checks for the device
time.sleep(CHECKINTERVAL)
check = subprocess.Popen(["sudo", "l2ping", DEVADDR, "-t", "1", "-c", "1"], shell=False, stdout=subprocess.PIPE)
check.wait()
retcode = check.returncode
if retcode == 0 and DEBUG == "Y":
print("ping OK!")
break
elif retcode > 0 and DEBUG == "Y":
#Prints returncode when debug is active
print("ping response code: %d" % (retcode))
time.sleep(1)
tries = tries + 1
if retcode == "Can\'t create socket: Operation not permitted":
print("Couldn't create a socket due to permissions")
#Unlocks when the device IS found
if retcode == 0 and mode == "locked":
mode = "unlocked"
if ENV == "LOGINCTL":
os.system("loginctl unlock-session")
elif ENV == "KDE":
os.system("loginctl unlock-session")
elif ENV == "GNOME":
os.system("gnome-screensaver-command -d")
elif ENV == "XSCREENSAVER":
os.system("pkill xscreensaver")
elif ENV == "MATE":
os.system("mate-screensaver-command -d")
elif ENV == "CINNAMON":
os.system("cinnamon-screensaver-command -d")
#Locks when the device ISN'T found
if retcode > 0 and mode == "unlocked":
mode = "locked"
if ENV == "LOGINCTL":
os.system("loginctl lock-session")
elif ENV == "KDE":
os.system("loginctl lock-session")
elif ENV == "GNOME":
os.system("gnome-screensaver-command -l")
elif ENV == "XSCREENSAVER":
os.system("xscreensaver-command -lock")
elif ENV == "MATE":
os.system("mate-screensaver-command -l")
elif ENV == "CINNAMON":
os.system("cinnamon-screensaver-command -l")
if mode == "locked":
time.sleep(1)
else:
time.sleep(CHECKINTERVAL)