mirror of
https://github.com/HIllya51/LunaTranslator.git
synced 2024-12-28 08:04:13 +08:00
tls
This commit is contained in:
parent
1e3769f2d8
commit
495623c397
@ -57,6 +57,16 @@ class Requester(Requester_common):
|
||||
|
||||
Accept_Encoding = "gzip, deflate, br, zstd"
|
||||
|
||||
def __init__(self) -> None:
|
||||
self._tls = threading.local()
|
||||
self._tls.curl = self.initcurl()
|
||||
|
||||
def initcurl(self):
|
||||
curl = AutoCURLHandle(curl_easy_init())
|
||||
curl_easy_setopt(curl, CURLoption.COOKIEJAR, "")
|
||||
curl_easy_setopt(curl, CURLoption.USERAGENT, self.default_UA.encode("utf8"))
|
||||
return curl
|
||||
|
||||
def raise_for_status(self):
|
||||
if self.last_error:
|
||||
raise CURLException(self.last_error)
|
||||
@ -121,6 +131,25 @@ class Requester(Requester_common):
|
||||
elif isinstance(headerqueue, list):
|
||||
return b"".join(headerqueue).decode("utf8")
|
||||
|
||||
@property
|
||||
def curl(self):
|
||||
if not getattr(self._tls, "curl", None):
|
||||
self._tls.curl = self.initcurl()
|
||||
return self._tls.curl
|
||||
|
||||
def _setheaders(self, curl, headers, cookies):
|
||||
lheaders = Autoslist()
|
||||
for _ in self._parseheader(headers, None):
|
||||
lheaders = curl_slist_append(
|
||||
cast(lheaders, POINTER(curl_slist)), _.encode("utf8")
|
||||
)
|
||||
self.last_error = curl_easy_setopt(curl, CURLoption.HTTPHEADER, lheaders)
|
||||
self.raise_for_status()
|
||||
|
||||
if cookies:
|
||||
cookie = self._parsecookie(cookies)
|
||||
curl_easy_setopt(curl, CURLoption.COOKIE, cookie.encode("utf8"))
|
||||
|
||||
@ExceptionFilter
|
||||
def request(
|
||||
self,
|
||||
@ -140,15 +169,8 @@ class Requester(Requester_common):
|
||||
timeout,
|
||||
allow_redirects,
|
||||
):
|
||||
curl = AutoCURLHandle(curl_easy_init())
|
||||
curl_easy_setopt(curl, CURLoption.COOKIEJAR, "")
|
||||
curl_easy_setopt(
|
||||
curl, CURLoption.USERAGENT, headers["User-Agent"].encode("utf8")
|
||||
)
|
||||
curl = self.curl
|
||||
|
||||
if cookies:
|
||||
cookie = self._parsecookie(cookies)
|
||||
curl_easy_setopt(curl, CURLoption.COOKIE, cookie.encode("utf8"))
|
||||
if timeout:
|
||||
curl_easy_setopt(curl, CURLoption.TIMEOUT_MS, timeout)
|
||||
curl_easy_setopt(curl, CURLoption.CONNECTTIMEOUT_MS, timeout)
|
||||
@ -164,13 +186,7 @@ class Requester(Requester_common):
|
||||
self.raise_for_status()
|
||||
curl_easy_setopt(curl, CURLoption.PORT, port)
|
||||
|
||||
lheaders = Autoslist()
|
||||
for _ in self._parseheader(headers, None):
|
||||
lheaders = curl_slist_append(
|
||||
cast(lheaders, POINTER(curl_slist)), _.encode("utf8")
|
||||
)
|
||||
self.last_error = curl_easy_setopt(curl, CURLoption.HTTPHEADER, lheaders)
|
||||
self.raise_for_status()
|
||||
self._setheaders(curl, headers, cookies)
|
||||
|
||||
self._set_verify(curl, verify)
|
||||
self._set_proxy(curl, proxy)
|
||||
|
@ -8,6 +8,7 @@ try:
|
||||
from .brotli_dec import decompress
|
||||
except:
|
||||
from traceback import print_exc
|
||||
|
||||
print_exc()
|
||||
|
||||
|
||||
@ -126,6 +127,19 @@ class Requester(Requester_common):
|
||||
hRequest, WINHTTP_OPTION_REDIRECT_POLICY, pointer(dwFlags), sizeof(dwFlags)
|
||||
)
|
||||
|
||||
def __init__(self) -> None:
|
||||
self.hSession = AutoWinHttpHandle(
|
||||
WinHttpOpen(
|
||||
self.default_UA,
|
||||
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
||||
WINHTTP_NO_PROXY_NAME,
|
||||
WINHTTP_NO_PROXY_BYPASS,
|
||||
0,
|
||||
)
|
||||
)
|
||||
if self.hSession == 0:
|
||||
raise WinhttpException(GetLastError())
|
||||
|
||||
@ExceptionFilter
|
||||
def request(
|
||||
self,
|
||||
@ -149,18 +163,8 @@ class Requester(Requester_common):
|
||||
flag = WINHTTP_FLAG_SECURE if scheme == "https" else 0
|
||||
# print(server,port,param,dataptr)
|
||||
headers = "\r\n".join(headers)
|
||||
hSession = AutoWinHttpHandle(
|
||||
WinHttpOpen(
|
||||
_headers["User-Agent"],
|
||||
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
||||
WINHTTP_NO_PROXY_NAME,
|
||||
WINHTTP_NO_PROXY_BYPASS,
|
||||
0,
|
||||
)
|
||||
)
|
||||
if hSession == 0:
|
||||
raise WinhttpException(GetLastError())
|
||||
hConnect = AutoWinHttpHandle(WinHttpConnect(hSession, server, port, 0))
|
||||
|
||||
hConnect = AutoWinHttpHandle(WinHttpConnect(self.hSession, server, port, 0))
|
||||
if hConnect == 0:
|
||||
raise WinhttpException(GetLastError())
|
||||
hRequest = AutoWinHttpHandle(
|
||||
@ -195,7 +199,7 @@ class Requester(Requester_common):
|
||||
|
||||
resp.status_code = self._getStatusCode(hRequest)
|
||||
if stream:
|
||||
resp.hSession = hSession
|
||||
resp.hSession = self.hSession
|
||||
resp.hconn = hConnect
|
||||
resp.hreq = hRequest
|
||||
return resp
|
||||
|
@ -5,6 +5,8 @@ from urllib.parse import urlencode, urlsplit
|
||||
from functools import partial
|
||||
from myutils.config import globalconfig
|
||||
|
||||
default_UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"
|
||||
|
||||
|
||||
class RequestException(Exception):
|
||||
pass
|
||||
@ -120,6 +122,7 @@ class ResponseBase:
|
||||
|
||||
class Requester_common:
|
||||
Accept_Encoding = "gzip, deflate, br"
|
||||
default_UA = default_UA
|
||||
|
||||
def request(self, *argc) -> ResponseBase: ...
|
||||
|
||||
@ -160,18 +163,20 @@ class Session:
|
||||
def __init__(self) -> None:
|
||||
self.requester = None
|
||||
self.requester_type = None
|
||||
self.UA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
||||
self.default_UA = default_UA
|
||||
|
||||
self.last_error = 0
|
||||
self.cookies = {}
|
||||
self.headers = CaseInsensitiveDict(
|
||||
{
|
||||
"User-Agent": self.UA,
|
||||
"Accept-Encoding": "gzip, deflate, br",
|
||||
"User-Agent": self.default_UA,
|
||||
# "Accept-Encoding": "gzip, deflate, br",
|
||||
"Accept": "*/*",
|
||||
"Connection": "keep-alive",
|
||||
}
|
||||
)
|
||||
self._requester = None
|
||||
self._libidx = -1
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
@ -253,11 +258,15 @@ class Session:
|
||||
return scheme, server, port, path, url
|
||||
|
||||
def loadrequester(self) -> Requester_common:
|
||||
if self._libidx == globalconfig["network"]:
|
||||
return self._requester
|
||||
if globalconfig["network"] == 1:
|
||||
from network.libcurl.requester import Requester
|
||||
elif globalconfig["network"] == 0:
|
||||
from network.winhttp.requester import Requester
|
||||
return Requester()
|
||||
self._requester = Requester()
|
||||
self._libidx = globalconfig["network"]
|
||||
return self._requester
|
||||
|
||||
def request(
|
||||
self,
|
||||
|
@ -29,7 +29,7 @@ include(generate_product_version)
|
||||
|
||||
set(VERSION_MAJOR 5)
|
||||
set(VERSION_MINOR 9)
|
||||
set(VERSION_PATCH 0)
|
||||
set(VERSION_PATCH 1)
|
||||
|
||||
add_library(pch pch.cpp)
|
||||
target_precompile_headers(pch PUBLIC pch.h)
|
||||
|
Loading…
x
Reference in New Issue
Block a user