3
0
Fork 0

Initial commit

This commit is contained in:
BuildTools 2022-08-12 21:08:47 +02:00
commit 570f501a76
16 changed files with 639 additions and 0 deletions

58
command/commands.go Executable file
View file

@ -0,0 +1,58 @@
package command
import (
"bufio"
"fmt"
"github.com/ParadoxPixel/ThemePark-Websocket/server"
"os"
"strings"
)
var commands = make(map[string]func([]string) bool)
func ReadConsole() {
loadCommands()
reader := bufio.NewReader(os.Stdin)
for {
text, _ := reader.ReadString('\n')
text = strings.Replace(text, "\n", "", -1)
args := strings.Split(text, " ")
command := strings.ToLower(args[0])
args = args[1:]
b := handleCommand(command, args)
if b {
break
}
}
}
func loadCommands() {
commands["exit"] = func(i []string) bool {
server.Stop()
return true
}
commands["ip"] = func(i []string) bool {
ip, err := GetPublicIp()
if err != nil {
fmt.Println("Unable to get public IP")
} else {
fmt.Println("IP:", ip)
}
return false
}
}
func handleCommand(cmd string, args []string) bool {
f := commands[cmd]
if f == nil {
fmt.Println("Unknown command:", cmd)
return false
}
return f(args)
}

22
command/public_ip.go Executable file
View file

@ -0,0 +1,22 @@
package command
import (
"io/ioutil"
"net/http"
)
func GetPublicIp() (str string, err error) {
url := "https://api.ipify.org?format=text"
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(ip), nil
}