Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (249 loc) · 10.7 KB
/
Copy pathmain.py
File metadata and controls
277 lines (249 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Copyright (C) 2025 FakerPK
# Licensed under the AGPL-3.0: https://www.gnu.org/licenses/agpl-3.0.html
# This software is provided "as-is" without any warranties.
import asyncio
import json
import random
import ssl
import time
import uuid
import aiohttp
import aiofiles
from colorama import Fore, Style, init
from loguru import logger
from websockets_proxy import Proxy, proxy_connect
init(autoreset=True)
CONFIG_FILE = "config.json"
PING_INTERVAL = 30
DIRECTOR_SERVER = "https://director.getgrass.io"
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36",
}
ERROR_PATTERNS = [
"Host unreachable",
"[SSL: WRONG_VERSION_NUMBER]",
"invalid length of packed IP address string",
"Empty connect reply",
"Device creation limit exceeded",
"sent 1011 (internal error) keepalive ping timeout"
]
async def get_ws_endpoints(user_id: str):
url = f"{DIRECTOR_SERVER}/checkin"
headers = {"Content-Type": "application/json"}
data = {
"browserId": user_id,
"userId": user_id,
"version": "4.29.0",
"extensionId": "lkbnfiajjmbhnfledhphioinpickokdi",
"userAgent": HEADERS["User-Agent"],
"deviceType": "extension"
}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=data, headers=headers) as response:
if response.status == 201:
try:
result = await response.json(content_type=None)
except Exception as e:
logger.error(f"Error decoding JSON: {e}")
text = await response.text()
result = json.loads(text)
destinations = result.get("destinations", [])
token = result.get("token", "")
destinations = [f"wss://{dest}" for dest in destinations]
return destinations, token
else:
logger.error(f"Failed to check in: Status {response.status}")
return [], ""
class WebSocketClient:
def __init__(self, socks5_proxy: str, user_id: str):
self.proxy = socks5_proxy
self.user_id = user_id
self.device_id = str(uuid.uuid3(uuid.NAMESPACE_DNS, socks5_proxy))
self.uri = None
async def connect(self) -> bool:
logger.info(f"🖥️ Device ID: {self.device_id}")
while True:
try:
endpoints, token = await get_ws_endpoints(self.user_id)
if not endpoints or not token:
logger.error("No valid WebSocket endpoints or token received")
return False
self.uri = f"{endpoints[0]}?token={token}"
logger.info(f"Connecting to WebSocket URI: {self.uri}")
await asyncio.sleep(0.1)
ssl_context = ssl.create_default_context()
ssl_context.check_hostname = False
ssl_context.verify_mode = ssl.CERT_NONE
async with proxy_connect(
self.uri,
proxy=Proxy.from_url(self.proxy),
ssl=ssl_context,
extra_headers=HEADERS
) as websocket:
ping_task = asyncio.create_task(self._send_ping(websocket))
try:
await self._handle_messages(websocket)
finally:
ping_task.cancel()
try:
await ping_task
except asyncio.CancelledError:
pass
except Exception as e:
logger.error(f"🚫 Error with proxy {self.proxy}: {str(e)}")
if any(pattern in str(e) for pattern in ERROR_PATTERNS):
logger.info(f"❌ Removing error proxy from list: {self.proxy}")
await self._remove_proxy_from_list()
return False
await asyncio.sleep(5)
async def _send_ping(self, websocket) -> None:
while True:
try:
message = {
"id": str(uuid.uuid4()),
"version": "1.0.0",
"action": "PING",
"data": {}
}
await websocket.send(json.dumps(message))
await asyncio.sleep(PING_INTERVAL)
except Exception as e:
logger.error(f"🚫 Error sending ping: {str(e)}")
break
async def _handle_messages(self, websocket) -> None:
handlers = {
"AUTH": self._handle_auth,
"PONG": self._handle_pong
}
while True:
response = await websocket.recv()
message = json.loads(response)
logger.info(f"📥 Received message: {message}")
handler = handlers.get(message.get("action"))
if handler:
await handler(websocket, message)
async def _handle_auth(self, websocket, message) -> None:
auth_response = {
"id": message["id"],
"origin_action": "AUTH",
"result": {
"browser_id": self.device_id,
"user_id": self.user_id,
"user_agent": HEADERS["User-Agent"],
"timestamp": int(time.time()),
"device_type": "extension",
"version": "4.29.0",
}
}
await websocket.send(json.dumps(auth_response))
async def _handle_pong(self, websocket, message) -> None:
pong_response = {
"id": message["id"],
"origin_action": "PONG"
}
await websocket.send(json.dumps(pong_response))
async def _remove_proxy_from_list(self) -> None:
try:
async with aiofiles.open("proxy.txt", "r") as file:
lines = await file.readlines()
async with aiofiles.open("proxy.txt", "w") as file:
await file.writelines(line for line in lines if line.strip() != self.proxy)
except Exception as e:
logger.error(f"🚫 Error removing proxy from file: {str(e)}")
class ProxyManager:
def __init__(self, user_id: str):
self.user_id = user_id
self.active_proxies = set()
self.all_proxies = set()
async def load_proxies(self) -> None:
try:
async with aiofiles.open("proxy.txt", "r") as file:
content = await file.read()
self.all_proxies = set(line.strip() for line in content.splitlines() if line.strip())
except Exception as e:
logger.error(f"❌ Error loading proxies: {str(e)}")
async def start(self, max_proxies: int) -> None:
await self.load_proxies()
if not self.all_proxies:
logger.error("❌ No proxies found in proxy.txt")
return
self.active_proxies = set(random.sample(list(self.all_proxies), min(len(self.all_proxies), max_proxies)))
tasks = {asyncio.create_task(self._run_client(proxy)): proxy for proxy in self.active_proxies}
while True:
done, pending = await asyncio.wait(tasks.keys(), return_when=asyncio.FIRST_COMPLETED)
for task in done:
proxy = tasks.pop(task)
if task.result() is False:
self.active_proxies.remove(proxy)
await self.load_proxies()
available_proxies = self.all_proxies - self.active_proxies
if available_proxies:
new_proxy = random.choice(list(available_proxies))
self.active_proxies.add(new_proxy)
new_task = asyncio.create_task(self._run_client(new_proxy))
tasks[new_task] = new_proxy
async def _run_client(self, proxy: str) -> bool:
client = WebSocketClient(proxy, self.user_id)
return await client.connect()
def setup_logger() -> None:
logger.remove()
logger.add(
"bot.log",
format=" <level>{level}</level> | <cyan>{message}</cyan>",
level="INFO",
rotation="1 day"
)
logger.add(
lambda msg: print(msg, end=""),
format=" <level>{level}</level> | <cyan>{message}</cyan>",
level="INFO",
colorize=True
)
async def load_user_config() -> dict:
try:
with open(CONFIG_FILE, "r") as config_file:
config_data = json.load(config_file)
return config_data if "user_ids" in config_data else {}
except Exception as e:
logger.error(f"❌ Error loading configuration: {str(e)}")
return {}
async def user_input() -> dict:
user_ids_input = input(f"{Fore.YELLOW}🔑 Enter your USER IDs (comma separated): {Style.RESET_ALL}")
user_ids = [uid.strip() for uid in user_ids_input.split(",") if uid.strip()]
config_data = {"user_ids": user_ids}
with open(CONFIG_FILE, "w") as config_file:
json.dump(config_data, config_file, indent=4)
logger.info(f"✅ Configuration saved! USER IDs: {user_ids}")
return config_data
async def main() -> None:
print(f"""{Fore.YELLOW}
██╗░░░░░██╗░██████╗░█████╗░
██║░░░░░██║██╔════╝██╔══██╗
██║░░░░░██║╚█████╗░███████║
██║░░░░░██║░╚═══██╗██╔══██║
███████╗██║██████╔╝██║░░██║
╚══════╝╚═╝╚═════╝░╚═╝░░╚═╝
{Style.RESET_ALL}""")
print(f"{Fore.MAGENTA}Get Grass Network Bot! AUTOMATE AND DOMINATE{Style.RESET_ALL}")
print(f"{Fore.RED}========================================{Style.RESET_ALL}")
setup_logger()
config = await load_user_config()
if not config or not config.get("user_ids"):
config = await user_input()
user_ids = config["user_ids"]
max_proxies_input = input(f"{Fore.MAGENTA}📡 Enter the maximum number of proxies to use: {Style.RESET_ALL}")
max_proxies = int(max_proxies_input)
for user_id in user_ids:
logger.info(f"🚀 Starting with USER_ID: {user_id}")
logger.info(f"📡 Using a maximum of {max_proxies} proxies")
logger.info(f"⏱️ Ping interval: {PING_INTERVAL} seconds")
manager = ProxyManager(user_id)
asyncio.create_task(manager.start(max_proxies))
await asyncio.Event().wait()
if __name__ == '__main__':
try:
asyncio.run(main())
except KeyboardInterrupt:
print("\n👋 Shutting down gracefully...")
except Exception as e:
logger.error(f"❌ Fatal error: {str(e)}")