Files
vrisingd/handlers.go
2022-05-25 19:40:12 -05:00

170 lines
3.8 KiB
Go

package main
import (
"archive/zip"
"bytes"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/gin-gonic/gin"
)
var server_sig = make(chan int, 1)
var server_started = false
func check(err error) {
if err != nil {
panic(err)
}
}
func index(c *gin.Context) {
index_page, err := indexHTML.ReadFile("html/index.html")
check(err)
c.Data(http.StatusOK, "text/html", index_page)
}
func update(c *gin.Context) {
cmd := exec.Command("wine", "/root/.wine/drive_c/steamcmd/steamcmd.exe", "+force_install_dir", `C:\VRisingServer`, "+login", "anonymous", "+app_update", "1829350", "+quit")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
c.String(http.StatusBadRequest, err.Error())
} else {
c.String(http.StatusOK, "Update Complete")
}
}
func RecursiveZip(pathToZip, destinationPath string) error {
destinationFile, err := os.Create(destinationPath)
if err != nil {
return err
}
myZip := zip.NewWriter(destinationFile)
err = filepath.Walk(pathToZip, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
relPath := strings.TrimPrefix(filePath, filepath.Dir(pathToZip))
zipFile, err := myZip.Create(relPath)
if err != nil {
return err
}
fsFile, err := os.Open(filePath)
if err != nil {
return err
}
_, err = io.Copy(zipFile, fsFile)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
err = myZip.Close()
if err != nil {
return err
}
return nil
}
func backup(c *gin.Context) {
err := RecursiveZip("/root/.wine/drive_c/users/root/AppData/LocalLow/Stunlock Studios/VRisingServer/Saves", "root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/backup.zip")
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
backup, err := os.ReadFile("root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/backup.zip")
if err != nil {
c.String(http.StatusBadRequest, err.Error())
return
}
c.Data(http.StatusOK, "application/zip", backup)
}
func configure(c *gin.Context) {
file, err := c.FormFile("ServerHostSettings.json")
if err == nil {
c.SaveUploadedFile(file, "root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/ServerHostSettings.json")
}
file, err = c.FormFile("ServerGameSettings.json")
if err == nil {
c.SaveUploadedFile(file, "root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/ServerGameSettings.json")
}
file, err = c.FormFile("adminlist.txt")
if err == nil {
c.SaveUploadedFile(file, "root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/adminlist.txt")
}
file, err = c.FormFile("banlist.txt")
if err == nil {
c.SaveUploadedFile(file, "root/.wine/drive_c/vrisingserver/VRisingServer_Data/StreamingAssets/Settings/banlist.txt")
}
updated_page, err := indexHTML.ReadFile("html/updated.html")
check(err)
c.Data(http.StatusOK, "text/html", updated_page)
}
func doIt() {
var server_process = exec.Command("xvfb-run", "-a", "wine", "/root/.wine/drive_c/vrisingserver/VRisingServer.exe")
err := server_process.Start()
check(err)
println("Starting...")
<-server_sig
// Kill Process
pkill_process := exec.Command("pkill", "-SIGINT", "Xvfb")
pkill_process.Run()
server_process.Process.Wait()
println("Stopped")
}
func startServer(c *gin.Context) {
if !server_started {
go doIt()
server_started = true
c.String(http.StatusOK, "Process Started")
} else {
c.String(http.StatusOK, "Already Started")
}
}
func stopServer(c *gin.Context) {
if server_started {
server_sig <- 1
server_started = false
} else {
c.String(http.StatusOK, "Already Stopped")
}
}
func serverRunning(c *gin.Context) {
if server_started {
c.String(http.StatusOK, "true")
} else {
c.String(http.StatusOK, "false")
}
}