Commit 06113ad8 authored by Zheng Yile's avatar Zheng Yile
Browse files

Update cache method

parent 4602c6fc
Pipeline #660 canceled with stages
......@@ -17,6 +17,7 @@
|`Fatal: Login failed.`|登陆失败|检查你的登录凭证|
|`Fatal: No username or cookie specified.`|用户名未输入|按照正确格式输入用户名|
|`Fatal: Unknown error.`|未知错误|请联系开发人员|
|`Warning: Cache loading failed.`|缓存失败|删除缓存文件|
|`Warning: Connect timeout.`|网络连接超时|检查你的计算机网络连接,或稍后重试|
|`Warning: Connection error.`|网络连接错误|检查你的计算机网络连接|
|`Warning: Invalid request.`|非法请求|检查你的请求格式,可以在程序中输入`help`以获得帮助|
......@@ -111,11 +112,12 @@ pid, eid 可以通过客户端查询, 也可以在网页端访问的链接中获
若非直接命令行运行, 建议在启动时加上`--origin`选项以获取**未解码**的数据, 数据以 ***Unicode*** 编码, *python* 可以直接以`str.encode('utf-8').decode('unicode-escape')`的形式解码, 可以参考`./run.py`.
## 配置相关
每次成功登录后默认会往配置文件 `./cache.passwd.conf` 中写入登录信息,包括:
每次成功登录后默认会往配置文件 `./codiaclient.cache` 中写入登录信息,包括:
- 用户名 *(明文 )*
- 用户邮箱 *(明文 )*
- 密码的 *sha256* 哈希值
- cookie用 *AES* 加密的值 *(密钥为密码明文 )*
这种记录方法在绝大多数情况下保证无法从配置文件中获取密码的值,并且在密码不正确时不能获取对应的cookie.
在登录时会首先尝试匹配配置中的信息。成功匹配则直接使用配置中的cookie尝试登录,失败则会向服务器提交数据以获得新的cookie.
若在启动程序时加上`--no-cache`的选项,则会禁用配置,即不会往配置文件中写入信息,也不会从配置文件中读取信息。**但已经写入的数据不会删除。**
\ No newline at end of file
若在启动程序时加上`--no-cache`的选项,则会禁用配置,即不会往配置文件中写入信息,也不会从配置文件中读取信息。**但已经写入的数据不会删除。**
......@@ -10,15 +10,19 @@ variables = {
'logindic': {}
}
def cache_username_passwd_cookie(userdic, passwd, cookie, file = './codiaclient.cache'):
def cache_for_login(userdic, passwd, cookie, file = './codiaclient.cache'):
if not variables['cacheOn']:
report("Invalid reference of function 'cache_username_passwd_cookie'.", 1)
return False
report("Caching cookie.")
username = userdic['login']
useremail = userdic['defaultEmail']
variables['logindic'][username] = {'username': username, 'email': useremail, 'passwd': passwd_hash(passwd), 'cookie': cookie_encrypt(cookie, passwd)}
variables['logindic'][useremail] = {'username': username, 'email': useremail, 'passwd': passwd_hash(passwd), 'cookie': cookie_encrypt(cookie, passwd)}
variables['logindic'][username] = variables['logindic'][useremail] = {
'username': username,
'email': useremail,
'passwd': passwd_hash(passwd),
'cookie': cookie_encrypt(cookie, passwd)
}
try:
dic_str = json.dumps({'logindic': variables['logindic']})
dic_b64 = b64encode(dic_str.encode('utf-8'))
......@@ -40,6 +44,6 @@ def cache_load(file = './codiaclient.cache'):
config = json.loads(dic_str)
if 'logindic' in config: variables['logindic'] = config['logindic']
except (zlib.error, UnicodeDecodeError, json.decoder.JSONDecodeError):
report('Cache loading failed.', 1)
report('Cache load failed.', 1)
except FileNotFoundError:
pass
from .report import report
from .utils import passwd_hash, cookie_encrypt, cookie_decrypt
from .cachectrl import variables as cache_var, cache_username_passwd_cookie as cache
from .cachectrl import variables as cache_var, cache_for_login as cache
import json
variables = {
'register': False
......
import codiaclient as cc
variables = {
"origin": False
}
if __name__ == "__main__":
args = cc.ArgParser().parse_args()
if args.no_cache: cc.cache_var['cacheOn'] = False
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment