-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from CR072/main
Add server power state controller
- Loading branch information
Showing
4 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters