-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
354 lines (287 loc) · 9.57 KB
/
main.go
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"database/sql"
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
_ "github.com/go-sql-driver/mysql"
"github.com/pmezard/go-difflib/difflib"
"golang.org/x/term"
)
func init() {
flag.BoolVar(&showUsage, "h", false, "Show usage information")
flag.BoolVar(&noColor, "no-color", false, "Disable colorized output")
flag.BoolVar(&showVersion, "version", false, "Show version information")
flag.Parse()
if showVersion {
printVersion()
os.Exit(0)
}
if showUsage {
flag.Usage()
os.Exit(0)
}
}
var (
version = "1.0.2"
green = "\033[32m"
red = "\033[31m"
yellow = "\033[33m"
reset = "\033[0m"
showUsage bool
noColor bool
showVersion bool
)
func main() {
printCopyright()
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)
go func() {
<-sigCh
// Handle cleanup or other tasks before exiting
fmt.Println("\nTerminating the application...")
os.Exit(0)
}()
for {
fmt.Println("Select an option:")
fmt.Println("1. Update the startup of already made servers to the egg startup")
fmt.Println("2. Stop stuck server transfers")
fmt.Println("3. Exit")
var choice int
fmt.Print("Enter your choice: ")
fmt.Scan(&choice)
// Consume the newline character
fmt.Scanln()
switch choice {
case 1:
fmt.Println("Selected: Update the startup of already made servers to the egg startup")
updateServerStartup()
case 2:
fmt.Println("Selected: Stop stuck server transfers")
stopStuckTransfers()
// Call your specific function for Option 4 here
case 3:
fmt.Println("Exiting the program.")
os.Exit(0)
default:
fmt.Println("Invalid choice. Please select a valid option.")
}
}
}
func connectToDatabase() (*sql.DB, error) {
// Prompt for MySQL connection details
host := GetUserInput("Enter MySQL host ", "127.0.0.1")
port := GetUserInput("Enter MySQL port ", "3306")
user := GetUserInput("Enter MySQL username ", "pterodactyl")
// Prompt for MySQL password
password := GetPasswordInput("Enter MySQL password: ")
// Prompt for Pterodactyl database name
pterodactylDBName := GetUserInput("Enter Pterodactyl database name ", "panel")
// Build the MySQL DSN (Data Source Name)
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", user, password, host, port, pterodactylDBName)
// Connect to MySQL
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("error connecting to MySQL: %v", err)
}
// Check if the connection is successful
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("error pinging MySQL server: %v", err)
}
if noColor {
fmt.Println("Connected to MySQL server successfully!")
} else {
printFormatted(green, "Connected to MySQL server successfully!\n")
}
return db, nil
}
func updateServerStartup(){
// Establish database connection
db, err := connectToDatabase()
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Prompt for egg ID
eggID := GetUserInput("Enter egg ID: ", "")
// Query the startup field for the specified egg ID from the eggs table
eggQuery := "SELECT startup FROM eggs WHERE id = ?"
var eggStartup string
err = db.QueryRow(eggQuery, eggID).Scan(&eggStartup)
if err != nil {
if err == sql.ErrNoRows {
printFormatted(red, "Egg ID not found\n")
return
} else {
log.Fatal("Error retrieving startup value from eggs table:", err)
}
}
fmt.Printf("\nStartup value for Egg ID %s: %s\n", eggID, eggStartup)
// Query servers with the specified egg ID
serverQuery := "SELECT uuidShort, name, startup FROM servers WHERE egg_id = ?"
rows, err := db.Query(serverQuery, eggID)
if err != nil {
log.Fatal("Error querying servers:", err)
}
defer rows.Close()
// Check if there are no servers for the specified egg ID
if !rows.Next() {
printFormatted(red, "No servers found for Egg ID %s\n", eggID)
return
}
fmt.Printf("Servers using Egg ID %s:\n\n", eggID)
for rows.Next() {
var uuidShort, name, serverStartup string
if err := rows.Scan(&uuidShort, &name, &serverStartup); err != nil {
log.Fatal("Error scanning row:", err)
}
// Compare startup value from eggs table with startup value from servers table
if eggStartup == serverStartup {
printFormatted(green, "UUID Short: %s, Name: %s, Startup Matches\n", uuidShort, name)
} else {
printFormatted(red, "UUID Short: %s, Name: %s, Startup Mismatch\n", uuidShort, name)
// Show the difference between eggStartup and serverStartup
diff := difflib.UnifiedDiff{
A: difflib.SplitLines(eggStartup),
B: difflib.SplitLines(serverStartup),
FromFile: "Egg Startup",
ToFile: "Server Startup",
Context: 3,
}
diffText, _ := difflib.GetUnifiedDiffString(diff)
printFormatted(yellow, "Difference:\n%s", diffText)
// Prompt the user to update the startup
fmt.Print("Do you want to update the startup for this server? (y/n/A): ")
var updateChoice string
fmt.Scanln(&updateChoice)
if updateChoice == "y" || updateChoice == "Y" {
// Update the startup for the server with the latest one from the egg
updateQuery := "UPDATE servers SET startup = ? WHERE uuidShort = ?"
_, err := db.Exec(updateQuery, eggStartup, uuidShort)
if err != nil {
log.Fatal("Error updating startup for server:", err)
}
printFormatted(green, "Startup updated for server with UUID Short %s\n", uuidShort)
} else if updateChoice == "A" || updateChoice == "a" {
// Update the startup for all subsequent servers
updateQuery := "UPDATE servers SET startup = ? WHERE egg_id = ? AND startup != ?"
_, err := db.Exec(updateQuery, eggStartup, eggID, eggStartup)
if err != nil {
log.Fatal("Error updating startup for servers:", err)
}
printFormatted(green, "Startup updated for all subsequent servers with Egg ID %s\n", eggID)
break // Exit the loop after updating all servers
}
}
}
// Exit the program after processing rows
os.Exit(0)
}
func stopStuckTransfers() {
// Establish database connection
db, err := connectToDatabase()
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Query for failed transfers
transfersQuery := "SELECT id, server_id FROM server_transfers WHERE successful IS NULL"
rows, err := db.Query(transfersQuery)
if err != nil {
log.Fatal("Error querying failed transfers:", err)
}
defer rows.Close()
// Check if there are no failed transfers
if !rows.Next() {
printFormatted(red, "No stuck transfers found\n")
return
}
fmt.Println("Stuck transfers found:\n")
for rows.Next() {
var transferID, serverID string
if err := rows.Scan(&transferID, &serverID); err != nil {
log.Fatal("Error scanning row:", err)
}
// Get server name from the servers table
serverNameQuery := "SELECT name FROM servers WHERE id = ?"
var serverName string
err := db.QueryRow(serverNameQuery, serverID).Scan(&serverName)
if err != nil {
log.Fatal("Error retrieving server name:", err)
}
// Display information about the stuck transfer
fmt.Printf("Transfer ID: %s\n", transferID)
fmt.Printf("Server ID: %s\n", serverID)
fmt.Printf("Server Name: %s\n", serverName)
// Prompt the user to remove the stuck transfer
fmt.Print("Do you want to remove this transfer? (y/n): ")
var removeChoice string
fmt.Scanln(&removeChoice)
if removeChoice == "y" || removeChoice == "Y" {
// Remove the stuck transfer entry
removeTransferQuery := "DELETE FROM server_transfers WHERE id = ?"
_, err := db.Exec(removeTransferQuery, transferID)
if err != nil {
log.Fatal("Error removing transfer:", err)
}
printFormatted(green, "Transfer removed\n")
}
printFormatted(green, "No more stuck transfers")
}
}
// printFormatted prints a formatted string with color (if enabled)
func printFormatted(colorCode, format string, a ...interface{}) {
if noColor {
fmt.Printf(format, a...)
} else {
fmt.Printf(colorCode+format+reset, a...)
}
}
// Print Copyright
func printCopyright() {
str := `
_______ __ __ ___ __ _ _______ _______ __ _ _______ __ __ ______ _______
| || | | || | | | | || || || | | || || | | || | | _ |
| _ || | | || | | |_| ||_ _|| ___|| |_| || _ || |_| || _ || | | |
| | | || |_| || | | | | | | |___ | || | | || || | | || | | |
| |_| || || | | _ | | | | ___|| _ || |_| || || |_| || |_| |
| | | || | | | | | | | | |___ | | | || | | | | || |
|____||_||_______||___| |_| |__| |___| |_______||_| |__||____||_| |___| |______| |_______|
`
fmt.Println(str)
}
// GetUserInput prompts the user for input with a given prompt and provides a default value.
func GetUserInput(prompt, defaultValue string) string {
fmt.Print(prompt)
// Set default value
if defaultValue != "" {
fmt.Printf("(default: %s): ", defaultValue)
}
// Read user input
var input string
fmt.Scanln(&input)
// Use default value if input is empty
if input == "" {
return defaultValue
}
return input
}
// GetPasswordInput prompts the user for a password without showing it on the screen.
func GetPasswordInput(prompt string) string {
fmt.Print(prompt)
// Read password without showing it
password, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
log.Fatal("Error reading password:", err)
}
// Print a new line after reading password
fmt.Println()
return string(password)
}
func printVersion() {
fmt.Printf("Version %s\n", version)
}