Skip to content

Commit

Permalink
add route for sharing the v2ray link
Browse files Browse the repository at this point in the history
  • Loading branch information
amiremohamadi committed May 24, 2024
1 parent 34b9311 commit 58fbf4a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ crate-type = ["cdylib"]

[dependencies]
tokio = { version = "1.28", features = ["io-util", "rt"] }
serde = { version = "1.0", features = ["derive"] }
getrandom = { version = "0.2", features = ["js"] }
worker = "0.0.18"
futures-util = "0.3.28"
pin-project-lite = "0.2"
Expand All @@ -17,7 +19,7 @@ aes-gcm = "0.10"
aes = "0.8"
sha2 = "0.10"
md-5 = "0.10"
getrandom = { version = "0.2", features = ["js"] }


[profile.release]
opt-level = "s"
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use uuid::Uuid;

pub enum Outbound {
Vless(VlessConfig),
Vmess(VlessConfig),
}

pub struct VlessConfig {
pub uuid: Uuid,
pub host: String,
}

pub struct Config {
Expand Down
63 changes: 55 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,79 @@ mod proxy;
use crate::config::{Config, Outbound, VlessConfig};
use crate::proxy::*;

use serde::Serialize;
use uuid::Uuid;
use worker::*;

#[event(fetch)]
async fn main(_: Request, env: Env, _: Context) -> Result<Response> {
async fn main(req: Request, env: Env, _: Context) -> Result<Response> {
let uuid = env
.var("UUID")
.map(|x| Uuid::parse_str(&x.to_string()).unwrap_or_default())?;

let outbound = env
.var("OUTBOUND")
.map(|x| x.to_string())
.unwrap_or_default();

let host = req.url()?.host().map(|x| x.to_string()).unwrap_or_default();

let config = Config {
outbound: Outbound::Vless(VlessConfig { uuid }),
outbound: match outbound.as_str() {
"vmess" => Outbound::Vmess(VlessConfig { uuid, host }),
"vless" => Outbound::Vless(VlessConfig { uuid, host }),
_ => return Err(Error::RustError("invalid outbound".to_string())),
},
};

Router::with_data(config)
.on_async("/", tunnel)
.on("/link", link)
.run(req, env)
.await
}

async fn tunnel(_: Request, cx: RouteContext<Config>) -> Result<Response> {
let WebSocketPair { server, client } = WebSocketPair::new()?;

server.accept()?;
wasm_bindgen_futures::spawn_local(async move {
let events = server.events().unwrap();
let mut stream = match config.outbound {
Outbound::Vless(c) => VmessStream::new(c, &server, events),
};

if let Err(e) = stream.process().await {
if let Err(e) = match cx.data.outbound {
Outbound::Vless(c) => VlessStream::new(c, &server, events).process().await,
Outbound::Vmess(c) => VmessStream::new(c, &server, events).process().await,
} {
console_debug!("error occured during tunneling stream: {}", e);
}
};
});

Response::from_websocket(client)
}

fn link(_: Request, cx: RouteContext<Config>) -> Result<Response> {
#[derive(Serialize)]
struct Link {
description: String,
link: String,
}

let link = match cx.data.outbound {
Outbound::Vmess(c) => {
format!(
"vmess://{}@104.24.30.167:80?encryption=zero&type=ws&security=none&host={}#tunl",
c.uuid, c.host
)
}
Outbound::Vless(c) => {
format!(
"vless://{}@104.24.30.167:80?type=ws&security=none&host={}#tunl",
c.uuid, c.host
)
}
};

Response::from_json(&Link {
link,
description: "visit https://scanner.github1.cloud/ and replace the IP address in the configuration with a clean one".to_string()
})
}
1 change: 1 addition & 0 deletions wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build = { command = "cargo install -q worker-build && worker-build --dev" }

[vars]
UUID = "0fbf4f81-2598-4b6a-a623-0ead4cb9efa8"
OUTBOUND = "vmess"

0 comments on commit 58fbf4a

Please sign in to comment.