Jdbdb / server.js
Jsjsjj8383's picture
Create server.js
99aa4fc verified
Raw
History Blame Contribute Delete
4.63 kB
const express = require("express");
const fs = require("fs");
const app = express();
app.use(express.json());
/* =========================
PORT CONFIG
========================= */
const PORT = process.env.PORT || 5900;
/* =========================
VPN DATABASE (IN MEMORY)
========================= */
let regions = [
{ code: "us", country: "USA", city: "New York", node: "us1" },
{ code: "de", country: "Germany", city: "Frankfurt", node: "de1" }
];
let session = null;
/* =========================
CREATE REGION (/create)
========================= */
app.post("/create", (req, res) => {
const { code, country, city, node } = req.body;
if (!code || !country || !city || !node) {
return res.json({ success: false, message: "Missing fields" });
}
const exists = regions.find(r => r.code === code);
if (exists) {
return res.json({ success: false, message: "Region already exists" });
}
const newRegion = { code, country, city, node };
regions.push(newRegion);
res.json({
success: true,
message: "Region created",
data: newRegion
});
});
/* =========================
LIST REGIONS
========================= */
app.get("/regions", (req, res) => {
res.json({ success: true, regions });
});
/* =========================
CONNECT VPN
========================= */
app.post("/connect", (req, res) => {
const { code } = req.body;
const region = regions.find(r => r.code === code);
if (!region) {
return res.json({ success: false, message: "Region not found" });
}
session = {
connected: true,
region: region.code,
ip: `10.${Math.floor(Math.random()*255)}.${Math.floor(Math.random()*255)}.1`
};
res.json({ success: true, message: "VPN Connected", data: session });
});
/* =========================
DISCONNECT VPN
========================= */
app.post("/disconnect", (req, res) => {
session = null;
res.json({ success: true, message: "VPN Disconnected" });
});
/* =========================
STATUS
========================= */
app.get("/status", (req, res) => {
res.json(session || { connected: false });
});
/* =========================
GENERATE HTML FILE
========================= */
app.get("/generate-html", (req, res) => {
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>VPN Control Panel</title>
</head>
<body>
<h2>VPN Control Panel</h2>
<select id="regions"></select>
<button onclick="connect()">Connect</button>
<button onclick="disconnect()">Disconnect</button>
<button onclick="status()">Status</button>
<h3>Create Region</h3>
<input id="code" placeholder="code">
<input id="country" placeholder="country">
<input id="city" placeholder="city">
<input id="node" placeholder="node">
<button onclick="create()">Create</button>
<pre id="out"></pre>
<script>
const API = "http://127.0.0.1:${PORT}";
async function load() {
const res = await fetch(API + "/regions");
const data = await res.json();
const sel = document.getElementById("regions");
sel.innerHTML = "";
data.regions.forEach(r => {
const opt = document.createElement("option");
opt.value = r.code;
opt.textContent = r.country + " - " + r.city;
sel.appendChild(opt);
});
}
async function connect() {
const code = document.getElementById("regions").value;
const res = await fetch(API + "/connect", {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({ code })
});
document.getElementById("out").textContent = await res.text();
}
async function disconnect() {
const res = await fetch(API + "/disconnect", { method:"POST" });
document.getElementById("out").textContent = await res.text();
}
async function status() {
const res = await fetch(API + "/status");
document.getElementById("out").textContent = await res.text();
}
async function create() {
const body = {
code: document.getElementById("code").value,
country: document.getElementById("country").value,
city: document.getElementById("city").value,
node: document.getElementById("node").value
};
const res = await fetch(API + "/create", {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify(body)
});
document.getElementById("out").textContent = await res.text();
load();
}
load();
</script>
</body>
</html>
`;
fs.writeFileSync("client.html", htmlContent);
res.json({
success: true,
message: "client.html created successfully"
});
});
/* =========================
START SERVER
========================= */
app.listen(PORT, () => {
console.log("server running on http://127.0.0.1:5900");
});