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
use err_derive::Error;
use packet::{Packet, PacketType};
use std::io;
use std::time::Duration;
use tokio::net::{TcpStream, ToSocketAddrs};
use tokio::time::delay_for;
mod packet;
const INITIAL_PACKET_ID: i32 = 1;
const DELAY_TIME_MILLIS: u64 = 3;
const MINECRAFT_MAX_PAYLOAD_SIZE: usize = 1413;
#[derive(Debug, Error)]
pub enum Error {
#[error(display = "authentication failed")]
Auth,
#[error(display = "command exceeds the maximum length")]
CommandTooLong,
#[error(display = "{}", _0)]
Io(#[error(source)] io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;
pub struct Connection {
stream: TcpStream,
next_packet_id: i32,
minecraft_quirks_enabled: bool,
factorio_quirks_enabled: bool,
}
impl Connection {
pub fn builder() -> Builder {
Builder::new()
}
pub async fn connect<T: ToSocketAddrs>(address: T, password: &str) -> Result<Connection> {
Self::builder()
.enable_minecraft_quirks(true)
.connect(address, password).await
}
pub async fn cmd(&mut self, cmd: &str) -> Result<String> {
if self.minecraft_quirks_enabled && cmd.len() > MINECRAFT_MAX_PAYLOAD_SIZE {
return Err(Error::CommandTooLong);
}
self.send(PacketType::ExecCommand, cmd).await?;
if self.minecraft_quirks_enabled {
delay_for(Duration::from_millis(DELAY_TIME_MILLIS)).await;
}
let response = self.receive_response().await?;
Ok(response)
}
async fn receive_response(&mut self) -> Result<String> {
if self.factorio_quirks_enabled {
self.receive_single_packet_response().await
} else {
self.receive_multi_packet_response().await
}
}
async fn receive_single_packet_response(&mut self) -> Result<String> {
let received_packet = self.receive_packet().await?;
Ok(received_packet.get_body().into())
}
async fn receive_multi_packet_response(&mut self) -> Result<String> {
let end_id = self.send(PacketType::ExecCommand, "").await?;
let mut result = String::new();
loop {
let received_packet = self.receive_packet().await?;
if received_packet.get_id() == end_id {
return Ok(result);
}
result += received_packet.get_body();
}
}
async fn auth(&mut self, password: &str) -> Result<()> {
self.send(PacketType::Auth, password).await?;
let received_packet = loop {
let received_packet = self.receive_packet().await?;
if received_packet.get_type() == PacketType::AuthResponse {
break received_packet;
}
};
if received_packet.is_error() {
Err(Error::Auth)
} else {
Ok(())
}
}
async fn send(&mut self, ptype: PacketType, body: &str) -> io::Result<i32> {
let id = self.generate_packet_id();
let packet = Packet::new(id, ptype, body.into());
packet.serialize(&mut self.stream).await?;
Ok(id)
}
async fn receive_packet(&mut self) -> io::Result<Packet> {
Packet::deserialize(&mut self.stream).await
}
fn generate_packet_id(&mut self) -> i32 {
let id = self.next_packet_id;
self.next_packet_id = self
.next_packet_id
.checked_add(1)
.unwrap_or(INITIAL_PACKET_ID);
id
}
}
#[derive(Default, Debug)]
pub struct Builder {
minecraft_quirks_enabled: bool,
factorio_quirks_enabled: bool,
}
impl Builder {
pub fn new() -> Self {
Self::default()
}
pub fn enable_minecraft_quirks(mut self, value: bool) -> Self {
self.minecraft_quirks_enabled = value;
self
}
pub fn enable_factorio_quirks(mut self, value: bool) -> Self {
self.factorio_quirks_enabled = value;
self
}
pub async fn connect<A>(self, address: A, password: &str) -> Result<Connection>
where
A: ToSocketAddrs
{
let stream = TcpStream::connect(address).await?;
let mut conn = Connection {
stream,
next_packet_id: INITIAL_PACKET_ID,
minecraft_quirks_enabled: self.minecraft_quirks_enabled,
factorio_quirks_enabled: self.factorio_quirks_enabled,
};
conn.auth(password).await?;
Ok(conn)
}
}