-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
167 lines (144 loc) · 3.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"bytes"
"errors"
"github.com/gorilla/websocket"
"io"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"sync"
"time"
)
const minecraftServerJarFileName = "minecraft_server.jar"
var (
stdin io.Writer
stdout io.Reader
connsLock sync.RWMutex
conns []*websocket.Conn
wg sync.WaitGroup
logRegexp = regexp.MustCompile("\\[(\\d{2}:\\d{2}:\\d{2})\\] \\[(.*)\\/(\\w*)\\]: (.*)")
)
type message struct {
Timestamp time.Time `json:"timestamp"`
Origin string `json:"origin"`
Level string `json:"level"`
Message string `json:"message"`
}
func main() {
if err := downloadVanilla(); err != nil {
log.Fatal(err)
}
wg.Add(1)
go execServer()
log.Println("starting mc")
wg.Wait()
log.Println("mc started")
go sendOutLoop()
http.HandleFunc("/", wsEndpoint)
log.Fatal(http.ListenAndServe(":8080", nil))
}
func sendOutLoop() {
reader := bufio.NewReader(stdout)
for {
line, err := reader.ReadBytes('\n')
if err != nil && err != io.EOF {
break
}
os.Stdout.Write(line)
message, err := parseLine(line)
if err != nil {
log.Println("could not parse line: ", err)
continue
}
connsLock.RLock()
for _, conn := range conns {
err := conn.WriteJSON(message)
if err != nil {
log.Println(err)
conn.Close()
}
}
connsLock.RUnlock()
}
}
func parseLine(data []byte) (message, error) {
// timestamp origin level message
// [14:10:49] [Server thread/INFO]: There are 0 of a max of 20 players online:
matches := logRegexp.FindStringSubmatch(string(data))
if len(matches) < 5 {
return message{}, errors.New("invalid console output")
}
timestamp, err := time.Parse("15:04:05", matches[1])
if err != nil {
return message{}, err
}
return message{
Timestamp: timestamp,
Origin: matches[2],
Level: matches[3],
Message: matches[4],
}, nil
}
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
func wsEndpoint(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Println(err)
return
}
conn.WriteMessage(1, []byte("hello"))
connsLock.Lock()
conns = append(conns, conn)
connsLock.Unlock()
for {
_, p, err := conn.ReadMessage()
if err != nil {
return
}
p = append(p, 0x0A)
io.Copy(stdin, bytes.NewReader(p))
}
conn.Close()
}
func downloadVanilla() error {
resp, err := http.Get("https://launcher.mojang.com/v1/objects/35139deedbd5182953cf1caa23835da59ca3d7cd/server.jar")
if err != nil {
return err
}
defer resp.Body.Close()
out, err := os.Create(minecraftServerJarFileName)
if err != nil {
return nil
}
defer out.Close()
_, err = io.Copy(out, resp.Body)
return err
}
func execServer() error {
cmd := exec.Command("java", "-jar", minecraftServerJarFileName)
var err error
stdin, err = cmd.StdinPipe()
if err != nil {
return err
}
stdout, err = cmd.StdoutPipe()
if err != nil {
return err
}
wg.Done()
log.Println("i did")
if err := cmd.Start(); err != nil {
return err
}
return cmd.Wait()
}