From f79af92599454b9ecd2dce126b254290da9de353 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 17 Jun 2024 11:02:13 -0400 Subject: [PATCH 1/4] Add Rust tcp-proxy base, add .clangd --- .clangd | 2 + .gitignore | 1 + tcp-proxy/Cargo.lock | 390 +++++++++++++++++++++ tcp-proxy/Cargo.toml | 15 + tcp-proxy/{ => old}/Makefile | 0 tcp-proxy/{ => old}/README.md | 0 tcp-proxy/{ => old}/tcp-proxy.cpp | 0 tcp-proxy/{ => old}/zerotier-proxy.service | 0 tcp-proxy/src/main.rs | 0 9 files changed, 408 insertions(+) create mode 100644 .clangd create mode 100644 tcp-proxy/Cargo.lock create mode 100644 tcp-proxy/Cargo.toml rename tcp-proxy/{ => old}/Makefile (100%) rename tcp-proxy/{ => old}/README.md (100%) rename tcp-proxy/{ => old}/tcp-proxy.cpp (100%) rename tcp-proxy/{ => old}/zerotier-proxy.service (100%) create mode 100644 tcp-proxy/src/main.rs diff --git a/.clangd b/.clangd new file mode 100644 index 000000000..c7a47fe97 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=c++17] diff --git a/.gitignore b/.gitignore index fd2f7a9a1..ba8b4afca 100755 --- a/.gitignore +++ b/.gitignore @@ -124,6 +124,7 @@ attic/world/mkworld workspace/ workspace2/ zeroidc/target/ +tcp-proxy/target #snapcraft specifics /parts/ diff --git a/tcp-proxy/Cargo.lock b/tcp-proxy/Cargo.lock new file mode 100644 index 000000000..bd726117d --- /dev/null +++ b/tcp-proxy/Cargo.lock @@ -0,0 +1,390 @@ +# 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 = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[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 = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + +[[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 = "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" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd" + +[[package]] +name = "io-uring" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "595a0399f411a508feb2ec1e970a4a30c249351e30208960d58298de8660b0e5" +dependencies = [ + "bitflags", + "libc", +] + +[[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 = "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 = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + +[[package]] +name = "slab" +version = "0.4.9" +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" +dependencies = [ + "libc", + "winapi", +] + +[[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 = "tcp-proxy" +version = "0.0.1" +dependencies = [ + "socket2 0.5.7", + "tokio", + "tokio-uring", +] + +[[package]] +name = "tokio" +version = "1.38.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba4f4a02a7a80d6f274636f0aa95c7e383b912d41fe721a31f29e29698585a4a" +dependencies = [ + "backtrace", + "libc", + "mio", + "pin-project-lite", + "socket2 0.5.7", + "windows-sys 0.48.0", +] + +[[package]] +name = "tokio-uring" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "748482e3e13584a34664a710168ad5068e8cb1d968aa4ffa887e83ca6dd27967" +dependencies = [ + "futures-util", + "io-uring", + "libc", + "slab", + "socket2 0.4.10", + "tokio", +] + +[[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" +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" diff --git a/tcp-proxy/Cargo.toml b/tcp-proxy/Cargo.toml new file mode 100644 index 000000000..b791923ad --- /dev/null +++ b/tcp-proxy/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "tcp-proxy" +version = "0.0.1" +edition = "2021" +license = "MPL-2.0" +authors = ["ZeroTier, Inc. "] + +[features] + +[dependencies] +socket2 = "^0" +tokio-uring = "0.5.0" +tokio = "^1.2" + +[dev-dependencies] diff --git a/tcp-proxy/Makefile b/tcp-proxy/old/Makefile similarity index 100% rename from tcp-proxy/Makefile rename to tcp-proxy/old/Makefile diff --git a/tcp-proxy/README.md b/tcp-proxy/old/README.md similarity index 100% rename from tcp-proxy/README.md rename to tcp-proxy/old/README.md diff --git a/tcp-proxy/tcp-proxy.cpp b/tcp-proxy/old/tcp-proxy.cpp similarity index 100% rename from tcp-proxy/tcp-proxy.cpp rename to tcp-proxy/old/tcp-proxy.cpp diff --git a/tcp-proxy/zerotier-proxy.service b/tcp-proxy/old/zerotier-proxy.service similarity index 100% rename from tcp-proxy/zerotier-proxy.service rename to tcp-proxy/old/zerotier-proxy.service diff --git a/tcp-proxy/src/main.rs b/tcp-proxy/src/main.rs new file mode 100644 index 000000000..e69de29bb From 8caede300c609843b7f207eeb1298cb1e852332b Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 24 Jun 2024 18:02:58 -0400 Subject: [PATCH 2/4] Add OS_ARCH to network config metadata. --- node/Constants.hpp | 64 +++++++++ node/Network.cpp | 1 + node/NetworkConfig.hpp | 4 +- tcp-proxy/Cargo.lock | 154 +++++++++------------ tcp-proxy/Cargo.toml | 15 ++- tcp-proxy/src/main.rs | 296 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 440 insertions(+), 94 deletions(-) diff --git a/node/Constants.hpp b/node/Constants.hpp index e310cc947..7c80910f6 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -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 +#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 +#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 */ diff --git a/node/Network.cpp b/node/Network.cpp index 2e03b2482..1643487fe 100644 --- a/node/Network.cpp +++ b/node/Network.cpp @@ -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); diff --git a/node/NetworkConfig.hpp b/node/NetworkConfig.hpp index 416bbfd78..65e4c8365 100644 --- a/node/NetworkConfig.hpp +++ b/node/NetworkConfig.hpp @@ -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; diff --git a/tcp-proxy/Cargo.lock b/tcp-proxy/Cargo.lock index bd726117d..89cd3d2e6 100644 --- a/tcp-proxy/Cargo.lock +++ b/tcp-proxy/Cargo.lock @@ -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" diff --git a/tcp-proxy/Cargo.toml b/tcp-proxy/Cargo.toml index b791923ad..4bd57c762 100644 --- a/tcp-proxy/Cargo.toml +++ b/tcp-proxy/Cargo.toml @@ -9,7 +9,18 @@ authors = ["ZeroTier, Inc. "] [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] diff --git a/tcp-proxy/src/main.rs b/tcp-proxy/src/main.rs index e69de29bb..1475608d3 100644 --- a/tcp-proxy/src/main.rs +++ b/tcp-proxy/src/main.rs @@ -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, + tcp_write_buf: Vec, + tcp_write_ptr: usize, + tcp_read_ptr: usize, + tcp: TcpStream, + udp: Arc, + last_activity: Instant, + new_version: bool, +} + +struct TcpProxyService { + udp_port_counter: Arc>, + clients: Arc>>, +} + +impl TcpProxyService { + async fn get_unused_udp(&self) -> Option> { + 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; +} +*/ From e2840b0eb8aaa3c428d99a248bfb91879f7164c8 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 24 Jun 2024 18:54:05 -0400 Subject: [PATCH 3/4] undo stuff from other branch --- tcp-proxy/Cargo.lock | 362 --------------------- tcp-proxy/Cargo.toml | 26 -- tcp-proxy/{old => }/Makefile | 0 tcp-proxy/{old => }/README.md | 0 tcp-proxy/src/main.rs | 296 ----------------- tcp-proxy/{old => }/tcp-proxy.cpp | 0 tcp-proxy/{old => }/zerotier-proxy.service | 0 7 files changed, 684 deletions(-) delete mode 100644 tcp-proxy/Cargo.lock delete mode 100644 tcp-proxy/Cargo.toml rename tcp-proxy/{old => }/Makefile (100%) rename tcp-proxy/{old => }/README.md (100%) delete mode 100644 tcp-proxy/src/main.rs rename tcp-proxy/{old => }/tcp-proxy.cpp (100%) rename tcp-proxy/{old => }/zerotier-proxy.service (100%) diff --git a/tcp-proxy/Cargo.lock b/tcp-proxy/Cargo.lock deleted file mode 100644 index 89cd3d2e6..000000000 --- a/tcp-proxy/Cargo.lock +++ /dev/null @@ -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" diff --git a/tcp-proxy/Cargo.toml b/tcp-proxy/Cargo.toml deleted file mode 100644 index 4bd57c762..000000000 --- a/tcp-proxy/Cargo.toml +++ /dev/null @@ -1,26 +0,0 @@ -[package] -name = "tcp-proxy" -version = "0.0.1" -edition = "2021" -license = "MPL-2.0" -authors = ["ZeroTier, Inc. "] - -[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] diff --git a/tcp-proxy/old/Makefile b/tcp-proxy/Makefile similarity index 100% rename from tcp-proxy/old/Makefile rename to tcp-proxy/Makefile diff --git a/tcp-proxy/old/README.md b/tcp-proxy/README.md similarity index 100% rename from tcp-proxy/old/README.md rename to tcp-proxy/README.md diff --git a/tcp-proxy/src/main.rs b/tcp-proxy/src/main.rs deleted file mode 100644 index 1475608d3..000000000 --- a/tcp-proxy/src/main.rs +++ /dev/null @@ -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, - tcp_write_buf: Vec, - tcp_write_ptr: usize, - tcp_read_ptr: usize, - tcp: TcpStream, - udp: Arc, - last_activity: Instant, - new_version: bool, -} - -struct TcpProxyService { - udp_port_counter: Arc>, - clients: Arc>>, -} - -impl TcpProxyService { - async fn get_unused_udp(&self) -> Option> { - 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; -} -*/ diff --git a/tcp-proxy/old/tcp-proxy.cpp b/tcp-proxy/tcp-proxy.cpp similarity index 100% rename from tcp-proxy/old/tcp-proxy.cpp rename to tcp-proxy/tcp-proxy.cpp diff --git a/tcp-proxy/old/zerotier-proxy.service b/tcp-proxy/zerotier-proxy.service similarity index 100% rename from tcp-proxy/old/zerotier-proxy.service rename to tcp-proxy/zerotier-proxy.service From 8d474e13953bd2c6fb1d4efc909a521fe5fdfda5 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Mon, 9 Sep 2024 14:36:22 -0400 Subject: [PATCH 4/4] Add ios differentiation, and .clangd that works w/Zed and clangd. --- .clangd | 6 +++++- node/Constants.hpp | 12 +++++++----- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/.clangd b/.clangd index c7a47fe97..0605ccdb7 100644 --- a/.clangd +++ b/.clangd @@ -1,2 +1,6 @@ CompileFlags: - Add: [-std=c++17] + Add: + - "-std=c++17" + - "-I../ext" + - "-I../ext/prometheus-cpp-lite-1.0/core/include" + - "-I../ext/prometheus-cpp-lite-1.0/simpleapi/include" diff --git a/node/Constants.hpp b/node/Constants.hpp index 7c80910f6..95f093b3e 100644 --- a/node/Constants.hpp +++ b/node/Constants.hpp @@ -223,11 +223,13 @@ #define ZT_PLATFORM_NAME "aix" // IBM AIX #elif defined(__APPLE__) && defined(__MACH__) // Apple OSX and iOS (Darwin) #include -#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 +#if defined(TARGET_IPHONE_SIMULATOR) && TARGET_IPHONE_SIMULATOR == 1 +#define ZT_PLATFORM_NAME "ios_sim" // Apple iOS +#elif defined(TARGET_OS_IPAD) && TARGET_OS_IPAD == 1 +#define ZT_PLATFORM_NAME "ios_ipad" +#elif defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE == 1 +#define ZT_PLATFORM_NAME "ios_iphone" // Apple iOS +#elif defined(TARGET_OS_MAC) && TARGET_OS_MAC == 1 #define ZT_PLATFORM_NAME "macos" // Apple OSX #endif #elif defined(__sun) && defined(__SVR4)