forked from ChickenAI/multizlogin
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathroutes-ui.js
More file actions
1393 lines (1283 loc) · 61.7 KB
/
routes-ui.js
File metadata and controls
1393 lines (1283 loc) · 61.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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import express from 'express';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { zaloAccounts, loginZaloAccount } from './api/zalo/zalo.js';
import { proxyService } from './proxyService.js';
import { broadcastLoginSuccess, broadcastEvent, wss, server } from './server.js';
import { basicAuth } from './middleware.js';
import { startSeleniumLogin, pollLoginStatus, stopSelenium } from './seleniumService.js';
const router = express.Router();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const configPath = path.join(__dirname, 'zalo_data', 'webhook-config.json');
const multiWebhookPath = '/app/zalo_data/webhooks.json';
const proxiesPath = path.join(__dirname, 'zalo_data', 'proxies.json');
const cookiesDir = '/app/cookies';
// In-memory log system
export let eventLogs = [];
export function addLog(action, details, ownId = 'System', extraData = null) {
const logEntry = {
timestamp: new Date().toLocaleString('vi-VN'),
action,
details,
ownId,
data: extraData ? JSON.stringify(extraData) : ''
};
eventLogs.push(logEntry);
if (eventLogs.length > 1000000) {
eventLogs = eventLogs.slice(-900000);
}
}
addLog('System', 'Ứng dụng đã khởi động');
// Khởi tạo file cấu hình mặc định nếu chưa tồn tại
const zaloDataDir = path.join(__dirname, 'zalo_data');
if (!fs.existsSync(zaloDataDir)) {
fs.mkdirSync(zaloDataDir, { recursive: true });
}
if (!fs.existsSync(proxiesPath)) {
fs.writeFileSync(proxiesPath, JSON.stringify([], null, 2));
}
if (!fs.existsSync(configPath)) {
fs.writeFileSync(configPath, JSON.stringify({
messageWebhookUrl: '',
groupEventWebhookUrl: '',
reactionWebhookUrl: '',
connectionWebhookUrl: ''
}, null, 2));
}
// Áp dụng middleware basicAuth cho tất cả các route giao diện
router.use(basicAuth);
router.get('/', (req, res) => {
res.redirect('/home');
});
router.get('/home', (req, res) => {
let accountsHtml = '<p class="text-muted">Chưa có tài khoản nào đăng nhập</p>';
if (zaloAccounts.length > 0) {
accountsHtml = `
<table class="table table-bordered table-hover">
<thead class="table-light">
<tr>
<th>Own ID</th>
<th>Số điện thoại</th>
<th>Proxy</th>
<th>Hành động</th>
</tr>
</thead>
<tbody>
`;
zaloAccounts.forEach((account) => {
accountsHtml += `
<tr>
<td>${account.ownId}</td>
<td>${account.phoneNumber || 'N/A'}</td>
<td>${account.proxy || 'Không có'}</td>
<td>
<form action="/deleteAccount" method="POST" class="d-inline">
<input type="hidden" name="ownId" value="${account.ownId}">
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Bạn có chắc muốn xóa tài khoản ${account.ownId}?');">Xóa</button>
</form>
</td>
</tr>`;
});
accountsHtml += '</tbody></table>';
}
const proxies = proxyService.getPROXIES();
let proxiesHtml = '<p class="text-muted">Chưa có proxy nào</p>';
if (proxies.length > 0) {
proxiesHtml = `
<table class="table table-bordered table-hover">
<thead class="table-light">
<tr>
<th>Proxy URL</th>
<th>Số tài khoản</th>
<th>Danh sách số điện thoại</th>
</tr>
</thead>
<tbody>
`;
proxies.forEach((proxy) => {
const accountsList =
proxy.accounts.length > 0
? proxy.accounts.map((acc) => acc.phoneNumber || 'N/A').join(', ')
: 'Chưa có';
proxiesHtml += `
<tr>
<td>${proxy.url}</td>
<td>${proxy.usedCount}</td>
<td>${accountsList}</td>
</tr>`;
});
proxiesHtml += '</tbody></table>';
}
// Per-account webhook config table
let perAccountWebhooks = {};
try {
if (fs.existsSync(multiWebhookPath)) {
perAccountWebhooks = JSON.parse(fs.readFileSync(multiWebhookPath, 'utf8'));
}
} catch (e) {}
let webhooksHtml = `
<div class="table-responsive">
<table class="table table-hover align-middle mb-0">
<thead class="bg-light">
<tr>
<th>Tài khoản</th>
<th>URL Webhook (GAS)</th>
<th>Sự kiện</th>
<th>Pull Tasks</th>
<th class="text-end">Thao tác</th>
</tr>
</thead>
<tbody>
`;
if (zaloAccounts.length > 0) {
zaloAccounts.forEach((account) => {
const cfg = perAccountWebhooks[account.ownId] || { url: '', settings: { receiveReaction: true, receiveGroupEvent: true }, pullMode: false };
webhooksHtml += `
<tr>
<form action="/updateAccountWebhook" method="POST">
<input type="hidden" name="ownId" value="${account.ownId}">
<td><span class="fw-bold">${account.ownId}</span><div class="small text-muted">${account.phoneNumber || ''}</div></td>
<td><input type="url" name="url" class="form-control form-control-sm" value="${cfg.url || ''}" placeholder="https://script.google.com/..."></td>
<td>
<div class="form-check form-check-inline mb-0">
<input class="form-check-input" type="checkbox" name="receiveReaction" id="reac_${account.ownId}" ${cfg.settings?.receiveReaction !== false ? 'checked' : ''}>
<label class="form-check-label small" for="reac_${account.ownId}">Reaction</label>
</div>
<div class="form-check form-check-inline mb-0">
<input class="form-check-input" type="checkbox" name="receiveGroupEvent" id="group_${account.ownId}" ${cfg.settings?.receiveGroupEvent !== false ? 'checked' : ''}>
<label class="form-check-label small" for="group_${account.ownId}">Group</label>
</div>
</td>
<td>
<div class="form-check form-switch">
<input class="form-check-input" type="checkbox" name="pullMode" id="pull_${account.ownId}" ${cfg.pullMode ? 'checked' : ''}>
<label class="form-check-label small" for="pull_${account.ownId}">Kích hoạt</label>
</div>
</td>
<td class="text-end"><button type="submit" class="btn btn-primary btn-sm px-3">Lưu</button></td>
</form>
</tr>
`;
});
} else {
webhooksHtml += '<tr><td colspan="5" class="text-center py-4 text-muted">Chưa có tài khoản nào đăng nhập</td></tr>';
}
webhooksHtml += '</tbody></table></div>';
// Lấy danh sách file tài khoản trong thư mục cookies
let accountFilesHtml = '<div class="alert alert-info py-2"><i class="bi bi-info-circle me-2"></i>Hệ thống chưa có tệp phiên làm việc (session)</div>';
try {
if (!fs.existsSync(cookiesDir)) {
fs.mkdirSync(cookiesDir, { recursive: true });
}
const cookieFiles = fs.readdirSync(cookiesDir).filter((file) => file.startsWith('cred_') && file.endsWith('.json'));
if (cookieFiles.length > 0) {
accountFilesHtml = `
<div class="list-group">
${cookieFiles
.map(
(file) => `
<div class="list-group-item d-flex justify-content-between align-items-center p-3">
<div>
<i class="bi bi-file-earmark-code me-2 text-primary"></i>
<span class="font-monospace small">${file}</span>
</div>
<a href="/export/account/${file}" class="btn btn-outline-primary btn-sm">
<i class="bi bi-download me-1"></i> Tải xuống
</a>
</div>`
)
.join('')}
</div>
`;
}
} catch (error) {
console.error('Lỗi khi đọc thư mục cookies:', error);
}
// Build ownId options for n8n template wizard
let n8nOwnIdOptions = '<option value="">-- Không chọn (nhập thủ công) --</option>';
zaloAccounts.forEach((acc) => {
n8nOwnIdOptions += '<option value="' + acc.ownId + '">' + acc.ownId + ' (' + (acc.phoneNumber || 'N/A') + ')</option>';
});
res.send(`
<!DOCTYPE html>
<html lang="vi">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Zalo Tools v4 — Tiến Tạ</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<style>
body { background-color: #f8f9fa; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; }
.card { border-radius: 12px; border: none; box-shadow: 0 4px 12px rgba(0,0,0,0.05); margin-bottom: 24px; }
.card-header { background-color: #fff; border-bottom: 1px solid #eee; border-radius: 12px 12px 0 0 !important; }
.btn-custom { border-radius: 8px; padding: 8px 16px; font-weight: 500; transition: all 0.2s; }
.btn-custom:hover { transform: translateY(-1px); box-shadow: 0 4px 8px rgba(0,0,0,0.1); }
.nav-tabs { border-bottom: none; }
.nav-link { border: none !important; color: #666; font-weight: 500; padding: 12px 20px; border-radius: 8px !important; margin-right: 5px; }
.nav-link.active { background-color: #0d6efd !important; color: #fff !important; }
</style>
</head>
<body>
<div class="container mt-4">
<h1 class="mb-2 text-center">Zalo Tools v4 — Tiến Tạ</h1>
<p class="text-center text-muted mb-4" style="font-size: 0.9rem;">Phiên bản v4.20260410 | Liên hệ: <strong>0387 553 113</strong></p>
<!-- Tabs -->
<ul class="nav nav-tabs mb-4" id="mainTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="accounts-tab" data-bs-toggle="tab" data-bs-target="#accounts" type="button" role="tab">Tài khoản</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="proxies-tab" data-bs-toggle="tab" data-bs-target="#proxies" type="button" role="tab">Proxy</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="webhook-tab" data-bs-toggle="tab" data-bs-target="#webhook" type="button" role="tab">Webhook</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="export-import-tab" data-bs-toggle="tab" data-bs-target="#export-import" type="button" role="tab">Export/Import</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="guides-tab" data-bs-toggle="tab" data-bs-target="#guides" type="button" role="tab">Hướng dẫn</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="logs-tab" data-bs-toggle="tab" data-bs-target="#logs" type="button" role="tab">Logs</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="n8n-tab" data-bs-toggle="tab" data-bs-target="#n8n-template" type="button" role="tab">n8n Template</button>
</li>
<li class="nav-item">
<a class="nav-link" href="/logout">Đăng xuất</a>
</li>
</ul>
<!-- Tab Content -->
<div class="tab-content" id="mainTabContent">
<!-- Tài khoản -->
<div class="tab-pane fade show active" id="accounts" role="tabpanel">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Danh sách tài khoản</h5>
</div>
<div class="card-body">
${accountsHtml}
<div class="mt-3">
<button class="btn btn-primary btn-custom px-4" onclick="startZaloLogin()">
<i class="bi bi-plus-circle me-2"></i>Thêm tài khoản Zalo mới
</button>
<form action="/restartApp" method="POST" class="d-inline">
<button type="submit" class="btn btn-warning btn-custom" onclick="return confirm('Bạn có chắc muốn khởi động lại ứng dụng? Trang sẽ không phản hồi trong vài giây.');">Khởi động lại ứng dụng</button>
</form>
</div>
<!-- Selenium Modal -->
<div class="modal fade" id="seleniumModal" data-bs-backdrop="static" tabindex="-1" aria-hidden="true">
<div class="modal-dialog modal-xl modal-dialog-centered">
<div class="modal-content shadow-lg border-0">
<div class="modal-header bg-primary text-white">
<h5 class="modal-title"><i class="bi bi-qr-code-scan me-2"></i>Đăng nhập Zalo</h5>
<button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close" onclick="stopZaloLoginSession()"></button>
</div>
<div class="modal-body p-0 position-relative d-flex align-items-center justify-content-center" style="height: 75vh; background: #f0f2f5;">
<!-- Loading State -->
<div id="zaloLoginLoading" class="text-center" style="z-index: 10;">
<div class="spinner-border text-primary mb-3" role="status" style="width: 3.5rem; height: 3.5rem; border-width: 0.3em;">
<span class="visually-hidden">Loading...</span>
</div>
<h5 class="fw-bold text-primary mb-1">Đang chuẩn bị môi trường đăng nhập...</h5>
<p class="text-muted small">Quá trình này có thể mất vài giây, vui lòng không đóng cửa sổ.</p>
</div>
<!-- Iframe -->
<iframe id="zaloLoginFrame" src="" style="width: 100%; height: 100%; border: none; display: none;"></iframe>
</div>
<div class="modal-footer bg-light py-2 shadow-sm">
<div class="container-fluid text-center">
<small class="text-muted"><i class="bi bi-shield-check me-1"></i>Vui lòng quét mã QR hoặc đăng nhập bằng SĐT. Hệ thống tự động đóng khi hoàn tất.</small>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Proxy -->
<div class="tab-pane fade" id="proxies" role="tabpanel">
<div class="card shadow-sm border-0">
<div class="card-header bg-white p-3">
<h5 class="card-title mb-0 fw-bold"><i class="bi bi-shield-shaded me-2 text-primary"></i>Quản lý Proxy</h5>
</div>
<div class="card-body p-4">
<div class="table-responsive border rounded-3 mb-4">
${proxiesHtml}
</div>
<div class="row g-3">
<div class="col-md-6">
<div class="p-3 border rounded-3 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-plus-lg me-2 text-success"></i>Thêm Proxy mới</h6>
<form action="/proxies" method="POST" class="input-group">
<input type="text" name="proxyUrl" class="form-control" placeholder="http://user:pass@ip:port" required>
<button type="submit" class="btn btn-success px-3">Thêm</button>
</form>
</div>
</div>
<div class="col-md-6">
<div class="p-3 border rounded-3 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-trash me-2 text-danger"></i>Xóa Proxy</h6>
<form action="/proxies" method="POST" class="input-group">
<input type="hidden" name="_method" value="DELETE">
<input type="text" name="proxyUrl" class="form-control" placeholder="Dán URL proxy cần xóa" required>
<button type="submit" class="btn btn-danger px-3">Xóa</button>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Webhook -->
<div class="tab-pane fade" id="webhook" role="tabpanel">
<div class="card shadow-sm border-0">
<div class="card-header bg-white p-3">
<h5 class="card-title mb-0 fw-bold"><i class="bi bi-link-45deg me-2 text-primary"></i>Cấu hình Webhook theo tài khoản</h5>
</div>
<div class="card-body p-4">
${webhooksHtml}
<div class="mt-4 border-top pt-3">
<small class="text-muted"><i class="bi bi-info-circle me-1"></i>Cấu hình global (legacy): <a href="/updateWebhookForm">Cập nhật webhook toàn cục</a></small>
</div>
</div>
</div>
</div>
<!-- Export/Import -->
<div class="tab-pane fade" id="export-import" role="tabpanel">
<div class="card shadow-sm border-0">
<div class="card-header bg-white p-3">
<h5 class="card-title mb-0 fw-bold"><i class="bi bi-box-arrow-in-down me-2 text-primary"></i>Dữ liệu hệ thống</h5>
</div>
<div class="card-body p-4">
<div class="row g-4">
<div class="col-md-6">
<h6 class="fw-bold mb-3"><i class="bi bi-key me-2 text-warning"></i>Phiên làm việc (Sessions)</h6>
<div class="border rounded-3 bg-light overflow-hidden">
${accountFilesHtml}
</div>
</div>
<div class="col-md-6">
<h6 class="fw-bold mb-3"><i class="bi bi-gear-wide-connected me-2 text-info"></i>Cấu hình chung</h6>
<div class="list-group list-group-flush border rounded-3 border-bottom-0 mb-4">
<a href="/export/proxies" class="list-group-item list-group-item-action d-flex justify-content-between p-3">
<span><i class="bi bi-shield-lock me-2"></i>Tải xuống proxies.json</span>
<i class="bi bi-chevron-right text-muted"></i>
</a>
<a href="/export/webhook-config" class="list-group-item list-group-item-action d-flex justify-content-between p-3 border-bottom">
<span><i class="bi bi-braces me-2"></i>Tải xuống webhook-config.json</span>
<i class="bi bi-chevron-right text-muted"></i>
</a>
</div>
<h6 class="fw-bold mb-3"><i class="bi bi-cloud-upload me-2 text-primary"></i>Import dữ liệu</h6>
<form action="/import" method="POST" enctype="multipart/form-data" class="bg-light p-3 border rounded-3">
<div class="input-group">
<input type="file" name="file" class="form-control" accept=".json" required>
<button type="submit" class="btn btn-success"><i class="bi bi-upload"></i></button>
</div>
<small class="text-muted d-block mt-2">Hỗ trợ: proxies.json, webhook-config.json, cred_*.json</small>
</form>
</div>
</div>
</div>
</div>
</div>
<!-- Hướng dẫn -->
<div class="tab-pane fade" id="guides" role="tabpanel">
<div class="card">
<div class="card-header">
<h5 class="card-title mb-0">Hướng dẫn giới hạn Zalo</h5>
</div>
<div class="card-body">
<ul class="list-group">
<li class="list-group-item"><strong>Thời gian nghỉ</strong> giữa 2 lần gửi tin nhắn: <em>60 - 150 giây</em></li>
<li class="list-group-item"><strong>Giới hạn gửi tin nhắn/ngày</strong>:
<ul>
<li>TK lâu năm (>1 năm, chưa bị hạn chế): Bắt đầu <strong>30</strong>, tăng dần +20 mỗi 3 ngày, tối đa 150.</li>
<li>TK mới: <strong>10 - 30</strong> tin nhắn/ngày.</li>
</ul>
</li>
<li class="list-group-item"><strong>Giới hạn tìm số điện thoại/giờ</strong>:
<ul>
<li>TK cá nhân: 15 tin nhắn/60 phút.</li>
<li>TK business: 30 tin nhắn/60 phút.</li>
</ul>
</li>
<li class="list-group-item"><strong>Kết bạn</strong>: Không vượt quá <strong>30 - 35 người/ngày</strong> (tách riêng nếu gửi tin nhắn nhiều).</li>
</ul>
<div class="mt-3">
<a href="/list" class="btn btn-primary btn-custom" target="_blank">Tài liệu API</a>
</div>
</div>
</div>
</div>
<!-- n8n Template -->
<div class="tab-pane fade" id="n8n-template" role="tabpanel">
<div class="card shadow-sm border-0">
<div class="card-header bg-white p-3">
<h5 class="card-title mb-0 fw-bold"><i class="bi bi-diagram-3 me-2 text-primary"></i>Tạo n8n Template</h5>
</div>
<div class="card-body p-4">
<!-- Wizard Steps Indicator -->
<div class="d-flex justify-content-center mb-4">
<div class="d-flex align-items-center gap-2">
<span class="badge rounded-pill bg-primary px-3 py-2 n8n-step-badge" id="n8nStepBadge1">1. Endpoint</span>
<i class="bi bi-chevron-right text-muted"></i>
<span class="badge rounded-pill bg-secondary px-3 py-2 n8n-step-badge" id="n8nStepBadge2">2. Xác thực</span>
<i class="bi bi-chevron-right text-muted"></i>
<span class="badge rounded-pill bg-secondary px-3 py-2 n8n-step-badge" id="n8nStepBadge3">3. Chọn Node</span>
<i class="bi bi-chevron-right text-muted"></i>
<span class="badge rounded-pill bg-secondary px-3 py-2 n8n-step-badge" id="n8nStepBadge4">4. Tải về</span>
</div>
</div>
<!-- Step 1: Endpoint URL -->
<div id="n8nStep1">
<div class="border rounded-3 p-4 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-globe me-2 text-success"></i>Cấu hình Endpoint</h6>
<p class="text-muted small">Nhập URL endpoint của container Zalo Tools (bao gồm cả port). Đây là địa chỉ mà n8n sẽ gửi HTTP Request đến.</p>
<div class="mb-3">
<label class="form-label fw-semibold">Base URL</label>
<input type="text" id="n8nBaseUrl" class="form-control" placeholder="http://zalo-tools:3000">
<div class="form-text">Ví dụ: http://localhost:3000, http://zalo-tools:3000 (Docker network), hoặc domain public.</div>
</div>
<div class="text-end">
<button class="btn btn-primary btn-custom" onclick="n8nGoToStep(2)">Tiếp theo <i class="bi bi-arrow-right ms-1"></i></button>
</div>
</div>
</div>
<!-- Step 2: Authentication -->
<div id="n8nStep2" style="display:none;">
<div class="border rounded-3 p-4 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-shield-lock me-2 text-warning"></i>Thông tin xác thực</h6>
<p class="text-muted small">Thông tin được tự động điền từ cấu hình Docker. Bạn có thể chỉnh sửa nếu cần.</p>
<div class="row g-3">
<div class="col-md-6">
<label class="form-label fw-semibold">API Key (X-API-Key)</label>
<div class="input-group">
<input type="password" id="n8nApiKey" class="form-control" value="${process.env.X_API_KEY || ''}">
<button class="btn btn-outline-secondary" type="button" onclick="toggleN8nPassword('n8nApiKey')"><i class="bi bi-eye"></i></button>
</div>
<div class="form-text">Dùng cho các API endpoint (sendmessage, findUser, v.v.)</div>
</div>
<div class="col-md-6">
<label class="form-label fw-semibold">Basic Auth Username</label>
<input type="text" id="n8nBasicUser" class="form-control" value="${process.env.ADMIN_USERNAME || ''}">
</div>
<div class="col-md-6">
<label class="form-label fw-semibold">Basic Auth Password</label>
<div class="input-group">
<input type="password" id="n8nBasicPass" class="form-control" value="${process.env.ADMIN_PASSWORD || ''}">
<button class="btn btn-outline-secondary" type="button" onclick="toggleN8nPassword('n8nBasicPass')"><i class="bi bi-eye"></i></button>
</div>
<div class="form-text">Dùng cho Web UI endpoints</div>
</div>
</div>
<div class="row g-3 mt-1">
<div class="col-md-6">
<label class="form-label fw-semibold">Tài khoản Zalo (OwnId)</label>
<select id="n8nOwnId" class="form-select">
${n8nOwnIdOptions}
</select>
<div class="form-text">Chọn tài khoản để tự động điền OwnId vào các node mẫu.</div>
</div>
</div>
<div class="text-end mt-3">
<button class="btn btn-outline-secondary btn-custom me-2" onclick="n8nGoToStep(1)"><i class="bi bi-arrow-left me-1"></i> Quay lại</button>
<button class="btn btn-primary btn-custom" onclick="n8nGoToStep(3)">Tiếp theo <i class="bi bi-arrow-right ms-1"></i></button>
</div>
</div>
</div>
<!-- Step 3: Select Nodes -->
<div id="n8nStep3" style="display:none;">
<div class="border rounded-3 p-4 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-check2-square me-2 text-info"></i>Chọn các node mẫu</h6>
<p class="text-muted small">Chọn các node HTTP Request và Webhook mẫu để đưa vào template. Bạn có thể copy các node này sang workflow khác trong n8n.</p>
<div class="row g-3">
<div class="col-md-6">
<div class="border rounded-3 bg-white p-3">
<h6 class="fw-semibold mb-2"><i class="bi bi-arrow-down-circle me-1 text-success"></i> Webhook (Nhận event)</h6>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeWebhook" checked>
<label class="form-check-label" for="n8nNodeWebhook">Webhook Receiver — Nhận tin nhắn/event từ Zalo</label>
</div>
</div>
</div>
<div class="col-md-6">
<div class="border rounded-3 bg-white p-3">
<h6 class="fw-semibold mb-2"><i class="bi bi-list-check me-1 text-primary"></i> Quản lý tài khoản</h6>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeAccounts" checked>
<label class="form-check-label" for="n8nNodeAccounts">GET /accounts — Danh sách tài khoản</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeHealth" checked>
<label class="form-check-label" for="n8nNodeHealth">GET /health — Kiểm tra trạng thái</label>
</div>
</div>
</div>
<div class="col-md-6">
<div class="border rounded-3 bg-white p-3">
<h6 class="fw-semibold mb-2"><i class="bi bi-chat-dots me-1 text-warning"></i> Nhắn tin</h6>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeSendMsg" checked>
<label class="form-check-label" for="n8nNodeSendMsg">POST /sendmessage — Gửi tin nhắn</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeSendImgUser">
<label class="form-check-label" for="n8nNodeSendImgUser">POST /sendImageToUser — Gửi ảnh cho user</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeSendImgGroup">
<label class="form-check-label" for="n8nNodeSendImgGroup">POST /sendImageToGroup — Gửi ảnh vào nhóm</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeSendFile">
<label class="form-check-label" for="n8nNodeSendFile">POST /sendFileToUser — Gửi file</label>
</div>
</div>
</div>
<div class="col-md-6">
<div class="border rounded-3 bg-white p-3">
<h6 class="fw-semibold mb-2"><i class="bi bi-people me-1 text-danger"></i> Liên hệ & Nhóm</h6>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeFindUser" checked>
<label class="form-check-label" for="n8nNodeFindUser">POST /findUser — Tìm user theo SĐT</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeGetUserInfo">
<label class="form-check-label" for="n8nNodeGetUserInfo">POST /getUserInfo — Thông tin user</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeGetGroupInfo">
<label class="form-check-label" for="n8nNodeGetGroupInfo">POST /getGroupInfo — Thông tin nhóm</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeCreateGroup">
<label class="form-check-label" for="n8nNodeCreateGroup">POST /createGroup — Tạo nhóm</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeAddUser">
<label class="form-check-label" for="n8nNodeAddUser">POST /addUserToGroup — Thêm vào nhóm</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeRemoveUser">
<label class="form-check-label" for="n8nNodeRemoveUser">POST /removeUserFromGroup — Xóa khỏi nhóm</label>
</div>
<div class="form-check mb-2">
<input class="form-check-input" type="checkbox" id="n8nNodeFriendReq">
<label class="form-check-label" for="n8nNodeFriendReq">POST /sendFriendRequest — Kết bạn</label>
</div>
</div>
</div>
</div>
<div class="text-end mt-3">
<button class="btn btn-outline-secondary btn-custom me-2" onclick="n8nGoToStep(2)"><i class="bi bi-arrow-left me-1"></i> Quay lại</button>
<button class="btn btn-success btn-custom" onclick="n8nGenerate()"><i class="bi bi-lightning me-1"></i> Tạo Template</button>
</div>
</div>
</div>
<!-- Step 4: Download -->
<div id="n8nStep4" style="display:none;">
<div class="border rounded-3 p-4 bg-light">
<h6 class="fw-bold mb-3"><i class="bi bi-download me-2 text-success"></i>Template đã sẵn sàng!</h6>
<p class="text-muted small">File template.json đã được tạo. Bạn có thể tải về và import vào n8n.</p>
<div class="alert alert-info small">
<i class="bi bi-info-circle me-1"></i>
<strong>Cách import:</strong> Mở n8n → Menu → Import from File → Chọn file template.json vừa tải.<br>
<strong>Copy node:</strong> Trong n8n, chọn node mẫu → Ctrl+C → Mở workflow khác → Ctrl+V.<br>
<strong>Cấu hình Credential:</strong> Sau khi import, mở 1 node HTTP Request → mục Credential → tạo mới "Basic Auth" với Username/Password ở bước 2.
</div>
<div class="mb-3">
<label class="form-label fw-semibold">Xem trước JSON</label>
<textarea id="n8nPreview" class="form-control font-monospace" rows="12" readonly style="font-size: 12px; background: #1e1e1e; color: #d4d4d4;"></textarea>
</div>
<div class="d-flex gap-2">
<button class="btn btn-success btn-custom" onclick="n8nDownload()"><i class="bi bi-download me-1"></i> Tải template.json</button>
<button class="btn btn-outline-primary btn-custom" onclick="n8nCopyJson()"><i class="bi bi-clipboard me-1"></i> Copy JSON</button>
<button class="btn btn-outline-secondary btn-custom" onclick="n8nGoToStep(3)"><i class="bi bi-arrow-left me-1"></i> Quay lại chỉnh sửa</button>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Logs -->
<div class="tab-pane fade" id="logs" role="tabpanel">
<div class="card shadow-sm border-0">
<div class="card-header bg-white p-3 d-flex justify-content-between align-items-center">
<h5 class="card-title mb-0 fw-bold"><i class="bi bi-journal-text me-2 text-primary"></i>Activity Logs</h5>
<button class="btn btn-outline-secondary btn-sm" onclick="loadLogs()"><i class="bi bi-arrow-clockwise me-1"></i>Làm mới</button>
</div>
<div class="card-body p-0">
<div class="table-responsive" style="max-height: 600px; overflow-y: auto;">
<table class="table table-sm table-hover mb-0" id="logsTable">
<thead class="bg-light sticky-top">
<tr>
<th style="width: 160px">Thời gian</th>
<th style="width: 120px">Action</th>
<th style="width: 120px">Tài khoản</th>
<th>Chi tiết</th>
</tr>
</thead>
<tbody id="logsBody">
<tr><td colspan="4" class="text-center py-4 text-muted">Nhấn "Làm mới" để tải logs</td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
let zaloLoginPollInterval;
const zaloLoginModal = new bootstrap.Modal(document.getElementById('seleniumModal'));
async function startZaloLogin() {
const loadingDiv = document.getElementById('zaloLoginLoading');
const iframe = document.getElementById('zaloLoginFrame');
iframe.style.display = 'none';
loadingDiv.style.display = 'block';
zaloLoginModal.show();
try {
const res = await fetch('/api/selenium/start', { method: 'POST' });
const data = await res.json();
if (data.success) {
// resize=remote giúp phía server Selenium tự điều chỉnh kích thước trình duyệt khớp với Iframe
const vncUrl = location.protocol + '//' + location.hostname + ':7900/?autoconnect=1&resize=remote&password=secret&reconnect=1';
iframe.src = vncUrl;
iframe.onload = () => {
loadingDiv.style.display = 'none';
iframe.style.display = 'block';
};
startZaloStatusPolling();
} else {
zaloLoginModal.hide();
alert('Lỗi khởi động hệ thống đăng nhập: ' + data.error);
}
} catch (err) {
zaloLoginModal.hide();
alert('Lỗi kết nối server: ' + err.message);
}
}
function startZaloStatusPolling() {
if (zaloLoginPollInterval) clearInterval(zaloLoginPollInterval);
zaloLoginPollInterval = setInterval(async () => {
const res = await fetch('/api/selenium/status');
const data = await res.json();
if (data.status === 'success') {
clearInterval(zaloLoginPollInterval);
alert('Đăng nhập thành công! Hệ thống đang khởi động bot...');
await fetch('/api/selenium/finish', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data.data)
});
location.reload();
}
}, 3000);
}
async function stopZaloLoginSession() {
clearInterval(zaloLoginPollInterval);
await fetch('/api/selenium/stop', { method: 'POST' });
}
// === n8n Template Wizard ===
// Auto-fill base URL from current address
document.getElementById('n8nBaseUrl').value = location.origin;
// UUID fallback for non-HTTPS contexts
function generateUUID() {
if (typeof crypto !== 'undefined' && crypto.randomUUID) return generateUUID();
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0;
return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
});
}
function toggleN8nPassword(id) {
const el = document.getElementById(id);
el.type = el.type === 'password' ? 'text' : 'password';
}
function n8nGoToStep(step) {
for (let i = 1; i <= 4; i++) {
document.getElementById('n8nStep' + i).style.display = i === step ? 'block' : 'none';
const badge = document.getElementById('n8nStepBadge' + i);
badge.className = 'badge rounded-pill px-3 py-2 n8n-step-badge ' + (i === step ? 'bg-primary' : (i < step ? 'bg-success' : 'bg-secondary'));
}
}
let n8nTemplateJson = '';
function n8nGenerate() {
const baseUrl = document.getElementById('n8nBaseUrl').value.replace(/\\/+$/, '');
const apiKey = document.getElementById('n8nApiKey').value;
const basicUser = document.getElementById('n8nBasicUser').value;
const basicPass = document.getElementById('n8nBasicPass').value;
const selectedOwnId = document.getElementById('n8nOwnId').value || 'YOUR_OWN_ID';
if (!baseUrl) { alert('Vui lòng nhập Base URL'); return; }
const nodes = [];
const connections = {};
let posX = 250, posY = 300;
const nodeSpacingX = 300;
// Helper: tạo n8n HTTP Request node với Basic Auth
function makeHttpNode(name, method, urlPath, bodyParams, notes) {
const id = generateUUID();
const node = {
parameters: {
method: method,
url: baseUrl + urlPath,
authentication: 'genericCredentialType',
genericAuthType: 'httpBasicAuth',
sendHeaders: true,
headerParameters: {
parameters: [
{ name: 'X-API-Key', value: apiKey }
]
},
sendBody: method === 'POST',
specifyBody: method === 'POST' ? 'json' : undefined,
jsonBody: method === 'POST' ? JSON.stringify(bodyParams || {}, null, 2) : undefined,
options: {}
},
id: id,
name: name,
type: 'n8n-nodes-base.httpRequest',
typeVersion: 4.2,
position: [posX, posY],
notesInFlow: !!notes,
notes: notes || '',
credentials: {
httpBasicAuth: {
id: generateUUID(),
name: 'Zalo Tools Basic Auth'
}
}
};
// Clean up undefined fields
if (method !== 'POST') {
delete node.parameters.specifyBody;
delete node.parameters.jsonBody;
delete node.parameters.sendBody;
}
posX += nodeSpacingX;
return node;
}
// Webhook Receiver Node
if (document.getElementById('n8nNodeWebhook').checked) {
const webhookId = generateUUID();
nodes.push({
parameters: {
httpMethod: 'POST',
path: 'zalo-events',
responseMode: 'onReceived',
responseData: 'allEntries',
options: {}
},
id: webhookId,
name: 'Zalo Webhook Receiver',
type: 'n8n-nodes-base.webhook',
typeVersion: 2,
position: [posX, posY],
webhookId: generateUUID().split('-')[0],
notesInFlow: true,
notes: 'Nhận event từ Zalo Tools.\\nCopy URL webhook này và cấu hình trong tab Webhook của Zalo Tools.'
});
posX += nodeSpacingX;
}
// Row 2 - API Nodes
posY += 200;
posX = 250;
// Manual trigger as starting point for API nodes
const triggerId = generateUUID();
nodes.push({
parameters: {},
id: triggerId,
name: 'Test Trigger',
type: 'n8n-nodes-base.manualTrigger',
typeVersion: 1,
position: [posX, posY],
notesInFlow: true,
notes: 'Click "Test workflow" de thu cac node mau'
});
const triggerNodeName = 'Test Trigger';
posX += nodeSpacingX;
let prevNodeName = triggerNodeName;
let firstApiNode = null;
// Accounts
if (document.getElementById('n8nNodeAccounts').checked) {
const n = makeHttpNode('Danh sach tai khoan', 'GET', '/accounts', null, 'GET /accounts');
nodes.push(n);
if (!firstApiNode) firstApiNode = n.name;
connections[prevNodeName] = { main: [[{ node: n.name, type: 'main', index: 0 }]] };
prevNodeName = n.name;
}
// Health
if (document.getElementById('n8nNodeHealth').checked) {
const n = makeHttpNode('Kiem tra trang thai', 'GET', '/health', null, 'GET /health');
nodes.push(n);
if (!firstApiNode) firstApiNode = n.name;
if (prevNodeName !== triggerNodeName || !firstApiNode) {
connections[prevNodeName] = connections[prevNodeName] || { main: [[]] };
}
}
// Send Message
if (document.getElementById('n8nNodeSendMsg').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Gui tin nhan', 'POST', '/sendmessage', {
message: 'Hello from n8n!',
threadId: 'USER_OR_GROUP_ID',
type: '0',
ownId: selectedOwnId
}, 'POST /sendmessage\\ntype: 0=User, 1=Group');
nodes.push(n);
}
// Send Image to User
if (document.getElementById('n8nNodeSendImgUser').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Gui anh cho user', 'POST', '/sendImageToUser', {
imagePath: 'https://example.com/image.jpg',
threadId: 'USER_ID',
ownId: selectedOwnId
}, 'POST /sendImageToUser\\nimagePath hoac imageData (base64)');
nodes.push(n);
}
// Send Image to Group
if (document.getElementById('n8nNodeSendImgGroup').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Gui anh vao nhom', 'POST', '/sendImageToGroup', {
imagePath: 'https://example.com/image.jpg',
threadId: 'GROUP_ID',
ownId: selectedOwnId
}, 'POST /sendImageToGroup\\nimagePath hoac imageData (base64)');
nodes.push(n);
}
// Send File
if (document.getElementById('n8nNodeSendFile').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Gui file', 'POST', '/sendFileToUser', {
filePath: 'https://example.com/file.pdf',
threadId: 'USER_ID',
ownId: selectedOwnId
}, 'POST /sendFileToUser\\nfilePath hoac fileData (base64)');
nodes.push(n);
}
// Find User
if (document.getElementById('n8nNodeFindUser').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Tim user theo SDT', 'POST', '/findUser', {
phone: '0901234567',
ownId: selectedOwnId
}, 'POST /findUser');
nodes.push(n);
}
// Get User Info
if (document.getElementById('n8nNodeGetUserInfo').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Thong tin user', 'POST', '/getUserInfo', {
userId: 'USER_ID',
ownId: selectedOwnId
}, 'POST /getUserInfo');
nodes.push(n);
}
// Get Group Info
if (document.getElementById('n8nNodeGetGroupInfo').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Thong tin nhom', 'POST', '/getGroupInfo', {
groupId: 'GROUP_ID',
ownId: selectedOwnId
}, 'POST /getGroupInfo');
nodes.push(n);
}
// Create Group
if (document.getElementById('n8nNodeCreateGroup').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Tao nhom', 'POST', '/createGroup', {
members: ['USER_ID_1', 'USER_ID_2'],
name: 'Ten nhom moi',
ownId: selectedOwnId
}, 'POST /createGroup');
nodes.push(n);
}
// Add User to Group
if (document.getElementById('n8nNodeAddUser').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Them vao nhom', 'POST', '/addUserToGroup', {
groupId: 'GROUP_ID',
memberId: ['USER_ID'],
ownId: selectedOwnId
}, 'POST /addUserToGroup\\nmemberId co the la mang');
nodes.push(n);
}
// Remove User from Group
if (document.getElementById('n8nNodeRemoveUser').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Xoa khoi nhom', 'POST', '/removeUserFromGroup', {
groupId: 'GROUP_ID',
memberId: ['USER_ID'],
ownId: selectedOwnId
}, 'POST /removeUserFromGroup\\nmemberId co the la mang');
nodes.push(n);
}
// Send Friend Request
if (document.getElementById('n8nNodeFriendReq').checked) {
posY += 200; posX = 550;
const n = makeHttpNode('Ket ban', 'POST', '/sendFriendRequest', {
userId: 'USER_ID',
ownId: selectedOwnId
}, 'POST /sendFriendRequest');
nodes.push(n);
}
// Sticky Note - Lưu ý
var stickyContent = '## Luu y quan trong\\n\\n';
stickyContent += '1. **Tao Basic Auth Credential:**\\n';
stickyContent += ' Mo bat ky node HTTP Request > Credential > Tao moi "Basic Auth"\\n';
stickyContent += ' - Username: ' + basicUser + '\\n';