Add OS_ARCH to network config metadata.

This commit is contained in:
Adam Ierymenko 2024-06-24 18:02:58 -04:00
parent f79af92599
commit 8caede300c
6 changed files with 440 additions and 94 deletions

View File

@ -202,6 +202,70 @@
#define ZT_PACKED_STRUCT(D) D __attribute__((packed))
#endif
#if defined(_WIN32)
#define ZT_PLATFORM_NAME "windows" // Windows
#elif defined(_WIN64)
#define ZT_PLATFORM_NAME "windows" // Windows
#elif defined(__CYGWIN__)
#define ZT_PLATFORM_NAME "windows" // Windows (Cygwin POSIX under Microsoft Window)
#elif defined(__ANDROID__)
#define ZT_PLATFORM_NAME "android" // Android (implies Linux, so it must come first)
#elif defined(__linux__)
#define ZT_PLATFORM_NAME "linux" // Debian, Ubuntu, Gentoo, Fedora, openSUSE, RedHat, Centos and other
#elif defined(__unix__) || !defined(__APPLE__) && defined(__MACH__)
#include <sys/param.h>
#if defined(BSD)
#define ZT_PLATFORM_NAME "bsd" // FreeBSD, NetBSD, OpenBSD, DragonFly BSD
#endif
#elif defined(__hpux)
#define ZT_PLATFORM_NAME "hp-ux" // HP-UX
#elif defined(_AIX)
#define ZT_PLATFORM_NAME "aix" // IBM AIX
#elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin)
#include <TargetConditionals.h>
#if TARGET_IPHONE_SIMULATOR == 1
#define ZT_PLATFORM_NAME "ios" // Apple iOS
#elif TARGET_OS_IPHONE == 1
#define ZT_PLATFORM_NAME "ios" // Apple iOS
#elif TARGET_OS_MAC == 1
#define ZT_PLATFORM_NAME "macos" // Apple OSX
#endif
#elif defined(__sun) && defined(__SVR4)
#define ZT_PLATFORM_NAME "solaris" // Oracle Solaris, Open Indiana
#else
#define ZT_PLATFORM_NAME "unknown"
#endif
#ifndef ZT_PLATFORM_NAME
#define ZT_PLATFORM_NAME "unknown"
#endif
#if defined(__amd64) || defined(__amd64__) || defined(__x86_64) || defined(__x86_64__) || defined(__AMD64) || defined(__AMD64__) || defined(_M_X64) || defined(_M_AMD64)
#define ZT_ARCH_NAME "x86_64"
#elif defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_X86_) || defined(_M_IX86) || defined(__X86__) || defined(__I86__) || defined(_M_I86)
#define ZT_ARCH_NAME "x86"
#elif defined(__aarch64__) || defined(__AARCH64EL__) || defined(_M_ARM64)
#define ZT_ARCH_NAME "arm64"
#elif defined(__arm__) || defined(__TARGET_ARCH_ARM) || defined(_ARM) || defined(_M_ARM) || defined(_M_ARMT) || defined(__arm) || defined(__thumb__)
#define ZT_ARCH_NAME "arm"
#elif defined(__loongarch__) || defined(_LOONGARCH_ARCH)
#define ZT_ARCH_NAME "loongarch"
#elif defined(__mips__) || defined(__MIPS__)
#define ZT_ARCH_NAME "mips"
#elif defined(__riscv) || defined(__riscv_xlen)
#define ZT_ARCH_NAME "riscv"
#elif defined(__powerpc__) || defined(__powerpc64__) || defined(__ppc__) || defined(__ppc64__) || defined (_M_PPC)
#define ZT_ARCH_NAME "powerpc"
#elif defined(__s390__) || defined(__s390x__) || defined(__zarch__)
#define ZT_ARCH_NAME "s390"
#else
#define ZT_ARCH_NAME "unknown"
#endif
#ifndef ZT_ARCH_NAME
#define ZT_ARCH_NAME "unknown"
#endif
#define ZT_TARGET_NAME (ZT_PLATFORM_NAME "/" ZT_ARCH_NAME)
/**
* Length of a ZeroTier address in bytes
*/

