Skip to content

Commit

Permalink
✨ update dependencies & various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Reverier-Xu committed Aug 15, 2023
1 parent 5897dac commit 42cb010
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 35 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@

Controlled TCP-over-WebSocket forwarding tunnel.

## Project Status

This tool is rewriting with Rust, maybe unavailable for a long time now.

You can find previous version in GitHub Releases and tags, it's written in go and could work as expected.

## Desktop App

WebSocket Reflector X provides a simple desktop app written in Tauri.
Expand Down
6 changes: 3 additions & 3 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
[package]
name = "wsrx-cli"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
authors = ["Reverier-Xu <[email protected]>"]
description = "Controlled TCP-over-WebSocket forwarding tunnel."

[dependencies]
wsrx = { version = "*", path = "../lib" }
tokio = { version = "1.29", features = ["full"] }
tokio = { version = "1.31", features = ["full"] }
clap = {version = "4.3", features = ["cargo"]}
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio-util = "0.7"
tokio-stream = "0.1"
tokio-tungstenite = { version = "0.18", features = ["rustls-tls-native-roots"] }
tokio-tungstenite = { version = "0.20", features = ["rustls-tls-native-roots"] }
thiserror = "1.0"
anyhow = "1.0"
once_cell = "1.18"
Expand Down
12 changes: 10 additions & 2 deletions cli/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@ use tokio::net::TcpListener;
use tokio::net::TcpStream;
use url::Url;

