waengine 1.0.5 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/client.js +77 -2
- package/src/session-manager.js +154 -0
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -2,6 +2,7 @@ import { makeWASocket, useMultiFileAuthState, DisconnectReason, fetchLatestBaile
|
|
|
2
2
|
import pino from "pino";
|
|
3
3
|
import { generateQRCode, closeBrowser } from "./qr.js";
|
|
4
4
|
import { Message } from "./message.js";
|
|
5
|
+
import { SessionManager } from "./session-manager.js";
|
|
5
6
|
|
|
6
7
|
export class WhatsAppClient {
|
|
7
8
|
constructor(options = {}) {
|
|
@@ -10,6 +11,7 @@ export class WhatsAppClient {
|
|
|
10
11
|
printQR: false,
|
|
11
12
|
browser: options.browser || ["Chrome", "121.0.0", ""], // Aktueller Chrome
|
|
12
13
|
logLevel: options.logLevel || "silent", // Sauber ohne Debug
|
|
14
|
+
autoCleanup: options.autoCleanup !== false, // Auto-Cleanup bei Logout
|
|
13
15
|
...options
|
|
14
16
|
};
|
|
15
17
|
|
|
@@ -19,6 +21,9 @@ export class WhatsAppClient {
|
|
|
19
21
|
this.isConnected = false;
|
|
20
22
|
this.eventHandlers = new Map();
|
|
21
23
|
|
|
24
|
+
// Session Manager
|
|
25
|
+
this.sessionManager = new SessionManager(this.options.authDir);
|
|
26
|
+
|
|
22
27
|
// Prefix System
|
|
23
28
|
this.prefix = null;
|
|
24
29
|
this.commands = new Map();
|
|
@@ -34,10 +39,27 @@ export class WhatsAppClient {
|
|
|
34
39
|
// ===== CONNECTION METHODS =====
|
|
35
40
|
|
|
36
41
|
async connect() {
|
|
42
|
+
console.log("🔌 WAEngine startet...");
|
|
43
|
+
|
|
37
44
|
if (this.socket && this.isConnected) {
|
|
38
45
|
return this.socket;
|
|
39
46
|
}
|
|
40
47
|
|
|
48
|
+
// Session-Validierung und Auto-Repair
|
|
49
|
+
await this.sessionManager.ensureAuthDir();
|
|
50
|
+
const sessionValidation = await this.sessionManager.validateSession();
|
|
51
|
+
|
|
52
|
+
if (!sessionValidation.valid) {
|
|
53
|
+
console.log(`📋 Session-Status: ${sessionValidation.reason}`);
|
|
54
|
+
|
|
55
|
+
if (sessionValidation.reason === 'corrupted') {
|
|
56
|
+
console.log("🔧 Repariere korrupte Session...");
|
|
57
|
+
await this.sessionManager.repairSession();
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
console.log(`✅ Gültige Session gefunden (User: ${sessionValidation.userId})`);
|
|
61
|
+
}
|
|
62
|
+
|
|
41
63
|
const { state, saveCreds } = await useMultiFileAuthState(this.options.authDir);
|
|
42
64
|
const isLoggedIn = !!state.creds?.me?.id;
|
|
43
65
|
|
|
@@ -129,9 +151,18 @@ export class WhatsAppClient {
|
|
|
129
151
|
this.socket = null;
|
|
130
152
|
setTimeout(() => this.connect().then(resolve).catch(reject), 3000);
|
|
131
153
|
} else {
|
|
132
|
-
console.log("👋 Ausgeloggt");
|
|
154
|
+
console.log("👋 Ausgeloggt - bereinige Session...");
|
|
155
|
+
this.isConnected = false;
|
|
156
|
+
this.socket = null;
|
|
157
|
+
|
|
158
|
+
// Auto-Cleanup bei Logout
|
|
159
|
+
if (this.options.autoCleanup) {
|
|
160
|
+
await this.sessionManager.cleanupSession();
|
|
161
|
+
console.log("🧹 Session automatisch bereinigt");
|
|
162
|
+
}
|
|
163
|
+
|
|
133
164
|
await closeBrowser();
|
|
134
|
-
this.emit('disconnected', { reason: 'logged_out' });
|
|
165
|
+
this.emit('disconnected', { reason: 'logged_out', cleaned: this.options.autoCleanup });
|
|
135
166
|
reject(new Error('Logged out'));
|
|
136
167
|
}
|
|
137
168
|
} else if (connection === "open") {
|
|
@@ -164,6 +195,50 @@ export class WhatsAppClient {
|
|
|
164
195
|
}
|
|
165
196
|
}
|
|
166
197
|
|
|
198
|
+
// ===== SESSION MANAGEMENT =====
|
|
199
|
+
|
|
200
|
+
async getSessionStatus() {
|
|
201
|
+
return await this.sessionManager.getSessionStatus();
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
async validateSession() {
|
|
205
|
+
return await this.sessionManager.validateSession();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async cleanupSession() {
|
|
209
|
+
console.log("🧹 Manuelle Session-Bereinigung...");
|
|
210
|
+
const success = await this.sessionManager.cleanupSession();
|
|
211
|
+
if (success) {
|
|
212
|
+
console.log("✅ Session erfolgreich bereinigt");
|
|
213
|
+
}
|
|
214
|
+
return success;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
async repairSession() {
|
|
218
|
+
console.log("🔧 Session-Reparatur...");
|
|
219
|
+
return await this.sessionManager.repairSession();
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async backupSession() {
|
|
223
|
+
return await this.sessionManager.backupSession();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Logout mit automatischer Bereinigung
|
|
227
|
+
async logout() {
|
|
228
|
+
if (this.socket) {
|
|
229
|
+
console.log("👋 Logout wird durchgeführt...");
|
|
230
|
+
this.socket.logout();
|
|
231
|
+
|
|
232
|
+
// Warte kurz und bereinige dann
|
|
233
|
+
setTimeout(async () => {
|
|
234
|
+
if (this.options.autoCleanup) {
|
|
235
|
+
await this.sessionManager.cleanupSession();
|
|
236
|
+
console.log("🧹 Session nach Logout bereinigt");
|
|
237
|
+
}
|
|
238
|
+
}, 1000);
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
|
|
167
242
|
// ===== EVENT SYSTEM =====
|
|
168
243
|
|
|
169
244
|
on(event, handler) {
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// Session Manager für WAEngine v1.0.6
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
|
|
5
|
+
export class SessionManager {
|
|
6
|
+
constructor(authDir) {
|
|
7
|
+
this.authDir = authDir;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// Prüft ob Session existiert und gültig ist
|
|
11
|
+
async validateSession() {
|
|
12
|
+
try {
|
|
13
|
+
if (!fs.existsSync(this.authDir)) {
|
|
14
|
+
return { valid: false, reason: 'no_auth_dir' };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const credsPath = path.join(this.authDir, 'creds.json');
|
|
18
|
+
if (!fs.existsSync(credsPath)) {
|
|
19
|
+
return { valid: false, reason: 'no_creds' };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const creds = JSON.parse(fs.readFileSync(credsPath, 'utf8'));
|
|
23
|
+
|
|
24
|
+
// Prüfe ob User-ID vorhanden (eingeloggt)
|
|
25
|
+
if (!creds.me?.id) {
|
|
26
|
+
return { valid: false, reason: 'not_logged_in' };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Prüfe ob Session nicht zu alt ist (optional)
|
|
30
|
+
const stats = fs.statSync(credsPath);
|
|
31
|
+
const ageInDays = (Date.now() - stats.mtime.getTime()) / (1000 * 60 * 60 * 24);
|
|
32
|
+
|
|
33
|
+
if (ageInDays > 30) { // Session älter als 30 Tage
|
|
34
|
+
return { valid: false, reason: 'session_too_old' };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
valid: true,
|
|
39
|
+
userId: creds.me.id,
|
|
40
|
+
lastModified: stats.mtime,
|
|
41
|
+
ageInDays: Math.round(ageInDays)
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
} catch (error) {
|
|
45
|
+
return { valid: false, reason: 'corrupted', error: error.message };
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Bereinigt Session komplett
|
|
50
|
+
async cleanupSession() {
|
|
51
|
+
try {
|
|
52
|
+
if (fs.existsSync(this.authDir)) {
|
|
53
|
+
console.log("🧹 Bereinige Session...");
|
|
54
|
+
|
|
55
|
+
// Alle Auth-Dateien löschen
|
|
56
|
+
const files = fs.readdirSync(this.authDir);
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
const filePath = path.join(this.authDir, file);
|
|
59
|
+
fs.unlinkSync(filePath);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Auth-Ordner löschen
|
|
63
|
+
fs.rmdirSync(this.authDir);
|
|
64
|
+
console.log("✅ Session bereinigt");
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
return true;
|
|
68
|
+
} catch (error) {
|
|
69
|
+
console.error("❌ Fehler beim Session-Cleanup:", error);
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Erstellt Auth-Ordner falls nicht vorhanden
|
|
75
|
+
async ensureAuthDir() {
|
|
76
|
+
if (!fs.existsSync(this.authDir)) {
|
|
77
|
+
fs.mkdirSync(this.authDir, { recursive: true });
|
|
78
|
+
console.log(`📁 Auth-Ordner erstellt: ${this.authDir}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Backup der Session erstellen
|
|
83
|
+
async backupSession() {
|
|
84
|
+
try {
|
|
85
|
+
if (!fs.existsSync(this.authDir)) return false;
|
|
86
|
+
|
|
87
|
+
const backupDir = `${this.authDir}_backup_${Date.now()}`;
|
|
88
|
+
fs.mkdirSync(backupDir, { recursive: true });
|
|
89
|
+
|
|
90
|
+
const files = fs.readdirSync(this.authDir);
|
|
91
|
+
for (const file of files) {
|
|
92
|
+
const srcPath = path.join(this.authDir, file);
|
|
93
|
+
const destPath = path.join(backupDir, file);
|
|
94
|
+
fs.copyFileSync(srcPath, destPath);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
console.log(`💾 Session-Backup erstellt: ${backupDir}`);
|
|
98
|
+
return backupDir;
|
|
99
|
+
} catch (error) {
|
|
100
|
+
console.error("❌ Backup-Fehler:", error);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Session-Status für Debugging
|
|
106
|
+
getSessionStatus() {
|
|
107
|
+
try {
|
|
108
|
+
if (!fs.existsSync(this.authDir)) {
|
|
109
|
+
return { status: 'no_session', files: [] };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const files = fs.readdirSync(this.authDir);
|
|
113
|
+
const fileDetails = files.map(file => {
|
|
114
|
+
const filePath = path.join(this.authDir, file);
|
|
115
|
+
const stats = fs.statSync(filePath);
|
|
116
|
+
return {
|
|
117
|
+
name: file,
|
|
118
|
+
size: stats.size,
|
|
119
|
+
modified: stats.mtime
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
status: 'session_exists',
|
|
125
|
+
files: fileDetails,
|
|
126
|
+
totalFiles: files.length,
|
|
127
|
+
authDir: this.authDir
|
|
128
|
+
};
|
|
129
|
+
} catch (error) {
|
|
130
|
+
return { status: 'error', error: error.message };
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Repariert korrupte Session
|
|
135
|
+
async repairSession() {
|
|
136
|
+
console.log("🔧 Versuche Session-Reparatur...");
|
|
137
|
+
|
|
138
|
+
const validation = await this.validateSession();
|
|
139
|
+
|
|
140
|
+
if (validation.reason === 'corrupted') {
|
|
141
|
+
console.log("🗑️ Korrupte Session erkannt - bereinige...");
|
|
142
|
+
await this.cleanupSession();
|
|
143
|
+
return { repaired: true, action: 'cleanup' };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (validation.reason === 'session_too_old') {
|
|
147
|
+
console.log("⏰ Session zu alt - bereinige...");
|
|
148
|
+
await this.cleanupSession();
|
|
149
|
+
return { repaired: true, action: 'cleanup_old' };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { repaired: false, reason: validation.reason };
|
|
153
|
+
}
|
|
154
|
+
}
|