File size: 4,633 Bytes
99aa4fc | 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 | 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");
}); |