Commit 07a5798e authored by Norris Hua's avatar Norris Hua
Browse files

Code refactoring

parent 1e53614e
Loading
Loading
Loading
Loading
+21 −42
Original line number Original line Diff line number Diff line
@@ -56,10 +56,7 @@ def loadConfig():
        newConfig()
        newConfig()
        return readConfig()
        return readConfig()


def ifLoggedIn(test):

    if test.status_code==204:
        return True
    return False
  
  
def main():
def main():
    printWithTimeStamp("Program started.")
    printWithTimeStamp("Program started.")
@@ -71,80 +68,62 @@ def main():
    while True:
    while True:
        printWithTimeStamp('Checking network status...')
        printWithTimeStamp('Checking network status...')
        try:
        try:
            test=requests.get(config['testUrl'])
            login=requests.Session()
            test=login.get(config['testUrl'])
        except:
        except:
            printWithTimeStamp('Connection FAILED. Try again in '+str(config['interval_retry_connection'])+' sec.')
            printWithTimeStamp('Connection FAILED. Try again in '+str(config['interval_retry_connection'])+' sec.')
            sleep(config['interval_retry_connection'])
            sleep(config['interval_retry_connection'])
            continue
            continue
        while not ifLoggedIn(test):
        while test.status_code!=204:
        
            printWithTimeStamp('You are offline. Starting login...')
            printWithTimeStamp('You are offline. Starting login...')
            printWithTimeStamp('Start to get login information')
            printWithTimeStamp('Start to get login information')
            start=test.text.find(r'wlanuserip')+13
            end=test.text.find(r'&locale=')
            if start != 12:
                wlanuserip=test.text[start:end]

            start=test.text.find(r'wlanacip')+11
            end=test.text.find(r'%26wlanuserip')
            if start != 10:
                wlanacip=test.text[start:end]
        
            h={'Host': 'weblogin.sustc.edu.cn', 'Connection': 'keep-alive', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests': '1' ,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'DNT': '1', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'}
            #Headers are acquired by capturing network traffic
            
            
            url='http://weblogin.sustc.edu.cn/cas/login?service=http%3A%2F%2Fenet.10000.gd.cn%3A10001%2Fsz%2Fsz112%2Findex.jsp%3Fwlanacip%3D'+wlanacip+'%26wlanuserip%3D'+wlanuserip
            start=test.text.find(r'action')+8
            #wlanacip & wlanuserip
            end=test.text.find(r'" method="post"')
            action=test.text[start:end] 


                
                
            prelogin=requests.get(url,headers=h)
            start=test.text.find(r'name="lt"')+17
                
            start=prelogin.text.find(r'name="lt"')+17
            end=start+50
            end=start+50
            lt=prelogin.text[start:end]
            lt=test.text[start:end]
            end=lt.find(r'" />')
            end=lt.find(r'" />')
            lt=lt[0:end]
            lt=lt[0:end]
            #Get parameter "lt"
            #Get parameter "lt"
            
            
            start=prelogin.text.find(r'name="execution"')+24
            start=test.text.find(r'name="execution"')+24
            end=start+10
            end=start+10
            execution=prelogin.text[start:end]
            execution=test.text[start:end]
            end=execution.find(r'" />')
            end=execution.find(r'" />')
            execution=execution[0:end]
            execution=execution[0:end]
            #Get parameter "execution"
            #Get parameter "execution"
        
        
            start=prelogin.text.find(r'name="_eventId"')+23
            start=test.text.find(r'name="_eventId"')+23
            end=start+10
            end=start+10
            _eventId=prelogin.text[start:end]
            _eventId=test.text[start:end]
            end=_eventId.find(r'" />')
            end=_eventId.find(r'" />')
            _eventId=_eventId[0:end]
            _eventId=_eventId[0:end]
            #Get parameter "_eventld"
            #Get parameter "_eventld"
                 
                 
            end=prelogin.headers['Set-Cookie'].find(r'; Path=/cas/;')
            JSESSIONID_SUSTC= prelogin.headers['Set-Cookie'][11:end]
            #JSESSIONID stored in set-cookies
            
            printWithTimeStamp('Login information acquired.')
            printWithTimeStamp('Login information acquired.')


        
        
            url='http://weblogin.sustc.edu.cn/cas/login;jsessionid='+JSESSIONID_SUSTC+'?service=http://enet.10000.gd.cn%3A10001%2Fsz%2Fsz112%2Findex.jsp%3Fwlanacip%3D'+wlanacip+'%26wlanuserip%3D'+wlanuserip
            url='http://weblogin.sustc.edu.cn'+action
            username=config['username']
            username=config['username']
            passwd=config['password']
            passwd=config['password']
            #SUSTC CAS username (StudentID) and password
            #SUSTC CAS username (StudentID) and password
            data='username='+username+'&password='+passwd+'&lt='+lt+'&execution='+execution+'&_eventId='+_eventId+'&submit=LOGIN'
            data='username='+username+'&password='+passwd+'&lt='+lt+'&execution='+execution+'&_eventId='+_eventId+'&submit=LOGIN'


            h={'Host': 'weblogin.sustc.edu.cn', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Origin': 'http://weblogin.sustc.edu.cn', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'DNT': '1', 'Referer': url, 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'}
            h={'Host': 'weblogin.sustc.edu.cn', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Origin': 'http://weblogin.sustc.edu.cn', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'DNT': '1', 'Referer': url, 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'}
            #Acquired by capturing traffic

            cookies=dict({'Cookie': 'JSESSIONID='+JSESSIONID_SUSTC})
            login=requests.Session()
            #Use session to maintain cookies
            printWithTimeStamp('Login as '+username)
            printWithTimeStamp('Login as '+username)


            login.post(url,data=data,headers=h,cookies=cookies)        
            login.post(url,data=data,headers=h)        
            printWithTimeStamp('Login information posted to the CAS server.')
            printWithTimeStamp('Login information posted to the CAS server.')


            test=requests.get(config['testUrl'])
            test=requests.get(config['testUrl'])
            
            
            if not ifLoggedIn(test) :
            if test.status_code!=204 :
                times_retry_login-=1
                times_retry_login-=1
                if times_retry_login>0: # If keep trying to login too many times, it may trigger security alarm on the CAS server
                if times_retry_login>0: # If keep trying to login too many times, it may trigger security alarm on the CAS server
                    printWithTimeStamp('Login FAILED. Try again in '+str(config['interval_retry_login'])+' sec. '+str(times_retry_login)+r' attempt(s) remaining.')
                    printWithTimeStamp('Login FAILED. Try again in '+str(config['interval_retry_login'])+' sec. '+str(times_retry_login)+r' attempt(s) remaining.')
@@ -153,7 +132,7 @@ def main():
                    sys.exit('Login FAILED')
                    sys.exit('Login FAILED')
                
                
                sleep(config['interval_retry_login'])
                sleep(config['interval_retry_login'])
            if ifLoggedIn(test):
            else:
                printWithTimeStamp('Login successful. Current user: '+username)
                printWithTimeStamp('Login successful. Current user: '+username)
                times_retry_login=config['max_times_retry_login']
                times_retry_login=config['max_times_retry_login']
                printWithTimeStamp('Login attempts reseted to '+str(times_retry_login)+' .')
                printWithTimeStamp('Login attempts reseted to '+str(times_retry_login)+' .')