This repository has been archived by the owner on Feb 20, 2021. It is now read-only.
/
Main.py
144 lines (109 loc) · 3.8 KB
/
Main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from includes.mechanize import Browser
import StatusIcon
from Wifi import *
import Credentials
from System import SystemType, systems
current_system = SystemType()
if current_system == "linux":
from lin.events import loop
elif current_system == "windows":
import sys
sys.path.append("win//gtkbin//")
from win.events import loop
elif current_system == "osx":
from osx.events import loop
elif current_system == "loop":
from loop.events import loop
USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; tr-TR; rv:1.8.1.9) Gecko/20071102 Pardus/2007 Firefox/2.0.0.9"
LOGIN_ADDRESS = "https://wwu.wlan.wwu.edu/fs/customwebauth/login-dmca-focus.html?switch_url=https://wwu.wlan.wwu.edu/login.html&wlan=WWUwireless"
LOGOUT_ADDRESS = "https://wwu.wlan.wwu.edu/logout.html"
def get_title(html):
return html.split("</title>")[0].split("<title>")[1]
class Manager:
def __init__(self):
self.statusicon = StatusIcon.StatusIcon(self)
self.CredentialManager = Credentials.CredentialManager(self)
self.browser = Browser()
self.browser.addheaders = [("User-agent", USER_AGENT)]
self.logged = False
self.check_initial_state()
if current_system == "windows":
self.MainLoop = loop(self)
else:
self.MainLoop = loop()
#when we get a signal from the wifi event loop we need to do some stuff
def on_signal(self, state):
if state == 0:
print "disconnected signal"
self.statusicon.set_visibility(False)
self.logged = False
elif state == 1:
print "connection established signal"
self.on_connection()
elif state == 2:
print "acquiring connection signal"
self.statusicon.set_blinking(True)
def on_password_change(self):
print "our stored password changed"
print self.CredentialManager.get_username()
print self.CredentialManager.get_password()
self.statusicon.set_blinking(True)
if self.wwu_auth():
self.logged = True
self.statusicon.set_blinking(False)
def on_connection(self):
print "on connection"
self.statusicon.set_blinking(False)
if is_wwu_wifi():
print "on wwu wifi"
self.statusicon.set_visibility(True)
if self.need_auth():
print "we need to auth"
self.statusicon.set_blinking(True)
if not self.wwu_auth():
print "we failed to auth"
#tries to log on if it failed this executes
self.CredentialManager.ask_user()
else:
print "we succeeded with auth"
self.logged = True
self.statusicon.set_blinking(False)
else:
print "we're already logged on"
self.logged = True
self.statusicon.set_blinking(False)
else:
print "not wwu wifi"
self.statusicon.set_visibility(False)
self.logged = False
def check_initial_state(self):
#we need to figure out whether we're already on a wifi network
#and if so whether it's a wwu wireless network
#and if so whether we need to log on
#this might eventually have some other stuff in it but right now we can
#just use the on_connection function
self.on_connection()
def need_auth(self):
response = self.browser.open("http://www.blankwebpage.com/")
return get_title(response.read()) == "Web Authentication"
def wwu_auth(self, statusicon="stat"):
self.browser.open(LOGIN_ADDRESS)
self.browser.select_form("MyForm")
self.browser.form.set_all_readonly(False)
self.browser["username"] = self.CredentialManager.get_username()
self.browser["password"] = self.CredentialManager.get_password()
self.browser["buttonClicked"] = "4"
self.browser.submit()
return not (self.need_auth())
def wwu_de_auth(self, statusicon="stat"):
self.statusicon.set_blinking(True)
response = self.browser.open(LOGOUT_ADDRESS)
self.browser.select_form(nr=0)
self.browser.submit()
def quit(self, statusicon="stat"):
self.MainLoop.quit()
main = Manager()
if current_system == 'linux':
main.MainLoop.run()