147 lines
3.7 KiB
Python
Executable File
147 lines
3.7 KiB
Python
Executable File
"""
|
|
Functions for network related stuff.
|
|
"""
|
|
|
|
import requests
|
|
import hashlib
|
|
from bs4 import BeautifulSoup
|
|
import os
|
|
|
|
not_login = 0
|
|
F_not_anymore = 0
|
|
session = requests.Session()
|
|
|
|
def _read_shadow() -> str:
|
|
"""
|
|
Reads the shadow file and returns it in
|
|
a suitable format for other functions.
|
|
"""
|
|
|
|
result = ""
|
|
try:
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "r") as f:
|
|
for row in f:
|
|
result += row+";"
|
|
except FileNotFoundError:
|
|
os.system(f"touch {os.environ['HOME']}/.local/lib/solendata/shadow")
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "r") as f:
|
|
for row in f:
|
|
result += row+";"
|
|
|
|
return result
|
|
|
|
def _save_creds(user: str, pwd: str):
|
|
"""
|
|
Saves credentials into the shadow file
|
|
by appending them.
|
|
"""
|
|
|
|
creds = ";" + _get_creds_hash(user, pwd)
|
|
|
|
try:
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "a+") as f:
|
|
if not creds in _read_shadow():
|
|
f.write(creds)
|
|
except FileNotFoundError:
|
|
os.system(f"touch {os.environ['HOME']}/.local/lib/solendata/shadow")
|
|
with open(f"{os.environ['HOME']}/.local/lib/solendata/shadow", "a+") as f:
|
|
if not creds in _read_shadow():
|
|
f.write(creds)
|
|
|
|
def _eval_creds(creds: str) -> bool:
|
|
"""
|
|
Evaluates the credentials for if
|
|
they are in the shadow file or not.
|
|
"""
|
|
|
|
shadow = _read_shadow()
|
|
|
|
if creds in shadow:
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def _get_creds_hash(user: str, pwd: str) -> str:
|
|
"""
|
|
Creates a sha512 hash from the username
|
|
and password.
|
|
"""
|
|
|
|
user_hash = hashlib.sha512(bytes(user, 'utf-8')).hexdigest()
|
|
pwd_hash = hashlib.sha512(bytes(pwd, 'utf-8')).hexdigest()
|
|
creds = user_hash+":"+pwd_hash
|
|
|
|
return creds
|
|
|
|
def get_user_hash(user: str) -> str:
|
|
return str(hashlib.sha512(bytes(user, 'utf-8')).hexdigest())
|
|
|
|
def login(user: str, pwd: str) -> bool:
|
|
"""
|
|
Tries to send a request to the SOL
|
|
servers in order to log in.
|
|
"""
|
|
|
|
if is_connected():
|
|
url: str = "https://aplikace.skolaonline.cz/SOL/Prihlaseni.aspx"
|
|
data = {
|
|
"__EVENTTARGET": "dnn$ctr994$SOLLogin$btnODeslat",
|
|
"__EVENTARGUMENT": "",
|
|
"__VIEWSTATE": "",
|
|
"__VIEWSTATEGENERATOR": "",
|
|
"__VIEWSTATEENCRYPTED": "",
|
|
"__PREVIOUSPAGE": "",
|
|
"__EVENTVALIDATION": "",
|
|
"dnn$dnnSearch$txtSearch": "",
|
|
"JmenoUzivatele": user, # username
|
|
"HesloUzivatele": pwd, # password
|
|
"ScrollTop": "",
|
|
"__dnnVariable": "",
|
|
"__RequestVerificationToken": ""
|
|
}
|
|
|
|
post_login = session.post(url, data=data)
|
|
if post_login.status_code != 200:
|
|
return False
|
|
else:
|
|
return True
|
|
else:
|
|
if _eval_creds(_get_creds_hash(user, pwd)):
|
|
return True
|
|
else:
|
|
return False
|
|
|
|
def is_connected() -> bool:
|
|
"""
|
|
Checks whether there is internet
|
|
access present.
|
|
"""
|
|
|
|
import socket
|
|
|
|
try:
|
|
host = socket.gethostbyname("one.one.one.one")
|
|
s = socket.create_connection((host, 80), 2)
|
|
s.close()
|
|
return True
|
|
except:
|
|
pass
|
|
return False
|
|
|
|
def get_html_from(params=None) -> str:
|
|
"""
|
|
Fetches the HTML markup of
|
|
a specified subsite of SOL.
|
|
"""
|
|
|
|
base = "https://aplikace.skolaonline.cz"
|
|
|
|
if params is not None:
|
|
base += params
|
|
|
|
response = session.get(base)
|
|
if response.status_code == 200:
|
|
return response
|
|
else:
|
|
return ""
|