Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 40 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,44 @@ jobs:
check:
name: Check
runs-on: ubuntu-latest
strategy:
matrix:
crate:
- --features tor
- --features socks
- --target wasm32-wasip2
- --target wasm32-unknown-unknown
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Check
run: make check
- name: Checkout
uses: actions/checkout@v4

- name: Install deps
run: sudo apt update && sudo apt install -y libdbus-1-dev pkg-config

- name: Install WASI SDK
if: "contains(matrix.crate, 'wasm32-wasip2')"
run: |
wget https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-29/wasi-sdk-29.0-x86_64-linux.tar.gz
tar xvf wasi-sdk-29.0-x86_64-linux.tar.gz

WASI_SDK="$(pwd)/wasi-sdk-29.0-x86_64-linux"

echo "WASI_SDK=$WASI_SDK" >> $GITHUB_ENV
echo "CC_wasm32_wasip2=$WASI_SDK/bin/clang" >> $GITHUB_ENV
echo "AR_wasm32_wasip2=$WASI_SDK/bin/llvm-ar" >> $GITHUB_ENV
echo "CFLAGS_wasm32_wasip2=--sysroot=$WASI_SDK/share/wasi-sysroot" >> $GITHUB_ENV

- name: Rust Cache
uses: Swatinem/rust-cache@v2.7.8
with:
key: ${{ matrix.crate }}

- name: Check
run: cargo check ${{ matrix.crate }}

- name: Clippy
run: cargo clippy ${{ matrix.crate }} -- -D warnings

- name: Test
if: "!contains(matrix.crate, 'wasm32-unknown-unknown')"
run: cargo test ${{ matrix.crate }}
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ tor-launch-service = ["tor", "arti-client?/onion-service-service", "dep:tor-hsse
futures-util = { version = "0.3", default-features = false, features = ["std", "sink"] }
url = { version = "2.5", default-features = false }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
[target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies]
tokio = { version = "1", features = ["net", "time"] }
tokio-rustls = { version = "0.26", default-features = false, features = ["ring", "tls12"] } # Required to enable the necessary features for tokio-tungstenite
tokio-socks = { version = "0.5", optional = true }
Expand All @@ -33,7 +33,7 @@ tor-hsservice = { version = "0.28", default-features = false, optional = true }
tor-hsrproxy = { version = "0.28", default-features = false, optional = true }
tor-rtcompat = { version = "0.28", default-features = false, features = ["rustls", "tokio"], optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
async-utility = "0.3"
futures = { version = "0.3", default-features = false, features = ["std"] } # TODO: remove this
js-sys = "0.3"
Expand Down
7 changes: 5 additions & 2 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[toolchain]
channel = "stable"
profile = "minimal"
components = ["clippy", "rust-docs", "rust-src", "rustc", "rustfmt"]
targets = ["wasm32-unknown-unknown"]
components = ["clippy", "rust-docs", "rustfmt"]
targets = [
"wasm32-wasip2", # WASI
"wasm32-unknown-unknown", # Browser and JS environments
]
43 changes: 32 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,34 @@
#![warn(clippy::large_futures)]
#![cfg_attr(feature = "default", doc = include_str!("../README.md"))]

#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "socks",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
use std::net::SocketAddr;
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "tor",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
use std::path::{Path, PathBuf};
use std::time::Duration;

pub use futures_util;
pub use url::{self, Url};

pub mod message;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub mod native;
pub mod prelude;
mod socket;
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub mod wasm;

pub use self::message::Message;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub use self::native::Error;
pub use self::socket::WebSocket;
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
pub use self::wasm::Error;

#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand All @@ -37,10 +43,16 @@ pub enum ConnectionMode {
#[default]
Direct,
/// Custom proxy
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "socks",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
Proxy(SocketAddr),
/// Embedded tor client
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "tor",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
Tor {
/// Path for cache and state data
///
Expand All @@ -58,7 +70,10 @@ impl ConnectionMode {

/// Proxy
#[inline]
#[cfg(all(feature = "socks", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "socks",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
pub fn proxy(addr: SocketAddr) -> Self {
Self::Proxy(addr)
}
Expand All @@ -68,7 +83,10 @@ impl ConnectionMode {
/// This not work on `android` and/or `ios` targets.
/// Use [`Connection::tor_with_path`] instead.
#[inline]
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "tor",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
pub fn tor() -> Self {
Self::Tor { custom_path: None }
}
Expand All @@ -77,7 +95,10 @@ impl ConnectionMode {
///
/// Specify a path where to store data
#[inline]
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "tor",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
pub fn tor_with_path<P>(data_path: P) -> Self
where
P: AsRef<Path>,
Expand Down
34 changes: 17 additions & 17 deletions src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

use std::{fmt, str};

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use tokio_tungstenite::tungstenite::protocol::frame::coding::CloseCode;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use tokio_tungstenite::tungstenite::protocol::CloseFrame as TungsteniteCloseFrame;
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
use tokio_tungstenite::tungstenite::protocol::Message as TungsteniteMessage;

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CloseFrame {
/// The reason as a code.
Expand All @@ -29,20 +29,20 @@ pub enum Message {
/// A ping message with the specified payload
///
/// The payload here must have a length less than 125 bytes
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Ping(Vec<u8>),
/// A pong message with the specified payload
///
/// The payload here must have a length less than 125 bytes
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Pong(Vec<u8>),
/// A close message with the optional close frame.
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Close(Option<CloseFrame>),
}

impl Message {
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
pub(crate) fn from_native(msg: TungsteniteMessage) -> Self {
match msg {
TungsteniteMessage::Text(text) => Self::Text(text.to_string()),
Expand All @@ -62,11 +62,11 @@ impl Message {
match self {
Self::Text(string) => string.len(),
Self::Binary(data) => data.len(),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Ping(data) => data.len(),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Pong(data) => data.len(),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Close(data) => data.as_ref().map(|d| d.reason.len()).unwrap_or(0),
}
}
Expand All @@ -82,11 +82,11 @@ impl Message {
match self {
Self::Text(string) => Some(string.as_str()),
Self::Binary(data) => str::from_utf8(data).ok(),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Ping(data) | Self::Pong(data) => str::from_utf8(data).ok(),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Close(None) => Some(""),
#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
Self::Close(Some(frame)) => Some(&frame.reason),
}
}
Expand All @@ -102,7 +102,7 @@ impl fmt::Display for Message {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl From<CloseFrame> for TungsteniteCloseFrame {
fn from(frame: CloseFrame) -> Self {
Self {
Expand All @@ -112,7 +112,7 @@ impl From<CloseFrame> for TungsteniteCloseFrame {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl From<Message> for TungsteniteMessage {
fn from(msg: Message) -> Self {
match msg {
Expand All @@ -125,7 +125,7 @@ impl From<Message> for TungsteniteMessage {
}
}

#[cfg(not(target_arch = "wasm32"))]
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
impl From<TungsteniteCloseFrame> for CloseFrame {
fn from(frame: TungsteniteCloseFrame) -> Self {
Self {
Expand Down
5 changes: 4 additions & 1 deletion src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#![doc(hidden)]

pub use crate::message::*;
#[cfg(all(feature = "tor", not(target_arch = "wasm32")))]
#[cfg(all(
feature = "tor",
not(all(target_arch = "wasm32", target_os = "unknown"))
))]
pub use crate::native::tor::{self, *};
pub use crate::*;
Loading
Loading