-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from ssrlive/main
Migrate to tun2
- Loading branch information
Showing
10 changed files
with
284 additions
and
269 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Push or PR | ||
|
||
on: | ||
[push, pull_request] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build_n_test: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, windows-latest] | ||
|
||
runs-on: ${{ matrix.os }} | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: rustfmt | ||
run: cargo fmt --all -- --check | ||
- name: check | ||
run: cargo check --verbose | ||
- name: clippy | ||
run: cargo clippy --all-targets --all-features -- -D warnings | ||
- name: Build | ||
run: cargo build --verbose --examples --tests --all-features | ||
- name: Test | ||
run: cargo test --all-features --examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.vscode/ | ||
.VSCodeCounter/ | ||
/target/ | ||
Cargo.lock | ||
.DS_Store | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// | ||
// tokio = { version = "1.33", features = ["full"] } | ||
// | ||
use std::{env, error::Error, io}; | ||
use tokio::{ | ||
io::{AsyncReadExt, AsyncWriteExt}, | ||
net::{TcpListener, UdpSocket}, | ||
}; | ||
|
||
const TCP_TIMEOUT: u64 = 10 * 1000; // 10sec | ||
|
||
async fn tcp_main(addr: &str) -> io::Result<()> { | ||
let listener = TcpListener::bind(addr).await?; | ||
println!("[TCP] listening on: {}", addr); | ||
loop { | ||
let (mut socket, peer) = listener.accept().await?; | ||
tokio::spawn(async move { | ||
let block = async move { | ||
let mut buf = vec![0; 1024]; | ||
println!("[TCP] incoming peer {}", peer); | ||
loop { | ||
let duration = std::time::Duration::from_millis(TCP_TIMEOUT); | ||
let n = tokio::time::timeout(duration, socket.read(&mut buf)).await??; | ||
if n == 0 { | ||
println!("[TCP] {} exit", peer); | ||
break; | ||
} | ||
let amt = socket.write(&buf[0..n]).await?; | ||
println!("[TCP] Echoed {}/{} bytes to {}", amt, n, peer); | ||
} | ||
Ok::<(), io::Error>(()) | ||
}; | ||
if let Err(err) = block.await { | ||
println!("[TCP] {}", err); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
async fn udp_main(addr: &str) -> io::Result<()> { | ||
let socket = UdpSocket::bind(&addr).await?; | ||
println!("[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?; | ||
println!("[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>> { | ||
let addr = env::args().nth(1).unwrap_or("127.0.0.1:8080".to_string()); | ||
|
||
let addr1 = addr.clone(); | ||
let tcp = tokio::spawn(async move { | ||
tcp_main(&addr1).await?; | ||
Ok::<(), io::Error>(()) | ||
}); | ||
|
||
let udp = tokio::spawn(async move { | ||
udp_main(&addr).await?; | ||
Ok::<(), io::Error>(()) | ||
}); | ||
|
||
tcp.await??; | ||
udp.await??; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.