Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Jimmy | 1fc94b7c43 | |
Jimmy | a1c170b9c9 |
|
@ -1,4 +1,3 @@
|
|||
ARG VERSION=17
|
||||
FROM golang AS builder
|
||||
|
||||
WORKDIR /app
|
||||
|
@ -10,8 +9,8 @@ COPY ./go.sum /app
|
|||
RUN go build -o /app/main /app/src/main.go
|
||||
RUN go build -o /app/cmd /app/src/cmd.go
|
||||
|
||||
|
||||
FROM openjdk:$VERSION
|
||||
ARG JAVA_VERSION=16
|
||||
FROM openjdk:8
|
||||
|
||||
ENV MIN_MEM=512M
|
||||
ENV MAX_MEM=1G
|
||||
|
@ -28,4 +27,4 @@ VOLUME [ "/server" ]
|
|||
|
||||
WORKDIR /server
|
||||
|
||||
CMD [ "/main" ]
|
||||
ENTRYPOINT [ "/main" ]
|
||||
|
|
|
@ -2,7 +2,7 @@ version: '3.7'
|
|||
|
||||
services:
|
||||
minecraft:
|
||||
image: magmise/minecraft:latest
|
||||
image: magmise/minecraft:java8
|
||||
container_name: minecraft
|
||||
build: .
|
||||
restart: unless-stopped
|
||||
|
@ -10,7 +10,7 @@ services:
|
|||
MIN_MEM: 1G
|
||||
MAX_MEM: 1G
|
||||
STOP: stop
|
||||
JAR: server.jar
|
||||
JAR: jar.jar
|
||||
REGEX: "Preparing spawn area:"
|
||||
ports:
|
||||
- 25565:25565
|
||||
|
|
31
src/main.go
31
src/main.go
|
@ -12,7 +12,6 @@ import (
|
|||
"syscall"
|
||||
"log"
|
||||
"regexp"
|
||||
"bytes"
|
||||
|
||||
"github.com/creack/pty"
|
||||
)
|
||||
|
@ -22,7 +21,6 @@ func main() {
|
|||
sigs := make(chan os.Signal, 1)
|
||||
signal.Notify(sigs, syscall.SIGTERM)
|
||||
|
||||
//get environmental variables
|
||||
var minram, maxram, args, stop, jar, regex string
|
||||
for _, e := range os.Environ() {
|
||||
pair := strings.SplitN(e, "=", 2)
|
||||
|
@ -42,24 +40,17 @@ 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)
|
||||
|
||||
//check if there is a server jar
|
||||
|
||||
|
||||
if _, err := os.Stat("/server/"+jar); os.IsNotExist(err) {
|
||||
log.Println("\033[31mJar \"" + jar + "\" not found\033[0m")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var cmd *exec.Cmd
|
||||
|
||||
if args == "" {
|
||||
cmd = exec.Command("java", minram, maxram, "-jar", jar)
|
||||
} else {
|
||||
|
@ -67,18 +58,16 @@ func main() {
|
|||
log.Printf("Args: %s \n\r", args)
|
||||
}
|
||||
|
||||
//start server
|
||||
|
||||
cmd.Dir = "/server"
|
||||
tty, err := pty.Start(cmd)
|
||||
|
||||
ln, _ := net.Listen("tcp", ":8081")
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
||||
}
|
||||
|
||||
ln, _ := net.Listen("tcp", ":8081")
|
||||
|
||||
//cleanup
|
||||
defer func() {
|
||||
cmd.Process.Kill()
|
||||
cmd.Process.Wait()
|
||||
|
@ -95,8 +84,6 @@ func main() {
|
|||
}
|
||||
}()
|
||||
|
||||
//accepts command from cmd.go and sends them to the server
|
||||
|
||||
go func() {
|
||||
//accept connections from clients
|
||||
for {
|
||||
|
@ -108,7 +95,7 @@ func main() {
|
|||
}
|
||||
}()
|
||||
|
||||
//copy to standard out. If the the is a regex pattern, do output anything that matches
|
||||
|
||||
go func() {
|
||||
filter := regexp.MustCompile(regex)
|
||||
if(regex != "") {
|
||||
|
@ -119,22 +106,20 @@ func main() {
|
|||
fmt.Println(text)
|
||||
}
|
||||
}
|
||||
} else { //don't filter
|
||||
} else {
|
||||
io.Copy(os.Stdout, tty)
|
||||
}
|
||||
}()
|
||||
|
||||
//copy stdin
|
||||
go func() {
|
||||
io.Copy(tty, os.Stdin)
|
||||
}()
|
||||
|
||||
cmd.Wait() //wait for server to stop
|
||||
cmd.Wait()
|
||||
tty.Write([]byte("\033[31mServer Stopped\033[0m"))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
//take commands from cmd.go and send it to sdtin of the server
|
||||
func handleClient(conn net.Conn, tty *os.File) {
|
||||
defer conn.Close()
|
||||
cmd, _ := bufio.NewReader(conn).ReadString('\n')
|
||||
|
|
Loading…
Reference in New Issue