Created
April 30, 2020 14:59
-
-
Save z4yx/ba7b6671084e8744ea34b6181c9872ab to your computer and use it in GitHub Desktop.
Yubikey 没拔! From: https://cfp.vim-cn.com/cbf05?py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import subprocess | |
import notify2 | |
import dbus | |
from gi.repository import GLib | |
from dbus.mainloop.glib import DBusGMainLoop | |
import yubico | |
def check(bus, msg): | |
if msg.get_path() != "/ScreenSaver": | |
return | |
args = msg.get_args_list() | |
if args[0]: | |
print("Screen locked", file=sys.stderr) | |
else: | |
print("Screen unlocked", file=sys.stderr) | |
return | |
try: | |
_ = yubico.find_yubikey(debug=False) | |
except yubico.yubico_exception.YubicoError: | |
return | |
else: | |
print("Found yubikey", file=sys.stderr) | |
n = notify2.Notification("Yubikey", "Yubikey 没拔!", "dialog-warning") | |
n.show() | |
subprocess.run(["loginctl", "unlock-session"]) | |
if __name__ == '__main__': | |
DBusGMainLoop(set_as_default=True) | |
bus = dbus.SessionBus() | |
bus.add_match_string( | |
"type='signal'," + | |
"path='/ScreenSaver'," + | |
"interface='org.freedesktop.ScreenSaver'," + | |
"member='ActiveChanged'") | |
bus.add_message_filter(check) | |
notify2.init(sys.argv[0]) | |
mainloop = GLib.MainLoop() | |
mainloop.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment