diff --git a/Cargo.toml b/Cargo.toml index e0bde91e6..c696bf961 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,6 @@ members = [ "network-hypervisor", "controller", "service", - "vl1-service", "utils", ] diff --git a/controller/Cargo.toml b/controller/Cargo.toml index 056bcad04..a143d2bcc 100644 --- a/controller/Cargo.toml +++ b/controller/Cargo.toml @@ -11,7 +11,7 @@ path = "src/main.rs" zerotier-crypto = { path = "../crypto" } zerotier-utils = { path = "../utils", features = ["tokio"] } zerotier-network-hypervisor = { path = "../network-hypervisor" } -zerotier-vl1-service = { path = "../vl1-service" } +zerotier-service = { path = "../service" } async-trait = "^0" serde = { version = "^1", features = ["derive"], default-features = false } serde_json = { version = "^1", features = ["std"], default-features = false } diff --git a/service/Cargo.toml b/service/Cargo.toml index 2e9be8981..db84b020e 100644 --- a/service/Cargo.toml +++ b/service/Cargo.toml @@ -5,6 +5,11 @@ authors = ["ZeroTier, Inc. ", "Adam Ierymenko , + pub auth_token_override: Option, +} diff --git a/service/src/cli/rootset.rs b/service/src/cli/rootset.rs index 1826c0c7e..52dc2088d 100644 --- a/service/src/cli/rootset.rs +++ b/service/src/cli/rootset.rs @@ -4,10 +4,11 @@ use std::io::Write; use clap::ArgMatches; -use crate::{exitcode, Flags}; +use super::Flags; use zerotier_network_hypervisor::vl1::RootSet; +use zerotier_utils::exitcode; use zerotier_utils::io::{read_limit, DEFAULT_FILE_IO_READ_LIMIT}; use zerotier_utils::json::to_json_pretty; use zerotier_utils::marshalable::Marshalable; diff --git a/service/src/lib.rs b/service/src/lib.rs new file mode 100644 index 000000000..3b42a3095 --- /dev/null +++ b/service/src/lib.rs @@ -0,0 +1,7 @@ +pub mod cli; +pub mod cmdline_help; +pub mod localconfig; +pub mod sys; +pub mod utils; +pub mod vl1; +pub mod vnic; diff --git a/service/src/localconfig.rs b/service/src/localconfig.rs index 318010305..fed7e7e02 100644 --- a/service/src/localconfig.rs +++ b/service/src/localconfig.rs @@ -6,7 +6,8 @@ use serde::{Deserialize, Serialize}; use zerotier_network_hypervisor::vl1::{Address, Endpoint}; use zerotier_network_hypervisor::vl2::NetworkId; -use zerotier_vl1_service::VL1Settings; + +use crate::vl1::VL1Settings; #[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] #[serde(default)] diff --git a/service/src/main.rs b/service/src/main.rs index 472fd4696..46a4d8a1f 100644 --- a/service/src/main.rs +++ b/service/src/main.rs @@ -1,11 +1,5 @@ // (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md. -pub mod cli; -pub mod cmdline_help; -pub mod localconfig; -pub mod utils; -pub mod vnic; - use std::io::Write; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; @@ -18,13 +12,17 @@ use clap::{Arg, ArgMatches, Command}; use zerotier_network_hypervisor::vl1::InnerProtocolLayer; use zerotier_network_hypervisor::{VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION}; use zerotier_utils::exitcode; -use zerotier_vl1_service::datadir::DataDir; -use zerotier_vl1_service::VL1Service; -use crate::localconfig::Config; +use zerotier_service::cli; +use zerotier_service::cli::Flags; +use zerotier_service::cmdline_help; +use zerotier_service::localconfig::Config; +use zerotier_service::utils; +use zerotier_service::vl1::datadir::DataDir; +use zerotier_service::vl1::{VL1Service, VL1Settings}; pub fn print_help() { - let h = crate::cmdline_help::make_cmdline_help(); + let h = cmdline_help::make_cmdline_help(); let _ = std::io::stdout().write_all(h.as_bytes()); } @@ -38,13 +36,6 @@ pub fn platform_default_home_path() -> String { "/var/lib/zerotier".into() } -pub struct Flags { - pub json_output: bool, - pub base_path: String, - pub auth_token_path_override: Option, - pub auth_token_override: Option, -} - fn open_datadir(flags: &Flags) -> Arc> { let datadir = DataDir::open(flags.base_path.as_str()); if datadir.is_ok() { @@ -212,7 +203,7 @@ fn main() { eprintln!("FATAL: error generator or writing identity: {}", e.to_string()); exitcode::ERR_IOERR } else { - let svc = VL1Service::new(id.unwrap(), test_inner, zerotier_vl1_service::VL1Settings::default()); + let svc = VL1Service::new(id.unwrap(), test_inner, VL1Settings::default()); if svc.is_ok() { let svc = svc.unwrap(); svc.node.init_default_roots(); diff --git a/vl1-service/src/sys/getifaddrs.rs b/service/src/sys/getifaddrs.rs similarity index 98% rename from vl1-service/src/sys/getifaddrs.rs rename to service/src/sys/getifaddrs.rs index 1163f717d..77ca5e612 100644 --- a/vl1-service/src/sys/getifaddrs.rs +++ b/service/src/sys/getifaddrs.rs @@ -5,7 +5,7 @@ use std::ptr::{copy_nonoverlapping, null_mut}; use zerotier_network_hypervisor::vl1::InetAddress; -use crate::localinterface::LocalInterface; +use crate::vl1::LocalInterface; #[allow(unused)] #[inline(always)] diff --git a/vl1-service/src/sys/ipv6.rs b/service/src/sys/ipv6.rs similarity index 100% rename from vl1-service/src/sys/ipv6.rs rename to service/src/sys/ipv6.rs diff --git a/vl1-service/src/sys/mod.rs b/service/src/sys/mod.rs similarity index 100% rename from vl1-service/src/sys/mod.rs rename to service/src/sys/mod.rs diff --git a/vl1-service/src/sys/udp.rs b/service/src/sys/udp.rs similarity index 99% rename from vl1-service/src/sys/udp.rs rename to service/src/sys/udp.rs index 19a950e46..58357bf25 100644 --- a/vl1-service/src/sys/udp.rs +++ b/service/src/sys/udp.rs @@ -10,7 +10,7 @@ use std::ptr::{null, null_mut}; use std::sync::atomic::{AtomicBool, AtomicI64, Ordering}; use std::sync::{Arc, RwLock}; -use crate::localinterface::LocalInterface; +use crate::vl1::LocalInterface; #[allow(unused_imports)] use num_traits::AsPrimitive; diff --git a/vl1-service/src/datadir.rs b/service/src/vl1/datadir.rs similarity index 99% rename from vl1-service/src/datadir.rs rename to service/src/vl1/datadir.rs index 630430c8c..d3c322b07 100644 --- a/vl1-service/src/datadir.rs +++ b/service/src/vl1/datadir.rs @@ -21,6 +21,7 @@ pub const CONFIG_FILENAME: &'static str = "local.conf"; const AUTH_TOKEN_DEFAULT_LENGTH: usize = 48; const AUTH_TOKEN_POSSIBLE_CHARS: &'static str = "0123456789abcdefghijklmnopqrstuvwxyz"; +/// ZeroTier home directory interface pub struct DataDir { pub base_path: PathBuf, config: RwLock>, diff --git a/vl1-service/src/localinterface.rs b/service/src/vl1/localinterface.rs similarity index 100% rename from vl1-service/src/localinterface.rs rename to service/src/vl1/localinterface.rs diff --git a/vl1-service/src/localsocket.rs b/service/src/vl1/localsocket.rs similarity index 100% rename from vl1-service/src/localsocket.rs rename to service/src/vl1/localsocket.rs diff --git a/vl1-service/src/lib.rs b/service/src/vl1/mod.rs similarity index 91% rename from vl1-service/src/lib.rs rename to service/src/vl1/mod.rs index 4dbd223a0..31e17dbd1 100644 --- a/vl1-service/src/lib.rs +++ b/service/src/vl1/mod.rs @@ -5,9 +5,7 @@ mod localsocket; mod vl1service; mod vl1settings; -pub mod constants; pub mod datadir; -pub mod sys; pub use localinterface::LocalInterface; pub use localsocket::LocalSocket; diff --git a/vl1-service/src/vl1service.rs b/service/src/vl1/vl1service.rs similarity index 97% rename from vl1-service/src/vl1service.rs rename to service/src/vl1/vl1service.rs index 690ce3303..395985bf0 100644 --- a/vl1-service/src/vl1service.rs +++ b/service/src/vl1/vl1service.rs @@ -12,10 +12,8 @@ use zerotier_network_hypervisor::vl1::identity::IdentitySecret; use zerotier_network_hypervisor::vl1::*; use zerotier_utils::{ms_monotonic, ms_since_epoch}; -use crate::constants::UNASSIGNED_PRIVILEGED_PORTS; +use super::vl1settings::{VL1Settings, UNASSIGNED_PRIVILEGED_PORTS}; use crate::sys::udp::{udp_test_bind, BoundUdpPort, UdpPacketHandler}; -use crate::vl1settings::VL1Settings; -use crate::LocalSocket; /// Update UDP bindings every this many seconds. const UPDATE_UDP_BINDINGS_EVERY_SECS: usize = 10; @@ -180,7 +178,7 @@ impl UdpPacketHandler for VL1Service self.as_ref(), self.inner.as_ref(), &Endpoint::IpUdp(source_address.clone()), - &LocalSocket::new(socket), + &super::localsocket::LocalSocket::new(socket), &socket.interface, time_ticks, packet, @@ -189,8 +187,8 @@ impl UdpPacketHandler for VL1Service } impl ApplicationLayer for VL1Service { - type LocalSocket = crate::LocalSocket; - type LocalInterface = crate::LocalInterface; + type LocalSocket = super::localsocket::LocalSocket; + type LocalInterface = super::localinterface::LocalInterface; #[inline] fn event(&self, event: Event) { diff --git a/vl1-service/src/constants.rs b/service/src/vl1/vl1settings.rs similarity index 53% rename from vl1-service/src/constants.rs rename to service/src/vl1/vl1settings.rs index 070e111ac..3dd5daefb 100644 --- a/vl1-service/src/constants.rs +++ b/service/src/vl1/vl1settings.rs @@ -1,5 +1,53 @@ // (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md. +use std::collections::HashSet; + +use serde::{Deserialize, Serialize}; + +use zerotier_network_hypervisor::vl1::InetAddress; + +#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] +#[serde(default)] +pub struct VL1Settings { + /// Primary ZeroTier port that is always bound, default is 9993. + pub fixed_ports: HashSet, + + /// Number of additional random ports to bind. + pub random_port_count: usize, + + /// Enable uPnP, NAT-PMP, and other router port mapping technologies? + pub port_mapping: bool, + + /// Interface name prefix blacklist for local bindings (not remote IPs). + pub interface_prefix_blacklist: HashSet, + + /// IP/bits CIDR blacklist for local bindings (not remote IPs). + pub cidr_blacklist: HashSet, +} + +impl VL1Settings { + #[cfg(target_os = "macos")] + pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 11] = ["lo", "utun", "gif", "stf", "iptap", "pktap", "feth", "zt", "llw", "anpi", "bridge"]; + + #[cfg(target_os = "linux")] + pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 5] = ["lo", "tun", "tap", "ipsec", "zt"]; + + #[cfg(windows)] + pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 0] = []; +} + +impl Default for VL1Settings { + fn default() -> Self { + Self { + fixed_ports: HashSet::from([9993u16]), + random_port_count: 5, + port_mapping: true, + interface_prefix_blacklist: Self::DEFAULT_PREFIX_BLACKLIST.iter().map(|s| s.to_string()).collect(), + cidr_blacklist: HashSet::new(), + } + } +} + /// A list of unassigned or obsolete ports under 1024 that could possibly be squatted. pub const UNASSIGNED_PRIVILEGED_PORTS: [u16; 299] = [ 4, 6, 8, 10, 12, 14, 15, 16, 26, 28, 30, 32, 34, 36, 40, 60, 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279, 285, 288, 289, 290, 291, 292, diff --git a/vl1-service/Cargo.toml b/vl1-service/Cargo.toml deleted file mode 100644 index aec9dca5e..000000000 --- a/vl1-service/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "zerotier-vl1-service" -version = "0.1.0" -authors = ["ZeroTier, Inc. ", "Adam Ierymenko "] -edition = "2021" -license = "MPL-2.0" - -[dependencies] -zerotier-network-hypervisor = { path = "../network-hypervisor" } -zerotier-crypto = { path = "../crypto" } -zerotier-utils = { path = "../utils" } -num-traits = "^0" -serde = { version = "^1", features = ["derive"], default-features = false } -serde_json = { version = "^1", features = ["std"], default-features = false } - -[target."cfg(windows)".dependencies] -winapi = { version = "^0", features = ["handleapi", "ws2ipdef", "ws2tcpip"] } - -[target."cfg(not(windows))".dependencies] -libc = "^0" diff --git a/vl1-service/rustfmt.toml b/vl1-service/rustfmt.toml deleted file mode 120000 index 39f97b043..000000000 --- a/vl1-service/rustfmt.toml +++ /dev/null @@ -1 +0,0 @@ -../rustfmt.toml \ No newline at end of file diff --git a/vl1-service/src/vl1settings.rs b/vl1-service/src/vl1settings.rs deleted file mode 100644 index 56bc4bf37..000000000 --- a/vl1-service/src/vl1settings.rs +++ /dev/null @@ -1,49 +0,0 @@ -// (c) 2020-2022 ZeroTier, Inc. -- currently proprietary pending actual release and licensing. See LICENSE.md. - -use std::collections::HashSet; - -use serde::{Deserialize, Serialize}; - -use zerotier_network_hypervisor::vl1::InetAddress; - -#[derive(Serialize, Deserialize, Clone, PartialEq, Eq)] -#[serde(default)] -pub struct VL1Settings { - /// Primary ZeroTier port that is always bound, default is 9993. - pub fixed_ports: HashSet, - - /// Number of additional random ports to bind. - pub random_port_count: usize, - - /// Enable uPnP, NAT-PMP, and other router port mapping technologies? - pub port_mapping: bool, - - /// Interface name prefix blacklist for local bindings (not remote IPs). - pub interface_prefix_blacklist: HashSet, - - /// IP/bits CIDR blacklist for local bindings (not remote IPs). - pub cidr_blacklist: HashSet, -} - -impl VL1Settings { - #[cfg(target_os = "macos")] - pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 11] = ["lo", "utun", "gif", "stf", "iptap", "pktap", "feth", "zt", "llw", "anpi", "bridge"]; - - #[cfg(target_os = "linux")] - pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 5] = ["lo", "tun", "tap", "ipsec", "zt"]; - - #[cfg(windows)] - pub const DEFAULT_PREFIX_BLACKLIST: [&'static str; 0] = []; -} - -impl Default for VL1Settings { - fn default() -> Self { - Self { - fixed_ports: HashSet::from([9993u16]), - random_port_count: 5, - port_mapping: true, - interface_prefix_blacklist: Self::DEFAULT_PREFIX_BLACKLIST.iter().map(|s| s.to_string()).collect(), - cidr_blacklist: HashSet::new(), - } - } -}