forked from snxraven/ravenscott-blog
update
This commit is contained in:
parent
fd20b088e4
commit
c9dd063557
@ -428,7 +428,7 @@ if (myArgs[4] === "SSH42113405732790") {
|
|||||||
Throughout the process, the script logs important events, such as when a new order is received or when a container is successfully deployed. These logs are written both to the console and to a log file (`orders_generated.log`). This ensures that administrators can monitor the deployment process and track potential issues.
|
Throughout the process, the script logs important events, such as when a new order is received or when a container is successfully deployed. These logs are written both to the console and to a log file (`orders_generated.log`). This ensures that administrators can monitor the deployment process and track potential issues.
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
log('------New Order Received--------', 'orders_generated.log');
|
log('New Order Received--', 'orders_generated.log');
|
||||||
log('Container Created Successfully', 'orders_generated.log');
|
log('Container Created Successfully', 'orders_generated.log');
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -679,7 +679,7 @@ This safeguard prevents users from accidentally deleting important containers or
|
|||||||
|
|
||||||
# Executing Shell Commands Using Discord Bot
|
# Executing Shell Commands Using Discord Bot
|
||||||
|
|
||||||
The platform’s ability to execute commands non-interactively via Discord is a game-changer for container management and remote server operations. This powerful feature lets users run commands, manage files, check system statuses, and more—all from a simple Discord interface, without needing further interaction after issuing a command. In this post, we will dive into how the system works, highlighting some special custom-coded commands like `cd`, `pwd`, and `ls`, and how the platform ensures non-interactive command execution.
|
The platform’s ability to execute commands non-interactively via Discord is a game-changer for container management and remote server operations. This powerful feature lets users run commands, manage files, check system statuses, and more—all from a simple Discord interface, without needing further interaction after issuing a command. We will dive into how the system works, highlighting some special custom-coded commands like `cd`, `pwd`, and `ls`, and how the platform ensures non-interactive command execution.
|
||||||
|
|
||||||
### Non-Interactive Command Execution Overview
|
### Non-Interactive Command Execution Overview
|
||||||
|
|
||||||
@ -1532,7 +1532,7 @@ With Holesail, you no longer need to rely on complex configurations or static IP
|
|||||||
|
|
||||||
Managing virtual hosts (VHOSTs) can be a complex and time-consuming process, requiring manual configurations, SSL certificate management, and network setup. By leveraging NGINX Proxy Manager's API and automating the process through a Discord bot, we've built a system that makes it easier than ever to create, update, list, and delete VHOSTs.
|
Managing virtual hosts (VHOSTs) can be a complex and time-consuming process, requiring manual configurations, SSL certificate management, and network setup. By leveraging NGINX Proxy Manager's API and automating the process through a Discord bot, we've built a system that makes it easier than ever to create, update, list, and delete VHOSTs.
|
||||||
|
|
||||||
This automation is powered by RESTful API requests to NGINX Proxy Manager, combined with user inputs via Discord. In this post, we will walk through how the system is structured, and how each VHOST command is executed through a combination of Discord and API interaction.
|
This automation is powered by RESTful API requests to NGINX Proxy Manager, combined with user inputs via Discord. We will walk through how the system is structured, and how each VHOST command is executed through a combination of Discord and API interaction.
|
||||||
|
|
||||||
### What is a VHOST?
|
### What is a VHOST?
|
||||||
|
|
||||||
@ -1802,7 +1802,6 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### REST Request for VHOST Deletion
|
### REST Request for VHOST Deletion
|
||||||
|
|
||||||
The following request is made to delete a VHOST:
|
The following request is made to delete a VHOST:
|
||||||
@ -1818,6 +1817,187 @@ By integrating Discord bots with the NGINX Proxy Manager API, we’ve created a
|
|||||||
|
|
||||||
Whether you’re hosting multiple domains or managing SSL certificates, the combination of Discord and NGINX Proxy Manager provides an intuitive, automated solution that makes managing web services easier than ever.
|
Whether you’re hosting multiple domains or managing SSL certificates, the combination of Discord and NGINX Proxy Manager provides an intuitive, automated solution that makes managing web services easier than ever.
|
||||||
|
|
||||||
|
|
||||||
|
### Creating a Custom Discord Notification Service
|
||||||
|
|
||||||
|
As our platform continued to evolve, we've introduced a powerful notification service that allows for direct messaging and alerts to be sent to users through Discord. Whether it’s for system alerts, status updates, or user-triggered notifications, this service provides seamless and instant communication between the platform and its users.
|
||||||
|
|
||||||
|
We’ll break down the architecture, explain how it works, and provide insight into both the server and client components of this notification system.
|
||||||
|
|
||||||
|
### The Architecture of the Notification System
|
||||||
|
|
||||||
|
At the core of our notification system are two components:
|
||||||
|
|
||||||
|
1. **Server-Side API**: A Node.js-based service that listens for HTTP requests and processes messages to be sent via Discord.
|
||||||
|
2. **Client Application**: A Go-based client that can send custom notifications to the platform users.
|
||||||
|
|
||||||
|
These components work together to deliver notifications from the platform to the user’s Discord account with minimal delay. Let's break down each part.
|
||||||
|
|
||||||
|
### Server-Side: The Notification API
|
||||||
|
|
||||||
|
The server-side of the notification service is built using Node.js with Express.js and `discord.js` for interfacing with Discord. It listens for HTTP requests containing the necessary parameters, verifies them, and then sends the appropriate message via Discord.
|
||||||
|
|
||||||
|
#### Key Components of the API
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var fs = require('fs');
|
||||||
|
var http = require('http');
|
||||||
|
var express = require('express');
|
||||||
|
var app = express();
|
||||||
|
const Discord = require('discord.js');
|
||||||
|
const { Client, Intents } = require('discord.js');
|
||||||
|
const client = new Client({ intents: 4097 });
|
||||||
|
const mysql = require('mysql2');
|
||||||
|
```
|
||||||
|
|
||||||
|
Here, we initialize several modules:
|
||||||
|
- **Express.js**: Handles incoming HTTP requests.
|
||||||
|
- **discord.js**: Manages communication with Discord, allowing us to send messages.
|
||||||
|
- **mysql2**: Connects to the MySQL database to fetch user details like Discord IDs.
|
||||||
|
|
||||||
|
#### MySQL Database Connection
|
||||||
|
|
||||||
|
The service uses MySQL to store user information, such as their Discord ID, which is crucial for sending direct messages.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
const connection = mysql.createConnection({
|
||||||
|
host: 'localhost',
|
||||||
|
user: 'myUser',
|
||||||
|
database: 'myDatabase',
|
||||||
|
password: 'myPassword'
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
This connection is essential as it allows the API to look up the user in the database using a unique identifier (hostname) provided by the client.
|
||||||
|
|
||||||
|
#### Handling Notification Requests
|
||||||
|
|
||||||
|
The `/` endpoint of the API is where the magic happens. When the client sends a request, the server processes the message and sends it to the appropriate Discord user.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
app.get('/', async (req, res) => {
|
||||||
|
const key = req.query.key;
|
||||||
|
const sshID = req.query.hostname;
|
||||||
|
const messageToSend = req.query.message;
|
||||||
|
|
||||||
|
// Your IP check logic here
|
||||||
|
|
||||||
|
if (key !== "KEYISHEREMAKEONE") {
|
||||||
|
console.log("Invalid key");
|
||||||
|
return res.end("Sorry, that did not work....");
|
||||||
|
}
|
||||||
|
|
||||||
|
connection.query(
|
||||||
|
"SELECT discord_id FROM users WHERE uid = ?",
|
||||||
|
[sshID],
|
||||||
|
function (err, results, fields) {
|
||||||
|
if (err) {
|
||||||
|
console.error("Error fetching Discord ID:", err);
|
||||||
|
return res.end("Sorry, something went wrong....");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (results.length === 0) {
|
||||||
|
console.log("User does not exist in database");
|
||||||
|
return res.end("User not found....");
|
||||||
|
}
|
||||||
|
|
||||||
|
const discordID = results[0].discord_id;
|
||||||
|
|
||||||
|
client.users.fetch(discordID).then((user) => {
|
||||||
|
user.send(messageToSend)
|
||||||
|
.then(() => {
|
||||||
|
console.log("Message sent to Discord user");
|
||||||
|
res.end("Message sent successfully!");
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error("Error sending message to Discord user:", err);
|
||||||
|
res.end("Error sending message to Discord user....");
|
||||||
|
});
|
||||||
|
}).catch((err) => {
|
||||||
|
console.error("Error fetching Discord user:", err);
|
||||||
|
res.end("Error fetching Discord user....");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
Here’s a breakdown:
|
||||||
|
1. **Key Verification**: The server checks the provided key to ensure that only authorized requests are processed.
|
||||||
|
2. **Database Lookup**: It queries the MySQL database to find the Discord ID associated with the provided `hostname`.
|
||||||
|
3. **Sending the Message**: Once the Discord ID is retrieved, the bot sends the message to the user via Discord.
|
||||||
|
4. **Error Handling**: If any issues arise during this process (e.g., invalid key, missing user, or Discord message failure), they are logged and returned to the client.
|
||||||
|
|
||||||
|
#### Discord Bot Initialization
|
||||||
|
|
||||||
|
To interact with users on Discord, we initialize a bot using the `discord.js` library.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
client.on('ready', async () => {
|
||||||
|
console.log('Bot is logged in and ready!');
|
||||||
|
});
|
||||||
|
|
||||||
|
client.login('DISCORD_BOT_TOKEN');
|
||||||
|
```
|
||||||
|
|
||||||
|
This code ensures the bot is connected to Discord and ready to send messages when the server processes a notification request.
|
||||||
|
|
||||||
|
### Client-Side: Sending Notifications
|
||||||
|
|
||||||
|
The client-side is written in Go and allows users to send notifications via a simple command-line interface. This script retrieves the server’s hostname and sends a GET request to the API along with the message content using `Golang`
|
||||||
|
|
||||||
|
#### Go Client Code
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"net/url"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
name, err := os.Hostname()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
argsWithoutProg := os.Args[1:]
|
||||||
|
msg := strings.Join(argsWithoutProg, " ")
|
||||||
|
|
||||||
|
resp, err := http.Get("https://api.yourdomain.com/?key=YOURKEYHERE&hostname=" + name + "&message=" + url.QueryEscape(msg))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalln(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sb := string(body)
|
||||||
|
log.Printf(sb)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### How It Works:
|
||||||
|
1. **Fetch Hostname**: The script retrieves the server's hostname using `os.Hostname()`, which is later sent as part of the request to identify the sender.
|
||||||
|
2. **Build Message**: The message is constructed from command-line arguments and passed into the API request.
|
||||||
|
3. **Send Notification**: The client sends a GET request to the server, including the API key, hostname, and message content.
|
||||||
|
|
||||||
|
### Use Cases
|
||||||
|
|
||||||
|
The notification system can be extended for various purposes:
|
||||||
|
- **System Alerts**: Notify users when their containers need attention or when system events occur.
|
||||||
|
- **User Reminders**: Users can send themselves reminders or updates.
|
||||||
|
- **Platform Announcements**: The system can send platform-wide announcements or alerts.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# My Final Thoughts
|
# My Final Thoughts
|
||||||
|
|
||||||
**Discord-Linux** is a transformative tool that merges the best of both worlds: the simplicity of Discord's chat interface with the powerful capabilities of Linux containerization. By doing so, it offers an intuitive yet robust platform for developers, system administrators, hobbyists, and tech enthusiasts to create, manage, and interact with containers in real-time, directly from a Discord server. This innovative fusion opens up a new realm of possibilities for container management by making it accessible to a broader audience, many of whom may not be familiar with the complexities of traditional container orchestration platforms like Docker.
|
**Discord-Linux** is a transformative tool that merges the best of both worlds: the simplicity of Discord's chat interface with the powerful capabilities of Linux containerization. By doing so, it offers an intuitive yet robust platform for developers, system administrators, hobbyists, and tech enthusiasts to create, manage, and interact with containers in real-time, directly from a Discord server. This innovative fusion opens up a new realm of possibilities for container management by making it accessible to a broader audience, many of whom may not be familiar with the complexities of traditional container orchestration platforms like Docker.
|
||||||
|
Loading…
Reference in New Issue
Block a user