-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathotpindicator
More file actions
executable file
·104 lines (89 loc) · 3.23 KB
/
Copy pathotpindicator
File metadata and controls
executable file
·104 lines (89 loc) · 3.23 KB
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
#!/usr/bin/python3
#
# Quick hack to have a totp token in the unity status bar. Pass the totp key as
# commandline argument. Only does 6 digit, 30 second totp tokens.
import dbus.mainloop.glib
import gi
gi.require_version('GObject', '2.0')
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import GObject as gobject
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
from gi.repository import AppIndicator3 as appindicator
import NetworkManager
import pyotp
import signal
import sys
import time
signal.signal(signal.SIGINT, signal.SIG_DFL)
APPINDICATOR_ID = 'totp'
step=30
icons = {
100: 'battery-full-symbolic',
60: 'battery-good-symbolic',
40: 'battery-low-symbolic',
20: 'battery-caution-symbolic',
10: 'battery-empty-symbolic',
}
def main():
token = sys.argv[1]
vpn = None
if len(sys.argv) > 2:
vpn = sys.argv[2]
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
indicator = appindicator.Indicator.new(APPINDICATOR_ID, icons[100], appindicator.IndicatorCategory.SYSTEM_SERVICES)
indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
indicator.set_label('......', '888888')
indicator.connect('scroll-event', lambda ind, *_: gtk.Clipboard.get(gdk.SELECTION_CLIPBOARD).set_text(ind.get_label(), -1))
set_token(indicator, token)
if vpn:
agent = SecretAgent(vpn, indicator)
menu = gtk.Menu()
item = gtk.MenuItem('Quit')
item.connect('activate', lambda source: gtk.main_quit())
menu.append(item)
if vpn:
item = gtk.MenuItem('Activate VPN')
item.connect('activate', lambda source: activate_vpn(vpn))
menu.append(item)
menu.show_all()
indicator.set_menu(menu)
gobject.timeout_add(1000 - (time.time() % 1) * 1000, second_sync, indicator, token)
gtk.main()
def second_sync(indicator, token):
gobject.timeout_add(1000, set_token, indicator, token)
set_token(indicator, token)
def set_token(indicator, token):
otp = pyotp.TOTP(token).now()
pct_remaining = int((step-(time.time() % step))/step * 100)
for icon in sorted(icons):
if icon > pct_remaining:
break
icon = icons[icon]
if indicator.get_icon() != icon:
indicator.set_icon(icon)
if indicator.get_label() != otp:
indicator.set_label(otp, '888888')
return True
def activate_vpn(vpn):
for connection in NetworkManager.Settings.ListConnections():
settings = connection.GetSettings()['connection']
if settings['id'] == vpn:
NetworkManager.NetworkManager.ActivateConnection(connection, "/", "/")
class SecretAgent(NetworkManager.SecretAgent):
def __init__(self, vpn, indicator):
self.vpn = vpn
self.indicator = indicator
super(SecretAgent, self).__init__('net.seveas.otpindicator')
def GetSecrets(self, settings, connection, setting_name, hints, flags):
try:
if setting_name != 'vpn' or settings['connection']['id'] != self.vpn:
return {}
return {setting_name: {'secrets': {'password': self.indicator.get_label()}}}
except:
import traceback
traceback.print_exc()
return {}
if __name__ == "__main__":
main()