pub mod aliases;
pub mod budget;
pub mod cycle;
pub mod guard;
pub mod host;
pub mod i2p_config;
pub mod tor_config;
pub mod kinds;
pub mod prefs;
pub mod prelogin;
pub mod realtime;
pub mod route;
pub mod status;
pub mod ws;
#[cfg(not(target_arch = "wasm32"))]
pub mod bridge;
#[cfg(not(target_arch = "wasm32"))]
pub mod runtime;
#[cfg(all(feature = "twin-check", not(target_arch = "wasm32")))]
pub mod twins;
#[cfg(test)]
mod tests;
#[cfg(not(target_arch = "wasm32"))]
pub use runtime::{runtime, spawn_on};
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::OnceLock;
pub use budget::{budget, Budgets, Op};
pub use route::{nip11_url, validate_relay_url, Dest, Refusal, Route};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
Clearnet,
Tor,
I2p,
}
impl Kind {
pub const ALL: [Kind; 3] = [Kind::Clearnet, Kind::Tor, Kind::I2p];
pub fn as_str(self) -> &'static str {
match self {
Kind::Clearnet => "clearnet",
Kind::Tor => "tor",
Kind::I2p => "i2p",
}
}
pub fn parse(s: &str) -> Option<Kind> {
Kind::ALL.into_iter().find(|k| k.as_str() == s)
}
pub fn label(self) -> &'static str {
match self {
Kind::Clearnet => "Standard",
Kind::Tor => "Tor",
Kind::I2p => "I2P",
}
}
pub fn compiled(self) -> bool {
match self {
Kind::Clearnet => true,
Kind::Tor => cfg!(all(feature = "tor", not(target_arch = "wasm32"))),
Kind::I2p => cfg!(all(feature = "i2p", not(target_arch = "wasm32"))),
}
}
pub fn native_suffixes(self) -> &'static [&'static str] {
match self {
Kind::Clearnet => &[],
Kind::Tor => &[".onion"],
Kind::I2p => &[".i2p"],
}
}
pub fn reaches_native(self) -> bool {
match self {
Kind::I2p => true,
Kind::Tor => cfg!(all(feature = "tor", not(target_arch = "wasm32"))),
Kind::Clearnet => false,
}
}
pub fn address_noun(self) -> &'static str {
match self {
Kind::Tor => "onion address",
Kind::I2p => "I2P address",
Kind::Clearnet => "address",
}
}
pub fn budgets(self) -> &'static Budgets {
budget::table(self)
}
fn code(self) -> u8 {
match self {
Kind::Clearnet => 1,
Kind::Tor => 2,
Kind::I2p => 3,
}
}
fn from_code(c: u8) -> Option<Kind> {
match c {
1 => Some(Kind::Clearnet),
2 => Some(Kind::Tor),
3 => Some(Kind::I2p),
_ => None,
}
}
}
pub fn supported() -> Vec<Kind> {
Kind::ALL.into_iter().filter(|k| k.compiled()).collect()
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Lane {
Account,
Shared,
}
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
impl Lane {
fn tag(self) -> char {
match self {
Lane::Account => 'a',
Lane::Shared => 's',
}
}
fn from_tag(c: &str) -> Option<Lane> {
match c {
"a" => Some(Lane::Account),
"s" => Some(Lane::Shared),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ExitPolicy {
#[default]
Allow,
Off,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TransportState {
Unknown,
Clearnet,
Active { kind: Kind },
RequiredButInactive { kind: Kind },
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Ticket {
pub owner: u64,
pub epoch: u64,
pub lane: Lane,
}
#[cfg_attr(target_arch = "wasm32", allow(dead_code))]
impl Ticket {
pub(crate) fn encode(&self) -> String {
format!("t1.{:x}.{:x}.{}", self.owner, self.epoch, self.lane.tag())
}
pub(crate) fn decode(s: &str) -> Option<Ticket> {
let mut parts = s.split('.');
if parts.next()? != "t1" {
return None;
}
let owner = u64::from_str_radix(parts.next()?, 16).ok()?;
let epoch = u64::from_str_radix(parts.next()?, 16).ok()?;
let lane = Lane::from_tag(parts.next()?)?;
if parts.next().is_some() {
return None;
}
Some(Ticket { owner, epoch, lane })
}
pub fn is_live(&self) -> bool {
self.owner == crate::db::live_session_id() && self.epoch == epoch()
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Egress {
Direct,
Proxy(Ticket),
Refuse(ConnectError),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ConnectError {
Refused(Refusal),
NotReady(Option<Kind>),
Blocked(String),
Stale,
RouterDown { port: u16 },
UnknownName,
Unreachable(String),
ExitUnreachable(String),
NoExit,
ExitRefusedPort(u16),
Timeout,
Cancelled,
Bridge(String),
}
impl ConnectError {
pub fn socks_reply(&self) -> u8 {
match self {
ConnectError::Refused(_) | ConnectError::Stale => 0x02,
ConnectError::NotReady(_) | ConnectError::Blocked(_) | ConnectError::Bridge(_) | ConnectError::Cancelled => 0x01,
ConnectError::RouterDown { .. } => 0x03,
ConnectError::UnknownName
| ConnectError::Unreachable(_)
| ConnectError::ExitUnreachable(_)
| ConnectError::NoExit
| ConnectError::ExitRefusedPort(_) => 0x04,
ConnectError::Timeout => 0x06,
}
}
pub fn text(&self) -> String {
match self {
ConnectError::Refused(r) => r.text(),
ConnectError::NotReady(None) => "Vector is still connecting.".into(),
ConnectError::NotReady(Some(k)) => format!("{} is still connecting.", k.label()),
ConnectError::Blocked(t) => t.clone(),
ConnectError::Stale => "The network changed. Try again.".into(),
ConnectError::RouterDown { port } => match preference() {
Some(Kind::Tor) => format!("Can't reach Tor at 127.0.0.1:{port}."),
_ => format!("Can't reach your I2P router at 127.0.0.1:{port}."),
},
ConnectError::UnknownName => "This I2P name isn't in your router's address book. Use its .b32.i2p address.".into(),
ConnectError::Unreachable(detail) => match preference() {
Some(Kind::I2p) => "This I2P address isn't online right now.".into(),
_ => detail.clone(),
},
ConnectError::ExitUnreachable(name) => format!("The {name} outproxy couldn't reach this server."),
ConnectError::NoExit => "No outproxy is reachable right now.".into(),
ConnectError::ExitRefusedPort(p) => format!("No outproxy here allows port {p}."),
ConnectError::Timeout => "Timed out.".into(),
ConnectError::Cancelled => "Cancelled.".into(),
ConnectError::Bridge(_) => "Vector couldn't open its local proxy.".into(),
}
}
pub fn is_transient(&self) -> bool {
match self {
ConnectError::NotReady(_) => true,
ConnectError::Blocked(_) => !status::waits_for_user(),
_ => false,
}
}
pub fn is_network(&self) -> bool {
matches!(
self,
ConnectError::NotReady(_)
| ConnectError::Blocked(_)
| ConnectError::Stale
| ConnectError::RouterDown { .. }
| ConnectError::NoExit
| ConnectError::ExitRefusedPort(_)
| ConnectError::Bridge(_)
)
}
}
impl std::fmt::Display for ConnectError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.text())
}
}
impl std::error::Error for ConnectError {}
pub trait AsyncStream: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + Unpin {}
impl<T: tokio::io::AsyncRead + tokio::io::AsyncWrite + Send + Unpin> AsyncStream for T {}
pub type BoxedStream = Box<dyn AsyncStream>;
pub type KindConfig = std::sync::Arc<dyn std::any::Any + Send + Sync>;
pub struct RouteCtx<'a> {
pub aliases: &'a aliases::AliasTable,
pub config: &'a KindConfig,
}
#[derive(Clone, Debug, Default)]
pub struct Dialed {
pub outproxy: Option<String>,
}
#[async_trait::async_trait]
pub trait Transport: Send + Sync + 'static {
fn kind(&self) -> Kind;
fn route(&self, dest: &Dest, port: u16, ctx: &RouteCtx) -> Route;
async fn dial(&self, route: &Route, lane: Lane) -> Result<(BoxedStream, Dialed), ConnectError>;
fn ready(&self) -> bool;
fn kind_status(&self) -> status::KindStatus;
async fn new_identity(&self);
async fn shutdown(&self);
fn into_any(self: std::sync::Arc<Self>) -> std::sync::Arc<dyn std::any::Any + Send + Sync>;
fn as_any(&self) -> Option<&(dyn std::any::Any + Send + Sync)> {
None
}
}
pub struct StartCtx {
pub owner: u64,
pub started_prelogin: bool,
pub dirs: Option<(std::path::PathBuf, std::path::PathBuf)>,
pub config: KindConfig,
}
#[async_trait::async_trait]
pub trait TransportFactory: Send + Sync + 'static {
fn kind(&self) -> Kind;
async fn start(&self, ctx: StartCtx) -> Result<std::sync::Arc<dyn Transport>, String>;
fn adopt_on_unlock(&self) -> bool;
fn compatible(&self, inst: &dyn Transport, cfg: &KindConfig) -> bool;
}
static EPOCH: AtomicU64 = AtomicU64::new(1);
static POLICY_GEN: AtomicU64 = AtomicU64::new(1);
fn epoch_tx() -> &'static tokio::sync::watch::Sender<u64> {
static TX: OnceLock<tokio::sync::watch::Sender<u64>> = OnceLock::new();
TX.get_or_init(|| tokio::sync::watch::channel(EPOCH.load(Ordering::Acquire)).0)
}
pub fn epoch() -> u64 {
EPOCH.load(Ordering::Acquire)
}
pub fn policy_gen() -> u64 {
POLICY_GEN.load(Ordering::Acquire)
}
pub(crate) fn bump_policy_gen() -> u64 {
POLICY_GEN.fetch_add(1, Ordering::AcqRel) + 1
}
pub(crate) fn bump_epoch() -> u64 {
let next = EPOCH.fetch_add(1, Ordering::AcqRel) + 1;
crate::net::forget_clients();
#[cfg(not(target_arch = "wasm32"))]
bridge::abort_where(|c| c.ticket.is_some_and(|t| t.epoch < next));
epoch_tx().send_replace(next);
next
}
pub async fn changed(since: u64) {
let mut rx = epoch_tx().subscribe();
loop {
if *rx.borrow_and_update() != since || epoch() != since {
return;
}
if rx.changed().await.is_err() {
std::future::pending::<()>().await;
}
}
}
pub(crate) fn on_session_installed(prev: u64, next: u64) {
if prev != next {
bump_epoch();
}
}
static STRICT: AtomicBool = AtomicBool::new(false);
pub fn strict_boot() {
STRICT.store(true, Ordering::Release);
}
pub(crate) fn is_strict() -> bool {
STRICT.load(Ordering::Acquire)
}
#[cfg(test)]
pub(crate) fn set_strict_for_test(on: bool) {
STRICT.store(on, Ordering::Release);
}
pub fn preference() -> Option<Kind> {
prefs::live().kind()
}
pub fn set_preference(kind: Option<Kind>) {
prefs::set_kind(&crate::db::live_session(), kind);
}
pub fn state() -> TransportState {
state_for(preference())
}
fn state_for(pref: Option<Kind>) -> TransportState {
match pref {
None => TransportState::Unknown,
Some(Kind::Clearnet) => TransportState::Clearnet,
Some(k) => match host::active() {
Some(a) if a.kind == k && a.owner() == crate::db::live_session_id() && a.transport().ready() => {
TransportState::Active { kind: k }
}
_ => TransportState::RequiredButInactive { kind: k },
},
}
}
pub fn blocked_reason() -> Option<String> {
match state() {
TransportState::Clearnet | TransportState::Active { .. } => None,
TransportState::Unknown => Some(ConnectError::NotReady(None).text()),
TransportState::RequiredButInactive { kind } => Some(blocked_error(kind).text()),
}
}
fn blocked_error(k: Kind) -> ConnectError {
match status::reason_for(k) {
Some(r) => ConnectError::Blocked(r.text),
None => ConnectError::NotReady(Some(k)),
}
}
pub fn egress(owner: u64, lane: Lane, host: &str, port: u16) -> Egress {
if owner != crate::db::live_session_id() {
return Egress::Refuse(ConnectError::Stale);
}
let dest = match Dest::parse(host) {
Some(d) => d,
None => return Egress::Refuse(ConnectError::Refused(Refusal::BadName)),
};
let pref = preference();
if let Some(r) = route::pre_route(pref.unwrap_or(Kind::Clearnet), &dest) {
return Egress::Refuse(ConnectError::Refused(r));
}
match state_for(pref) {
TransportState::Clearnet => Egress::Direct,
TransportState::Unknown => Egress::Refuse(ConnectError::NotReady(None)),
TransportState::RequiredButInactive { kind } => Egress::Refuse(blocked_error(kind)),
TransportState::Active { kind } => {
let Some(active) = host::active().filter(|a| a.kind == kind) else {
return Egress::Refuse(ConnectError::NotReady(Some(kind)));
};
let p = prefs::live();
let (aliases, config) = (p.aliases(), p.config(kind));
let ctx = RouteCtx { aliases: &aliases, config: &config };
match active.transport().route(&dest, port, &ctx) {
Route::Refuse(r) => Egress::Refuse(ConnectError::Refused(r)),
_ => Egress::Proxy(Ticket { owner, epoch: epoch(), lane }),
}
}
}
}
pub(crate) fn host_port(url_or_host: &str) -> Option<(String, u16)> {
if let Ok(u) = url::Url::parse(url_or_host) {
if let (Some(h), Some(p)) = (u.host_str(), u.port_or_known_default()) {
return Some((h.trim_start_matches('[').trim_end_matches(']').to_string(), p));
}
}
if url_or_host.contains("://") || url_or_host.is_empty() {
return None;
}
Some((url_or_host.to_string(), 443))
}
pub fn refuses(url_or_host: &str) -> Option<String> {
refusal(url_or_host).map(|r| r.text())
}
pub fn refusal(url_or_host: &str) -> Option<Refusal> {
let (host, port) = host_port(url_or_host)?;
let dest = Dest::parse(&host)?;
let pref = preference()?;
if let Some(r) = route::pre_route(pref, &dest) {
return Some(r);
}
if let TransportState::Active { kind } = state_for(Some(pref)) {
let active = host::active().filter(|a| a.kind == kind)?;
let p = prefs::live();
let (aliases, config) = (p.aliases(), p.config(kind));
if let Route::Refuse(r) = active.transport().route(&dest, port, &RouteCtx { aliases: &aliases, config: &config }) {
return Some(r);
}
}
None
}
pub fn route_view(url: &str) -> status::RouteView {
status::route_view(url)
}
pub async fn wait_ready(max: std::time::Duration) -> Result<(), String> {
let deadline = web_time::Instant::now() + max;
loop {
let since = epoch();
match state() {
TransportState::Clearnet | TransportState::Active { .. } => return Ok(()),
TransportState::Unknown => {
if web_time::Instant::now() >= deadline {
return Err(ConnectError::NotReady(None).text());
}
}
TransportState::RequiredButInactive { kind } => {
if status::waits_for_user() || web_time::Instant::now() >= deadline {
return Err(blocked_error(kind).text());
}
}
}
let left = deadline.saturating_duration_since(web_time::Instant::now());
let _ = crate::rt::time::timeout(left.min(std::time::Duration::from_secs(1)), changed(since)).await;
}
}