Initial commit

This commit is contained in:
Jimmy Allen 2020-10-13 21:21:37 +13:00
parent 0be5068d88
commit 4277fffcf5
12 changed files with 197 additions and 0 deletions

15
Dockerfile Normal file
View File

@ -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" ]

BIN
bin/cmd Executable file

Binary file not shown.

BIN
bin/main Executable file

Binary file not shown.

5
build.sh Executable file
View File

@ -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 .

BIN
cmd Normal file

Binary file not shown.

29
docker-compose.yml Normal file
View File

@ -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

4
entry.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/bash
echo $MIN_MEM $MAX_MEM
/main $MIN_MEM $MAX_MEM

14
go.mod Normal file
View File

@ -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
)

23
go.sum Normal file
View File

@ -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=

BIN
main Normal file

Binary file not shown.

18
src/cmd.go Normal file
View File

@ -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")
}

89
src/main.go Normal file
View File

@ -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))
}