diff --git a/src/tests/test_app.py b/src/tests/test_app.py index 9ea3d63..2413169 100644 --- a/src/tests/test_app.py +++ b/src/tests/test_app.py @@ -47,7 +47,7 @@ def test_api(self): self.assertEqual(404, rv.status_code) def test_comment_login_logout(self): - user = "test_" + generate_random() + user = ("test_" + generate_random()).lower() pwd = "pwd123" rv = self.login(user, pwd) self.assertEqual(200, rv.status_code) diff --git a/src/utils/cli.py b/src/utils/cli.py index 7537d13..2d86fc9 100644 --- a/src/utils/cli.py +++ b/src/utils/cli.py @@ -25,6 +25,7 @@ def echo(msg, color=None): def exec_createuser(username, password, **kwargs): """创建账号""" ak = rsp("accounts") + username = username.lower() if check_username(username): if not password or len(password) < 6: echo("密码最少6位", "yellow") diff --git a/src/utils/tool.py b/src/utils/tool.py index ab08b57..b6148d8 100644 --- a/src/utils/tool.py +++ b/src/utils/tool.py @@ -15,7 +15,7 @@ from uuid import uuid4 from time import time from datetime import datetime -from random import randrange, sample +from random import randrange, sample, randint, choice from redis import from_url from .log import Logger from ._compat import string_types, text_type, PY2, urlparse @@ -280,3 +280,29 @@ def parse_data_uri(datauri): is_base64=is_base64, data=data, )) + + +def gen_ua(): + first_num = randint(55, 62) + third_num = randint(0, 3200) + fourth_num = randint(0, 140) + os_type = [ + '(Windows NT 6.1; WOW64)', + '(Windows NT 10.0; WOW64)', + '(X11; Linux x86_64)', + '(Macintosh; Intel Mac OS X 10_12_6)' + ] + chrome_version = 'Chrome/{}.0.{}.{}'.format( + first_num, third_num, fourth_num + ) + ua = ' '.join( + [ + 'Mozilla/5.0', + choice(os_type), + 'AppleWebKit/537.36', + '(KHTML, like Gecko)', + chrome_version, + 'Safari/537.36' + ] + ) + return ua diff --git a/src/utils/web.py b/src/utils/web.py index 258dfa8..0f996e1 100644 --- a/src/utils/web.py +++ b/src/utils/web.py @@ -10,9 +10,7 @@ """ import imghdr -import requests from posixpath import basename, splitext -from random import choice from io import BytesIO from functools import wraps from base64 import urlsafe_b64decode as b64decode, b64decode as pic64decode @@ -23,7 +21,7 @@ from libs.storage import get_storage from .tool import logger, get_current_timestamp, rsp, sha256, username_pat, \ parse_valid_comma, parse_data_uri, format_apires, url_pat, ALLOWED_EXTS, \ - parse_valid_verticaline, parse_valid_colon, is_true + parse_valid_verticaline, parse_valid_colon, is_true, gen_ua from ._compat import PY2, text_type, urlsplit @@ -165,7 +163,7 @@ def dfr(res, default='en-US'): ) if language == "zh-Hans-CN": language = "zh-CN" - except: + except (ValueError, TypeError, KeyError, Exception): language = default # 翻译转换字典库 trans = { @@ -353,34 +351,17 @@ def __init__(self, imgurl, filename=None, allowed_exts=[]): @property def Headers(self): - UserAgents = [ - "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0" - "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 *OPR*/44.0.2510.1218", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 *2345Explorer*/8.4.1.14855", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *JuziBrowser*", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75 Safari/537.36 *LBBROWSER*", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2441.400 *QQBrowser*/9.5.10632.400", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X *MetaSr* 1.0", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) *Maxthon*/5.0.2.2000 Chrome/47.0.2526.73 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 *UBrowser*/6.1.2107.204 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 *BIDUBrowser*/8.4 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36 *TheWorld* 7", - "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 *YaBrowser*/17.3.1.840 *Yowser*/2.5 Safari/537.36", - "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.63 Safari/537.36 *Qiyu*/2.1.0.0", - "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_3 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8J2 Safari/6533.18.5", - ] - return {"User-Agent": choice(UserAgents)} + return {"User-Agent": gen_ua()} def __download(self, imgurl): if imgurl and url_pat.match(imgurl) and \ (splitext(imgurl)[-1] in self._allowed_exts or self._filename): try: + import requests resp = requests.get(imgurl, headers=self.Headers, timeout=5) resp.raise_for_status() + except ImportError: + logger.error("Please install the requests module") except requests.exceptions.RequestException as e: logger.debug(e, exc_info=True) else: diff --git a/src/views/api.py b/src/views/api.py index 8bcaa7a..8da2f68 100644 --- a/src/views/api.py +++ b/src/views/api.py @@ -159,6 +159,8 @@ def register(): res.update( msg="The username is invalid or registration is not allowed" ) + else: + res.update(msg="Parameter error") return res