-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpn_lib.py
184 lines (162 loc) · 5.98 KB
/
vpn_lib.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
try:
import gtk
except:
pass
import notify2
import os
import time
from signal import SIGTERM
import errno
import sys
import traceback
import ConfigParser
import json
from icons import *
class vpn_lib:
def __init__(self, homeDir, scriptName, textonly=False, debug=False, noicon=False):
self.homeDir = homeDir
self.scriptName = scriptName
self.textonly = textonly
self.debug = debug
self.noicon = noicon
self.pidfile = self.homeDir + "/" + self.scriptName + ".pid"
self.config = ConfigParser.ConfigParser()
self.config.read(self.homeDir + "/" + self.scriptName + ".secret")
if not self.textonly and not self.debug:
# create a new window
self.statusIcon = gtk.StatusIcon()
self.statusIcon.connect('popup-menu', self.on_right_click)
notify2.init(self.scriptName)
def getTokenPass(self, tokenPass):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(350, 60)
window.set_position(gtk.WIN_POS_CENTER)
window.set_title(self.scriptName + ", PIN from token:")
window.connect("delete_event", lambda w, e: gtk.main_quit())
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
entry = gtk.Entry()
entry.set_max_length(50)
entry.connect("activate", self.myCallback, entry, window, tokenPass)
vbox.pack_start(entry, True, True, 0)
entry.show()
hbox = gtk.HBox(False, 0)
vbox.add(hbox)
hbox.show()
button = gtk.Button(stock=gtk.STOCK_CANCEL)
# button.connect("clicked", lambda w: quit())
button.connect("clicked", lambda w: sys.exit(2))
hbox.pack_start(button, True, True, 0)
button.set_flags(gtk.CAN_DEFAULT)
button.grab_default()
button.show()
button = gtk.Button(stock=gtk.STOCK_OK)
button.connect("clicked", self.myCallback, entry, window, tokenPass)
hbox.pack_start(button, True, True, 0)
button.set_flags(gtk.CAN_DEFAULT)
button.grab_default()
button.show()
window.show()
def myCallback(self, widget, entry, window, tokenPass):
tokenPass[0] = entry.get_text()
tokenPass[0] = "".join(tokenPass[0].split())
window.destroy()
if len(tokenPass[0]) != 6:
self.message(data="Bad tokena length")
sys.exit(1)
if not tokenPass[0].isdigit():
self.message(data="Bad chars in token")
sys.exit(1)
gtk.main_quit()
def message(self, data=None, type=gtk.BUTTONS_CLOSE):
msg = gtk.MessageDialog(None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, type, data)
rval = msg.run()
msg.destroy()
return rval
def set_icon(self, icon_file):
self.statusIcon.set_from_file(icon_file)
def make_menu(self, event_button, event_time, data=None):
menu = gtk.Menu()
close_item = gtk.MenuItem("Close " + self.scriptName)
menu.append(close_item)
# add callback
close_item.connect_object("activate", self.close_app, "Really close " + self.scriptName + "?")
close_item.show()
# Popup the menu
menu.popup(None, None, None, event_button, event_time)
def on_right_click(self, data, event_button, event_time):
self.make_menu(event_button, event_time)
def close_app(self, data=None):
pidfile = self.homeDir + "/" + self.scriptName + ".pid"
if os.path.exists(pidfile):
os.remove(pidfile)
self.sysCmds("postVPNstop")
sys.exit(0)
def checkPidFile(self):
msg = [
"pidfile " + self.pidfile + " exists!",
"Connection is already established or\npreceding terminated with error.\nCheck processes and delete file."
]
if os.path.exists(self.pidfile):
if self.textonly or self.debug:
print msg[0], msg[1]
else:
n = notify2.Notification(msg[0], msg[1], errorIcon)
n.set_urgency(2)
n.show()
sys.exit(1)
def sysCmds(self, section):
try:
cmds = json.loads(self.config.get(section, "systemCMD"))
if self.textonly or self.debug:
print "\n", section + ":"
for cmd in cmds:
if self.textonly or self.debug:
print " ", cmd
try:
os.system(cmd)
except Exception:
print traceback.format_exc()
raise
except:
pass
def killVpn(self):
try:
pf = file(self.pidfile,'r')
pid = int(pf.read().strip())
pf.close()
except IOError:
pid = None
if not pid:
msg = ["pidfile " + self.pidfile + " does not exists!", "Has been deleted?\nClose connection by killing process"]
if self.textonly or self.debug:
print msg[0], msg[1]
else:
n = notify2.Notification(msg[0], msg[1], errorIcon)
n.set_urgency(2)
n.show()
sys.exit(1)
try:
self.sysCmds("preVPNstop")
while 1:
os.kill(pid, SIGTERM)
time.sleep(0.1)
except OSError, err:
if err.errno == errno.ESRCH:
if os.path.exists(self.pidfile):
os.remove(self.pidfile)
msg = "Closed VPN connection: " + self.scriptName
if self.textonly or self.debug:
print msg
else:
n = notify2.Notification(msg, "", connectingIcon)
n.show()
self.sysCmds("postVPNstop")
else:
n = notify2.Notification(str(err))
n.set_urgency(2)
n.show()
sys.exit(1)