Compare commits

...

6 Commits

Author SHA1 Message Date
Jimmy 21e2aefbe8 Change to CMD 2021-12-02 22:55:10 +13:00
Jimmy 265879de92 change jar to server.jar 2021-11-27 11:44:56 +13:00
Jimmy 84d7bc4d25 Print java version 2021-11-27 11:44:38 +13:00
Jimmy baea748f90 Update to java 17 2021-11-27 11:44:19 +13:00
Jimmy 0f78b51945 Open socket before defer close 2021-11-12 00:55:15 +13:00
Jimmy 0d05c20961 Add comments 2021-11-03 18:59:52 +13:00
3 changed files with 28 additions and 12 deletions

View File

@ -1,3 +1,4 @@
ARG VERSION=17
FROM golang AS builder FROM golang AS builder
WORKDIR /app WORKDIR /app
@ -9,8 +10,8 @@ COPY ./go.sum /app
RUN go build -o /app/main /app/src/main.go RUN go build -o /app/main /app/src/main.go
RUN go build -o /app/cmd /app/src/cmd.go RUN go build -o /app/cmd /app/src/cmd.go
ARG JAVA_VERSION=16
FROM openjdk:16 FROM openjdk:$VERSION
ENV MIN_MEM=512M ENV MIN_MEM=512M
ENV MAX_MEM=1G ENV MAX_MEM=1G
@ -27,4 +28,4 @@ VOLUME [ "/server" ]
WORKDIR /server WORKDIR /server
ENTRYPOINT [ "/main" ] CMD [ "/main" ]

View File

@ -10,7 +10,7 @@ services:
MIN_MEM: 1G MIN_MEM: 1G
MAX_MEM: 1G MAX_MEM: 1G
STOP: stop STOP: stop
JAR: jar.jar JAR: server.jar
REGEX: "Preparing spawn area:" REGEX: "Preparing spawn area:"
ports: ports:
- 25565:25565 - 25565:25565

View File

@ -12,6 +12,7 @@ import (
"syscall" "syscall"
"log" "log"
"regexp" "regexp"
"bytes"
"github.com/creack/pty" "github.com/creack/pty"
) )
@ -21,6 +22,7 @@ func main() {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGTERM)
//get environmental variables
var minram, maxram, args, stop, jar, regex string var minram, maxram, args, stop, jar, regex string
for _, e := range os.Environ() { for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2) pair := strings.SplitN(e, "=", 2)
@ -40,17 +42,24 @@ func main() {
} }
} }
//create command
var cmdversion *exec.Cmd
cmdversion = exec.Command("java", "--version")
var out bytes.Buffer
cmdversion.Stdout = &out
cmdversion.Run()
log.Println(out.String())
log.Printf("Ram: Min: %s Max: %s Stop: %s Jar: %s\n\r", minram, maxram, stop, jar) log.Printf("Ram: Min: %s Max: %s Stop: %s Jar: %s\n\r", minram, maxram, stop, jar)
//check if there is a server jar
if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) { if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) {
log.Println("\033[31mJar \"" + jar + "\" not found\033[0m") log.Println("\033[31mJar \"" + jar + "\" not found\033[0m")
os.Exit(1) os.Exit(1)
} }
var cmd *exec.Cmd var cmd *exec.Cmd
if args == "" { if args == "" {
cmd = exec.Command("java", minram, maxram, "-jar", jar) cmd = exec.Command("java", minram, maxram, "-jar", jar)
} else { } else {
@ -58,16 +67,18 @@ func main() {
log.Printf("Args: %s \n\r", args) log.Printf("Args: %s \n\r", args)
} }
//start server
cmd.Dir = "/server" cmd.Dir = "/server"
tty, err := pty.Start(cmd) tty, err := pty.Start(cmd)
ln, _ := net.Listen("tcp", ":8081")
if err != nil { if err != nil {
panic(err) panic(err)
} }
ln, _ := net.Listen("tcp", ":8081")
//cleanup
defer func() { defer func() {
cmd.Process.Kill() cmd.Process.Kill()
cmd.Process.Wait() cmd.Process.Wait()
@ -84,6 +95,8 @@ func main() {
} }
}() }()
//accepts command from cmd.go and sends them to the server
go func() { go func() {
//accept connections from clients //accept connections from clients
for { for {
@ -95,7 +108,7 @@ func main() {
} }
}() }()
//copy to standard out. If the the is a regex pattern, do output anything that matches
go func() { go func() {
filter := regexp.MustCompile(regex) filter := regexp.MustCompile(regex)
if(regex != "") { if(regex != "") {
@ -106,20 +119,22 @@ func main() {
fmt.Println(text) fmt.Println(text)
} }
} }
} else { } else { //don't filter
io.Copy(os.Stdout, tty) io.Copy(os.Stdout, tty)
} }
}() }()
//copy stdin
go func() { go func() {
io.Copy(tty, os.Stdin) io.Copy(tty, os.Stdin)
}() }()
cmd.Wait() cmd.Wait() //wait for server to stop
tty.Write([]byte("\033[31mServer Stopped\033[0m")) tty.Write([]byte("\033[31mServer Stopped\033[0m"))
os.Exit(0) os.Exit(0)
} }
//take commands from cmd.go and send it to sdtin of the server
func handleClient(conn net.Conn, tty *os.File) { func handleClient(conn net.Conn, tty *os.File) {
defer conn.Close() defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n') cmd, _ := bufio.NewReader(conn).ReadString('\n')