Skip to content

Commit

Permalink
Bump version 0.5.11
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Mar 4, 2024
1 parent 0ec2abb commit 8a70cb4
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ jobs:
cargo build --all-features --release --examples --target ${{ matrix.target }}
fi
if [[ "${{ matrix.host_os }}" == "windows-latest" ]]; then
powershell Compress-Archive -Path target/${{ matrix.target }}/release/examples/dns2socks.exe, target/${{ matrix.target }}/release/examples/s5-server.exe, target/${{ matrix.target }}/release/examples/udp-client.exe, target/${{ matrix.target }}/release/examples/dns-query.exe -DestinationPath release/socks5-utilities-${{ matrix.target }}.zip
powershell Compress-Archive -Path target/${{ matrix.target }}/release/examples/dns2socks.exe, target/${{ matrix.target }}/release/examples/s5-server.exe, target/${{ matrix.target }}/release/examples/udp-client.exe, target/${{ matrix.target }}/release/examples/dns-query.exe, target/${{ matrix.target }}/release/examples/echo-server.exe -DestinationPath release/socks5-utilities-${{ matrix.target }}.zip
elif [[ "${{ matrix.host_os }}" == "macos-latest" ]]; then
zip -j release/socks5-utilities-${{ matrix.target }}.zip target/${{ matrix.target }}/release/examples/dns2socks target/${{ matrix.target }}/release/examples/s5-server target/${{ matrix.target }}/release/examples/udp-client target/${{ matrix.target }}/release/examples/dns-query
zip -j release/socks5-utilities-${{ matrix.target }}.zip target/${{ matrix.target }}/release/examples/dns2socks target/${{ matrix.target }}/release/examples/s5-server target/${{ matrix.target }}/release/examples/udp-client target/${{ matrix.target }}/release/examples/dns-query target/${{ matrix.target }}/release/examples/echo-server
elif [[ "${{ matrix.host_os }}" == "ubuntu-latest" ]]; then
zip -j release/socks5-utilities-${{ matrix.target }}.zip target/${{ matrix.target }}/release/examples/dns2socks target/${{ matrix.target }}/release/examples/s5-server target/${{ matrix.target }}/release/examples/udp-client target/${{ matrix.target }}/release/examples/dns-query
zip -j release/socks5-utilities-${{ matrix.target }}.zip target/${{ matrix.target }}/release/examples/dns2socks target/${{ matrix.target }}/release/examples/s5-server target/${{ matrix.target }}/release/examples/udp-client target/${{ matrix.target }}/release/examples/dns-query target/${{ matrix.target }}/release/examples/echo-server
fi
- name: Upload
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "socks5-impl"
version = "0.5.10"
version = "0.5.11"
authors = ["ssrlive <[email protected]>"]
description = "Fundamental abstractions and async read / write functions for SOCKS5 protocol and Relatively low-level asynchronized SOCKS5 server implementation based on tokio"
categories = ["network-programming", "asynchronous"]
Expand Down
94 changes: 94 additions & 0 deletions examples/echo-server.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::{error::Error, net::SocketAddr};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, ToSocketAddrs, UdpSocket},
};

/// Simple echo server.
#[derive(clap::Parser, Debug, Clone, PartialEq, Eq)]
#[command(author, version, about = "Simple echo server.", long_about = None)]
pub struct CmdOpt {
/// Echo server listen address.
#[clap(short, long, value_name = "address:port", default_value = "127.0.0.1:8080")]
listen_addr: SocketAddr,

/// timeout for TCP connection
#[clap(short, long, value_name = "seconds", default_value = "10")]
tcp_timeout: u64,
}

async fn tcp_main<A: ToSocketAddrs>(addr: A, tcp_timeout: u64) -> std::io::Result<()> {
let listener = TcpListener::bind(addr).await?;
log::info!("[TCP] listening on: {}", listener.local_addr()?);
loop {
let (mut socket, peer) = listener.accept().await?;
tokio::spawn(async move {
let block = async move {
let mut buf = vec![0; 1024];
log::info!("[TCP] incoming peer {}", peer);
loop {
let duration = std::time::Duration::from_secs(tcp_timeout);
let n = tokio::time::timeout(duration, socket.read(&mut buf)).await??;
if n == 0 {
log::info!("[TCP] {} exit", peer);
break;
}
let amt = socket.write(&buf[0..n]).await?;
log::info!("[TCP] Echoed {}/{} bytes to {}", amt, n, peer);
}
Ok::<(), std::io::Error>(())
};
if let Err(err) = block.await {
log::info!("[TCP] {}", err);
}
});
}
}

async fn udp_main<A: ToSocketAddrs>(addr: A) -> std::io::Result<()> {
let socket = UdpSocket::bind(&addr).await?;
log::info!("[UDP] Listening on: {}", socket.local_addr()?);

let mut buf = vec![0; 1024];
let mut to_send = None;

loop {
if let Some((size, peer)) = to_send {
let amt = socket.send_to(&buf[..size], &peer).await?;
log::info!("[UDP] Echoed {}/{} bytes to {}", amt, size, peer);
}

to_send = Some(socket.recv_from(&mut buf).await?);
}
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
let opt: CmdOpt = clap::Parser::parse();

env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();

let addr = opt.listen_addr;
let _tcp = tokio::spawn(async move {
tcp_main(&addr, opt.tcp_timeout).await?;
Ok::<(), std::io::Error>(())
});

let _udp = tokio::spawn(async move {
udp_main(&addr).await?;
Ok::<(), std::io::Error>(())
});

let (tx, mut rx) = tokio::sync::mpsc::channel::<()>(1);

ctrlc2::set_async_handler(async move {
tx.send(()).await.unwrap();
log::info!("Ctrl-C received, shutting down...");
})
.await;

rx.recv().await.unwrap();
log::info!("Exiting...");

Ok(())
}

0 comments on commit 8a70cb4

Please sign in to comment.