mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2024-12-18 12:46:25 +00:00
undo stuff from other branch
This commit is contained in:
parent
f4edb092b7
commit
e2840b0eb8
362
tcp-proxy/Cargo.lock
generated
362
tcp-proxy/Cargo.lock
generated
@ -1,362 +0,0 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.22.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6e4503c46a5c0c7844e948c9a4d6acd9f50cccb4de1c48eb9e291ea17470c678"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.73"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5cc23269a4f8976d0a4d2e7109211a419fe30e8d88d677cd60b6bc79c5732e0a"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.99"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.155"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87dfd01fe195c66b572b37921ad8803d010623c0aca821bea2302239d155cdae"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "0.8.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4a650543ca06a924e8b371db273b2756685faae30f8487da1b56505a8f78b0c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"wasi",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.16.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.36.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "576dfe1fc8f9df304abb159d767a29d0476f7750fbf8aa7ad07816004a207434"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.85"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.24"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "socket2"
|
||||
version = "0.5.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ce305eb0b4296696835b71df73eb912e0f1ffd2556a501fcede6e0c50349191c"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"windows-sys 0.52.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tcp-proxy"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"socket2",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.38.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
"bytes",
|
||||
"libc",
|
||||
"mio",
|
||||
"num_cpus",
|
||||
"pin-project-lite",
|
||||
"signal-hook-registry",
|
||||
"socket2",
|
||||
"tokio-macros",
|
||||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-macros"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.11.0+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
|
||||
dependencies = [
|
||||
"windows-targets 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.48.5",
|
||||
"windows_aarch64_msvc 0.48.5",
|
||||
"windows_i686_gnu 0.48.5",
|
||||
"windows_i686_msvc 0.48.5",
|
||||
"windows_x86_64_gnu 0.48.5",
|
||||
"windows_x86_64_gnullvm 0.48.5",
|
||||
"windows_x86_64_msvc 0.48.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-targets"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb"
|
||||
dependencies = [
|
||||
"windows_aarch64_gnullvm 0.52.5",
|
||||
"windows_aarch64_msvc 0.52.5",
|
||||
"windows_i686_gnu 0.52.5",
|
||||
"windows_i686_gnullvm",
|
||||
"windows_i686_msvc 0.52.5",
|
||||
"windows_x86_64_gnu 0.52.5",
|
||||
"windows_x86_64_gnullvm 0.52.5",
|
||||
"windows_x86_64_msvc 0.52.5",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_gnullvm"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_aarch64_msvc"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnu"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_gnullvm"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
|
||||
|
||||
[[package]]
|
||||
name = "windows_i686_msvc"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnu"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_gnullvm"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.48.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
|
||||
|
||||
[[package]]
|
||||
name = "windows_x86_64_msvc"
|
||||
version = "0.52.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0"
|
@ -1,26 +0,0 @@
|
||||
[package]
|
||||
name = "tcp-proxy"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
license = "MPL-2.0"
|
||||
authors = ["ZeroTier, Inc. <contact@zerotier.com>"]
|
||||
|
||||
[features]
|
||||
|
||||
[dependencies]
|
||||
socket2 = "^0"
|
||||
#tokio-uring = "0.5.0"
|
||||
tokio = { version = "^1", default-features = false, features = [
|
||||
"fs",
|
||||
"io-util",
|
||||
"io-std",
|
||||
"net",
|
||||
"rt",
|
||||
"signal",
|
||||
"sync",
|
||||
"time",
|
||||
"macros",
|
||||
"rt-multi-thread",
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
@ -1,296 +0,0 @@
|
||||
use std::{
|
||||
net::{Ipv6Addr, SocketAddrV6},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
const ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS: Duration = Duration::from_secs(300);
|
||||
const ZT_TCP_PROXY_TCP_PORT: u16 = 443;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let listener =
|
||||
socket2::Socket::new(socket2::Domain::IPV6, socket2::Type::STREAM, None).unwrap();
|
||||
listener.set_only_v6(false).expect("error setting V6ONLY");
|
||||
let _ = listener.set_linger(None);
|
||||
listener
|
||||
.bind(&SocketAddrV6::new(Ipv6Addr::UNSPECIFIED, ZT_TCP_PROXY_TCP_PORT, 0, 0).into())
|
||||
.expect("error binding to port");
|
||||
listener.listen(1024).expect("error listening");
|
||||
let listener = TcpListener::from_std(listener.into()).unwrap();
|
||||
|
||||
loop {
|
||||
if let Ok((stream, from_address)) = listener.accept().await {
|
||||
tokio::task::spawn(async move {
|
||||
//
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
use std::collections::HashMap;
|
||||
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::net::{TcpListener, TcpStream, UdpSocket};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
const ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS: u64 = 300;
|
||||
const ZT_TCP_PROXY_TCP_PORT: u16 = 443;
|
||||
|
||||
struct Client {
|
||||
tcp_read_buf: Vec<u8>,
|
||||
tcp_write_buf: Vec<u8>,
|
||||
tcp_write_ptr: usize,
|
||||
tcp_read_ptr: usize,
|
||||
tcp: TcpStream,
|
||||
udp: Arc<UdpSocket>,
|
||||
last_activity: Instant,
|
||||
new_version: bool,
|
||||
}
|
||||
|
||||
struct TcpProxyService {
|
||||
udp_port_counter: Arc<Mutex<u16>>,
|
||||
clients: Arc<Mutex<HashMap<SocketAddr, Client>>>,
|
||||
}
|
||||
|
||||
impl TcpProxyService {
|
||||
async fn get_unused_udp(&self) -> Option<Arc<UdpSocket>> {
|
||||
for _ in 0..65535 {
|
||||
let mut port = self.udp_port_counter.lock().await;
|
||||
*port += 1;
|
||||
if *port > 0xfffe {
|
||||
*port = 1024;
|
||||
}
|
||||
let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), *port);
|
||||
match UdpSocket::bind(addr).await {
|
||||
Ok(udp) => return Some(Arc::new(udp)),
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
async fn handle_udp(&self, addr: SocketAddr, data: &[u8]) {
|
||||
if data.len() < 16 || data.len() >= 2048 {
|
||||
return;
|
||||
}
|
||||
|
||||
let mut clients = self.clients.lock().await;
|
||||
if let Some(client) = clients.get_mut(&addr) {
|
||||
client.last_activity = Instant::now();
|
||||
|
||||
let mlen = if client.new_version {
|
||||
data.len() + 7
|
||||
} else {
|
||||
data.len()
|
||||
};
|
||||
|
||||
if client.tcp_write_ptr + 5 + mlen <= client.tcp_write_buf.len() {
|
||||
client.tcp_write_buf[client.tcp_write_ptr..client.tcp_write_ptr + 5]
|
||||
.copy_from_slice(&[0x17, 0x03, 0x03, (mlen >> 8) as u8, mlen as u8]);
|
||||
client.tcp_write_ptr += 5;
|
||||
|
||||
if client.new_version {
|
||||
client.tcp_write_buf[client.tcp_write_ptr] = 4; // IPv4
|
||||
client.tcp_write_ptr += 1;
|
||||
if let IpAddr::V4(ip) = addr.ip() {
|
||||
client.tcp_write_buf[client.tcp_write_ptr..client.tcp_write_ptr + 4]
|
||||
.copy_from_slice(&ip.octets());
|
||||
client.tcp_write_ptr += 4;
|
||||
}
|
||||
client.tcp_write_buf[client.tcp_write_ptr..client.tcp_write_ptr + 2]
|
||||
.copy_from_slice(&addr.port().to_be_bytes());
|
||||
client.tcp_write_ptr += 2;
|
||||
}
|
||||
|
||||
client.tcp_write_buf[client.tcp_write_ptr..client.tcp_write_ptr + data.len()]
|
||||
.copy_from_slice(data);
|
||||
client.tcp_write_ptr += data.len();
|
||||
|
||||
if let Err(e) = client.tcp.writable().await {
|
||||
eprintln!("Error waiting for TCP stream to be writable: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
println!(
|
||||
"<< UDP {}:{} -> {:#016x}",
|
||||
addr.ip(),
|
||||
addr.port(),
|
||||
client as *const _ as usize
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_tcp(&self, mut stream: TcpStream, addr: SocketAddr) {
|
||||
let udp = match self.get_unused_udp().await {
|
||||
Some(udp) => udp,
|
||||
None => {
|
||||
println!("** TCP rejected, no more UDP ports to assign");
|
||||
return;
|
||||
}
|
||||
};
|
||||
|
||||
let mut client = Client {
|
||||
tcp_read_buf: vec![0; 131072],
|
||||
tcp_write_buf: vec![0; 131072],
|
||||
tcp_write_ptr: 0,
|
||||
tcp_read_ptr: 0,
|
||||
tcp: stream.clone(),
|
||||
udp: udp.clone(),
|
||||
last_activity: Instant::now(),
|
||||
new_version: false,
|
||||
};
|
||||
|
||||
let mut clients = self.clients.lock().await;
|
||||
clients.insert(addr, client);
|
||||
drop(clients);
|
||||
|
||||
println!(
|
||||
"<< TCP from {} -> {:#016x}",
|
||||
addr, &client as *const _ as usize
|
||||
);
|
||||
|
||||
let mut buf = [0; 4096];
|
||||
loop {
|
||||
match stream.read(&mut buf).await {
|
||||
Ok(0) => break,
|
||||
Ok(n) => {
|
||||
let mut clients = self.clients.lock().await;
|
||||
if let Some(client) = clients.get_mut(&addr) {
|
||||
client.last_activity = Instant::now();
|
||||
|
||||
client.tcp_read_buf[client.tcp_read_ptr..client.tcp_read_ptr + n]
|
||||
.copy_from_slice(&buf[..n]);
|
||||
client.tcp_read_ptr += n;
|
||||
|
||||
while client.tcp_read_ptr >= 5 {
|
||||
let mlen = (client.tcp_read_buf[3] as usize) << 8
|
||||
| client.tcp_read_buf[4] as usize;
|
||||
if client.tcp_read_ptr >= mlen + 5 {
|
||||
if mlen == 4 {
|
||||
client.new_version = true;
|
||||
println!("<< TCP {:#016x} HELLO", client as *const _ as usize);
|
||||
} else if mlen >= 7 {
|
||||
let payload = &client.tcp_read_buf[5..mlen + 5];
|
||||
let mut payload_len = mlen;
|
||||
|
||||
let mut dest =
|
||||
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0);
|
||||
if client.new_version {
|
||||
if payload[0] == 4 {
|
||||
let ip = Ipv4Addr::new(
|
||||
payload[1], payload[2], payload[3], payload[4],
|
||||
);
|
||||
let port = u16::from_be_bytes([payload[5], payload[6]]);
|
||||
dest = SocketAddr::new(IpAddr::V4(ip), port);
|
||||
payload_len -= 7;
|
||||
}
|
||||
} else {
|
||||
dest = SocketAddr::new(
|
||||
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
|
||||
9993,
|
||||
);
|
||||
}
|
||||
|
||||
if dest.port() > 1024 && payload_len >= 16 {
|
||||
if let Err(e) =
|
||||
udp.send_to(&payload[7..payload_len + 7], dest).await
|
||||
{
|
||||
eprintln!("Error sending UDP packet: {}", e);
|
||||
}
|
||||
println!(
|
||||
">> TCP {:#016x} to {}:{}",
|
||||
client as *const _ as usize,
|
||||
dest.ip(),
|
||||
dest.port()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
client.tcp_read_ptr -= mlen + 5;
|
||||
client.tcp_read_buf.copy_within(mlen + 5.., 0);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Error reading from TCP stream: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut clients = self.clients.lock().await;
|
||||
clients.remove(&addr);
|
||||
println!("** TCP {:#016x} closed", &client as *const _ as usize);
|
||||
}
|
||||
|
||||
async fn housekeeping(&self) {
|
||||
let now = Instant::now();
|
||||
let mut clients = self.clients.lock().await;
|
||||
let mut to_close = Vec::new();
|
||||
|
||||
for (addr, client) in clients.iter() {
|
||||
if now.duration_since(client.last_activity)
|
||||
>= Duration::from_secs(ZT_TCP_PROXY_CONNECTION_TIMEOUT_SECONDS)
|
||||
{
|
||||
to_close.push(*addr);
|
||||
}
|
||||
}
|
||||
|
||||
for addr in to_close {
|
||||
clients.remove(&addr);
|
||||
}
|
||||
}
|
||||
|
||||
async fn run(&self) {
|
||||
let listener = TcpListener::bind(SocketAddr::new(
|
||||
IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)),
|
||||
ZT_TCP_PROXY_TCP_PORT,
|
||||
))
|
||||
.await
|
||||
.expect("Failed to bind TCP listener");
|
||||
|
||||
println!(
|
||||
"TCP proxy server listening on {}",
|
||||
listener.local_addr().unwrap()
|
||||
);
|
||||
|
||||
let udp = UdpSocket::bind("0.0.0.0:0")
|
||||
.await
|
||||
.expect("Failed to bind UDP socket");
|
||||
|
||||
let mut buf = [0u8; 2048];
|
||||
loop {
|
||||
tokio::select! {
|
||||
Ok((stream, addr)) = listener.accept() => {
|
||||
tokio::spawn(self.handle_tcp(stream, addr));
|
||||
}
|
||||
Ok((len, addr)) = udp.recv_from(&mut buf) => {
|
||||
self.handle_udp(addr, &buf[..len]).await;
|
||||
}
|
||||
_ = tokio::time::sleep(Duration::from_secs(120)) => {
|
||||
self.housekeeping().await;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let service = TcpProxyService {
|
||||
udp_port_counter: Arc::new(Mutex::new(1023)),
|
||||
clients: Arc::new(Mutex::new(HashMap::new())),
|
||||
};
|
||||
|
||||
service.run().await;
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user