mirror of
https://github.com/Flowseal/tg-ws-proxy.git
synced 2026-07-23 06:00:12 +03:00
pool fronting
This commit is contained in:
+22
-15
@@ -19,6 +19,7 @@ class _WsPool:
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._idle: Dict[Tuple[int, bool], deque] = {}
|
self._idle: Dict[Tuple[int, bool], deque] = {}
|
||||||
self._refilling: Set[Tuple[int, bool]] = set()
|
self._refilling: Set[Tuple[int, bool]] = set()
|
||||||
|
self.try_fronting_first = False
|
||||||
|
|
||||||
async def get(self, dc: int, is_media: bool,
|
async def get(self, dc: int, is_media: bool,
|
||||||
target_ip: str, domains: List[str]
|
target_ip: str, domains: List[str]
|
||||||
@@ -75,20 +76,17 @@ class _WsPool:
|
|||||||
finally:
|
finally:
|
||||||
self._refilling.discard(key)
|
self._refilling.discard(key)
|
||||||
|
|
||||||
@staticmethod
|
async def _connect_one(self, target_ip, domains) -> Optional[RawWebSocket]:
|
||||||
async def _connect_one(target_ip, domains) -> Optional[RawWebSocket]:
|
|
||||||
for domain in domains:
|
for domain in domains:
|
||||||
|
if self.try_fronting_first:
|
||||||
|
ws = await self._connect_fronted(target_ip, domain)
|
||||||
|
if ws:
|
||||||
|
return ws
|
||||||
try:
|
try:
|
||||||
return await RawWebSocket.connect(
|
return await RawWebSocket.connect(
|
||||||
target_ip, domain, timeout=8)
|
target_ip, domain, timeout=8)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
try:
|
return await self._connect_fronted(target_ip, domain)
|
||||||
ws = await RawWebSocket.connect(
|
|
||||||
target_ip, domain, timeout=7, sni="sprinthost.ru")
|
|
||||||
stats.connections_fronting += 1
|
|
||||||
return ws
|
|
||||||
except Exception:
|
|
||||||
return None
|
|
||||||
except WsHandshakeError as exc:
|
except WsHandshakeError as exc:
|
||||||
if exc.is_redirect:
|
if exc.is_redirect:
|
||||||
continue
|
continue
|
||||||
@@ -97,8 +95,18 @@ class _WsPool:
|
|||||||
return None
|
return None
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
async def _connect_fronted(self, target_ip, domain) -> Optional[RawWebSocket]:
|
||||||
async def _quiet_close(ws):
|
try:
|
||||||
|
ws = await RawWebSocket.connect(
|
||||||
|
target_ip, domain, timeout=7, sni="sprinthost.ru")
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
stats.connections_fronting += 1
|
||||||
|
self.try_fronting_first = True
|
||||||
|
return ws
|
||||||
|
|
||||||
|
async def _quiet_close(self, ws):
|
||||||
try:
|
try:
|
||||||
await ws.close()
|
await ws.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
@@ -116,6 +124,7 @@ class _WsPool:
|
|||||||
def reset(self):
|
def reset(self):
|
||||||
self._idle.clear()
|
self._idle.clear()
|
||||||
self._refilling.clear()
|
self._refilling.clear()
|
||||||
|
self.try_fronting_first = False
|
||||||
|
|
||||||
|
|
||||||
class _CfWorkerPool:
|
class _CfWorkerPool:
|
||||||
@@ -178,8 +187,7 @@ class _CfWorkerPool:
|
|||||||
finally:
|
finally:
|
||||||
self._refilling.discard(key)
|
self._refilling.discard(key)
|
||||||
|
|
||||||
@staticmethod
|
async def _connect_one(self, worker_domain, fallback_dst, dc) -> Optional[RawWebSocket]:
|
||||||
async def _connect_one(worker_domain, fallback_dst, dc) -> Optional[RawWebSocket]:
|
|
||||||
query = urlencode({
|
query = urlencode({
|
||||||
'dst': fallback_dst,
|
'dst': fallback_dst,
|
||||||
'dc': str(dc),
|
'dc': str(dc),
|
||||||
@@ -191,8 +199,7 @@ class _CfWorkerPool:
|
|||||||
except Exception:
|
except Exception:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
async def _quiet_close(self, ws):
|
||||||
async def _quiet_close(ws):
|
|
||||||
try:
|
try:
|
||||||
await ws.close()
|
await ws.close()
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
Reference in New Issue
Block a user