#[derive(Clone, Debug)]
pub struct Frame {
pub packet_id: u32,
pub body: Vec<u8>,
}
pub enum Verdict {
Forward,
Drop,
Replace(Vec<Frame>),
}
pub trait ProxyPlugin: Send + Sync {
fn name(&self) -> &'static str;
fn on_session_start(&self) {}
fn on_clientbound(&self, _frame: &Frame) -> Verdict {
Verdict::Forward
}
fn on_serverbound(&self, _frame: &Frame) -> Verdict {
Verdict::Forward
}
}
pub struct Pipeline {
pub plugins: Vec<Box<dyn ProxyPlugin>>,
}
impl Pipeline {
pub fn clientbound(&self, frame: Frame) -> Vec<Frame> {
self.route(frame, true)
}
pub fn serverbound(&self, frame: Frame) -> Vec<Frame> {
self.route(frame, false)
}
fn route(&self, frame: Frame, clientbound: bool) -> Vec<Frame> {
for p in &self.plugins {
let verdict = if clientbound {
p.on_clientbound(&frame)
} else {
p.on_serverbound(&frame)
};
match verdict {
Verdict::Forward => continue,
Verdict::Drop => return Vec::new(),
Verdict::Replace(frames) => return frames,
}
}
vec![frame]
}
}