Add dummy server for testing

This commit is contained in:
Jimmy 2022-01-12 22:31:57 +00:00
parent 21e2aefbe8
commit 478b393917
8 changed files with 29 additions and 161 deletions

View File

@ -8,24 +8,12 @@ COPY ./go.mod /app
COPY ./go.sum /app
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/cmd.go
FROM openjdk:$VERSION
ENV MIN_MEM=512M
ENV MAX_MEM=1G
ENV STOP="stop"
ENV JAR=server.jar
ENV REGEX=""
FROM debian
COPY --from=builder /app/main /main
COPY --from=builder /app/cmd /usr/local/bin/cmd
EXPOSE 25565
VOLUME [ "/server" ]
WORKDIR /server
EXPOSE 8081
CMD [ "/main" ]

View File

@ -1,2 +0,0 @@
#!/bin/sh
sudo docker build -t magmise/minecraft:latest .

View File

@ -1,20 +1,13 @@
version: '3.7'
services:
minecraft:
image: magmise/minecraft:latest
container_name: minecraft
dummy:
image: magmise/dummy
container_name: dummy
build: .
restart: unless-stopped
environment:
MIN_MEM: 1G
MAX_MEM: 1G
STOP: stop
JAR: server.jar
REGEX: "Preparing spawn area:"
ports:
- 25565:25565
volumes:
- ./server/:/server
stdin_open: true
tty: true
ports:
- 8081:8081

View File

@ -1,5 +0,0 @@
#!/bin/bash
mkdir server
wget https://launcher.mojang.com/v1/objects/35139deedbd5182953cf1caa23835da59ca3d7cd/server.jar -O server/server.jar
echo eula=true > server/eula.txt

15
go.mod
View File

@ -1,14 +1,5 @@
module minecrafttty
module dummy
go 1.15
go 1.17
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
)
require github.com/creack/pty v1.1.17

25
go.sum
View File

@ -1,23 +1,2 @@
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=
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=

View File

@ -4,17 +4,12 @@ import (
"bufio"
"fmt"
"io"
"log"
"net"
"os"
"os/exec"
"os/signal"
"strings"
"syscall"
"log"
"regexp"
"bytes"
"github.com/creack/pty"
"time"
)
func main() {
@ -22,67 +17,10 @@ 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)
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]
case "JAR":
jar = pair[1]
case "REGEX":
regex = pair[1]
}
}
//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 {
cmd = exec.Command("java", minram, maxram, args, "-jar", jar)
log.Printf("Args: %s \n\r", args)
}
//start server
cmd.Dir = "/server"
tty, err := pty.Start(cmd)
if err != nil {
panic(err)
}
ln, _ := net.Listen("tcp", ":8081")
//cleanup
defer func() {
cmd.Process.Kill()
cmd.Process.Wait()
tty.Close()
ln.Close()
}()
@ -91,12 +29,12 @@ func main() {
sig := <-sigs
if sig == syscall.SIGTERM {
fmt.Println("\033[31mStopping server\033[0m")
tty.WriteString(stop + "\n\r")
os.Exit(0)
}
}()
//accepts command from cmd.go and sends them to the server
go func() {
//accept connections from clients
for {
@ -104,39 +42,25 @@ func main() {
if err != nil {
continue
}
go handleClient(conn, tty)
}
}()
//copy to standard out. If the the is a regex pattern, do output anything that matches
go func() {
filter := regexp.MustCompile(regex)
if(regex != "") {
scanner := bufio.NewScanner(tty)
for scanner.Scan() {
text := scanner.Text()
if(!filter.MatchString(text)) {
fmt.Println(text)
}
}
} else { //don't filter
io.Copy(os.Stdout, tty)
go handleClient(conn)
}
}()
//copy stdin
go func() {
io.Copy(tty, os.Stdin)
io.Copy(os.Stdout, os.Stdin)
}()
cmd.Wait() //wait for server to stop
tty.Write([]byte("\033[31mServer Stopped\033[0m"))
os.Exit(0)
i := 0
for {
fmt.Printf("Hello %d \n", i)
i += 1
time.Sleep(time.Second)
}
}
//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) {
defer conn.Close()
cmd, _ := bufio.NewReader(conn).ReadString('\n')
tty.Write([]byte(cmd))
fmt.Println(cmd)
}