test socks with tcp only

This commit is contained in:
Roy Olav Purser 2021-06-04 10:37:04 +02:00
parent 1176c35981
commit aa21855d81
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -8,8 +8,6 @@ import logging
import asyncio import asyncio
import tornado.web import tornado.web
import tornado.routing import tornado.routing
import aiohttp
import aiohttp_socks
import stream_providers import stream_providers
logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', stream=sys.stdout, level=logging.INFO) logging.basicConfig(format='[%(filename)s:%(lineno)d] %(message)s', stream=sys.stdout, level=logging.INFO)
@ -73,17 +71,6 @@ class ProxyElem():
else: else:
return urls return urls
class AsyncSessionData():
def __init__(self, resp, current):
self.resp = resp
self.current = current
class AsyncSession():
def __init__(self, resp, current):
self.sdata = AsyncSessionData(resp, current)
async def task(self):
resp = await self.sdata.resp
return AsyncSessionData(resp, self.sdata.current)
proxies = {} proxies = {}
for key in providers: for key in providers:
proxies[key] = [] proxies[key] = []
@ -111,6 +98,20 @@ class UpstreamHandler():
self.valid = False self.valid = False
self.proxy = None self.proxy = None
self.upstream = None self.upstream = None
async def test_socks(self, proxy):
try:
splitted = proxy.rsplit(":", 1)
host = proxy
port = 1080
if len(splitted) == 2:
host = splitted[0]
port = splitted[1]
future = asyncio.open_connection(host=host, port=port)
await asyncio.wait_for(future, timeout=1)
except Exception as e:
return (False, proxy)
else:
return (True, proxy)
async def setup(self, handler): async def setup(self, handler):
self.provider = handler.get_query_argument("provider", None) self.provider = handler.get_query_argument("provider", None)
if self.provider in providers.keys(): if self.provider in providers.keys():
@ -120,38 +121,21 @@ class UpstreamHandler():
path = path.removesuffix("/").removesuffix("download").removesuffix("/") path = path.removesuffix("/").removesuffix("download").removesuffix("/")
elif self.provider == "youtube": elif self.provider == "youtube":
path = path.removeprefix("/") path = path.removeprefix("/")
src = providers[self.provider] + path self.upstream = providers[self.provider] + path
proxy_list = proxies.get(self.provider) proxy_list = proxies.get(self.provider)
if isinstance(proxy_list, list): if isinstance(proxy_list, list):
futures = [] futures = []
sessions = []
for current in proxy_list: for current in proxy_list:
session = current.session() future = asyncio.create_task(self.test_socks(current))
sessions.append(session) futures.append(future)
future = AsyncSession(session.head(src), current)
task = asyncio.create_task(future.task())
futures.append(task)
done = False
for future in asyncio.as_completed(futures): for future in asyncio.as_completed(futures):
try: success, current = await future
result = await future if success:
resp = result.resp self.proxy = current
except Exception as e:
logger.info(e)
else:
done = True
new_url = str(resp.url)
if new_url.lower().startswith("https://consent.youtube.com"):
self.upstream = src
else:
self.upstream = new_url
self.proxy = result.current
break break
for future in futures: for future in futures:
if not future.done(): if not future.done():
future.cancel() future.cancel()
for session in sessions:
await session.close()
if icecast_server is not None and stream_server is not None: if icecast_server is not None and stream_server is not None:
try: try: