-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathvpn-indicator-daemon
executable file
·74 lines (63 loc) · 2.6 KB
/
vpn-indicator-daemon
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
#!/usr/bin/python
import os
import dbus
import dbus.service
import dbus.mainloop.glib
import gobject
import subprocess
class Daemon(dbus.service.Object):
INTERFACE = "me.imtx.vpndaemon"
ENV = {'PATH': '/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin'}
@dbus.service.method(INTERFACE,
in_signature='s', out_signature='')
def load_config(self, path):
os.system('ln -sf %s /etc/openvpn/openvpn.conf' % path)
@dbus.service.method(INTERFACE,
in_signature='', out_signature='ss')
def stop_vpn(self):
return subprocess.Popen(['service', 'openvpn', 'stop'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self.ENV).communicate()
@dbus.service.method(INTERFACE,
in_signature='', out_signature='ss')
def start_vpn(self):
return subprocess.Popen(['service', 'openvpn', 'start'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self.ENV).communicate()
@dbus.service.method(INTERFACE,
in_signature='', out_signature='b')
def is_running(self):
# Fedora/GNOME Shell use this
output = ''.join(subprocess.Popen(['service', 'openvpn', 'status'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self.ENV).communicate())
if 'not started' in output:
return False
else:
return True
@dbus.service.method(INTERFACE,
in_signature='', out_signature='b')
def is_connected(self):
# Fedora/GNOME Shell use this
output = ''.join(subprocess.Popen(['ifconfig', '-s'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
env=self.ENV).communicate())
if 'tun' in output:
return True
else:
return False
@dbus.service.method(INTERFACE,
in_signature='', out_signature='')
def exit(self):
mainloop.quit()
if __name__ == '__main__':
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
system_bus = dbus.SystemBus()
name = dbus.service.BusName(Daemon.INTERFACE, system_bus)
object = Daemon(system_bus, '/Daemon')
mainloop = gobject.MainLoop()
mainloop.run()