diff --git a/pixivpy_async/aapi.py b/pixivpy_async/aapi.py index 4811fac..6a858c9 100644 --- a/pixivpy_async/aapi.py +++ b/pixivpy_async/aapi.py @@ -6,11 +6,11 @@ class AppPixivAPI(BasePixivAPI): - def __init__(self, bypass=False, **requests_kwargs): + def __init__(self, **requests_kwargs): """ initialize requests kwargs if need be """ - super(AppPixivAPI, self).__init__(bypass=bypass, **requests_kwargs) + super(AppPixivAPI, self).__init__(**requests_kwargs) # 用户详情 async def user_detail( diff --git a/pixivpy_async/bapi.py b/pixivpy_async/bapi.py index 886ba68..6145203 100644 --- a/pixivpy_async/bapi.py +++ b/pixivpy_async/bapi.py @@ -16,7 +16,7 @@ class BasePixivAPI(Net, Utils): - def __init__(self, bypass, **requests_kwargs): + def __init__(self, **requests_kwargs): self.additional_headers = {} self.client_id = 'MOBrBDS8blbauoSck0ZfDbtuzpyT' self.client_secret = 'lsACyCD94FhDUtGTXi3QzcFE2uU1hqtDaKeqrdwj' @@ -32,7 +32,7 @@ def __init__(self, bypass, **requests_kwargs): 'image/png': '.png', 'image/gif': '.gif', } - super().__init__(bypass=bypass, **requests_kwargs) + super().__init__(**requests_kwargs) async def requests_( self, diff --git a/pixivpy_async/bypass_sni.py b/pixivpy_async/bypass_sni.py index 52b1988..8af0302 100644 --- a/pixivpy_async/bypass_sni.py +++ b/pixivpy_async/bypass_sni.py @@ -68,6 +68,8 @@ async def require_appapi_hosts(self, hostname, timeout=3) -> List[str]: "cd": "false", } + # print("resolve: %s" % hostname) + async with aiohttp.ClientSession() as session: results = await asyncio.gather( *(asyncio.create_task(self.fetch(session, url, params, ClientTimeout(total=timeout))) for url in URLS), @@ -76,37 +78,4 @@ async def require_appapi_hosts(self, hostname, timeout=3) -> List[str]: for r in results: if not isinstance(r, Exception): return r - - -RESOLVER = ByPassResolver() - - -def get_bypass_client() -> aiohttp.ClientSession: - ssl_ctx = ssl.SSLContext() - ssl_ctx.check_hostname = False - ssl_ctx.verify_mode = ssl.CERT_NONE - connector = aiohttp.TCPConnector(ssl=ssl_ctx, resolver=RESOLVER) - client = aiohttp.ClientSession(connector=connector) - return client - - -class BypassClient: - def __init__(self): - self.client = get_bypass_client() - - async def __aenter__(self) -> aiohttp.ClientSession: - return self.client - - async def __aexit__(self, exc_type, exc_val, exc_tb): - await self.client.close() - - -async def test(): - async with BypassClient() as client: - async with client.get( - "https://www.pixiv.net/ajax/search/artworks/%E7%99%BE%E5%90%88?word=%E7%99%BE%E5%90%88&order=date_d&mode=all&p=99999990&s_mode=s_tag&type=all&lang=zh") as rsp: - print(await rsp.json()) - - -if __name__ == '__main__': - asyncio.run(test()) + \ No newline at end of file diff --git a/pixivpy_async/client.py b/pixivpy_async/client.py index d3770fe..eb6d3fb 100644 --- a/pixivpy_async/client.py +++ b/pixivpy_async/client.py @@ -1,8 +1,6 @@ import asyncio import aiohttp -from .bypass_sni import get_bypass_client - class PixivClient: def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, bypass=False): """ @@ -19,30 +17,40 @@ def __init__(self, limit=30, timeout=10, env=False, internal=False, proxy=None, """ + kwargs = {'limit_per_host': limit} + + if bypass: + import ssl + from .bypass_sni import ByPassResolver + + ssl_ctx = ssl.SSLContext() + ssl_ctx.check_hostname = False + ssl_ctx.verify_mode = ssl.CERT_NONE + + kwargs.update({'ssl': ssl_ctx, 'resolver': ByPassResolver()}) + if proxy: try: from aiohttp_socks import ProxyConnector - self.conn = ProxyConnector.from_url(proxy, limit_per_host=limit) + self.conn = ProxyConnector.from_url(proxy, **kwargs) _flag = False except ModuleNotFoundError as e: if proxy.startswith('socks'): raise e else: - self.conn = aiohttp.TCPConnector(limit_per_host=limit) + self.conn = aiohttp.TCPConnector(**kwargs) _flag = True else: - self.conn = aiohttp.TCPConnector(limit_per_host=limit) + self.conn = aiohttp.TCPConnector(**kwargs) self.internal = internal - if bypass: - self.client = get_bypass_client() - else: - self.client = aiohttp.ClientSession( + self.client = aiohttp.ClientSession( connector=self.conn, timeout=aiohttp.ClientTimeout(total=timeout), trust_env=env, ) + if proxy and _flag: from functools import partial diff --git a/pixivpy_async/net.py b/pixivpy_async/net.py index b62b84b..ce06660 100644 --- a/pixivpy_async/net.py +++ b/pixivpy_async/net.py @@ -9,9 +9,9 @@ class ClientManager: - def __init__(self, s, env, proxy, bypass): + def __init__(self, s, **opt): if s is None: - self.session = PixivClient(internal=True, env=env, proxy=proxy, bypass=bypass) + self.session = PixivClient(internal=True, **opt) else: self.session = s @@ -30,14 +30,12 @@ async def __aexit__(self, exception_type, exception_value, traceback): class Net(object): def __init__(self, client=None, env=False, proxy=None, bypass=False, **requests_kwargs): self.session = client - self.env = env - self.proxy = proxy - self.bypass = bypass + self.conn_opt = {'env': env, 'proxy': proxy, 'bypass': bypass} self.requests_kwargs = requests_kwargs @retry(*retryable_error, retries=10, cooldown=random.randint(1, 3)) async def down(self, _url, _referer): - async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session: + async with ClientManager(self.session, **self.conn_opt) as session: async with session.get(_url, headers={'Referer': _referer}, **self.requests_kwargs) as res: c = await res.read() t = res.content_type @@ -47,7 +45,7 @@ async def down(self, _url, _referer): @retry(*retryable_error, retries=10, cooldown=random.randint(1, 3)) async def auth(self, _url, _headers, _data): - async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session: + async with ClientManager(self.session, **self.conn_opt) as session: async with session.post(_url, headers=_headers, data=_data, **self.requests_kwargs) as res: r, b, q = await res.json(), res.status in [200, 301, 302], res.status @@ -56,7 +54,7 @@ async def auth(self, _url, _headers, _data): @retry(*retryable_error, retries=10, cooldown=random.randint(1, 3)) async def fetch(self, _url, _headers, _params): - async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session: + async with ClientManager(self.session, **self.conn_opt) as session: async with session.get(_url, headers=_headers, params=_params, **self.requests_kwargs) as res: q = await res.json() @@ -65,7 +63,7 @@ async def fetch(self, _url, _headers, _params): @retry(*retryable_error, retries=10, cooldown=random.randint(1, 3)) async def post(self, _url, _data, _headers, _params): - async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session: + async with ClientManager(self.session, **self.conn_opt) as session: async with session.post(_url, data=_data, headers=_headers, params=_params, **self.requests_kwargs) as res: r = await res.json() @@ -74,7 +72,7 @@ async def post(self, _url, _data, _headers, _params): @retry(*retryable_error, retries=10, cooldown=random.randint(1, 3)) async def delete(self, _url, _headers, _params): - async with ClientManager(self.session, self.env, self.proxy, self.bypass) as session: + async with ClientManager(self.session, **self.conn_opt) as session: async with session.delete(_url, headers=_headers, params=_params, **self.requests_kwargs) as res: q = await res.json() diff --git a/pixivpy_async/papi.py b/pixivpy_async/papi.py index 78a3c91..5f11b91 100644 --- a/pixivpy_async/papi.py +++ b/pixivpy_async/papi.py @@ -6,11 +6,11 @@ class PixivAPI(BasePixivAPI): - def __init__(self, bypass=False,**requests_kwargs): + def __init__(self, **requests_kwargs): """ initialize requests kwargs if need be """ - super().__init__(bypass,**requests_kwargs) + super().__init__(**requests_kwargs) async def works( self, diff --git a/socks5_server.py b/socks5_server.py index af7802d..846d238 100644 --- a/socks5_server.py +++ b/socks5_server.py @@ -52,7 +52,6 @@ def handle(self): remote = socket.socket(socket.AF_INET, socket.SOCK_STREAM) remote.connect((address, port)) bind_address = remote.getsockname() - # logging.info('Connected to %s %s' % (address, port)) else: self.server.close_request(self.request) diff --git a/test.py b/test.py index f3b2339..b19e7ab 100644 --- a/test.py +++ b/test.py @@ -34,6 +34,30 @@ def test_login(self): self.assertIsNotNone(credential.refresh_token) # self.assertIsNotNone(newpapi.login(_USERNAME, _PASSWORD)) + def test_bypass(self): + newaapi = AppPixivAPI(bypass=True) + credential = newaapi.login(refresh_token=_TOKEN) + self.assertIsNotNone(credential) + self.assertIsNotNone(credential.access_token) + self.assertIsNotNone(credential.refresh_token) + + detail = newaapi.user_detail(275527) + self.assertIsNotNone(detail) + self.assertIsNotNone(detail.user) + self.assertEqual(detail.user.id, 275527) + + def test_bypass_2(self): + newaapi = AppPixivAPI(bypass=True, proxy="socks5://127.0.0.1:9011") + credential = newaapi.login(refresh_token=_TOKEN) + self.assertIsNotNone(credential) + self.assertIsNotNone(credential.access_token) + self.assertIsNotNone(credential.refresh_token) + + detail = newaapi.user_detail(275527) + self.assertIsNotNone(detail) + self.assertIsNotNone(detail.user) + self.assertEqual(detail.user.id, 275527) + def test_socks_1(self): newaapi = AppPixivAPI(proxy="socks5://127.0.0.1:9011") credential = newaapi.login(refresh_token=_TOKEN)