Last active
June 7, 2016 06:28
-
-
Save mildronize/225679f7570cec2f545f to your computer and use it in GitHub Desktop.
PSU Authentication Version 1.2.0 Download `wget https://gist.githubusercontent.com/mildronize/225679f7570cec2f545f/raw/dbfdebd1193ad39c94fabe380c67680a0d42e1ed/psu-auth-1.2.0.py && chmod a+x psu-auth-1.2.0.py`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Tested on Python 3.4 | |
# Version 1.2.0 | |
# Original file Version 1.00 from : Thanathip Limna, Chatchai Jantaraprim | |
# Modified by Thada Wangthammang | |
# Change log 1.2.0 | |
# - Use Environment variable for storing PSU Passport | |
# - Enable logout CLI (./auth-psu-1.2.0.py logout) | |
# Change log v1.1.2 | |
# - Transform to OOP | |
# Change log v1.1.1 | |
# - Fix check_auth for checking new string 'This computer has IP =' | |
# - Change line separator from Windows to Unix format | |
# - enter pass from terimal | |
# Change log v1.1.0 | |
# - Support Python 3.3.2 | |
# - Modified user interface | |
# - Support on Windows and Linux (haven't tested on Mac) | |
import urllib.request, urllib.parse, urllib.error, urllib.request, urllib.error, urllib.parse | |
import time, datetime | |
import sys | |
import re | |
import os | |
import getpass | |
class PsuAuth: | |
def __init__(self): | |
self.CHECK = 'http://login.psu.ac.th' | |
self.LOGIN = 'https://cp-xml.psu.ac.th/auth.php' | |
self.LOGOUT = 'http://login.psu.ac.th/cgi-bin/logout-cp.cgi' | |
self.SLEEP = 600 | |
self.USER = '__Your_Username__' | |
self.PASS = '__Your_Password__' | |
if os.getenv('PSU_USER') != None or os.getenv('PSU_PASS') != None: | |
self.USER = str(os.getenv('PSU_USER')) | |
self.PASS = str(os.getenv('PSU_PASS')) | |
else: | |
print("Environment var should be assigned in `PSU_USER` and `PSU_PASS`") | |
print("Otherwise, you must edit your PSU username and password in this file") | |
print("Example of assign environment variable: ") | |
print("export PSU_USER=xxxxxxxx && export PSU_PASS=yyyyyyyy") | |
print( ('You are logging in by '+self.USER+'\n') ) | |
self.log = None | |
if os.name == 'nt': | |
# -- For Windows | |
self.log = open('C:\\Users\\MILDRO~1\\AppData\\Local\\Temp\\psu-auth.log', 'w') | |
if not self.log: | |
self.log = open('C:\\Users\\MILDRO~1\\AppData\\Local\\Temp\\psu-auth.log', 'w') | |
else : | |
# -- For Unix | |
self.log = open('/tmp/psu-auth.log', 'w') | |
if not self.log: | |
self.log = open('/tmp/psu-auth.log', 'w') | |
def check_auth(self, data): | |
string = str(data, 'utf-8') | |
# print(string) | |
if re.search('ipv4 =',string) and re.search('ipv6 =',string) and re.search('Last refreshed :', string): | |
return True | |
else: | |
return False | |
def do_auth(self): | |
req = urllib.request.urlopen( self.CHECK ) | |
data = req.read() | |
req.close() | |
if self.check_auth(data): | |
self.log.write('psu auth: authed\n') | |
return True | |
else: | |
self.log.write('psu auth: do auth\n') | |
p = urllib.parse.urlencode( { 'user': self.USER, 'passwd': self.PASS, 'ok':'Login' } ) | |
byte_data = p.encode('utf-8') | |
req = urllib.request.Request( self.LOGIN, byte_data ) | |
req = urllib.request.urlopen(req) | |
data = req.read() | |
req.close() | |
return self.check_auth( data ) | |
def logout(self): | |
p = urllib2.parse.urlencode( { } ) | |
req = urllib2.request.Request( self.LOGOUT, p ) | |
# req = urllib.request.urlopen(req) | |
req = urllib.request.urlopen(req) | |
data = req.read() | |
req.close() | |
def run(self): | |
while True: | |
timestamp = datetime.datetime.today() | |
while True: | |
try: | |
if self.do_auth(): | |
break; | |
else: | |
print("auth fail") | |
time.sleep(5) | |
except Exception as e: | |
print(("error: ", e)) | |
time.sleep(10) | |
print(('['+str(timestamp)+'] '+'psu auth: sleep '+str(self.SLEEP)+'s')) | |
self.log.write('psu auth: sleep '+str(self.SLEEP)+'s\n') | |
time.sleep( self.SLEEP ) | |
if __name__ == '__main__': | |
if len(sys.argv) == 1: | |
option = 'loop' | |
else: | |
option = sys.argv[1] | |
if option == 'loop': | |
ps = PsuAuth().run() | |
elif option == 'logout': | |
print((datetime.datetime.today())) | |
PsuAuth().logout() | |
print('psu auth: logout success') | |
else: | |
print((datetime.datetime.today())) | |
print ('psu auth: invalid case') | |
# elif option == 'self.login': | |
# #print((datetime.datetime.today())) | |
# print( ('You are self.logging in by '+USER) ) | |
# do_auth() | |
# elif option == 'self.logout': | |
# print((datetime.datetime.today())) | |
# do_self.logout() | |
# print('psu auth: self.logout success') | |
# else: | |
# print((datetime.datetime.today())) | |
# print ('psu auth: invalid case') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment