2 releases

0.1.1 Jun 18, 2024
0.1.0 Jun 18, 2024

#16 in #forwarding

Apache-2.0

11KB
189 lines

udpproxi

udp forwarding library

A simple udp forwarding library that can easily manage udp sessions

Latest version Documentation License

Usage

link udpproxi crate

Cargo.toml

[dependencies]
udpproxi = "0.1"
tokio = { version = "1", features = ["full"] }

main.rs

use std::io::Result;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use udpproxi::UdpProxi;

#[tokio::main]
async fn main() -> Result<()> {
    let bind = SocketAddr::from((IpAddr::from([127, 0, 0, 1]), 53));
    let forward_to = SocketAddr::from((IpAddr::from([8, 8, 8, 8]), 53));
    
    let socket = tokio::net::UdpSocket::bind(bind).await?;
    let socket = Arc::new(socket);

    let mut proxy = UdpProxi::new(socket.clone(), udpproxi::default_endpoint_creator);
    let mut buff = vec![0u8; 2048];
    
    loop {
        let (len, from) = socket.recv_from(&mut buff).await?;
        
        proxy.send_packet(
            &buff[..len],
            from,
            forward_to,
        ).await?;
    }
}

Dependencies

~4–12MB
~129K SLoC