forked from pahnin/cyberoam-linux
/
cyberoam.py
executable file
·225 lines (174 loc) · 6.2 KB
/
cyberoam.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
#!/usr/bin/env python
#
# Author of this software - phanindra srungavarapu <pahninsd@gmail.com> - phanindras.com
#
# Cyberoam login client GUI for linux
# This is a free software distributed under GPL without any warrenty and support
# you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without esavingven the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/#GPL>.
#
import urllib, urllib2, os, sys, gobject
from xml.dom.minidom import parseString
import pygtk
pygtk.require('2.0')
import gtk
import re
class Cyberoam():
def __init__(self):
self.url = 'http://192.168.1.250:8090/corporate/servlet/CyberoamHTTPClient'
self.params = {
'mode' : '192'
}
self.status="new"
print "Cyberoam client for linux started.."
self.build_gui()
def build_gui(self):
print "Starting GUI for cyberoam-linux client.."
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
self.window.connect("delete_event", self.gotoTray)
self.window.connect("destroy", self.destroy)
#self.window.set_border_width(24)
self.window.set_size_request(400, 200)
self.window.set_title("Cyberoam client")
self.window.set_position(gtk.WIN_POS_CENTER_ALWAYS)
self.window.set_resizable(False)
topwid=gtk.VBox(gtk.FALSE, 0)
mb = gtk.MenuBar()
appmenu = gtk.Menu()
app = gtk.MenuItem("App")
app.set_submenu(appmenu)
exit = gtk.MenuItem("Exit")
exit.connect("activate", self.destroy)
appmenu.append(exit)
mb.append(app)
vbox = gtk.VBox(False, 2)
vbox.pack_start(mb, False, False, 0)
topwid.pack_start(vbox)
table = gtk.Table(3, 3, True)
topwid.pack_start(table)
label = gtk.Label("Username")
table.attach(label,0,1,0,1)
label.show()
self.username=gtk.Entry(max=10)
table.attach(self.username,1,3,0,1)
self.username.show()
label1 = gtk.Label("Password")
table.attach(label1,0,1,1,2)
label1.show()
self.password=gtk.Entry(max=10)
table.attach(self.password,1,3,1,2)
self.password.set_visibility(False)
self.password.show()
self.button=gtk.Button("Login")
self.button.connect("clicked",self.click_button)
table.attach(self.button,1,3,2,3)
self.button.show()
#
table.show()
self.window.add(topwid)
topwid.show()
self.statusicon = gtk.status_icon_new_from_stock(gtk.STOCK_NETWORK)
self.statusicon.connect('activate', self.status_clicked )
self.statusicon.set_name("Cyberoam")
self.window.show_all()
self.winstatus='showing'
gtk.main()
def gotoTray(self, widget, event, data=None):
self.window.hide_on_delete()
self.winstatus='hiding'
print "minimizing app to tray"
return True
def destroy(self, widget, data=None):
if self.status=='login':
self.doLogout()
gtk.main_quit()
sys.exit(0)
def status_clicked(self,status):
if self.winstatus == 'showing':
self.window.hide()
print "Hiding app"
self.winstatus='hiding'
else:
print "Showing app"
self.window.show_all()
self.winstatus='showing'
def conServer(self):
print "connecting to server"
encodedata = urllib.urlencode(self.params)
req = urllib2.Request(self.url, encodedata)
self.response = urllib2.urlopen(req)
self.responsedata = self.response.read()
print self.responsedata
def chkLogin(self):
try:
self.conServer()
hasip = re.search('((25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)',self.responsedata)
if self.responsedata == "<?xml version='1.0' ?><liverequestresponse><loginstatus>false</loginstatus><livemessage>Critical error has occured</livemessage></liverequestresponse>":
self.doLogin()
elif self.responsedata == "<?xml version='1.0' ?><liverequestresponse><liverequesttime>180</liverequesttime><livemessage></livemessage></liverequestresponse>":
print "you are already logged in"
self.doLogin()
elif hasip:
print "some one already logged in on this computer"
self.doLogin()
elif self.responsedata == "<?xml version='1.0' ?><liverequestresponse><loginstatus>false</loginstatus><livemessage>You have been logged on from somewhere else, please relogon</livemessage></liverequestresponse>":
print "you are loggedd in some where else"
self.doLogout()
self.doLogin()
except Exception, detail:
print "Error Occured ", detail
def keepalive(self):
if self.status=="login":
print "updating status"
self.conServer()
return True
else:
return False
def doLogin(self):
self.params['mode']=191
self.conServer()
self.status="login"
self.button.set_label('Logout')
self.gotoTray(self,self.window,"minimize_event")
self.params['mode']=192
def doLogout(self):
self.params['mode']=193
self.conServer()
print self.responsedata
self.status="logout"
self.button.set_label('Login')
self.params['mode']=192
def click_button(self, widget, data=None):
if self.username.get_text_length() != 0 and self.password.get_text_length() != 0:
self.params['username']=self.username.get_text()
self.params['password']=self.password.get_text()
if self.status=="login":
self.doLogout()
elif self.status=="logout":
self.doLogin()
gobject.timeout_add(150000,self.keepalive)
elif self.status=="new":
self.chkLogin()
self.button.set_label('Logout')
gobject.timeout_add(150000,self.keepalive)
# if self.chkLogin():
# print self.responsedata
# else:
# msgpop=gtk.MessageDialog(parent=None, flags=gtk.DIALOG_DESTROY_WITH_PARENT, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format="Some error occured")
# msgpop.run()
# msgpop.destroy()
else:
msgpop=gtk.MessageDialog(parent=None, flags=gtk.DIALOG_DESTROY_WITH_PARENT, type=gtk.MESSAGE_ERROR, buttons=gtk.BUTTONS_CLOSE, message_format="Need password and username fields")
msgpop.run()
msgpop.destroy()
cyb=Cyberoam()