Skip to content

Commit d048626

Browse files
committed
proxy error fix
1 parent d5d39ac commit d048626

File tree

7 files changed

+40
-20
lines changed

7 files changed

+40
-20
lines changed

bot/core/helper.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ def move_session_to_deleted(client: Client):
3131
os.makedirs("sessions/deleted_sessions", exist_ok=True)
3232
shutil.move(session_file, f"sessions/deleted_sessions/{client.name}.session")
3333

34-
def set_proxy_for_tg_client(client: Client, proxy):
35-
proxy = Proxy.from_str(proxy)
34+
def set_proxy_for_tg_client(client: Client, proxy: Proxy):
3635
proxy_dict = dict(
3736
scheme=proxy.protocol,
3837
hostname=proxy.host,

bot/core/tapper.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
from random import randint, uniform
44
from time import time
55

6+
from better_proxy import Proxy
67
from aiocfscrape import CloudflareScraper
7-
from aiohttp_proxy import ProxyConnector
8+
from aiohttp_socks import ProxyConnector
89
from pyrogram import Client
910

1011
from bot.config import settings
@@ -13,7 +14,7 @@
1314
from bot.core.headers import headers as default_headers
1415
from bot.core.helper import get_blum_database, set_proxy_for_tg_client, format_duration, move_session_to_deleted
1516
from bot.core.tg_auth import get_tg_web_data
16-
from bot.exceptions import NeedReLoginError, TelegramInvalidSessionException
17+
from bot.exceptions import NeedReLoginError, TelegramInvalidSessionException, TelegramProxyError
1718
from bot.utils.payload import check_payload_server, get_payload
1819
from bot.utils.logger import SessionLogger
1920
from bot.utils.checkers import check_proxy, wait_proxy
@@ -135,7 +136,7 @@ async def check_tasks(self):
135136
await asyncio.sleep(uniform(0.5, 1))
136137
await self.update_balance()
137138

138-
async def play_drop_game(self, proxy):
139+
async def play_drop_game(self):
139140
if settings.PLAY_GAMES is not True or not self.play_passes:
140141
return
141142

@@ -238,10 +239,11 @@ async def check_farming(self):
238239
await asyncio.sleep(uniform(0.1, 0.5))
239240
await self.update_balance()
240241

241-
async def run(self, proxy: str | None) -> None:
242-
await self.random_delay()
242+
async def run(self, proxy: Proxy | None) -> None:
243+
if not settings.DEBUG:
244+
await self.random_delay()
243245

244-
proxy_conn = ProxyConnector().from_url(proxy) if proxy else None
246+
proxy_conn = ProxyConnector().from_url(proxy.as_url) if proxy else None
245247
headers = default_headers.copy()
246248
headers.update({'User-Agent': check_user_agent(self.tg_client.name)})
247249
async with CloudflareScraper(headers=headers, connector=proxy_conn) as session:
@@ -250,11 +252,14 @@ async def run(self, proxy: str | None) -> None:
250252
if not ip:
251253
self._log.warning(f"Proxy {proxy} not available. Waiting for the moment when it will work...")
252254
ip = await wait_proxy(session)
253-
self._log.info(f"Used proxy {proxy}. Real ip: {ip}")
255+
self._log.info(f"Used proxy <y>{proxy}</y>. Real ip: <g>{ip}</g>")
254256
set_proxy_for_tg_client(self.tg_client, proxy)
255257
else:
256258
self._log.warning("Proxy not installed! This may lead to account ban! Be careful.")
257-
await self.auth(session)
259+
try:
260+
await self.auth(session)
261+
except TelegramProxyError:
262+
return self._log.error(f"<r>The selected proxy cannot be applied to the Telegram client.</r>")
258263

259264
timer = 0
260265
while True:
@@ -272,7 +277,7 @@ async def run(self, proxy: str | None) -> None:
272277
# todo: add "api/v1/wallet/my/balance?fiat=usd", "api/v1/tribe/leaderboard" and another human behavior
273278
await self.check_tribe()
274279
await self.check_tasks()
275-
await self.play_drop_game(proxy)
280+
await self.play_drop_game()
276281
except NeedReLoginError:
277282
await self.auth(session)
278283
except Exception:
@@ -282,7 +287,7 @@ async def run(self, proxy: str | None) -> None:
282287
timer = time()
283288

284289

285-
async def run_tapper(tg_client: Client, proxy: str | None):
290+
async def run_tapper(tg_client: Client, proxy: Proxy | None):
286291
session_logger = SessionLogger(tg_client.name)
287292
try:
288293
await Tapper(tg_client, session_logger).run(proxy=proxy)

bot/core/tg_auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from pyrogram.raw.types import InputBotAppShortName
1010

1111
from bot.core.helper import get_referral_token
12-
from bot.exceptions import TelegramInvalidSessionException
12+
from bot.exceptions import TelegramInvalidSessionException, TelegramProxyError
1313
from bot.utils.logger import SessionLogger
1414

1515

@@ -34,6 +34,8 @@ async def get_tg_web_data(client: Client, log: SessionLogger):
3434
except (Unauthorized, UserDeactivated, AuthKeyUnregistered, UserDeactivatedBan, AuthKeyDuplicated,
3535
SessionExpired, SessionRevoked):
3636
raise TelegramInvalidSessionException(f"Telegram session is invalid. Client: {client.name}")
37+
except AttributeError as e:
38+
raise TelegramProxyError(e)
3739
finally:
3840
if client.is_connected:
3941
await client.disconnect()

bot/exceptions/telegram.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11

22
class TelegramInvalidSessionException(Exception):
3+
pass
4+
5+
class TelegramProxyError(Exception):
36
pass

bot/utils/checkers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
from asyncio import sleep
1+
import traceback
2+
from asyncio import sleep, CancelledError
23
from aiohttp import ClientSession, ClientTimeout
34
from aiohttp.client_exceptions import ClientProxyConnectionError
5+
from python_socks import ProxyError
6+
47
from bot.utils.logger import logger
58

69
async def check_proxy(http_client: ClientSession) -> str | None:
@@ -9,10 +12,12 @@ async def check_proxy(http_client: ClientSession) -> str | None:
912
data = await response.json()
1013
if data and data.get('ip'):
1114
return data.get('ip')
12-
except (ConnectionRefusedError, ClientProxyConnectionError):
15+
except (ConnectionRefusedError, ClientProxyConnectionError, CancelledError):
1316
logger.trace(f"Proxy not available")
17+
except ProxyError as e:
18+
logger.error(f"The proxy type may be incorrect! Error: {type(e).__name__}: {e}")
1419
except Exception as e:
15-
logger.error(f"Proxy check error {e.__name__}: {e}")
20+
logger.error(f"{traceback.format_exc()}")
1621

1722

1823
async def wait_proxy(http_client: ClientSession, time_between_checks_sec: int = 5) -> str | None:

bot/utils/launcher.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,16 @@ def get_session_names() -> list[str]:
3030

3131

3232
def get_proxies() -> list[Proxy]:
33+
proxies = []
3334
if settings.USE_PROXY_FROM_FILE:
3435
with open(file="bot/config/proxies.txt", encoding="utf-8-sig") as file:
35-
proxies = [Proxy.from_str(proxy=row.strip()).as_url for row in file]
36-
else:
37-
proxies = []
38-
36+
for line in file:
37+
if "type://" in line:
38+
continue
39+
try:
40+
proxies.append(Proxy.from_str(proxy=line.strip()))
41+
except ValueError as e:
42+
print(f"{e} - {line}")
3943
return proxies
4044

4145

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ aiocfscrape==1.0.0
22
aiohappyeyeballs==2.4.3
33
aiohttp==3.10.10
44
aiohttp-proxy==0.1.2
5+
aiohttp_socks==0.9.0
56
aiosignal==1.3.1
67
annotated-types==0.7.0
78
async-timeout==4.0.3
@@ -22,6 +23,7 @@ pyjsparser==2.7.1
2223
Pyrogram==2.0.106
2324
PySocks==1.7.1
2425
python-dotenv==1.0.1
26+
python-socks==2.5.3
2527
six==1.16.0
2628
TgCrypto==1.2.5
2729
typing_extensions==4.12.2

0 commit comments

Comments
 (0)