Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Jimmy | 1fc94b7c43 | |
Jimmy | a1c170b9c9 |
|
@ -1,4 +1,3 @@
|
||||||
ARG VERSION=17
|
|
||||||
FROM golang AS builder
|
FROM golang AS builder
|
||||||
|
|
||||||
WORKDIR /app
|
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/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:$VERSION
|
FROM openjdk:8
|
||||||
|
|
||||||
ENV MIN_MEM=512M
|
ENV MIN_MEM=512M
|
||||||
ENV MAX_MEM=1G
|
ENV MAX_MEM=1G
|
||||||
|
@ -28,4 +27,4 @@ VOLUME [ "/server" ]
|
||||||
|
|
||||||
WORKDIR /server
|
WORKDIR /server
|
||||||
|
|
||||||
CMD [ "/main" ]
|
ENTRYPOINT [ "/main" ]
|
||||||
|
|
|
@ -2,7 +2,7 @@ version: '3.7'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
minecraft:
|
minecraft:
|
||||||
image: magmise/minecraft:latest
|
image: magmise/minecraft:java8
|
||||||
container_name: minecraft
|
container_name: minecraft
|
||||||
build: .
|
build: .
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
@ -10,7 +10,7 @@ services:
|
||||||
MIN_MEM: 1G
|
MIN_MEM: 1G
|
||||||
MAX_MEM: 1G
|
MAX_MEM: 1G
|
||||||
STOP: stop
|
STOP: stop
|
||||||
JAR: server.jar
|
JAR: jar.jar
|
||||||
REGEX: "Preparing spawn area:"
|
REGEX: "Preparing spawn area:"
|
||||||
ports:
|
ports:
|
||||||
- 25565:25565
|
- 25565:25565
|
||||||
|
|
31
src/main.go
31
src/main.go
|
@ -12,7 +12,6 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"log"
|
"log"
|
||||||
"regexp"
|
"regexp"
|
||||||
"bytes"
|
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
)
|
)
|
||||||
|
@ -22,7 +21,6 @@ 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)
|
||||||
|
@ -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)
|
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 {
|
||||||
|
@ -67,18 +58,16 @@ 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()
|
||||||
|
@ -95,8 +84,6 @@ 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 {
|
||||||
|
@ -108,7 +95,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 != "") {
|
||||||
|
@ -119,22 +106,20 @@ func main() {
|
||||||
fmt.Println(text)
|
fmt.Println(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else { //don't filter
|
} else {
|
||||||
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() //wait for server to stop
|
cmd.Wait()
|
||||||
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')
|
||||||
|
|
Loading…
Reference in New Issue