0605ee689f
Each player's writer thread compressed every packet it sent, so a chunk column sent to fifty players was deflated fifty times; compression was most of the writer threads' time and about half of the server's CPU. Bodies of 8 KiB or more (chunk columns, mostly) now go through a shared cache of compressed frame contents keyed by their length, two independently keyed 64-bit hashes and the threshold, holding the newest 32 MiB. The bytes on the wire are the same: frame::encode is split into frame::contents and frame::prefixed, and ConnWriter::send_contents sends a frame from its contents. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
415 lines
14 KiB
Rust
415 lines
14 KiB
Rust
//! A blocking connection with vanilla's pipeline: optional AES/CFB8
|
|
//! encryption, VarInt21 framing and threshold compression. After login it
|
|
//! splits into a reader (for a receive thread) and a shareable writer. It runs
|
|
//! over TCP, or over an in-memory pipe between two threads of one process
|
|
//! (vanilla's `LocalChannel`, which an integrated server's player uses).
|
|
use crate::{Error, Result, buf::Reader, crypto::Cfb8, frame};
|
|
use std::{
|
|
io::{Read, Write},
|
|
net::{Shutdown, TcpStream, ToSocketAddrs},
|
|
sync::{
|
|
Arc, Condvar, Mutex,
|
|
atomic::{AtomicI64, Ordering},
|
|
},
|
|
time::{Duration, Instant},
|
|
};
|
|
|
|
/// One direction of a [`Pipe`]: the bytes written and not read yet, and
|
|
/// whether either end closed it.
|
|
#[derive(Default)]
|
|
struct Channel {
|
|
state: Mutex<(std::collections::VecDeque<u8>, bool)>,
|
|
ready: Condvar,
|
|
}
|
|
impl Channel {
|
|
fn close(&self) {
|
|
self.state.lock().unwrap().1 = true;
|
|
self.ready.notify_all();
|
|
}
|
|
}
|
|
/// The channels of one end; dropping the end's last clone closes them, as
|
|
/// a socket closes with its process.
|
|
struct End {
|
|
incoming: Arc<Channel>,
|
|
outgoing: Arc<Channel>,
|
|
}
|
|
impl Drop for End {
|
|
fn drop(&mut self) {
|
|
self.incoming.close();
|
|
self.outgoing.close();
|
|
}
|
|
}
|
|
/// One end of an in-memory byte pipe: what one end writes the other reads.
|
|
pub struct Pipe {
|
|
end: Arc<End>,
|
|
/// The read timeout (`set_read_timeout`).
|
|
timeout: Mutex<Option<Duration>>,
|
|
}
|
|
impl Pipe {
|
|
/// Two connected ends.
|
|
pub fn pair() -> (Self, Self) {
|
|
let (a, b) = (Arc::new(Channel::default()), Arc::new(Channel::default()));
|
|
let end = |incoming, outgoing| Self {
|
|
end: Arc::new(End { incoming, outgoing }),
|
|
timeout: Mutex::new(None),
|
|
};
|
|
(end(a.clone(), b.clone()), end(b, a))
|
|
}
|
|
fn try_clone(&self) -> Self {
|
|
Self {
|
|
end: self.end.clone(),
|
|
timeout: Mutex::new(*self.timeout.lock().unwrap()),
|
|
}
|
|
}
|
|
/// Close both directions: reads at either end then see the end of the stream.
|
|
pub fn shutdown(&self) {
|
|
self.end.incoming.close();
|
|
self.end.outgoing.close();
|
|
}
|
|
}
|
|
impl Read for Pipe {
|
|
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
|
|
let deadline = self.timeout.lock().unwrap().map(|t| Instant::now() + t);
|
|
let mut state = self.end.incoming.state.lock().unwrap();
|
|
while state.0.is_empty() && !state.1 {
|
|
state = match deadline {
|
|
None => self.end.incoming.ready.wait(state).unwrap(),
|
|
Some(deadline) => {
|
|
let left = deadline.saturating_duration_since(Instant::now());
|
|
if left.is_zero() {
|
|
return Err(std::io::ErrorKind::TimedOut.into());
|
|
}
|
|
self.end.incoming.ready.wait_timeout(state, left).unwrap().0
|
|
}
|
|
};
|
|
}
|
|
let n = out.len().min(state.0.len());
|
|
for (o, b) in out.iter_mut().zip(state.0.drain(..n)) {
|
|
*o = b;
|
|
}
|
|
Ok(n)
|
|
}
|
|
}
|
|
impl Write for Pipe {
|
|
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
|
|
let mut state = self.end.outgoing.state.lock().unwrap();
|
|
if state.1 {
|
|
return Err(std::io::ErrorKind::BrokenPipe.into());
|
|
}
|
|
state.0.extend(data);
|
|
self.end.outgoing.ready.notify_all();
|
|
Ok(data.len())
|
|
}
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
/// What a connection runs over.
|
|
enum Stream {
|
|
Tcp(TcpStream),
|
|
Memory(Pipe),
|
|
}
|
|
impl Stream {
|
|
fn try_clone(&self) -> std::io::Result<Self> {
|
|
Ok(match self {
|
|
Self::Tcp(s) => Self::Tcp(s.try_clone()?),
|
|
Self::Memory(p) => Self::Memory(p.try_clone()),
|
|
})
|
|
}
|
|
fn set_read_timeout(&self, timeout: Option<Duration>) -> std::io::Result<()> {
|
|
match self {
|
|
Self::Tcp(s) => s.set_read_timeout(timeout),
|
|
Self::Memory(p) => {
|
|
*p.timeout.lock().unwrap() = timeout;
|
|
Ok(())
|
|
}
|
|
}
|
|
}
|
|
fn shutdown(&self) {
|
|
match self {
|
|
Self::Tcp(s) => {
|
|
let _ = s.shutdown(Shutdown::Both);
|
|
}
|
|
Self::Memory(p) => p.shutdown(),
|
|
}
|
|
}
|
|
}
|
|
impl Read for Stream {
|
|
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
|
|
match self {
|
|
Self::Tcp(s) => s.read(out),
|
|
Self::Memory(p) => p.read(out),
|
|
}
|
|
}
|
|
}
|
|
impl Write for Stream {
|
|
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
|
|
match self {
|
|
Self::Tcp(s) => s.write(data),
|
|
Self::Memory(p) => p.write(data),
|
|
}
|
|
}
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
match self {
|
|
Self::Tcp(s) => s.flush(),
|
|
Self::Memory(p) => p.flush(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Packets received/sent and bytes in/out, for the debug screen.
|
|
#[derive(Debug, Default)]
|
|
pub struct Counters {
|
|
pub received: AtomicI64,
|
|
pub sent: AtomicI64,
|
|
pub bytes_in: AtomicI64,
|
|
pub bytes_out: AtomicI64,
|
|
}
|
|
|
|
pub struct ConnReader {
|
|
stream: Stream,
|
|
buffer: Vec<u8>,
|
|
decrypt: Option<Cfb8>,
|
|
pub threshold: Option<usize>,
|
|
pub counters: Arc<Counters>,
|
|
}
|
|
pub struct ConnWriter {
|
|
stream: Stream,
|
|
encrypt: Option<Cfb8>,
|
|
pub threshold: Option<usize>,
|
|
counters: Arc<Counters>,
|
|
}
|
|
/// Both halves before the split.
|
|
pub struct Connection {
|
|
pub reader: ConnReader,
|
|
pub writer: ConnWriter,
|
|
}
|
|
impl Connection {
|
|
pub fn connect(host: &str, port: u16, timeout: Duration) -> Result<Self> {
|
|
let addrs: Vec<_> = (host, port)
|
|
.to_socket_addrs()
|
|
.map_err(|e| Error::new(format!("Unknown host {host}: {e}")))?
|
|
.collect();
|
|
let mut last = Error::new(format!("Unknown host {host}"));
|
|
for addr in addrs {
|
|
match TcpStream::connect_timeout(&addr, timeout) {
|
|
Ok(stream) => return Self::from_stream(stream),
|
|
Err(e) => last = Error::new(format!("Connection refused: {e}")),
|
|
}
|
|
}
|
|
Err(last)
|
|
}
|
|
pub fn from_stream(stream: TcpStream) -> Result<Self> {
|
|
stream.set_nodelay(true)?;
|
|
Self::over(Stream::Tcp(stream))
|
|
}
|
|
/// A connection over one end of an in-memory [`Pipe`].
|
|
pub fn local(pipe: Pipe) -> Self {
|
|
Self::over(Stream::Memory(pipe)).expect("a pipe end clones")
|
|
}
|
|
fn over(stream: Stream) -> Result<Self> {
|
|
let counters = Arc::new(Counters::default());
|
|
let write = stream.try_clone()?;
|
|
Ok(Self {
|
|
reader: ConnReader {
|
|
stream,
|
|
buffer: Vec::new(),
|
|
decrypt: None,
|
|
threshold: None,
|
|
counters: counters.clone(),
|
|
},
|
|
writer: ConnWriter {
|
|
stream: write,
|
|
encrypt: None,
|
|
threshold: None,
|
|
counters,
|
|
},
|
|
})
|
|
}
|
|
/// `Connection.isMemoryConnection`: through a [`Pipe`] rather than a socket.
|
|
pub fn is_memory(&self) -> bool {
|
|
matches!(self.reader.stream, Stream::Memory(_))
|
|
}
|
|
/// ClientboundLoginCompressionPacket: negative thresholds disable compression.
|
|
pub fn set_compression(&mut self, threshold: i32) {
|
|
let t = usize::try_from(threshold).ok();
|
|
self.reader.threshold = t;
|
|
self.writer.threshold = t;
|
|
}
|
|
/// Connection.setEncryptionKey: both directions keyed with the shared secret.
|
|
pub fn enable_encryption(&mut self, secret: &[u8; 16]) {
|
|
let mut decrypt = Cfb8::new(secret, secret);
|
|
// Bytes already read past the last packet were sent encrypted.
|
|
decrypt.decrypt(&mut self.reader.buffer);
|
|
self.reader.decrypt = Some(decrypt);
|
|
self.writer.encrypt = Some(Cfb8::new(secret, secret));
|
|
}
|
|
pub fn send(&mut self, body: &[u8]) -> Result<()> {
|
|
self.writer.send(body)
|
|
}
|
|
pub fn recv(&mut self) -> Result<(i32, Vec<u8>)> {
|
|
self.reader.recv()
|
|
}
|
|
pub fn split(self) -> (ConnReader, ConnWriter) {
|
|
(self.reader, self.writer)
|
|
}
|
|
}
|
|
impl ConnReader {
|
|
/// Block until a whole packet arrives: (id, payload).
|
|
pub fn recv(&mut self) -> Result<(i32, Vec<u8>)> {
|
|
loop {
|
|
if let Some(frame) = frame::split(&mut self.buffer)? {
|
|
let body = frame::decompress(frame, self.threshold)?;
|
|
let mut r = Reader::new(&body);
|
|
let id = r.varint()?;
|
|
let start = r.position();
|
|
self.counters.received.fetch_add(1, Ordering::Relaxed);
|
|
return Ok((id, body[start..].to_vec()));
|
|
}
|
|
let mut chunk = [0u8; 16384];
|
|
let n = self.stream.read(&mut chunk)?;
|
|
if n == 0 {
|
|
return Err(Error::new("Connection closed by the server"));
|
|
}
|
|
self.counters
|
|
.bytes_in
|
|
.fetch_add(n as i64, Ordering::Relaxed);
|
|
let data = &mut chunk[..n];
|
|
if let Some(c) = &mut self.decrypt {
|
|
c.decrypt(data);
|
|
}
|
|
self.buffer.extend_from_slice(data);
|
|
}
|
|
}
|
|
pub fn set_read_timeout(&self, timeout: Option<Duration>) -> Result<()> {
|
|
Ok(self.stream.set_read_timeout(timeout)?)
|
|
}
|
|
}
|
|
impl ConnWriter {
|
|
/// Send one packet body (VarInt id + payload).
|
|
pub fn send(&mut self, body: &[u8]) -> Result<()> {
|
|
self.send_contents(&frame::contents(body, self.threshold)?)
|
|
}
|
|
/// Send a frame whose contents [`frame::contents`] made with this
|
|
/// writer's threshold.
|
|
pub fn send_contents(&mut self, contents: &[u8]) -> Result<()> {
|
|
let mut framed = frame::prefixed(contents);
|
|
if let Some(c) = &mut self.encrypt {
|
|
c.encrypt(&mut framed);
|
|
}
|
|
self.stream.write_all(&framed)?;
|
|
self.counters.sent.fetch_add(1, Ordering::Relaxed);
|
|
self.counters
|
|
.bytes_out
|
|
.fetch_add(framed.len() as i64, Ordering::Relaxed);
|
|
Ok(())
|
|
}
|
|
pub fn shutdown(&self) {
|
|
self.stream.shutdown();
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::net::TcpListener;
|
|
|
|
#[test]
|
|
fn loopback_with_compression_and_encryption() {
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
let port = listener.local_addr().unwrap().port();
|
|
let server = std::thread::spawn(move || {
|
|
let (stream, _) = listener.accept().unwrap();
|
|
let mut c = Connection::from_stream(stream).unwrap();
|
|
let (id, payload) = c.recv().unwrap();
|
|
assert_eq!((id, payload.as_slice()), (0, &b"plain"[..]));
|
|
c.enable_encryption(&[7; 16]);
|
|
c.set_compression(64);
|
|
let (id, payload) = c.recv().unwrap();
|
|
assert_eq!((id, payload.len()), (5, 1000));
|
|
let mut body = vec![9];
|
|
body.extend_from_slice(b"reply");
|
|
c.send(&body).unwrap();
|
|
});
|
|
let mut c = Connection::connect("127.0.0.1", port, Duration::from_secs(5)).unwrap();
|
|
let mut body = vec![0];
|
|
body.extend_from_slice(b"plain");
|
|
c.send(&body).unwrap();
|
|
c.enable_encryption(&[7; 16]);
|
|
c.set_compression(64);
|
|
let mut big = vec![5];
|
|
big.extend(std::iter::repeat_n(3u8, 1000));
|
|
c.send(&big).unwrap();
|
|
let (id, payload) = c.recv().unwrap();
|
|
assert_eq!((id, payload.as_slice()), (9, &b"reply"[..]));
|
|
server.join().unwrap();
|
|
}
|
|
|
|
#[test]
|
|
fn local_pipe_carries_packets_and_closes() {
|
|
let (a, b) = Pipe::pair();
|
|
let server = std::thread::spawn(move || {
|
|
let mut c = Connection::local(b);
|
|
c.set_compression(64);
|
|
let (id, payload) = c.recv().unwrap();
|
|
assert_eq!((id, payload.len()), (5, 1000));
|
|
c.send(&[9, 1, 2]).unwrap();
|
|
let (_, writer) = c.split();
|
|
writer.shutdown();
|
|
});
|
|
let mut c = Connection::local(a);
|
|
c.set_compression(64);
|
|
let mut big = vec![5];
|
|
big.extend(std::iter::repeat_n(3u8, 1000));
|
|
c.send(&big).unwrap();
|
|
assert_eq!(c.recv().unwrap(), (9, vec![1, 2]));
|
|
assert_eq!(c.reader.counters.sent.load(Ordering::Relaxed), 1);
|
|
assert_eq!(c.reader.counters.received.load(Ordering::Relaxed), 1);
|
|
server.join().unwrap();
|
|
assert!(c.recv().is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn dropping_a_pipe_end_closes_it() {
|
|
let (a, b) = Pipe::pair();
|
|
let mut c = Connection::local(a);
|
|
drop(b);
|
|
assert!(c.recv().is_err());
|
|
assert!(c.send(&[1]).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn local_pipe_read_times_out() {
|
|
let (a, _b) = Pipe::pair();
|
|
let mut c = Connection::local(a);
|
|
c.reader
|
|
.set_read_timeout(Some(Duration::from_millis(20)))
|
|
.unwrap();
|
|
assert!(c.recv().is_err());
|
|
}
|
|
|
|
/// Encrypted bytes that arrive in the same read as the last plain packet
|
|
/// are decrypted once encryption is enabled.
|
|
#[test]
|
|
fn encrypted_bytes_read_before_enabling_encryption() {
|
|
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
|
|
let port = listener.local_addr().unwrap().port();
|
|
let (sent, both_sent) = std::sync::mpsc::channel();
|
|
let client = std::thread::spawn(move || {
|
|
let mut c = Connection::connect("127.0.0.1", port, Duration::from_secs(5)).unwrap();
|
|
c.send(&[0, 1, 2]).unwrap();
|
|
c.enable_encryption(&[7; 16]);
|
|
c.send(&[5, 4, 3]).unwrap();
|
|
sent.send(c).unwrap();
|
|
});
|
|
let (stream, _) = listener.accept().unwrap();
|
|
let mut c = Connection::from_stream(stream).unwrap();
|
|
let _keep_open = both_sent.recv().unwrap();
|
|
assert_eq!(c.recv().unwrap(), (0, vec![1, 2]));
|
|
c.enable_encryption(&[7; 16]);
|
|
assert_eq!(c.recv().unwrap(), (5, vec![4, 3]));
|
|
client.join().unwrap();
|
|
}
|
|
}
|