@vielhuber/wahelper 1.5.0 → 1.5.2
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/wahelper-daemon.js +11 -25
- package/wahelper.js +16 -9
package/package.json
CHANGED
package/wahelper-daemon.js
CHANGED
|
@@ -37,7 +37,7 @@ export default class wahelperDaemon {
|
|
|
37
37
|
this.authFolder = 'auth_' + this.device;
|
|
38
38
|
this.dbPath = 'whatsapp_' + this.device + '.sqlite';
|
|
39
39
|
this.logPath = 'whatsapp_' + this.device + '.log';
|
|
40
|
-
this.
|
|
40
|
+
this.port = this.computePort(this.device);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
@@ -88,6 +88,11 @@ export default class wahelperDaemon {
|
|
|
88
88
|
return args;
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
computePort(device) {
|
|
92
|
+
// range 29000-31999: below linux ephemeral (32768+) and windows ephemeral (49152+)
|
|
93
|
+
return 29000 + (parseInt(device.slice(-5)) % 3000);
|
|
94
|
+
}
|
|
95
|
+
|
|
91
96
|
formatNumber(number) {
|
|
92
97
|
// replace leading zero with 49
|
|
93
98
|
number = number.replace(/^0+/, '49');
|
|
@@ -627,13 +632,8 @@ export default class wahelperDaemon {
|
|
|
627
632
|
});
|
|
628
633
|
});
|
|
629
634
|
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
if (fs.existsSync(socketPath)) {
|
|
633
|
-
fs.rmSync(socketPath, { force: true });
|
|
634
|
-
}
|
|
635
|
-
this.httpServer.listen(socketPath, () => {
|
|
636
|
-
this.log('HTTP server listening on ' + socketPath);
|
|
635
|
+
this.httpServer.listen(this.port, '127.0.0.1', () => {
|
|
636
|
+
this.log('HTTP server listening on 127.0.0.1:' + this.port);
|
|
637
637
|
});
|
|
638
638
|
|
|
639
639
|
this.httpServer.on('error', error => {
|
|
@@ -686,22 +686,12 @@ export default class wahelperDaemon {
|
|
|
686
686
|
this.db.close();
|
|
687
687
|
this.dbIsOpen = false;
|
|
688
688
|
}
|
|
689
|
-
// clean up socket file
|
|
690
|
-
let socketPath = this.dirname + '/' + this.socketPath;
|
|
691
|
-
if (fs.existsSync(socketPath)) {
|
|
692
|
-
fs.rmSync(socketPath, { force: true });
|
|
693
|
-
}
|
|
694
689
|
this.log('Daemon stopped');
|
|
695
690
|
}
|
|
696
691
|
|
|
697
692
|
isAlreadyRunning() {
|
|
698
693
|
return new Promise(resolve => {
|
|
699
|
-
let
|
|
700
|
-
if (!fs.existsSync(socketPath)) {
|
|
701
|
-
resolve(false);
|
|
702
|
-
return;
|
|
703
|
-
}
|
|
704
|
-
let req = http.request({ socketPath, path: '/status', method: 'GET' }, res => {
|
|
694
|
+
let req = http.request({ host: '127.0.0.1', port: this.port, path: '/status', method: 'GET' }, res => {
|
|
705
695
|
resolve(res.statusCode === 200);
|
|
706
696
|
});
|
|
707
697
|
req.on('error', () => resolve(false));
|
|
@@ -732,12 +722,8 @@ export default class wahelperDaemon {
|
|
|
732
722
|
process.exit(1);
|
|
733
723
|
}
|
|
734
724
|
|
|
735
|
-
this.log(
|
|
736
|
-
|
|
737
|
-
);
|
|
738
|
-
console.log(
|
|
739
|
-
'Daemon starting... (device: ' + this.device + ', socket: ' + this.dirname + '/' + this.socketPath + ')'
|
|
740
|
-
);
|
|
725
|
+
this.log('Daemon starting.. (device: ' + this.device + ', port: 127.0.0.1:' + this.port + ')');
|
|
726
|
+
console.log('Daemon starting... (device: ' + this.device + ', port: 127.0.0.1:' + this.port + ')');
|
|
741
727
|
|
|
742
728
|
this.initDatabase();
|
|
743
729
|
this.initExitHooks();
|
package/wahelper.js
CHANGED
|
@@ -24,7 +24,7 @@ export default class wahelper {
|
|
|
24
24
|
this.dbPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.sqlite';
|
|
25
25
|
this.logPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.log';
|
|
26
26
|
this.dataPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.json';
|
|
27
|
-
this.
|
|
27
|
+
this.port = this.computePort(this.formatNumber(this.args.device));
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
@@ -220,18 +220,19 @@ export default class wahelper {
|
|
|
220
220
|
return { connected: false, message: 'daemon_timeout' };
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
+
computePort(device) {
|
|
224
|
+
// range 29000-31999: below linux ephemeral (32768+) and windows ephemeral (49152+)
|
|
225
|
+
return 29000 + (parseInt(device.slice(-5)) % 3000);
|
|
226
|
+
}
|
|
227
|
+
|
|
223
228
|
async callDaemon(method, path, body = null) {
|
|
224
229
|
return new Promise(resolve => {
|
|
225
230
|
let postData = body !== null ? JSON.stringify(body) : '';
|
|
226
|
-
let
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
method,
|
|
230
|
-
headers: {
|
|
231
|
-
'Content-Type': 'application/json',
|
|
232
|
-
'Content-Length': Buffer.byteLength(postData)
|
|
233
|
-
}
|
|
231
|
+
let headers = {
|
|
232
|
+
'Content-Type': 'application/json',
|
|
233
|
+
'Content-Length': Buffer.byteLength(postData)
|
|
234
234
|
};
|
|
235
|
+
let options = { host: '127.0.0.1', port: this.port, path, method, headers };
|
|
235
236
|
let req = http.request(options, res => {
|
|
236
237
|
let data = '';
|
|
237
238
|
res.on('data', chunk => {
|
|
@@ -453,10 +454,16 @@ export default class wahelper {
|
|
|
453
454
|
}
|
|
454
455
|
|
|
455
456
|
formatNumber(number) {
|
|
457
|
+
// strip surrounding quotes that some LLMs inject
|
|
458
|
+
number = String(number).replace(/^["'\s]+|["'\s]+$/g, '');
|
|
456
459
|
// replace leading zero with 49
|
|
457
460
|
number = number.replace(/^0+/, '49');
|
|
458
461
|
// remove non-digit characters
|
|
459
462
|
number = number.replace(/\D/g, '');
|
|
463
|
+
// if no country code present, prepend 49 (German default)
|
|
464
|
+
if (!number.startsWith('49')) {
|
|
465
|
+
number = '49' + number;
|
|
466
|
+
}
|
|
460
467
|
return number;
|
|
461
468
|
}
|
|
462
469
|
}
|