From 0a698220c59bc3ef7f62f898d6e4c1e7ac0368a9 Mon Sep 17 00:00:00 2001 From: "bt.cn" <287962566@qq.com> Date: Wed, 19 Jun 2019 16:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=AE=98=E7=BD=91=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BTPanel.pyproj | 1 + BTPanel/__init__.py | 6 ++++ class/public.py | 83 ++++++++++++++++++++++++++++++++++++++------- config/hosts.json | 1 + 4 files changed, 78 insertions(+), 13 deletions(-) create mode 100644 config/hosts.json diff --git a/BTPanel.pyproj b/BTPanel.pyproj index 8b8f5fd9..17519136 100644 --- a/BTPanel.pyproj +++ b/BTPanel.pyproj @@ -838,6 +838,7 @@ + diff --git a/BTPanel/__init__.py b/BTPanel/__init__.py index 56af7296..abb2aad7 100644 --- a/BTPanel/__init__.py +++ b/BTPanel/__init__.py @@ -20,6 +20,11 @@ from werkzeug.wrappers import Response from flask_socketio import SocketIO,emit,send +from flask_basicauth import BasicAuth +app.config['BASIC_AUTH_USERNAME'] = 'admin' +app.config['BASIC_AUTH_PASSWORD'] = 'amwyygyyv' +app.config['BASIC_AUTH_FORCE'] = True +basic_auth = BasicAuth(app) cache = SimpleCache() @@ -30,6 +35,7 @@ jobs.control_init() app.secret_key = uuid.UUID(int=uuid.getnode()).hex[-12:] + try: from flask_sqlalchemy import SQLAlchemy app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////dev/shm/session.db' diff --git a/class/public.py b/class/public.py index 28952c50..281f440a 100644 --- a/class/public.py +++ b/class/public.py @@ -22,13 +22,20 @@ def M(table): sql = db.Sql() return sql.table(table); -def HttpGet(url,timeout = 10): +def HttpGet(url,timeout = 3,headers = {}): """ 发送GET请求 @url 被请求的URL地址(必需) @timeout 超时时间默认60秒 return string """ + home = 'www.bt.cn' + host_home = 'data/home_host.pl' + old_url = url + if url.find(home) != -1: + if os.path.exists(host_home): + headers['host'] = home + url = url.replace(home,readFile(host_home)) if sys.version_info[0] == 2: try: import urllib2,ssl @@ -38,9 +45,12 @@ def HttpGet(url,timeout = 10): try: ssl._create_default_https_context = ssl._create_unverified_context except:pass; - response = urllib2.urlopen(url,timeout=timeout) + req = urllib2.Request(url, headers = headers) + response = urllib2.urlopen(req,timeout = timeout,) return response.read() except Exception as ex: + if old_url.find(home) != -1: return http_get_home(old_url,timeout,str(ex)) + if headers: return False return str(ex); else: try: @@ -48,19 +58,37 @@ def HttpGet(url,timeout = 10): try: ssl._create_default_https_context = ssl._create_unverified_context except:pass; - response = urllib.request.urlopen(url,timeout=timeout) + req = urllib.request.Request(url,headers = headers) + response = urllib.request.urlopen(req,timeout = timeout) result = response.read() if type(result) == bytes: result = result.decode('utf-8') return result except Exception as ex: + if old_url.find(home) != -1: return http_get_home(old_url,timeout,str(ex)) + if headers: return False return str(ex) - -def httpGet(url,timeout=10): +def http_get_home(url,timeout,ex): + try: + home = 'www.bt.cn' + if url.find(home) == -1: return ex + hosts_file = "config/hosts.json" + if not os.path.exists(hosts_file): return ex + hosts = json.loads(readFile(hosts_file)) + headers = {"host":home} + for host in hosts: + new_url = url.replace(home,host) + res = HttpGet(new_url,timeout,headers) + if res: + writeFile("data/home_host.pl",host) + return res + return ex + except: return ex + +def httpGet(url,timeout=3): return HttpGet(url,timeout) - -def HttpPost(url,data,timeout = 10): +def HttpPost(url,data,timeout = 3,headers = {}): """ 发送POST请求 @url 被请求的URL地址(必需) @@ -68,17 +96,27 @@ def HttpPost(url,data,timeout = 10): @timeout 超时时间默认60秒 return string """ + home = 'www.bt.cn' + host_home = 'data/home_host.pl' + old_url = url + if url.find(home) != -1: + if os.path.exists(host_home): + headers['host'] = home + url = url.replace(home,readFile(host_home)) + if sys.version_info[0] == 2: try: import urllib,urllib2,ssl try: ssl._create_default_https_context = ssl._create_unverified_context except:pass - data = urllib.urlencode(data) - req = urllib2.Request(url, data) - response = urllib2.urlopen(req,timeout = timeout) + data2 = urllib.urlencode(data) + req = urllib2.Request(url, data2,headers = headers) + response = urllib2.urlopen(req,timeout=timeout) return response.read() except Exception as ex: + if old_url.find(home) != -1: return http_post_home(old_url,data,timeout,str(ex)) + if headers: return False return str(ex); else: try: @@ -86,16 +124,35 @@ def HttpPost(url,data,timeout = 10): try: ssl._create_default_https_context = ssl._create_unverified_context except:pass; - data = urllib.parse.urlencode(data).encode('utf-8') - req = urllib.request.Request(url, data) + data2 = urllib.parse.urlencode(data).encode('utf-8') + req = urllib.request.Request(url, data2,headers = headers) response = urllib.request.urlopen(req,timeout = timeout) result = response.read() if type(result) == bytes: result = result.decode('utf-8') return result except Exception as ex: + if old_url.find(home) != -1: return http_post_home(old_url,data,timeout,str(ex)) + if headers: return False return str(ex); -def httpPost(url,data,timeout=10): +def http_post_home(url,data,timeout,ex): + try: + home = 'www.bt.cn' + if url.find(home) == -1: return ex + hosts_file = "config/hosts.json" + if not os.path.exists(hosts_file): return ex + hosts = json.loads(readFile(hosts_file)) + headers = {"host":home} + for host in hosts: + new_url = url.replace(home,host) + res = HttpPost(new_url,data,timeout,headers) + if res: + writeFile("data/home_host.pl",host) + return res + return ex + except: return ex + +def httpPost(url,data,timeout=3): return HttpPost(url,data,timeout) def check_home(): diff --git a/config/hosts.json b/config/hosts.json new file mode 100644 index 00000000..8c3f40f2 --- /dev/null +++ b/config/hosts.json @@ -0,0 +1 @@ +[ "103.224.251.67", "119.188.210.21", "45.32.116.160", "125.88.182.172"] \ No newline at end of file