diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..cb4d81f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM java:8 + +ENV MIN_MEM=512M +ENV MAX_MEM=8G +ENV STOP="stop" + +COPY ./bin/main /main +COPY ./bin/cmd /usr/local/bin/cmd +COPY ./entry.sh /entry.sh + +EXPOSE 25565 + +VOLUME [ "/server" ] + +ENTRYPOINT [ "/main" ] diff --git a/bin/cmd b/bin/cmd new file mode 100755 index 0000000..1c630d2 Binary files /dev/null and b/bin/cmd differ diff --git a/bin/main b/bin/main new file mode 100755 index 0000000..bec5d7d Binary files /dev/null and b/bin/main differ diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..5ee2624 --- /dev/null +++ b/build.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +go build -o bin/main src/main.go +go build -o bin/cmd src/cmd.go +sudo docker build -t minecraft . diff --git a/cmd b/cmd new file mode 100644 index 0000000..838db6b Binary files /dev/null and b/cmd differ diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b18a4cd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,29 @@ +version: '3.7' + + +services: + minecraft: + image: minecraft + container_name: minecraft + restart: unless-stopped + environment: + MAX_MEM: 1G + ports: + - 25565:25565 + volumes: + - /home/jimmy/misc/paper/:/server + stdin_open: true + tty: true + + minecraft2: + image: minecraft + container_name: minecraft2 + restart: unless-stopped + environment: + MAX_MEM: 1G + ports: + - 25566:25565 + volumes: + - /home/jimmy/misc/paper2/:/server + stdin_open: true + tty: true \ No newline at end of file diff --git a/entry.sh b/entry.sh new file mode 100644 index 0000000..e0154a0 --- /dev/null +++ b/entry.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +echo $MIN_MEM $MAX_MEM +/main $MIN_MEM $MAX_MEM \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8598a4d --- /dev/null +++ b/go.mod @@ -0,0 +1,14 @@ +module minecrafttty + +go 1.15 + +require ( + github.com/creack/pty v1.1.11 + github.com/gorilla/mux v1.8.0 + github.com/gorilla/websocket v1.4.2 + github.com/hpcloud/tail v1.0.0 + github.com/kr/pty v1.1.8 + github.com/sirupsen/logrus v1.6.0 + gopkg.in/fsnotify.v1 v1.4.7 // indirect + gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..0f30fb1 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= +github.com/creack/pty v1.1.11 h1:07n33Z8lZxZ2qwegKbObQohDhXDQxiMMz1NOUGYlesw= +github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= +github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/pty v1.1.8 h1:AkaSdXYQOWeaO3neb8EM634ahkXXe3jYbVh/F9lq+GI= +github.com/kr/pty v1.1.8/go.mod h1:O1sed60cT9XZ5uDucP5qwvh+TE3NnUj51EiZO/lmSfw= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= diff --git a/main b/main new file mode 100644 index 0000000..7d81985 Binary files /dev/null and b/main differ diff --git a/src/cmd.go b/src/cmd.go new file mode 100644 index 0000000..bde8a2f --- /dev/null +++ b/src/cmd.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "net" + "os" +) + +func main() { + command := "" + // connect to this socket + conn, _ := net.Dial("tcp", "127.0.0.1:8081") + for _, s := range os.Args[1:] { + command += s + " " + } + // send to socket + fmt.Fprintf(conn, command+"\n") +} diff --git a/src/main.go b/src/main.go new file mode 100644 index 0000000..1ca5d1d --- /dev/null +++ b/src/main.go @@ -0,0 +1,89 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "net" + "os" + "os/exec" + "os/signal" + "strings" + "syscall" + + "github.com/creack/pty" +) + +func main() { + sigs := make(chan os.Signal, 1) + signal.Notify(sigs, syscall.SIGTERM) + + var minram, maxram, args, stop string + for _, e := range os.Environ() { + pair := strings.SplitN(e, "=", 2) + switch pair[0] { + case "MIN_MEM": + minram = "-Xms" + pair[1] + case "MAX_MEM": + maxram = "-Xmx" + pair[1] + case "ARGS": + args = pair[1] + case "STOP": + stop = pair[1] + } + } + var cmd *exec.Cmd + if args == "" { + cmd = exec.Command("java", minram, maxram, "-jar", "server.jar") + } else { + cmd = exec.Command("java", minram, maxram, args, "-jar", "server.jar") + } + + cmd.Dir = "/server" + cmd.Stdout = os.Stdout + tty, err := pty.Start(cmd) + ln, _ := net.Listen("tcp", ":8081") + + if err != nil { + panic(err) + } + + defer func() { + cmd.Process.Kill() + cmd.Process.Wait() + tty.Close() + ln.Close() + }() + + go func() { + sig := <-sigs + if sig == syscall.SIGTERM { + fmt.Println("Stopping server") + tty.WriteString(stop + "\n\r") + } + }() + + go func() { + //accept connections from clients + for { + conn, err := ln.Accept() + if err != nil { + continue + } + go handleClient(conn, tty) + } + }() + + go func() { + io.Copy(tty, os.Stdin) + }() + + cmd.Wait() + os.Exit(0) +} + +func handleClient(conn net.Conn, tty *os.File) { + defer conn.Close() + cmd, _ := bufio.NewReader(conn).ReadString('\n') + tty.Write([]byte(cmd)) +}