-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.go
215 lines (182 loc) · 3.79 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"bufio"
"bytes"
"errors"
"fmt"
"log"
"net"
"os"
"strings"
"github.com/alecthomas/kingpin/v2"
quake "github.com/icedream/go-q3net"
)
var (
flagCommand = kingpin.Flag("command",
"Run a one-off command and then exit.").
Short('c').String()
argAddress = kingpin.Arg("address",
"Server IP/hostname and port, written as \"server:port\".")
argPassword = kingpin.Arg("password", "The RCON password.")
address *net.UDPAddr
addressStr string
password string
socket *net.UDPConn
socketBuffer = make([]byte, 64*1024)
bufferedStdin *bufio.Reader
errNotSupported = errors.New("Not supported")
)
func initSocketAddr(addr string) (err error) {
newAddr, err := net.ResolveUDPAddr("udp", addr)
if err != nil {
return
}
address, addressStr = newAddr, addr
return
}
func initSocket() (err error) {
socket, err = net.ListenUDP("udp", nil)
if err != nil {
return
}
return
}
func receive() (msg *quake.Message, err error) {
length, _, err := socket.ReadFromUDP(socketBuffer)
if err != nil {
return
}
msg, err = quake.UnmarshalMessage(socketBuffer[0:length])
if err != nil {
return
}
return
}
func receiveRcon() (msg *quake.Message, err error) {
msg, err = receive()
if err != nil {
return
}
if !strings.EqualFold(msg.Name, "print") {
err = errors.New("rcon: Unexpected response from server: " + msg.Name)
}
return
}
func sendRcon(input string) (err error) {
buf := new(bytes.Buffer)
msg := &quake.Message{
Header: quake.OOBHeader,
Name: "rcon",
Data: []byte(fmt.Sprintf("%s %s", password, input)),
}
if err = msg.Marshal(buf); err != nil {
return
}
if _, err = socket.WriteToUDP(buf.Bytes(), address); err != nil {
return
}
return
}
func readLineFromInput() (input string, err error) {
if bufferedStdin == nil {
bufferedStdin = bufio.NewReader(os.Stdin)
}
for {
if line, hasMoreInLine, err := bufferedStdin.ReadLine(); err != nil {
return input, err
} else {
input += string(line)
if !hasMoreInLine {
break
}
}
}
return
}
func usage() {
kingpin.Usage()
}
func main() {
fmt.Println("IceCon - Icedream's RCON Client")
fmt.Println("\t\u00A9 2016-2023 Carl Kittelberger/Icedream")
fmt.Println()
argAddressTCP := argAddress.TCP()
argPasswordStr := argPassword.String()
kingpin.Parse()
// If no arguments, fall back to running the shell
wantGui := (*argAddressTCP == nil && *flagCommand == "") || *flagGui
// Command-line shell doesn't support starting up without arguments
// but graphical Windows UI does
if !(hasGraphicalUI && wantGui) {
argAddress = argAddress.Required()
argPassword = argPassword.Required()
kingpin.Parse()
}
// Initialize socket
initSocket()
// Set target address if given
if *argAddressTCP != nil {
initSocketAddr((*argAddressTCP).String())
}
// Get password
password = *argPasswordStr
// Run one-off command?
if *flagCommand != "" {
// Send
err := sendRcon(*flagCommand)
if err != nil {
log.Fatal(err)
return
}
// Receive
msg, err := receiveRcon()
if err != nil {
log.Fatal(err)
return
}
switch strings.ToLower(msg.Name) {
case "print":
fmt.Println(string(msg.Data))
}
return
}
// Which UI should be run?
if wantGui {
if err := runGraphicalUi(); err != nil {
log.Fatal(err)
return
}
} else {
runConsoleShell()
}
if socket != nil {
socket.Close()
}
}
func runConsoleShell() {
for {
input, err := readLineFromInput()
if err != nil {
log.Fatal(err)
continue
}
// "quit" => exit shell
if strings.EqualFold(strings.TrimSpace(input), "quit") {
break
}
err = sendRcon(input)
if err != nil {
log.Println(err)
continue
}
msg, err := receiveRcon()
if err != nil {
log.Println(err)
continue
}
switch strings.ToLower(msg.Name) {
case "print":
log.Println(string(msg.Data))
}
}
}