401 lines
13 KiB
HTML
Executable File
401 lines
13 KiB
HTML
Executable File
<!DOCTYPE html>
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>WhatsApp Bot Scan</title>
|
|
<style>
|
|
body {
|
|
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 100vh;
|
|
margin: 0;
|
|
background-color: #f0f2f5;
|
|
color: #333;
|
|
}
|
|
|
|
.container {
|
|
background: white;
|
|
padding: 2rem;
|
|
border-radius: 10px;
|
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
|
text-align: center;
|
|
max-width: 400px;
|
|
width: 90%;
|
|
transition: all 0.3s ease;
|
|
}
|
|
|
|
h1 {
|
|
margin-bottom: 1rem;
|
|
color: #128C7E;
|
|
}
|
|
|
|
#qrcode {
|
|
margin: 20px auto;
|
|
border: 1px solid #ddd;
|
|
padding: 10px;
|
|
min-height: 250px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
#status {
|
|
margin-top: 1rem;
|
|
font-weight: bold;
|
|
color: #666;
|
|
}
|
|
|
|
.logs {
|
|
margin-top: 20px;
|
|
text-align: left;
|
|
font-size: 0.8rem;
|
|
max-height: 150px;
|
|
overflow-y: auto;
|
|
background: #eee;
|
|
padding: 10px;
|
|
border-radius: 5px;
|
|
}
|
|
|
|
.success {
|
|
color: #25D366 !important;
|
|
}
|
|
|
|
.waiting {
|
|
color: #FFA500 !important;
|
|
}
|
|
|
|
/* Login Form Styles */
|
|
#login-form {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 15px;
|
|
}
|
|
|
|
input {
|
|
padding: 10px;
|
|
border: 1px solid #ddd;
|
|
border-radius: 5px;
|
|
font-size: 16px;
|
|
}
|
|
|
|
button {
|
|
padding: 10px;
|
|
background-color: #128C7E;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
font-size: 16px;
|
|
font-weight: bold;
|
|
}
|
|
|
|
button:hover {
|
|
background-color: #075E54;
|
|
}
|
|
|
|
.hidden {
|
|
display: none !important;
|
|
}
|
|
|
|
.error {
|
|
color: red;
|
|
font-size: 0.9rem;
|
|
margin-top: -10px;
|
|
}
|
|
|
|
#captcha-container {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
background: #f9f9f9;
|
|
padding: 5px;
|
|
border-radius: 5px;
|
|
cursor: pointer;
|
|
}
|
|
|
|
#captcha-svg {
|
|
width: 100%;
|
|
height: auto;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div class="container" id="login-card">
|
|
<h1>Login Admin</h1>
|
|
<div id="login-form">
|
|
|
|
<input type="text" id="username" placeholder="Username" required
|
|
onkeypress="if(event.key === 'Enter') handleLogin()">
|
|
<input type="password" id="password" placeholder="Password" required
|
|
onkeypress="if(event.key === 'Enter') handleLogin()">
|
|
|
|
<div id="captcha-container" title="Click to refresh">
|
|
<!-- SVG goes here -->
|
|
</div>
|
|
<input type="text" id="captcha-input" placeholder="Enter Captcha" required
|
|
onkeypress="if(event.key === 'Enter') handleLogin()">
|
|
|
|
<div id="login-error" class="error hidden"></div>
|
|
|
|
<button onclick="handleLogin()">Login</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="container hidden" id="app-card">
|
|
<h1>WhatsApp Bot</h1>
|
|
<div id="status" class="waiting">Menunggu server...</div>
|
|
<div id="qrcode">
|
|
<p>Menunggu QR Code...</p>
|
|
</div>
|
|
<div id="qr-status" style="margin-top: 5px; color: #888; font-size: 0.9em;" class="hidden">
|
|
QR Age: <span id="qr-timer">0s</span>
|
|
</div>
|
|
<div class="logs" id="logs">
|
|
<div>System logs...</div>
|
|
</div>
|
|
|
|
<div style="margin-top: 20px; display: flex; gap: 10px; justify-content: center;">
|
|
<button onclick="handleReset()" style="background-color: #f0ad4e;">Reset Session</button>
|
|
<button onclick="handleLogout()" style="background-color: #d9534f;">Logout</button>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="/socket.io/socket.io.js"></script>
|
|
<script>
|
|
let socket;
|
|
let sessionId = '';
|
|
let qrTimerInterval;
|
|
|
|
const loginCard = document.getElementById('login-card');
|
|
const appCard = document.getElementById('app-card');
|
|
const loginError = document.getElementById('login-error');
|
|
const statusEl = document.getElementById('status');
|
|
const qrEl = document.getElementById('qrcode');
|
|
const qrTimerEl = document.getElementById('qr-timer');
|
|
const qrStatusEl = document.getElementById('qr-status');
|
|
const logsEl = document.getElementById('logs');
|
|
const captchaContainer = document.getElementById('captcha-container');
|
|
|
|
// Check token on load
|
|
function checkAuth() {
|
|
const token = localStorage.getItem('authToken');
|
|
if (token) {
|
|
showApp(token);
|
|
} else {
|
|
showLogin();
|
|
}
|
|
}
|
|
|
|
async function loadCaptcha() {
|
|
try {
|
|
const res = await fetch('/api/captcha');
|
|
const data = await res.json();
|
|
sessionId = data.sessionId;
|
|
captchaContainer.innerHTML = data.image;
|
|
} catch (e) {
|
|
console.error("Failed to load captcha", e);
|
|
}
|
|
}
|
|
|
|
captchaContainer.addEventListener('click', loadCaptcha);
|
|
|
|
function showLogin() {
|
|
loginCard.classList.remove('hidden');
|
|
appCard.classList.add('hidden');
|
|
loadCaptcha();
|
|
}
|
|
|
|
function showApp(token) {
|
|
loginCard.classList.add('hidden');
|
|
appCard.classList.remove('hidden');
|
|
initSocket(token);
|
|
}
|
|
|
|
async function handleLogin() {
|
|
const username = document.getElementById('username').value;
|
|
const password = document.getElementById('password').value;
|
|
const captcha = document.getElementById('captcha-input').value;
|
|
|
|
if (!username || !password || !captcha) {
|
|
showError('Please fill all fields');
|
|
return;
|
|
}
|
|
|
|
// Clear error
|
|
loginError.classList.add('hidden');
|
|
|
|
try {
|
|
const res = await fetch('/api/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ username, password, captcha, sessionId })
|
|
});
|
|
|
|
const data = await res.json();
|
|
|
|
if (data.status) {
|
|
localStorage.setItem('authToken', data.token);
|
|
showApp(data.token);
|
|
} else {
|
|
showError(data.message);
|
|
loadCaptcha(); // Refresh captcha on fail
|
|
document.getElementById('captcha-input').value = '';
|
|
}
|
|
} catch (e) {
|
|
showError('Login failed: ' + e.message);
|
|
}
|
|
}
|
|
|
|
async function handleReset() {
|
|
if (!confirm('Are you sure you want to reset the session? This will disconnect the current WhatsApp session and require a new scan.')) return;
|
|
|
|
const token = localStorage.getItem('authToken');
|
|
try {
|
|
const res = await fetch('/api/reset', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': token
|
|
}
|
|
});
|
|
const data = await res.json();
|
|
if (data.status) {
|
|
addLog('System Reset Initiated...');
|
|
} else {
|
|
alert('Reset failed: ' + data.message);
|
|
}
|
|
} catch (e) {
|
|
alert('Error: ' + e.message);
|
|
}
|
|
}
|
|
|
|
function showError(msg) {
|
|
loginError.textContent = msg;
|
|
loginError.classList.remove('hidden');
|
|
}
|
|
|
|
function handleLogout() {
|
|
localStorage.removeItem('authToken');
|
|
if (socket) socket.disconnect();
|
|
location.reload();
|
|
}
|
|
|
|
function startQrTimer(timestamp) {
|
|
if (qrTimerInterval) clearInterval(qrTimerInterval);
|
|
|
|
const updateTimer = () => {
|
|
const now = Date.now();
|
|
const diff = Math.floor((now - timestamp) / 1000);
|
|
qrTimerEl.innerText = diff + 's ago';
|
|
|
|
// Visual warning if old
|
|
if (diff > 50) {
|
|
qrTimerEl.style.color = 'red';
|
|
qrTimerEl.style.fontWeight = 'bold';
|
|
} else {
|
|
qrTimerEl.style.color = 'inherit';
|
|
qrTimerEl.style.fontWeight = 'normal';
|
|
}
|
|
};
|
|
|
|
updateTimer();
|
|
qrTimerInterval = setInterval(updateTimer, 1000);
|
|
}
|
|
|
|
function initSocket(token) {
|
|
if (socket && socket.connected) return;
|
|
|
|
socket = io({
|
|
auth: { token: token }
|
|
});
|
|
|
|
socket.on('connect_error', (err) => {
|
|
if (err.message === "Authentication error") {
|
|
handleLogout(); // Invalid token
|
|
}
|
|
statusEl.textContent = 'Connection Failed: ' + err.message;
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
statusEl.textContent = 'Terhubung ke Server';
|
|
addLog('Connected to Socket.io');
|
|
});
|
|
|
|
socket.on('disconnect_message', (msg) => {
|
|
addLog(msg);
|
|
statusEl.textContent = msg;
|
|
});
|
|
|
|
socket.on('qr', (data) => {
|
|
let url, timestamp;
|
|
// Handle new format {url, timestamp} and legacy
|
|
if (typeof data === 'string') {
|
|
url = data;
|
|
timestamp = Date.now();
|
|
} else if (data.timestamp) {
|
|
url = data.url;
|
|
timestamp = data.timestamp;
|
|
} else {
|
|
// Fallback
|
|
url = data.url;
|
|
timestamp = Date.now();
|
|
}
|
|
|
|
statusEl.textContent = 'Silakan Scan QR Code';
|
|
statusEl.className = 'waiting';
|
|
qrEl.innerHTML = `<img src="${url}" alt="QR Code" style="width:100%"/>`;
|
|
|
|
qrStatusEl.classList.remove('hidden');
|
|
startQrTimer(timestamp);
|
|
|
|
addLog('New QR Code received');
|
|
});
|
|
|
|
socket.on('ready', (data) => {
|
|
if (qrTimerInterval) clearInterval(qrTimerInterval);
|
|
statusEl.textContent = 'WhatsApp Terhubung!';
|
|
statusEl.className = 'success';
|
|
|
|
let numberDisplay = '';
|
|
if (data && data.number) {
|
|
numberDisplay = `<div style="font-size: 0.9em; margin-top: 5px; color: #555;">Connected: +${data.number}</div>`;
|
|
}
|
|
|
|
// Use flex-direction column to stack elements
|
|
qrEl.style.flexDirection = 'column';
|
|
qrEl.innerHTML = '<div style="font-size: 2em;">✅</div><div style="margin-top: 10px; font-weight: bold;">Bot Siap Digunakan</div>' + numberDisplay;
|
|
qrStatusEl.classList.add('hidden');
|
|
addLog('Client is ready' + (data && data.number ? ` (${data.number})` : ''));
|
|
});
|
|
|
|
socket.on('authenticated', () => {
|
|
statusEl.textContent = 'Terautentikasi!';
|
|
addLog('Authenticated successfully');
|
|
});
|
|
|
|
socket.on('disconnect', () => {
|
|
statusEl.textContent = 'Terputus dari server';
|
|
statusEl.className = '';
|
|
});
|
|
}
|
|
|
|
function addLog(msg) {
|
|
const div = document.createElement('div');
|
|
div.textContent = `> ${msg}`;
|
|
logsEl.insertBefore(div, logsEl.firstChild);
|
|
}
|
|
|
|
// Init
|
|
checkAuth();
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html> |