-
Notifications
You must be signed in to change notification settings - Fork 0
/
glame.gleam
155 lines (132 loc) · 3.6 KB
/
glame.gleam
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
import gleam/string
import gleam/bit_array
import mug
import glame/packet
import glame/errors
import gleam/option
/// The default timeout used for connecting, reading, and writing to the socket
const default_timeout_ms = 5000
/// Thin wrapper over a TCP socket
pub type Connection {
Connection(sock: mug.Socket, timeout_ms: Int)
}
/// Connects to the TCP socket and auths with the RCON server
pub fn dial(
host: String,
port: Int,
password: String,
connect_timeout_ms: option.Option(Int),
rw_timeout_ms: option.Option(Int),
) -> Result(Connection, errors.Error) {
let sock_result =
mug.new(host, port)
|> mug.timeout(option.unwrap(connect_timeout_ms, default_timeout_ms))
|> mug.connect()
case sock_result {
Ok(sock) -> {
let conn =
Connection(sock, option.unwrap(rw_timeout_ms, default_timeout_ms))
case auth(conn, password) {
Ok(_) -> Ok(conn)
Error(e) -> Error(e)
}
}
Error(e) -> Error(errors.SocketError(e))
}
}
/// Executes a command on the RCON server
pub fn execute(conn: Connection, cmd: String) -> Result(String, errors.Error) {
let cmd_len = string.length(cmd)
case cmd_len {
0 -> Error(errors.BodyEmpty)
_ if cmd_len > 4096 -> Error(errors.BodyTooLarge(cmd_len))
_ -> {
case write(conn, packet.ServerDataExecCommand, 3, cmd) {
Ok(_) -> {
case read(conn) {
Ok(pkt) -> {
case bit_array.to_string(pkt.body) {
Ok(body) -> Ok(body)
Error(_) -> Error(errors.BodyNotUTF8(pkt.body))
}
}
Error(e) -> Error(e)
}
}
Error(e) -> Error(e)
}
}
}
}
/// Authorizes the connection with the RCON server
fn auth(conn: Connection, password: String) -> Result(Nil, errors.Error) {
case write(conn, packet.ServerDataAuth, 2, password) {
Ok(_) -> {
case read(conn) {
Ok(pkt) -> {
case pkt.typ {
0 -> {
// Some servers will send an empty SERVERDATA_RESPONSE_VALUE packet
// immediately followed by an auth response, so we need to discard the first
// packet if necessary
case read(conn) {
Ok(p) -> {
validate_auth_response(p, 2)
}
Error(e) -> Error(e)
}
}
_ -> {
validate_auth_response(pkt, 2)
}
}
}
Error(e) -> Error(e)
}
}
Error(e) -> Error(e)
}
}
/// Makes sure that authentication was successful
fn validate_auth_response(
pkt: packet.Packet,
auth_id: Int,
) -> Result(Nil, errors.Error) {
case pkt.typ {
2 -> {
case pkt.id {
-1 -> Error(errors.AuthFailed)
_ if pkt.id == auth_id -> Ok(Nil)
got -> Error(errors.WrongResponseId(auth_id, got))
}
}
got -> Error(errors.WrongResponseType(2, got))
}
}
/// Sends a packet to the server
fn write(
conn: Connection,
packet_type: packet.PacketType,
packet_id: Int,
cmd: String,
) -> Result(Nil, errors.Error) {
case packet.new(packet_type, packet_id, cmd) {
Ok(pkt) -> {
let bytes = packet.to_bytes(pkt)
case mug.send(conn.sock, bytes) {
Ok(_) -> Ok(Nil)
Error(e) -> Error(errors.SocketError(e))
}
}
Error(e) -> Error(e)
}
}
/// Waits to receive a packet from the server
fn read(conn: Connection) -> Result(packet.Packet, errors.Error) {
case mug.receive(conn.sock, conn.timeout_ms) {
Ok(bytes) -> {
packet.from_bytes(bytes)
}
Error(e) -> Error(errors.SocketError(e))
}
}