| const express = require("express"); |
| const fs = require("fs"); |
|
|
| const app = express(); |
| app.use(express.json()); |
|
|
| |
| |
| |
| const PORT = process.env.PORT || 5900; |
|
|
| |
| |
| |
| let regions = [ |
| { code: "us", country: "USA", city: "New York", node: "us1" }, |
| { code: "de", country: "Germany", city: "Frankfurt", node: "de1" } |
| ]; |
|
|
| let session = null; |
|
|
| |
| |
| |
| 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 |
| }); |
| }); |
|
|
| |
| |
| |
| app.get("/regions", (req, res) => { |
| res.json({ success: true, regions }); |
| }); |
|
|
| |
| |
| |
| 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 }); |
| }); |
|
|
| |
| |
| |
| app.post("/disconnect", (req, res) => { |
| session = null; |
| res.json({ success: true, message: "VPN Disconnected" }); |
| }); |
|
|
| |
| |
| |
| app.get("/status", (req, res) => { |
| res.json(session || { connected: false }); |
| }); |
|
|
| |
| |
| |
| 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" |
| }); |
| }); |
|
|
| |
| |
| |
| app.listen(PORT, () => { |
| console.log("server running on http://127.0.0.1:5900"); |
| }); |