-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
381 lines (332 loc) · 13.2 KB
/
main.js
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import axios from 'axios';
import axiosRetry from 'axios-retry';
import WebSocket from 'ws';
import crypto from 'crypto';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';
import fs from 'fs';
import banner from './utils/banner.js';
import log from './utils/logger.js';
const headers = {
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
"Sec-Ch-Ua": '"Google Chrome";v="131", "Chromium";v="131", "Not_A_Brand";v="24"',
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": '"Windows"',
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
}
function readFile(pathFile) {
try {
const datas = fs.readFileSync(pathFile, 'utf8')
.split('\n')
.map(data => data.trim())
.filter(data => data.length > 0);
return datas;
} catch (error) {
log.error(`Error reading file: ${error.message}`);
return [];
}
}
const newAgent = (proxy = null) => {
if (proxy && proxy.startsWith('http://')) {
return new HttpsProxyAgent(proxy);
} else if (proxy && (proxy.startsWith('socks4://') || proxy.startsWith('socks5://'))) {
return new SocksProxyAgent(proxy);
}
return null;
};
class WebSocketClient {
constructor(authToken, address, proxy, index) {
this.url = `wss://apitn.openledger.xyz/ws/v1/orch?authToken=${authToken}`;
this.ws = null;
this.reconnect = true
this.index = index
this.intervalId = null
this.registered = false;
this.proxy = proxy;
this.address = address;
this.identity = btoa(address);
this.capacity = generateRandomCapacity();
this.id = crypto.randomUUID();
this.heartbeat = {
"message": {
"Worker": {
"Identity": this.identity,
"ownerAddress": this.address,
"type": "LWEXT",
"Host": "chrome-extension://ekbbplmjjgoobhdlffmgeokalelnmjjc"
},
Capacity: this.capacity
},
"msgType": "HEARTBEAT",
"workerType": "LWEXT",
"workerID": this.identity
};
this.regWorkerID = {
"workerID": this.identity,
"msgType": "REGISTER",
"workerType": "LWEXT",
"message": {
"id": this.id,
"type": "REGISTER",
"worker": {
"host": "chrome-extension://ekbbplmjjgoobhdlffmgeokalelnmjjc",
"identity": this.identity,
"ownerAddress": this.address,
"type": "LWEXT"
}
}
};
}
loadJobData = async (event) => {
if (event && event.data) {
const message = JSON.parse(event.data);
if (message?.MsgType == "JOB") {
this.ws.send(
JSON.stringify({
workerID: this.identity,
msgType: "JOB_ASSIGNED",
workerType: "LWEXT",
message: {
Status: true,
Ref: message?.UUID,
},
})
);
}
}
};
connect() {
const agent = newAgent(this.proxy);
const options = agent ? { agent } : {};
this.ws = new WebSocket(this.url, options);
this.ws.on('open', (type) => {
log.info(`WebSocket connection established for account ${this.index}`);
// this.ws.send(JSON.stringify({
// type: "WEBSOCKET_CONNECTED",
// message: type,
// }))
// this.sendMessage({
// type: "ALREADY_CONNECTED",
// });
if (!this.registered) {
log.info(`Trying to register worker ID for account ${this.index}...`);
this.sendMessage(this.regWorkerID);
this.registered = true;
}
this.intervalId = setInterval(() => {
log.info(`Sending heartbeat for account ${this.index}...`);
this.sendMessage(this.heartbeat)
}, 30 * 1000);
});
this.ws.on('message', (event) => {
const message = JSON.parse(event);
log.info(`Account ${this.index} Received message:`, message);
if (message && message.data) {
if (message?.data?.MsgType !== "JOB") {
this.sendMessage({
type: "WEBSOCKET_RESPONSE",
data: message.data,
});
} else {
this.loadJobData(message);
}
}
});
this.ws.on('error', (error) => {
log.error(`WebSocket error for Account ${this.index}:`, error.message || error);
});
this.ws.on('close', () => {
clearInterval(this.intervalId);
if (this.reconnect) {
log.warn(`WebSocket connection closed for Account ${this.index}, reconnecting...`);
setTimeout(() => this.connect("reconnect"), 5000); // Reconnect after 5 seconds
} else {
log.warn(`WebSocket connection closed for Account ${this.index}`);
}
});
}
sendMessage(message) {
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
this.ws.send(JSON.stringify(message));
} else {
log.error(`WebSocket connection is not open for Account ${this.index}, cannot send message.`);
}
}
close() {
if (this.ws) {
this.ws.close();
this.reconnect = false
}
}
}
axiosRetry(axios, {
retries: 3,
retryDelay: (retryCount) => retryCount * 1000,
retryCondition: (error) => error.response?.status >= 400 || error.code === 'ECONNABORTED'
});
async function generateToken(data, proxy) {
const agent = newAgent(proxy);
try {
const response = await axios.post('https://apitn.openledger.xyz/api/v1/auth/generate_token', data, {
headers: {
...headers,
'Content-Type': 'application/json',
},
agent: agent
});
return response.data.data;
} catch (error) {
return null;
}
}
async function getUserInfo(token, proxy, index) {
const agent = newAgent(proxy);
try {
const response = await axios.get('https://rewardstn.openledger.xyz/api/v1/reward_realtime', {
headers: {
...headers,
'Authorization': 'Bearer ' + token
},
agent: agent
});
const { total_heartbeats } = response?.data?.data[0] || { total_heartbeats: '0' };
log.info(`Account ${index} has gained points today:`, { PointsToday: total_heartbeats });
return response.data.data;
} catch (error) {
if (error.response && error.response.status === 401) {
log.error('Unauthorized, token is invalid or expired');
return 'unauthorized';
};
log.error('Error fetching user info:', error.message || error);
return null;
}
}
async function getClaimDetails(token, proxy, index) {
const agent = newAgent(proxy);
try {
const response = await axios.get('https://rewardstn.openledger.xyz/api/v1/claim_details', {
headers: {
...headers,
'Authorization': 'Bearer ' + token
},
agent: agent
});
const { tier, dailyPoint, claimed, nextClaim = 'Not Claimed' } = response?.data?.data || {};
log.info(`Details for Account ${index}:`, { tier, dailyPoint, claimed, nextClaim });
return response.data.data;
} catch (error) {
log.error('Error fetching claim info:', error.message || error);
return null;
}
}
async function claimRewards(token, proxy, index) {
const agent = newAgent(proxy);
try {
const response = await axios.get('https://rewardstn.openledger.xyz/api/v1/claim_reward', {
headers: {
...headers,
'Authorization': 'Bearer ' + token
},
agent: agent
});
log.info(`Daily Rewards Claimed for Account ${index}:`, response.data.data);
return response.data.data;
} catch (error) {
log.error('Error claiming daily reward:', error.message || error);
return null;
}
}
function generateRandomCapacity() {
function getRandomFloat(min, max, decimals = 2) {
return (Math.random() * (max - min) + min).toFixed(decimals);
}
return {
AvailableMemory: parseFloat(getRandomFloat(10, 64)),
AvailableStorage: getRandomFloat(10, 500),
AvailableGPU: '',
AvailableModels: []
};
}
// Main function
const main = async () => {
log.info(banner);
const wallets = readFile("wallets.txt")
if (wallets.length === 0) {
log.error('No wallets found in wallets.txt');
return;
}
const proxies = readFile("proxy.txt");
log.info(`Starting Program for all accounts:`, wallets.length);
const accountsProcessing = wallets.map(async (address, index) => {
const proxy = proxies[index % proxies.length];
let isConnected = false;
log.info(`Processing Account ${index + 1} with proxy: ${proxy || 'No proxy'}`);
let claimDetailsInterval;
let userInfoInterval;
while (!isConnected) {
try {
let response = await generateToken({ address }, proxy);
while (!response && !response.token) {
response = await generateToken({ address }, proxy);
await new Promise(resolve => setTimeout(resolve, 3000));
}
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZGRyZXNzIjoiMHhkYWMyNzBmZDk2YjYzZTQwZmFjZTg5MTdmNTFkNGRjYWYwOWMwODBlIiwiaWQiOjAsImV4cCI6MTc2Nzk3MDUyM30.wzOooTf3XlTJSYJehuWx4Hr4_x8jofiajnA2-QVZdcg';
log.info(`login success for Account ${index + 1}:`, token.slice(0, 36) + "-" + token.slice(-24));
log.info(`Getting user info and claim details for account ${index + 1}...`);
const claimDaily = await getClaimDetails(token, proxy, index + 1);
if (claimDaily && !claimDaily.claimed) {
log.info(`Trying to Claim Daily rewards for Account ${index + 1}...`);
await claimRewards(token, proxy, index + 1);
}
await getUserInfo(token, proxy, index + 1)
const socket = new WebSocketClient(token, address, proxy, index + 1);
socket.connect();
isConnected = true;
userInfoInterval = setInterval(async () => {
log.info(`Fetching total points gained today for account ${index + 1}...`);
const user = await getUserInfo(token, proxy, index + 1);
if (user === 'unauthorized') {
log.info(`Unauthorized: Token is invalid or expired for account ${index + 1}, reconnecting...`);
isConnected = false;
socket.close();
clearInterval(userInfoInterval);
clearInterval(claimDetailsInterval);
}
}, 10 * 60 * 1000); // Fetch user points every 10 minutes
claimDetailsInterval = setInterval(async () => {
try {
log.info(`Checking Daily Rewards for Account ${index + 1}...`)
const claimDetails = await getClaimDetails(token, proxy, index + 1);
if (claimDetails && !claimDetails.claimed) {
log.info(`Trying to Claim Daily rewards for Account ${index + 1}...`);
await claimRewards(token, proxy, index + 1);
}
} catch (error) {
log.error(`Error fetching claim details for Account ${index + 1}: ${error.message || 'unknown error'}`);
}
}, 60 * 60 * 1000); // Fetch claim details every 60 minutes
} catch (error) {
log.error(`Failed to start WebSocket client for Account ${index + 1}:`, error.message || 'unknown error');
isConnected = false;
await new Promise(resolve => setTimeout(resolve, 3000));
}
}
process.on('SIGINT', () => {
log.warn(`Process received SIGINT, cleaning up and exiting program...`);
clearInterval(claimDetailsInterval);
clearInterval(userInfoInterval);
process.exit(0);
});
process.on('SIGTERM', () => {
log.warn(`Process received SIGTERM, cleaning up and exiting program...`);
clearInterval(claimDetailsInterval);
clearInterval(userInfoInterval);
process.exit(0);
});
});
await Promise.all(accountsProcessing);
};
//run
main();