-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
222 lines (197 loc) · 7.17 KB
/
Copy pathmain.js
File metadata and controls
222 lines (197 loc) · 7.17 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
const fs = require("fs");
const path = require("path");
const { authenticateAllWallets } = require("./src/api/signin");
const {
initDashboard,
registerKeyHandler,
render,
updateStatus,
widgets,
} = require("./src/ui");
const {
initAutomation,
startAutomation,
pauseAutomation,
resumeAutomation,
manualSwitchAccount,
getRunningState,
} = require("./src/automation");
const { auth } = require("./src/api");
const {
log,
logToFile,
checkLogSize,
clearLogFile,
backupLogFile,
} = require("./src/utils");
function readPrivateKeysFromFile(filePath) {
const absolutePath = path.resolve(filePath);
if (!fs.existsSync(absolutePath)) {
console.error(`File ${absolutePath} not exists.`);
return [];
}
const data = fs.readFileSync(absolutePath, "utf8");
return data.split(/\r?\n/).filter(line => line.trim() !== "");
}
function clearSessionTokenFile() {
const tokenPath = path.join(process.cwd(), "session-token.key");
fs.writeFileSync(tokenPath, "", "utf8");
console.log("[INFO] session-token.key has been cleared.");
}
async function main() {
try {
checkLogSize();
initDashboard();
clearSessionTokenFile();
log("Welcome to KlokApp Chat Automation", "info");
log("Press S to start, P to pause, R to resume, H for help", "info");
logToFile("KlokApp Chat Automation started");
const validTokenCount = await auth.verifyAndCleanupTokens();
log(`Token Length: ${validTokenCount}`);
if (validTokenCount === 0) {
log("No valid session tokens found. Attempting to authenticate...", "info");
updateStatus("Authenticating...", "info");
render();
const privateKeys = readPrivateKeysFromFile("priv.txt");
if (privateKeys.length === 0) {
log("No private keys found in priv.txt file.", "error");
updateStatus("Missing private keys in priv.txt file", "error");
} else {
log(`Found ${privateKeys.length} private keys. Authenticating...`, "info");
await authenticateAllWallets(privateKeys);
const tokens = auth.readAllSessionTokensFromFile();
if (tokens.length === 0) {
log("Authentication failed. No valid tokens received.", "error");
updateStatus("Authentication failed", "error");
} else {
log(`Authentication successful! ${tokens.length} accounts ready.`, "success");
updateStatus(`${tokens.length} accounts ready. Press S to start`, "success");
}
}
} else if (validTokenCount === 1) {
log("One valid session token found! Ready for login.", "success");
updateStatus("Session token ready. Press S to start", "success");
} else {
log(`${validTokenCount} valid session tokens found! Ready for login.`, "success");
updateStatus(`${validTokenCount} accounts ready. Press S to start`, "success");
}
render();
await initAutomation();
registerKeyHandler("s", async () => {
if (!getRunningState()) {
const tokens = auth.readAllSessionTokensFromFile();
if (tokens.length === 0) {
log("No session tokens found. Please add session-token.key file.", "error");
updateStatus("Missing session-token.key", "error");
render();
return;
}
log("Starting automation...", "info");
logToFile("Starting automation (user initiated)");
startAutomation();
} else {
log("Automation already running", "warning");
logToFile("Start request ignored - automation already running");
}
});
registerKeyHandler("p", () => {
if (getRunningState()) {
log("Pausing automation...", "info");
logToFile("Pausing automation (user initiated)");
pauseAutomation();
} else {
log("Automation not running", "warning");
logToFile("Pause request ignored - automation not running");
}
});
registerKeyHandler("r", () => {
if (!getRunningState()) {
log("Resuming automation...", "info");
logToFile("Resuming automation (user initiated)");
resumeAutomation();
} else {
log("Automation already running", "warning");
logToFile("Resume request ignored - automation already running");
}
});
registerKeyHandler("a", async () => {
const success = await manualSwitchAccount();
if (success) {
log("Account switched successfully", "success");
} else {
log("Manual switch is disabled in concurrency mode", "warning");
}
});
registerKeyHandler("l", () => {
const backupPath = backupLogFile();
clearLogFile();
if (backupPath) {
log(`Log file cleared and backed up to ${backupPath}`, "success");
logToFile("Log file cleared and backed up (user initiated)");
} else {
log("Log file cleared", "success");
logToFile("Log file cleared (user initiated)");
}
render();
});
registerKeyHandler("i", () => {
const fs = require("fs");
const path = require("path");
try {
const logPath = path.join(process.cwd(), "info.log");
if (fs.existsSync(logPath)) {
const stats = fs.statSync(logPath);
const fileSizeMB = (stats.size / (1024 * 1024)).toFixed(2);
const lastModified = new Date(stats.mtime).toLocaleString();
log(`Log file: Size=${fileSizeMB}MB, Last Modified: ${lastModified}`, "info");
} else {
log("Log file does not exist yet", "info");
}
} catch (error) {
log(`Error reading log info: ${error.message}`, "error");
}
try {
const tokens = auth.readAllSessionTokensFromFile();
const tokenInfo = auth.getTokenInfo();
if (tokens.length === 0) {
log("No accounts found", "warning");
} else if (tokens.length === 1) {
log("1 account configured", "info");
} else {
log(`${tokens.length} accounts configured.`, "info");
}
} catch (error) {
log(`Error checking accounts: ${error.message}`, "error");
}
updateStatus("Info displayed", "info");
setTimeout(() => {
updateStatus(getRunningState() ? "Running" : "Ready", getRunningState() ? "success" : "info");
render();
}, 5000);
render();
});
registerKeyHandler("h", () => {
log("Controls:", "info");
log("S - Start automation (requires at least one session token)", "info");
log("P - Pause automation", "info");
log("R - Resume automation", "info");
log("A - Switch account (disabled in concurrency mode)", "info");
log("L - Clear log file and make backup", "info");
log("I - Show file and account information", "info");
log("H - Show this help", "info");
log("Q or Esc - Quit application", "info");
updateStatus("Help - press any key to continue", "info");
render();
setTimeout(() => {
updateStatus(getRunningState() ? "Running" : "Ready", getRunningState() ? "success" : "info");
render();
}, 8000);
});
} catch (error) {
log(`Application error: ${error.message}`, "error");
logToFile("Application error", { error: error.message, stack: error.stack });
updateStatus("Error", "error");
render();
}
}
main();