/
rconws.go
108 lines (97 loc) · 2.15 KB
/
rconws.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
package pufferpanel
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
"github.com/pufferpanel/pufferpanel/v3/logging"
"github.com/spf13/cast"
"io"
"net"
"sync"
"time"
)
type RCONWSConnection struct {
io.WriteCloser
IP string
Port string
Password string
Reconnect bool
Environment Environment
connection *websocket.Conn
ready bool
identifier int
identifierLocker sync.Mutex
}
type rconwsMessage struct {
Message string `json:"Message"`
Identifier int `json:"Identifier"`
Stacktrace string `json:"Stacktrace"`
}
func (tc *RCONWSConnection) Write(p []byte) (n int, err error) {
if !tc.ready {
time.Sleep(1 * time.Second)
if !tc.ready {
return 0, errors.New("rconws not available")
}
}
if tc.connection != nil {
tc.identifierLocker.Lock()
defer tc.identifierLocker.Unlock()
tc.identifier++
return len(p), tc.connection.WriteJSON(&rconwsMessage{
Message: string(p),
Identifier: tc.identifier,
})
}
return 0, errors.New("rconws not available")
}
func (tc *RCONWSConnection) Start() {
tc.Reconnect = true
if tc.IP == "" {
tc.IP = "127.0.0.1"
}
go tc.reconnector()
}
func (tc *RCONWSConnection) Close() error {
tc.Reconnect = false
if tc.connection == nil {
return nil
}
return tc.connection.Close()
}
func (tc *RCONWSConnection) reconnector() {
init := true
for tc.Reconnect {
tc.ready = false
if !init {
time.Sleep(5 * time.Second)
} else {
init = false
}
ipAddr := &net.TCPAddr{
IP: net.ParseIP(tc.IP),
Port: cast.ToInt(tc.Port),
}
conn, _, err := websocket.DefaultDialer.Dial("ws://"+ipAddr.String()+"/"+tc.Password, nil)
if err != nil {
logging.Debug.Printf("Error waiting for RCON WS socket: %s", err.Error())
continue
}
tc.connection = conn
tc.ready = true
listening := true
for listening {
var data []byte
var msg rconwsMessage
_, data, err = conn.ReadMessage()
if err != nil {
listening = false
} else if len(data) > 0 {
err = json.Unmarshal(data, &msg)
if err == nil {
tc.Environment.DisplayToConsole(false, "[RCON] %s", msg.Message)
}
}
}
}
}