pub async fn connect(url: impl AsRef<str>, port: Option<u16>) -> anyhow::Result<()> {
pub async fn connect(url: impl AsRef<str>, port: Option<u16>, bind_global: bool) -> anyhow::Result<()> {
let port = port.unwrap_or(0);
let listener = TcpListener::bind(format!("127.0.0.1:{port}")).await?;
let interface = if bind_global {
"0.0.0.0"
} else {
"127.0.0.1"
};
let listener = TcpListener::bind(format!("{interface}:{port}")).await?;
let port = listener.local_addr()?.port();
let url = Url::parse(url.as_ref())?;
if url.scheme() != "ws" && url.scheme() != "wss" {
return Err(anyhow::anyhow!("invalid scheme: {}", url.scheme()));
}
let url = url.as_ref().to_string();
log::info!("Hi, I am not RX, RX is here -> 127.0.0.1:{port}");
if bind_global {
log::info!("This proxy is bound to 0.0.0.0, access it from other device is supported.");
}
loop {
let (tcp, _) = listener.accept().await.expect("Failed to accept tcp connection");
let url = url.clone();
Expand Down
11 changes: 9 additions & 2 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,27 @@ async fn main() {
)
.required(false).value_parser(value_parser!(u16)),
)
.arg(
arg!(
-g --global "Listen on all interfaces"
)
.required(false).value_parser(value_parser!(bool)),
)
.get_matches();

let url = matches
.get_one::<String>("url")
.expect("The aim WebSocket URL is required")
.to_string();
let bind_global = matches.get_one::<bool>("global").unwrap_or(&false).to_owned();
match matches.get_one::<u16>("port") {
Some(port) => {
connection::connect(url, Some(port.to_owned()))
connection::connect(url, Some(port.to_owned()), bind_global)
.await
.expect("Failed to connect to WebSocket server");
}
None => {
connection::connect(url, None)
connection::connect(url, None, bind_global)
.await
.expect("Failed to connect to WebSocket server");
}
Expand Down
2 changes: 1 addition & 1 deletion desktop/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "desktop",
"private": true,
"version": "0.1.4",
"version": "0.1.5",
"type": "module",
"scripts": {
"dev": "vite",
Expand Down
7 changes: 4 additions & 3 deletions desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wsrx-desktop"
version = "0.1.4"
version = "0.1.5"
description = "WSRX Desktop Frontend"
authors = ["Reverier-Xu <[email protected]>"]
edition = "2021"
Expand All @@ -15,15 +15,16 @@ tauri = { version = "1.4", features = ["dialog-all", "fs-all", "protocol-all", "
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
wsrx = { version = "0.1", path = "../../lib" }
tokio = { version = "1.29", features = ["full"] }
tokio = { version = "1.31", features = ["full"] }
tokio-util = "0.7"
tokio-stream = "0.1"
tokio-tungstenite = { version = "0.18", features = ["rustls-tls-native-roots"] }
tokio-tungstenite = { version = "0.20", features = ["rustls-tls-native-roots"] }
thiserror = "1.0"
anyhow = "1.0"
once_cell = "1.18"
url = "2.4"
log = "0.4"
reqwest = "0.11"

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
52 changes: 41 additions & 11 deletions desktop/src-tauri/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,19 @@ pub struct Log {

static RUNTIME_LOG: Lazy<Arc<RwLock<Vec<Log>>>> = Lazy::new(|| Arc::new(RwLock::new(Vec::new())));

pub async fn add_ws_connection(target_addr: impl AsRef<str>, bind_addr: impl AsRef<str>) -> anyhow::Result<()> {
let listener = TcpListener::bind(bind_addr.as_ref().to_owned() + ":0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let url = Url::parse(target_addr.as_ref()).unwrap();
let id = format!("{}#{}", url.host_str().unwrap(), port);
pub async fn add_ws_connection(
target_addr: impl AsRef<str>,
bind_addr: impl AsRef<str>,
) -> anyhow::Result<()> {
let listener = TcpListener::bind(bind_addr.as_ref().trim().to_owned() + ":0").await?;
let port = listener.local_addr()?.port();
let url = Url::parse(target_addr.as_ref().trim())?;
let id = format!(
"{}#{}",
url.host_str()
.ok_or_else(|| anyhow::anyhow!("extract URL host failed"))?,
port
);
let conn = Connection {
id: id.clone(),
url: target_addr.as_ref().to_string(),
Expand Down Expand Up @@ -135,20 +143,42 @@ pub async fn refresh_latency() -> anyhow::Result<()> {
let mut dead_conns = Vec::new();
for (_, conn) in cm.connections.iter_mut() {
let start = std::time::Instant::now();
let (mut ws, _) = match tokio_tungstenite::connect_async(conn.url.as_str()).await {
Ok(tup) => tup,
Err(_) => {
match reqwest::get(
conn.url
.as_str()
.replace("wss://", "https://")
.replace("ws://", "http://"),
)
.await
{
Ok(resp) => {
if !resp.status().is_success() {
let mut logs = RUNTIME_LOG.write().await;
logs.push(Log {
level: "warning".to_owned(),
message: format!(
"Remote server returned non-200 status code: {}, removing link.",
resp.status()
),
addr: conn.url.clone(),
});
dead_conns.push(conn.id.clone());
continue;
}
}
Err(err) => {
let mut logs = RUNTIME_LOG.write().await;
logs.push(Log {
level: "warning".to_owned(),
message: format!("Dead link detected from remote server, removed."),
level: "error".to_owned(),
message: format!(
"Can not connect to remote server with reason: {err}, removing link."
),
addr: conn.url.clone(),
});
dead_conns.push(conn.id.clone());
continue;
}
};
let _ = ws.close(None).await;
conn.latency = start.elapsed().as_millis() as u32;
}
for id in dead_conns {
Expand Down
2 changes: 1 addition & 1 deletion desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "WSRX Desktop",
"version": "0.1.4"
"version": "0.1.5"
},
"tauri": {
"allowlist": {
Expand Down
7 changes: 4 additions & 3 deletions desktop/src/views/ConnectionsView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<span>{{ connection.id }}</span>
<span class="opacity-80">-&gt; localhost:{{ connection.port }}</span>
<span class="rounded-full bg-base-content/5 pl-3 pr-3 text-sm text-success">
{{ connection.latency }} ms
{{ connection.latency === 0 ? "--" : connection.latency }} ms
</span>
<span class="flex-1"></span>
</h2>
Expand All @@ -44,12 +44,13 @@
<h2 class="flex flex-row items-center space-x-4">
<span>{{ connection.id }}</span>
<span class="rounded-full bg-base-content/5 pl-3 pr-3 text-sm text-warning">
{{ connection.latency }} ms
{{ connection.latency === 0 ? "--" : connection.latency }} ms
</span>
</h2>
<p class="text-sm opacity-60">{{ connection.url }}</p>
</div>
<button class="btn btn-sm btn-square btn-ghost hidden group-hover:inline-flex">
<button class="btn btn-sm btn-square btn-ghost hidden group-hover:inline-flex"
@click="closeConnection(connection.id)">
<dismiss20-regular class="w-5 h-5" />
</button>
</div>
Expand Down
6 changes: 3 additions & 3 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wsrx"
version = "0.1.4"
version = "0.1.5"
edition = "2021"
authors = ["Reverier-Xu <[email protected]>"]
description = "Controlled TCP-over-WebSocket forwarding tunnel."
Expand All @@ -11,7 +11,7 @@ readme = "README.md"
license = "MPL-2.0"

[dependencies]
tokio = { version = "1.29", features = ["full"] }
tokio = { version = "1.31", features = ["full"] }
axum = { version = "0.6", features = [
"headers",
"ws",
Expand All @@ -29,6 +29,6 @@ anyhow = "1.0"
toml = "0.7"
uuid = "1.4"
base64 = "0.21"
tokio-tungstenite = { version = "0.18", features = ["rustls-tls-native-roots"] }
tokio-tungstenite = { version = "0.20", features = ["rustls-tls-native-roots"] }
tracing = "0.1"
futures-util = "0.3"

0 comments on commit 42cb010

Please sign in to comment.