View File

@ -1313,6 +1313,7 @@ void Network::requestConfiguration(void *tPtr)
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_MAX_NETWORK_TAGS,(uint64_t)ZT_MAX_NETWORK_TAGS);
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_FLAGS,(uint64_t)0);
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_RULES_ENGINE_REV,(uint64_t)ZT_RULES_ENGINE_REVISION);
rmd.add(ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_OS_ARCH,ZT_TARGET_NAME);
RR->t->networkConfigRequestSent(tPtr,*this,ctrl);

View File

@ -105,6 +105,8 @@ namespace ZeroTier {
// Network config version
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_VERSION "v"
// Network config version
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_OS_ARCH "o"
// Protocol version (see Packet.hpp)
#define ZT_NETWORKCONFIG_REQUEST_METADATA_KEY_PROTOCOL_VERSION "pv"
// Software vendor
@ -687,7 +689,7 @@ public:
/**
* Time current authentication expires or 0 if external authentication is disabled
*
*
* Not used if authVersion >= 1
*/
uint64_t authenticationExpiryTime;

154
tcp-proxy/Cargo.lock generated
View File

@ -17,12 +17,6 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "autocfg"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0"
[[package]]
name = "backtrace"
version = "0.3.73"
@ -39,10 +33,10 @@ dependencies = [
]
[[package]]
name = "bitflags"
version = "1.3.2"
name = "bytes"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9"
[[package]]
name = "cc"
@ -56,31 +50,6 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "futures-core"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d"
[[package]]
name = "futures-task"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004"
[[package]]
name = "futures-util"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48"
dependencies = [
"futures-core",
"futures-task",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
name = "gimli"
version = "0.29.0"
@ -88,14 +57,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
[[package]]
name = "io-uring"
version = "0.6.4"
name = "hermit-abi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "595a0399f411a508feb2ec1e970a4a30c249351e30208960d58298de8660b0e5"
dependencies = [
"bitflags",
"libc",
]
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
[[package]]
name = "libc"
@ -129,6 +94,16 @@ dependencies = [
"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"
@ -145,10 +120,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02"
[[package]]
name = "pin-utils"
version = "0.1.0"
name = "proc-macro2"
version = "1.0.85"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
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"
@ -157,22 +144,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f"
[[package]]
name = "slab"
version = "0.4.9"
name = "signal-hook-registry"
version = "1.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f92a496fb766b417c996b9c5e57daf2f7ad3b0bebe1ccfca4856390e3d3bb67"
dependencies = [
"autocfg",
]
[[package]]
name = "socket2"
version = "0.4.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7916fc008ca5542385b89a3d3ce689953c143e9304a9bf8beec1de48994c0d"
checksum = "a9e9e0b4211b72e7b8b6e85c807d36c212bdb33ea8587f7569562a84df5465b1"
dependencies = [
"libc",
"winapi",
]
[[package]]
@ -185,13 +162,23 @@ dependencies = [
"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 0.5.7",
"socket2",
"tokio",
"tokio-uring",
]
[[package]]
@ -201,55 +188,40 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a"
dependencies = [
"backtrace",
"bytes",
"libc",
"mio",
"num_cpus",
"pin-project-lite",
"socket2 0.5.7",
"signal-hook-registry",
"socket2",
"tokio-macros",
"windows-sys 0.48.0",
]
[[package]]
name = "tokio-uring"
version = "0.5.0"
name = "tokio-macros"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "748482e3e13584a34664a710168ad5068e8cb1d968aa4ffa887e83ca6dd27967"
checksum = "5f5ae998a069d4b5aba8ee9dad856af7d520c3699e6159b185c2acd48155d39a"
dependencies = [
"futures-util",
"io-uring",
"libc",
"slab",
"socket2 0.4.10",
"tokio",
"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 = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.48.0"

View File

@ -9,7 +9,18 @@ authors = ["ZeroTier, Inc. <contact@zerotier.com>"]
[dependencies]
socket2 = "^0"
tokio-uring = "0.5.0"
tokio = "^1.2"
#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]

View File

@ -0,0 +1,296 @@
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;
}
*/