Skip to content

Commit

Permalink
Merge pull request #2 from CR072/main
Browse files Browse the repository at this point in the history
Add server power state controller
  • Loading branch information
ManucrackYT authored Dec 23, 2023
2 parents 88b0a7d + 7f546d6 commit 009cd6a
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ If you download from the [Releases](https://github.com/ManucrackYT/LapsusClient/

The best way to contact the developers is on Discord.

[![discord](https://discordapp.com/api/guilds/954138561509203978/embed.png?style=banner3)][discord]
[![discord](https://discordapp.com/api/guilds/954138561509203978/embed.png?style=banner3)](https://discord.com/invite/Xa3sm5fZKg)

---

Expand Down
113 changes: 113 additions & 0 deletions api/power.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const settings = require("../settings.json");
const fetch = require('node-fetch');

module.exports.load = async function(app, db) {
app.get("/api/servers/start", async (req, res) => {
if (!req.session.pterodactyl) return res.redirect("/login");
const id = req.query.id;
const user = req.query.user;

if (!id || !user) {
return;
}

if (user != req.session.userinfo.id) {
return res.json({ "success": false, "message": "Unauthorized request" });
}

try {
const response = await fetch(`${settings.pterodactyl.domain}/api/client/servers/${id}/power`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `Bearer ${settings.pterodactyl.account_key}`
},
body: JSON.stringify({ signal: "start" })
});

if (!response.ok) {
console.error(`Failed to start server. Status: ${response.status}`);
return res.json({ "success": false, "message": "Failed to start server" });
}

console.log("Server started successfully");
return res.json({ "success": true, "message": "Operation success!" });
} catch (error) {
console.error("Error during server start request:", error);
return res.json({ "success": false, "message": "Internal server error" });
}
});
app.get("/api/servers/restart", async (req, res) => {
if (!req.session.pterodactyl) return res.redirect("/login");
const id = req.query.id;
const user = req.query.user;

if (!id || !user) {
return;
}

if (user != req.session.userinfo.id) {
return res.json({ "success": false, "message": "Unauthorized request" });
}

try {
const response = await fetch(`${settings.pterodactyl.domain}/api/client/servers/${id}/power`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `Bearer ${settings.pterodactyl.account_key}`
},
body: JSON.stringify({ signal: "restart" })
});

if (!response.ok) {
console.error(`Failed to start server. Status: ${response.status}`);
return res.json({ "success": false, "message": "Failed to start server" });
}

console.log("Server started successfully");
return res.json({ "success": true, "message": "Operation success!" });
} catch (error) {
console.error("Error during server start request:", error);
return res.json({ "success": false, "message": "Internal server error" });
}
});
app.get("/api/servers/stop", async (req, res) => {
if (!req.session.pterodactyl) return res.redirect("/login");
const id = req.query.id;
const user = req.query.user;

if (!id || !user) {
return;
}

if (user != req.session.userinfo.id) {
return res.json({ "success": false, "message": "Unauthorized request" });
}

try {
const response = await fetch(`${settings.pterodactyl.domain}/api/client/servers/${id}/power`, {
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"Authorization": `Bearer ${settings.pterodactyl.account_key}`
},
body: JSON.stringify({ signal: "stop" })
});

if (!response.ok) {
console.error(`Failed to start server. Status: ${response.status}`);
return res.json({ "success": false, "message": "Failed to start server" });
}

console.log("Server started successfully");
return res.json({ "success": true, "message": "Operation success!" });
} catch (error) {
console.error("Error during server start request:", error);
return res.json({ "success": false, "message": "Internal server error" });
}
});
}
3 changes: 2 additions & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
},
"pterodactyl": {
"domain": "https://panel.yourdomain.com",
"key": "ptla_"
"key": "ptla_",
"account_key": ""
},
"linkvertise": {
"userid": "679241",
Expand Down
66 changes: 64 additions & 2 deletions themes/lapsus/servers.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@
</div>
<div class="mt-5 flex lg:ml-4 lg:mt-0">
<span class="sm:ml-3">
<a id="start<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>" onclick="start('<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>')" class="transition inline-flex items-center rounded-md bg-green-400 px-3 py-2 text-sm font-semibold text-white hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-rose-600">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M5.636 5.636a9 9 0 1 0 12.728 0M12 3v9" />
</svg>
Start
</a>
<a id="restart<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>" onclick="restart('<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>')" class="transition inline-flex items-center rounded-md bg-yellow-400 px-3 py-2 text-sm font-semibold text-white hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-rose-600">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99" />
</svg>
Restart
</a>
<a id="stop<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>" onclick="stop('<%= pterodactyl.relationships.servers.data[i].attributes.identifier %>')" class="transition inline-flex items-center rounded-md bg-red-500 px-3 py-2 text-sm font-semibold text-white hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-rose-600">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M11.412 15.655 9.75 21.75l3.745-4.012M9.257 13.5H3.75l2.659-2.849m2.048-2.194L14.25 2.25 12 10.5h8.25l-4.707 5.043M8.457 8.457 3 3m5.457 5.457 7.086 7.086m0 0L21 21" />
</svg>
Stop
</a>
<a href="../edit?id=<%= pterodactyl.relationships.servers.data[i].attributes.id %>" class="transition inline-flex items-center rounded-md bg-black-600 px-3 py-2 text-sm font-semibold text-white hover:bg-gray-700 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-rose-600">
<svg class="-ml-0.5 mr-1.5 h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M16.862 4.487l1.687-1.688a1.875 1.875 0 112.652 2.652L10.582 16.07a4.5 4.5 0 01-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 011.13-1.897l8.932-8.931zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0115.75 21H5.25A2.25 2.25 0 013 18.75V8.25A2.25 2.25 0 015.25 6H10" />
Expand All @@ -108,7 +126,51 @@
<svg class="-ml-0.5 mr-1.5 h-5 w-5" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" d="M14.74 9l-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 01-2.244 2.077H8.084a2.25 2.25 0 01-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 00-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 013.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 00-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 00-7.5 0" />
</svg> Delete
<script>
async function start(id) {
try {
let response = await fetch(`/api/servers/start?id=${id}&user=<%= userinfo.id %>`);
const data = await response.json();
if (data.success == true) {
document.getElementById(`start${id}`).textContent = "Started!";
} else {
console.error("Failed to start server:", data.message);
}
} catch (error) {
console.error("Error during server start request:", error);
}
}
async function restart(id) {
try {
let response = await fetch(`/api/servers/restart?id=${id}&user=<%= userinfo.id %>`);
const data = await response.json();
if (data.success == true) {
document.getElementById(`restart${id}`).textContent = "Restarted!";
} else {
console.error("Failed to start server:", data.message);
}
} catch (error) {
console.error("Error during server start request:", error);
}
}
async function stop(id) {
try {
let response = await fetch(`/api/servers/stop?id=${id}&user=<%= userinfo.id %>`)
const data = await response.json()
if (data.success == true) {
document.getElementById(`stop${id}`).textContent = "Stopped!";
} else {
console.error("Failed to stop server:", data.message);
}
} catch (error) {
console.error("Error during server start request:", error);
}
}
</script>
</a>
</span>
</div>
Expand Down Expand Up @@ -210,4 +272,4 @@
</main>
</div>
</div>
</body>
</body>

0 comments on commit 009cd6a

Please sign in to comment.