web
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1,4 +1,6 @@
|
|||||||
node_modules/
|
node_modules/
|
||||||
dist/
|
dist/
|
||||||
.env
|
.env
|
||||||
data/
|
data/*.json
|
||||||
|
data/xray-backups/
|
||||||
|
!data/.gitkeep
|
||||||
|
|||||||
0
data/.gitkeep
Normal file
0
data/.gitkeep
Normal file
@@ -19,6 +19,8 @@ services:
|
|||||||
- XRAY_USERNAME2=
|
- XRAY_USERNAME2=
|
||||||
- XRAY_PASSWORD2=
|
- XRAY_PASSWORD2=
|
||||||
|
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
networks:
|
networks:
|
||||||
- applications
|
- applications
|
||||||
extra_hosts:
|
extra_hosts:
|
||||||
|
|||||||
@@ -1,13 +1,5 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
INTERVAL="${PARSE_INTERVAL:-3600}"
|
echo "[entrypoint] Starting web server..."
|
||||||
|
exec npx ts-node src/server.ts
|
||||||
echo "[entrypoint] PARSE_INTERVAL=${INTERVAL}s"
|
|
||||||
|
|
||||||
while true; do
|
|
||||||
echo "[entrypoint] Запуск парсера..."
|
|
||||||
npx ts-node src/index.ts
|
|
||||||
echo "[entrypoint] Следующий запуск через ${INTERVAL} секунд"
|
|
||||||
sleep "${INTERVAL}"
|
|
||||||
done
|
|
||||||
|
|||||||
949
package-lock.json
generated
949
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -3,15 +3,20 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "ts-node src/index.ts",
|
"start": "ts-node src/server.ts",
|
||||||
|
"start:cli": "ts-node src/index.ts",
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"typecheck": "tsc --noEmit"
|
"typecheck": "tsc --noEmit"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"cookie-parser": "^1.4.7",
|
||||||
"dotenv": "^16.4.7",
|
"dotenv": "^16.4.7",
|
||||||
|
"express": "^5.2.1",
|
||||||
"playwright": "^1.51.0"
|
"playwright": "^1.51.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/cookie-parser": "^1.4.10",
|
||||||
|
"@types/express": "^5.0.6",
|
||||||
"@types/node": "^22.13.0",
|
"@types/node": "^22.13.0",
|
||||||
"ts-node": "^10.9.2",
|
"ts-node": "^10.9.2",
|
||||||
"typescript": "^5.7.3"
|
"typescript": "^5.7.3"
|
||||||
|
|||||||
206
public/app.js
Normal file
206
public/app.js
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
const $ = (sel) => document.querySelector(sel);
|
||||||
|
|
||||||
|
const loginScreen = $("#login-screen");
|
||||||
|
const dashboardScreen = $("#dashboard-screen");
|
||||||
|
const loginForm = $("#login-form");
|
||||||
|
const loginError = $("#login-error");
|
||||||
|
const logoutBtn = $("#logout-btn");
|
||||||
|
const novavpsUrlInput = $("#novavps-url");
|
||||||
|
const saveConfigBtn = $("#save-config-btn");
|
||||||
|
const configMsg = $("#config-msg");
|
||||||
|
const runBtn = $("#run-btn");
|
||||||
|
const runMsg = $("#run-msg");
|
||||||
|
const runningIndicator = $("#running-indicator");
|
||||||
|
const currentStep = $("#current-step");
|
||||||
|
const lastRunEl = $("#last-run");
|
||||||
|
|
||||||
|
const POLL_INTERVAL = 10000;
|
||||||
|
|
||||||
|
let pollTimer = null;
|
||||||
|
|
||||||
|
function showScreen(screen) {
|
||||||
|
loginScreen.classList.add("hidden");
|
||||||
|
dashboardScreen.classList.add("hidden");
|
||||||
|
screen.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatTime(iso) {
|
||||||
|
if (!iso) return "";
|
||||||
|
const d = new Date(iso);
|
||||||
|
return d.toLocaleString();
|
||||||
|
}
|
||||||
|
|
||||||
|
function updatePanelStatus(prefix, data) {
|
||||||
|
const badge = $(`#${prefix}-status`);
|
||||||
|
const detail = $(`#${prefix}-detail`);
|
||||||
|
const time = $(`#${prefix}-time`);
|
||||||
|
|
||||||
|
badge.textContent = data.status;
|
||||||
|
badge.className = "status-badge " + data.status;
|
||||||
|
|
||||||
|
let detailText = "";
|
||||||
|
if (prefix === "novavps" && data.count != null) {
|
||||||
|
detailText = `${data.count} connections`;
|
||||||
|
} else if (data.outboundsCount != null) {
|
||||||
|
detailText = `${data.outboundsCount} outbounds`;
|
||||||
|
if (data.synced) detailText += " (synced)";
|
||||||
|
}
|
||||||
|
if (data.error) detailText += (detailText ? " | " : "") + data.error;
|
||||||
|
detail.textContent = detailText;
|
||||||
|
time.textContent = formatTime(data.timestamp);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchStatus() {
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/status");
|
||||||
|
if (res.status === 401) {
|
||||||
|
showScreen(loginScreen);
|
||||||
|
stopPolling();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
|
const isRunning = data.status === "running";
|
||||||
|
runBtn.disabled = isRunning;
|
||||||
|
runningIndicator.classList.toggle("hidden", !isRunning);
|
||||||
|
|
||||||
|
const stepLabels = {
|
||||||
|
initializing: "Initializing...",
|
||||||
|
novavps_fetching: "Fetching Novavps data...",
|
||||||
|
xray1_parsing: "Parsing Xray 1...",
|
||||||
|
xray1_syncing: "Syncing Xray 1...",
|
||||||
|
xray2_parsing: "Parsing Xray 2...",
|
||||||
|
xray2_syncing: "Syncing Xray 2...",
|
||||||
|
done: "Completed",
|
||||||
|
novavps_url_missing: "Error: NOVAVPS URL not set",
|
||||||
|
no_xray_panels: "Error: No Xray panels configured",
|
||||||
|
fatal: "Fatal error",
|
||||||
|
};
|
||||||
|
currentStep.textContent = stepLabels[data.currentStep] || data.currentStep;
|
||||||
|
|
||||||
|
updatePanelStatus("novavps", data.novavps);
|
||||||
|
updatePanelStatus("xray1", data.xray1);
|
||||||
|
updatePanelStatus("xray2", data.xray2);
|
||||||
|
|
||||||
|
lastRunEl.textContent = data.lastRun ? formatTime(data.lastRun) : "Never";
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Status fetch error:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function fetchConfig() {
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/config");
|
||||||
|
if (res.status === 401) {
|
||||||
|
showScreen(loginScreen);
|
||||||
|
stopPolling();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = await res.json();
|
||||||
|
novavpsUrlInput.value = data.novavpsUrl || "";
|
||||||
|
} catch (err) {
|
||||||
|
console.error("Config fetch error:", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function startPolling() {
|
||||||
|
fetchStatus();
|
||||||
|
fetchConfig();
|
||||||
|
if (pollTimer) clearInterval(pollTimer);
|
||||||
|
pollTimer = setInterval(fetchStatus, POLL_INTERVAL);
|
||||||
|
}
|
||||||
|
|
||||||
|
function stopPolling() {
|
||||||
|
if (pollTimer) {
|
||||||
|
clearInterval(pollTimer);
|
||||||
|
pollTimer = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function showMsg(el, text, type) {
|
||||||
|
el.textContent = text;
|
||||||
|
el.className = "msg " + type;
|
||||||
|
el.classList.remove("hidden");
|
||||||
|
setTimeout(() => el.classList.add("hidden"), 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
loginForm.addEventListener("submit", async (e) => {
|
||||||
|
e.preventDefault();
|
||||||
|
loginError.classList.add("hidden");
|
||||||
|
|
||||||
|
const username = $("#username").value;
|
||||||
|
const password = $("#password").value;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/login", {
|
||||||
|
method: "POST",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ username, password }),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok) {
|
||||||
|
loginError.textContent = data.error || "Login failed";
|
||||||
|
loginError.classList.remove("hidden");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
showScreen(dashboardScreen);
|
||||||
|
startPolling();
|
||||||
|
} catch (err) {
|
||||||
|
loginError.textContent = "Network error";
|
||||||
|
loginError.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
logoutBtn.addEventListener("click", async () => {
|
||||||
|
try {
|
||||||
|
await fetch("/api/logout", { method: "POST" });
|
||||||
|
} catch {}
|
||||||
|
stopPolling();
|
||||||
|
showScreen(loginScreen);
|
||||||
|
});
|
||||||
|
|
||||||
|
saveConfigBtn.addEventListener("click", async () => {
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/config", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify({ novavpsUrl: novavpsUrlInput.value }),
|
||||||
|
});
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok) {
|
||||||
|
showMsg(configMsg, data.error || "Save failed", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
showMsg(configMsg, "Saved", "success");
|
||||||
|
} catch (err) {
|
||||||
|
showMsg(configMsg, "Network error", "error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
runBtn.addEventListener("click", async () => {
|
||||||
|
runMsg.classList.add("hidden");
|
||||||
|
try {
|
||||||
|
const res = await fetch("/api/run", { method: "POST" });
|
||||||
|
const data = await res.json();
|
||||||
|
if (!res.ok) {
|
||||||
|
showMsg(runMsg, data.error || "Failed to start", "error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
showMsg(runMsg, "Parser started", "success");
|
||||||
|
fetchStatus();
|
||||||
|
} catch (err) {
|
||||||
|
showMsg(runMsg, "Network error", "error");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Check if already logged in
|
||||||
|
fetch("/api/status")
|
||||||
|
.then((res) => {
|
||||||
|
if (res.ok) {
|
||||||
|
showScreen(dashboardScreen);
|
||||||
|
startPolling();
|
||||||
|
} else {
|
||||||
|
showScreen(loginScreen);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(() => showScreen(loginScreen));
|
||||||
90
public/index.html
Normal file
90
public/index.html
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>VPN Parser</title>
|
||||||
|
<link rel="stylesheet" href="/style.css">
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id="app">
|
||||||
|
<div id="login-screen" class="screen">
|
||||||
|
<div class="login-box">
|
||||||
|
<h1>VPN Parser</h1>
|
||||||
|
<form id="login-form">
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="username">Username</label>
|
||||||
|
<input type="text" id="username" autocomplete="username" required>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="password">Password</label>
|
||||||
|
<input type="password" id="password" autocomplete="current-password" required>
|
||||||
|
</div>
|
||||||
|
<button type="submit">Login</button>
|
||||||
|
<div id="login-error" class="error hidden"></div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div id="dashboard-screen" class="screen hidden">
|
||||||
|
<header>
|
||||||
|
<h1>VPN Parser Dashboard</h1>
|
||||||
|
<button id="logout-btn">Logout</button>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main>
|
||||||
|
<section class="config-section">
|
||||||
|
<h2>Configuration</h2>
|
||||||
|
<div class="config-row">
|
||||||
|
<label for="novavps-url">NOVAVPS URL</label>
|
||||||
|
<input type="text" id="novavps-url" placeholder="https://novavps.app/s/...">
|
||||||
|
<button id="save-config-btn">Save</button>
|
||||||
|
</div>
|
||||||
|
<div id="config-msg" class="msg hidden"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="actions-section">
|
||||||
|
<button id="run-btn">Update Now</button>
|
||||||
|
<div id="run-msg" class="msg hidden"></div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section id="running-indicator" class="running-indicator hidden">
|
||||||
|
<div class="spinner"></div>
|
||||||
|
<span id="current-step">Initializing...</span>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="status-section">
|
||||||
|
<h2>Status</h2>
|
||||||
|
<div class="status-grid">
|
||||||
|
<div class="status-card" id="novavps-card">
|
||||||
|
<h3>Novavps</h3>
|
||||||
|
<div class="status-badge" id="novavps-status">idle</div>
|
||||||
|
<div class="status-detail" id="novavps-detail"></div>
|
||||||
|
<div class="status-time" id="novavps-time"></div>
|
||||||
|
</div>
|
||||||
|
<div class="status-card" id="xray1-card">
|
||||||
|
<h3>Xray 1</h3>
|
||||||
|
<div class="status-badge" id="xray1-status">idle</div>
|
||||||
|
<div class="status-detail" id="xray1-detail"></div>
|
||||||
|
<div class="status-time" id="xray1-time"></div>
|
||||||
|
</div>
|
||||||
|
<div class="status-card" id="xray2-card">
|
||||||
|
<h3>Xray 2</h3>
|
||||||
|
<div class="status-badge" id="xray2-status">idle</div>
|
||||||
|
<div class="status-detail" id="xray2-detail"></div>
|
||||||
|
<div class="status-time" id="xray2-time"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
|
||||||
|
<section class="last-run-section">
|
||||||
|
<h2>Last Run</h2>
|
||||||
|
<div id="last-run">Never</div>
|
||||||
|
</section>
|
||||||
|
</main>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script src="/app.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
298
public/style.css
Normal file
298
public/style.css
Normal file
@@ -0,0 +1,298 @@
|
|||||||
|
* {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
box-sizing: border-box;
|
||||||
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
|
||||||
|
background: #0f1117;
|
||||||
|
color: #e1e4e8;
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
.screen {
|
||||||
|
min-height: 100vh;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hidden {
|
||||||
|
display: none !important;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Login */
|
||||||
|
#login-screen {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-box {
|
||||||
|
background: #161b22;
|
||||||
|
padding: 2rem;
|
||||||
|
border-radius: 12px;
|
||||||
|
width: 100%;
|
||||||
|
max-width: 360px;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-box h1 {
|
||||||
|
text-align: center;
|
||||||
|
margin-bottom: 1.5rem;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group {
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 0.4rem;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
color: #8b949e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group input {
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.6rem 0.8rem;
|
||||||
|
background: #0d1117;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
border-radius: 6px;
|
||||||
|
color: #e1e4e8;
|
||||||
|
font-size: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.form-group input:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #58a6ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-box button {
|
||||||
|
width: 100%;
|
||||||
|
padding: 0.7rem;
|
||||||
|
background: #238636;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 1rem;
|
||||||
|
cursor: pointer;
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.login-box button:hover {
|
||||||
|
background: #2ea043;
|
||||||
|
}
|
||||||
|
|
||||||
|
.error {
|
||||||
|
color: #f85149;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
margin-top: 0.75rem;
|
||||||
|
text-align: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Dashboard */
|
||||||
|
header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
|
padding: 1rem 2rem;
|
||||||
|
background: #161b22;
|
||||||
|
border-bottom: 1px solid #30363d;
|
||||||
|
}
|
||||||
|
|
||||||
|
header h1 {
|
||||||
|
font-size: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
#logout-btn {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
background: transparent;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
border-radius: 6px;
|
||||||
|
color: #8b949e;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#logout-btn:hover {
|
||||||
|
border-color: #f85149;
|
||||||
|
color: #f85149;
|
||||||
|
}
|
||||||
|
|
||||||
|
main {
|
||||||
|
padding: 2rem;
|
||||||
|
max-width: 900px;
|
||||||
|
margin: 0 auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
section {
|
||||||
|
margin-bottom: 2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
section h2 {
|
||||||
|
font-size: 1rem;
|
||||||
|
color: #8b949e;
|
||||||
|
margin-bottom: 1rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.05em;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Config */
|
||||||
|
.config-row {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.75rem;
|
||||||
|
align-items: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-row label {
|
||||||
|
font-size: 0.875rem;
|
||||||
|
color: #8b949e;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-row input {
|
||||||
|
flex: 1;
|
||||||
|
padding: 0.5rem 0.75rem;
|
||||||
|
background: #0d1117;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
border-radius: 6px;
|
||||||
|
color: #e1e4e8;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-row input:focus {
|
||||||
|
outline: none;
|
||||||
|
border-color: #58a6ff;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-row button {
|
||||||
|
padding: 0.5rem 1rem;
|
||||||
|
background: #1f6feb;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
cursor: pointer;
|
||||||
|
white-space: nowrap;
|
||||||
|
}
|
||||||
|
|
||||||
|
.config-row button:hover {
|
||||||
|
background: #388bfd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Actions */
|
||||||
|
#run-btn {
|
||||||
|
padding: 0.75rem 1.5rem;
|
||||||
|
background: #238636;
|
||||||
|
color: #fff;
|
||||||
|
border: none;
|
||||||
|
border-radius: 6px;
|
||||||
|
font-size: 1rem;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#run-btn:hover:not(:disabled) {
|
||||||
|
background: #2ea043;
|
||||||
|
}
|
||||||
|
|
||||||
|
#run-btn:disabled {
|
||||||
|
opacity: 0.5;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg {
|
||||||
|
margin-top: 0.5rem;
|
||||||
|
font-size: 0.875rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg.success {
|
||||||
|
color: #3fb950;
|
||||||
|
}
|
||||||
|
|
||||||
|
.msg.error {
|
||||||
|
color: #f85149;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Running indicator */
|
||||||
|
.running-indicator {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.75rem;
|
||||||
|
padding: 1rem;
|
||||||
|
background: #161b22;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
border-radius: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.spinner {
|
||||||
|
width: 20px;
|
||||||
|
height: 20px;
|
||||||
|
border: 2px solid #30363d;
|
||||||
|
border-top-color: #58a6ff;
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: spin 0.8s linear infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
to { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Status grid */
|
||||||
|
.status-grid {
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||||
|
gap: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-card {
|
||||||
|
background: #161b22;
|
||||||
|
border: 1px solid #30363d;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-card h3 {
|
||||||
|
font-size: 0.875rem;
|
||||||
|
color: #8b949e;
|
||||||
|
margin-bottom: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge {
|
||||||
|
display: inline-block;
|
||||||
|
padding: 0.25rem 0.5rem;
|
||||||
|
border-radius: 4px;
|
||||||
|
font-size: 0.75rem;
|
||||||
|
font-weight: 600;
|
||||||
|
text-transform: uppercase;
|
||||||
|
margin-bottom: 0.5rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge.idle {
|
||||||
|
background: #30363d;
|
||||||
|
color: #8b949e;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge.success {
|
||||||
|
background: #122d1a;
|
||||||
|
color: #3fb950;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-badge.error {
|
||||||
|
background: #2d1215;
|
||||||
|
color: #f85149;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-detail {
|
||||||
|
font-size: 0.875rem;
|
||||||
|
color: #c9d1d9;
|
||||||
|
margin-bottom: 0.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-time {
|
||||||
|
font-size: 0.75rem;
|
||||||
|
color: #484f58;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Last run */
|
||||||
|
#last-run {
|
||||||
|
font-size: 0.875rem;
|
||||||
|
color: #c9d1d9;
|
||||||
|
}
|
||||||
97
src/data-store.ts
Normal file
97
src/data-store.ts
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import path from "path";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
import { existsSync } from "fs";
|
||||||
|
|
||||||
|
const DATA_DIR = path.resolve(__dirname, "..", "data");
|
||||||
|
|
||||||
|
export interface AppConfig {
|
||||||
|
novavpsUrl: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PanelStatus {
|
||||||
|
status: "idle" | "success" | "error";
|
||||||
|
count?: number;
|
||||||
|
outboundsCount?: number;
|
||||||
|
synced?: boolean;
|
||||||
|
timestamp: string | null;
|
||||||
|
error: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface AppState {
|
||||||
|
lastRun: string | null;
|
||||||
|
status: "idle" | "running" | "completed" | "error";
|
||||||
|
currentStep: string;
|
||||||
|
novavps: PanelStatus;
|
||||||
|
xray1: PanelStatus;
|
||||||
|
xray2: PanelStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CONFIG_FILE = path.join(DATA_DIR, "config.json");
|
||||||
|
const STATE_FILE = path.join(DATA_DIR, "state.json");
|
||||||
|
|
||||||
|
const defaultConfig: AppConfig = {
|
||||||
|
novavpsUrl: "",
|
||||||
|
};
|
||||||
|
|
||||||
|
const defaultState: AppState = {
|
||||||
|
lastRun: null,
|
||||||
|
status: "idle",
|
||||||
|
currentStep: "",
|
||||||
|
novavps: { status: "idle", count: 0, timestamp: null, error: null },
|
||||||
|
xray1: { status: "idle", outboundsCount: 0, synced: false, timestamp: null, error: null },
|
||||||
|
xray2: { status: "idle", outboundsCount: 0, synced: false, timestamp: null, error: null },
|
||||||
|
};
|
||||||
|
|
||||||
|
async function ensureDataDir(): Promise<void> {
|
||||||
|
await fs.mkdir(DATA_DIR, { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function readJson<T>(filePath: string, defaults: T): Promise<T> {
|
||||||
|
if (!existsSync(filePath)) return defaults;
|
||||||
|
try {
|
||||||
|
const raw = await fs.readFile(filePath, "utf-8");
|
||||||
|
return JSON.parse(raw) as T;
|
||||||
|
} catch {
|
||||||
|
return defaults;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function writeJson<T>(filePath: string, data: T): Promise<void> {
|
||||||
|
await ensureDataDir();
|
||||||
|
await fs.writeFile(filePath, JSON.stringify(data, null, 2), "utf-8");
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadConfig(envNovavpsUrl: string): Promise<AppConfig> {
|
||||||
|
const saved = await readJson<AppConfig>(CONFIG_FILE, defaultConfig);
|
||||||
|
if (!saved.novavpsUrl && envNovavpsUrl) {
|
||||||
|
saved.novavpsUrl = envNovavpsUrl;
|
||||||
|
await writeJson(CONFIG_FILE, saved);
|
||||||
|
}
|
||||||
|
return saved;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveConfig(config: AppConfig): Promise<void> {
|
||||||
|
await writeJson(CONFIG_FILE, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadState(): Promise<AppState> {
|
||||||
|
return readJson<AppState>(STATE_FILE, defaultState);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function saveState(state: AppState): Promise<void> {
|
||||||
|
await writeJson(STATE_FILE, state);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function resetState(): Promise<AppState> {
|
||||||
|
const now = new Date().toISOString();
|
||||||
|
const state: AppState = {
|
||||||
|
lastRun: now,
|
||||||
|
status: "running",
|
||||||
|
currentStep: "initializing",
|
||||||
|
novavps: { status: "idle", count: 0, timestamp: null, error: null },
|
||||||
|
xray1: { status: "idle", outboundsCount: 0, synced: false, timestamp: null, error: null },
|
||||||
|
xray2: { status: "idle", outboundsCount: 0, synced: false, timestamp: null, error: null },
|
||||||
|
};
|
||||||
|
await saveState(state);
|
||||||
|
return state;
|
||||||
|
}
|
||||||
316
src/server.ts
Normal file
316
src/server.ts
Normal file
@@ -0,0 +1,316 @@
|
|||||||
|
import dotenv from "dotenv";
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
import express, { Request, Response, NextFunction } from "express";
|
||||||
|
import cookieParser from "cookie-parser";
|
||||||
|
import crypto from "crypto";
|
||||||
|
import path from "path";
|
||||||
|
import fs from "fs/promises";
|
||||||
|
|
||||||
|
import { parseNovavps, getValidConnections } from "./novavps";
|
||||||
|
import { parseXrayPanel, syncNovavpsToXray } from "./xray-panel";
|
||||||
|
import { closeBrowser } from "./browser";
|
||||||
|
import {
|
||||||
|
loadConfig,
|
||||||
|
saveConfig,
|
||||||
|
loadState,
|
||||||
|
saveState,
|
||||||
|
resetState,
|
||||||
|
AppState,
|
||||||
|
} from "./data-store";
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const PORT = parseInt(process.env.PORT || "3000", 10);
|
||||||
|
|
||||||
|
const SESSION_SECRET = process.env.SESSION_SECRET || crypto.randomBytes(32).toString("hex");
|
||||||
|
const SESSION_TTL = 60 * 60 * 1000; // 1 hour
|
||||||
|
|
||||||
|
const sessions = new Map<string, { createdAt: number; lastAccess: number }>();
|
||||||
|
|
||||||
|
app.use(express.json({ limit: "1mb" }));
|
||||||
|
app.use(cookieParser());
|
||||||
|
app.use(express.static(path.resolve(__dirname, "..", "public")));
|
||||||
|
|
||||||
|
interface PanelConfig {
|
||||||
|
url: string;
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPanels(): PanelConfig[] {
|
||||||
|
const panels: PanelConfig[] = [];
|
||||||
|
const url1 = process.env.XRAY_URL;
|
||||||
|
const user1 = process.env.XRAY_USERNAME;
|
||||||
|
const pass1 = process.env.XRAY_PASSWORD;
|
||||||
|
if (url1 && user1 && pass1) {
|
||||||
|
panels.push({ url: url1, username: user1, password: pass1, label: "xray1" });
|
||||||
|
}
|
||||||
|
const url2 = process.env.XRAY_URL2;
|
||||||
|
const user2 = process.env.XRAY_USERNAME2;
|
||||||
|
const pass2 = process.env.XRAY_PASSWORD2;
|
||||||
|
if (url2 && user2 && pass2) {
|
||||||
|
panels.push({ url: url2, username: user2, password: pass2, label: "xray2" });
|
||||||
|
}
|
||||||
|
return panels;
|
||||||
|
}
|
||||||
|
|
||||||
|
function validCredentials(username: string, password: string): boolean {
|
||||||
|
const panels = getPanels();
|
||||||
|
return panels.some(
|
||||||
|
(p) => p.username === username && p.password === password
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSession(): string {
|
||||||
|
const token = crypto.randomBytes(32).toString("hex");
|
||||||
|
const now = Date.now();
|
||||||
|
sessions.set(token, { createdAt: now, lastAccess: now });
|
||||||
|
return token;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSession(token: string | undefined): { createdAt: number; lastAccess: number } | null {
|
||||||
|
if (!token) return null;
|
||||||
|
const session = sessions.get(token);
|
||||||
|
if (!session) return null;
|
||||||
|
if (Date.now() - session.lastAccess > SESSION_TTL) {
|
||||||
|
sessions.delete(token);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
session.lastAccess = Date.now();
|
||||||
|
return session;
|
||||||
|
}
|
||||||
|
|
||||||
|
function authMiddleware(req: Request, res: Response, next: NextFunction) {
|
||||||
|
const token = req.cookies?.session;
|
||||||
|
if (!getSession(token)) {
|
||||||
|
res.status(401).json({ error: "Unauthorized" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
next();
|
||||||
|
}
|
||||||
|
|
||||||
|
function cleanupSessions() {
|
||||||
|
const now = Date.now();
|
||||||
|
for (const [token, session] of sessions.entries()) {
|
||||||
|
if (now - session.lastAccess > SESSION_TTL) {
|
||||||
|
sessions.delete(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setInterval(cleanupSessions, 5 * 60 * 1000);
|
||||||
|
|
||||||
|
let isRunning = false;
|
||||||
|
|
||||||
|
async function runParser(): Promise<void> {
|
||||||
|
if (isRunning) return;
|
||||||
|
isRunning = true;
|
||||||
|
|
||||||
|
const state = await resetState();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const config = await loadConfig(process.env.NOVAVPS_URL || "");
|
||||||
|
const novavpsUrl = config.novavpsUrl;
|
||||||
|
|
||||||
|
if (!novavpsUrl) {
|
||||||
|
state.status = "error";
|
||||||
|
state.currentStep = "novavps_url_missing";
|
||||||
|
await saveState(state);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const panels = getPanels();
|
||||||
|
if (panels.length === 0) {
|
||||||
|
state.status = "error";
|
||||||
|
state.currentStep = "no_xray_panels";
|
||||||
|
await saveState(state);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
state.currentStep = "novavps_fetching";
|
||||||
|
await saveState(state);
|
||||||
|
|
||||||
|
let novavpsConnections: Awaited<ReturnType<typeof parseNovavps>> = [];
|
||||||
|
try {
|
||||||
|
novavpsConnections = await parseNovavps(novavpsUrl);
|
||||||
|
state.novavps = {
|
||||||
|
status: "success",
|
||||||
|
count: novavpsConnections.length,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
} catch (err) {
|
||||||
|
state.novavps = {
|
||||||
|
status: "error",
|
||||||
|
count: 0,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
await saveState(state);
|
||||||
|
|
||||||
|
const valid = getValidConnections(novavpsConnections);
|
||||||
|
|
||||||
|
for (const panel of panels) {
|
||||||
|
state.currentStep = `${panel.label}_parsing`;
|
||||||
|
await saveState(state);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const xray = await parseXrayPanel(panel.url, panel.username, panel.password);
|
||||||
|
if (panel.label === "xray1") {
|
||||||
|
state.xray1 = {
|
||||||
|
status: "success",
|
||||||
|
outboundsCount: xray.length,
|
||||||
|
synced: false,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
state.xray2 = {
|
||||||
|
status: "success",
|
||||||
|
outboundsCount: xray.length,
|
||||||
|
synced: false,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
error: null,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const errorState = {
|
||||||
|
status: "error" as const,
|
||||||
|
outboundsCount: 0,
|
||||||
|
synced: false,
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
error: err instanceof Error ? err.message : String(err),
|
||||||
|
};
|
||||||
|
if (panel.label === "xray1") {
|
||||||
|
state.xray1 = errorState;
|
||||||
|
} else {
|
||||||
|
state.xray2 = errorState;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await saveState(state);
|
||||||
|
|
||||||
|
if (valid.length > 0) {
|
||||||
|
state.currentStep = `${panel.label}_syncing`;
|
||||||
|
await saveState(state);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await syncNovavpsToXray(valid, panel.url, panel.username, panel.password, panel.label);
|
||||||
|
if (panel.label === "xray1") {
|
||||||
|
state.xray1.synced = true;
|
||||||
|
} else {
|
||||||
|
state.xray2.synced = true;
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
const errMsg = err instanceof Error ? err.message : String(err);
|
||||||
|
if (panel.label === "xray1") {
|
||||||
|
state.xray1.error = errMsg;
|
||||||
|
} else {
|
||||||
|
state.xray2.error = errMsg;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await saveState(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
state.status = "completed";
|
||||||
|
state.currentStep = "done";
|
||||||
|
await saveState(state);
|
||||||
|
} catch (err) {
|
||||||
|
state.status = "error";
|
||||||
|
state.currentStep = "fatal";
|
||||||
|
if (state.novavps.status === "idle") {
|
||||||
|
state.novavps = { status: "error", count: 0, timestamp: new Date().toISOString(), error: err instanceof Error ? err.message : String(err) };
|
||||||
|
}
|
||||||
|
await saveState(state);
|
||||||
|
} finally {
|
||||||
|
await closeBrowser();
|
||||||
|
isRunning = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
app.post("/api/login", async (req: Request, res: Response) => {
|
||||||
|
const { username, password } = req.body;
|
||||||
|
if (!username || !password) {
|
||||||
|
res.status(400).json({ error: "username and password required" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!validCredentials(username, password)) {
|
||||||
|
res.status(401).json({ error: "Invalid credentials" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const token = createSession();
|
||||||
|
res.cookie("session", token, {
|
||||||
|
httpOnly: true,
|
||||||
|
sameSite: "strict",
|
||||||
|
maxAge: SESSION_TTL,
|
||||||
|
path: "/",
|
||||||
|
});
|
||||||
|
res.json({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/api/logout", (_req: Request, res: Response) => {
|
||||||
|
res.clearCookie("session");
|
||||||
|
res.json({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/api/status", authMiddleware, async (_req: Request, res: Response) => {
|
||||||
|
const state = await loadState();
|
||||||
|
res.json(state);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.post("/api/run", authMiddleware, async (_req: Request, res: Response) => {
|
||||||
|
if (isRunning) {
|
||||||
|
res.status(409).json({ error: "Parser already running" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
runParser().catch((err) => console.error("[server] Parser error:", err));
|
||||||
|
res.json({ ok: true, message: "Parser started" });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/api/config", authMiddleware, async (_req: Request, res: Response) => {
|
||||||
|
const config = await loadConfig(process.env.NOVAVPS_URL || "");
|
||||||
|
res.json(config);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.put("/api/config", authMiddleware, async (req: Request, res: Response) => {
|
||||||
|
const { novavpsUrl } = req.body;
|
||||||
|
if (!novavpsUrl) {
|
||||||
|
res.status(400).json({ error: "novavpsUrl required" });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const config = await loadConfig(process.env.NOVAVPS_URL || "");
|
||||||
|
config.novavpsUrl = novavpsUrl;
|
||||||
|
await saveConfig(config);
|
||||||
|
res.json({ ok: true });
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/api/data/novavps", authMiddleware, async (_req: Request, res: Response) => {
|
||||||
|
const filePath = path.resolve(__dirname, "..", "data", "novavps-connections.json");
|
||||||
|
try {
|
||||||
|
const raw = await fs.readFile(filePath, "utf-8");
|
||||||
|
res.json(JSON.parse(raw));
|
||||||
|
} catch {
|
||||||
|
res.json([]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
app.get("/api/data/xray", authMiddleware, async (_req: Request, res: Response) => {
|
||||||
|
const filePath = path.resolve(__dirname, "..", "data", "xray-outbounds.json");
|
||||||
|
try {
|
||||||
|
const raw = await fs.readFile(filePath, "utf-8");
|
||||||
|
res.json(JSON.parse(raw));
|
||||||
|
} catch {
|
||||||
|
res.json([]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
const config = await loadConfig(process.env.NOVAVPS_URL || "");
|
||||||
|
|
||||||
|
app.listen(PORT, "0.0.0.0", () => {
|
||||||
|
console.log(`[server] Web interface running on http://0.0.0.0:${PORT}`);
|
||||||
|
console.log(`[server] NOVAVPS_URL: ${config.novavpsUrl || "(not set)"}`);
|
||||||
|
console.log(`[server] Panels: ${getPanels().map((p) => p.label).join(", ") || "(none)"}`);
|
||||||
|
});
|
||||||
|
})();
|
||||||
Reference in New Issue
Block a user