/
comm.go
152 lines (123 loc) · 2.91 KB
/
comm.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
package main
import (
"bufio"
"io/ioutil"
"net"
"strings"
"time"
)
func (s *Server) IsMinecraftServerRunning() bool {
conn, err := net.Dial("tcp",
s.IPAddress+":"+globalConfig.CommunicationsPort)
if err != nil {
s.Log("communications", "Failed to connect to remote:", err)
return false
}
defer conn.Close()
reader := bufio.NewReader(conn)
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
data, err := reader.ReadBytes('\n')
if err != nil {
s.Log("communications", "Failed to read response from remote:", err)
return false
}
if string(data[:len(data)-1]) == "started" {
return true
}
return false
}
func (s *Server) StopMinecraftServer() {
s.TellRemote("stop")
s.notifyChannel = make(chan interface{})
go func() {
time.Sleep(time.Second * 30)
if s.notifyStopped {
s.notifyStopped = false
close(s.notifyChannel)
}
}()
s.notifyStopped = true
<-s.notifyChannel
}
func (s *Server) TellRemote(message string) {
for i := 0; i < 3; i++ {
conn, err := net.DialTimeout("tcp",
s.IPAddress+":"+globalConfig.CommunicationsPort, time.Second*5)
if err != nil {
s.Log("communications", "Failed to connect to remote:", err)
continue
}
defer conn.Close()
_, err = conn.Write([]byte(message))
if err != nil {
s.Log("communications", "Failed to send stop message:", err)
continue
}
break
}
}
func startComm() {
listener, err := net.Listen("tcp", ":"+globalConfig.CommunicationsPort)
if err != nil {
Fatal("communications", "Failed to listen:", err)
}
for {
conn, err := listener.Accept()
if err != nil {
Log("communications", "Communications stopped due to an error:",
err)
return
}
go handleConnection(conn)
}
}
func handleConnection(conn net.Conn) {
defer conn.Close()
remoteAddr := strings.Split(conn.RemoteAddr().String(), ":")[0]
// Check IP address
var server *Server
for _, checkServer := range allServers {
if checkServer.IPAddress == remoteAddr {
server = checkServer
break
}
}
if server == nil {
Log("communications", "Attempted connection from unknown IP:",
remoteAddr)
return
}
if !server.Available {
return
}
if server.State == stateDestroy || server.State == stateSnapshot {
// Ignore request, you're about to be crushed!
return
}
conn.SetReadDeadline(time.Now().Add(time.Second * 5))
data, err := ioutil.ReadAll(conn)
if err != nil {
server.Log("communications",
"Error receiving request from remote:", err)
return
}
request := string(data)
server.Log("communications", "Received request:", request)
switch request {
case "started":
if server.IsMinecraftServerResponding() {
server.SetState(stateStarted)
} else {
server.SetState(stateStarting)
}
case "stopped":
if server.notifyStopped {
server.notifyStopped = false
close(server.notifyChannel)
return
}
server.SetState(stateUnavailable)
default:
server.Log("communications", "Unknown request:", request)
}
}