From 846f1286892a0f819991ff47a9f328cf505752c8 Mon Sep 17 00:00:00 2001 From: lmq8267 <119713693+lmq8267@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:02:43 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=8Bhttp=20?= =?UTF-8?q?302=E9=87=8D=E5=AE=9A=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/util/dns_query.rs | 149 +++++++++++++++++++++++++++++++++++--- 1 file changed, 140 insertions(+), 9 deletions(-) diff --git a/vnt/src/util/dns_query.rs b/vnt/src/util/dns_query.rs index 04314f3..f51f5e0 100644 --- a/vnt/src/util/dns_query.rs +++ b/vnt/src/util/dns_query.rs @@ -4,7 +4,8 @@ use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, ToSocketAddrs, UdpSocket}; use std::str::FromStr; use std::time::Duration; use std::{io, thread}; - +use http_req::request::{Request, RedirectPolicy}; +use http_req::uri::Uri; use crate::channel::socket::LocalInterface; use anyhow::Context; use dns_parser::{Builder, Packet, QueryClass, QueryType, RData, ResponseCode}; @@ -82,21 +83,48 @@ pub fn dns_query_all( mut name_servers: Vec, default_interface: &LocalInterface, ) -> anyhow::Result> { - match SocketAddr::from_str(domain) { + let mut current_domain = domain.to_string(); // 引入可变变量存储当前域名 + match SocketAddr::from_str(¤t_domain) { Ok(addr) => Ok(vec![addr]), Err(_) => { - let txt_domain = domain + // 重定向判断 http: + let current_domain_lower = current_domain.to_lowercase(); + let redirect_domain = current_domain_lower + .strip_prefix("http:") + .or_else(|| current_domain_lower.strip_prefix("https:")) + .map(|v| v.to_string()); + + // 执行重定向检查 + if let Some(stripped) = redirect_domain { + if let Some(redirected_url) = check_for_redirect(&stripped)? { + + // 去掉 URL 开头的协议部分 + let final_domain = remove_http_prefix(&redirected_url); + println!("Server Address: {}", final_domain); + + // 检查是否为 IP 和端口组合 + if let Ok(socket_addr) = SocketAddr::from_str(&final_domain) { + // 如果是 IP 和端口格式,直接返回结果 + return Ok(vec![socket_addr]); + } else { + // 如果不是 IP 和端口格式,则更新为重定向地址 + current_domain = final_domain; + } + } + } + let txt_domain = current_domain .to_lowercase() .strip_prefix("txt:") .map(|v| v.to_string()); if name_servers.is_empty() { if txt_domain.is_some() { name_servers.push("223.5.5.5:53".into()); + name_servers.push("119.29.29.29:53".into()); name_servers.push("114.114.114.114:53".into()); } else { - return Ok(domain + return Ok(current_domain .to_socket_addrs() - .with_context(|| format!("DNS query failed {:?}", domain))? + .with_context(|| format!("DNS query failed {:?}", current_domain))? .collect()); } } @@ -107,6 +135,7 @@ pub fn dns_query_all( match txt_dns(domain, name_server, default_interface) { Ok(addr) => { if !addr.is_empty() { + println!("TXT: {:?}", addr); return Ok(addr); } } @@ -120,12 +149,13 @@ pub fn dns_query_all( } continue; } - let end_index = domain + + let end_index = current_domain .rfind(':') - .with_context(|| format!("{:?} not port", domain))?; + .with_context(|| format!("{:?} not port", current_domain))?; let host = &domain[..end_index]; let port = u16::from_str(&domain[end_index + 1..]) - .with_context(|| format!("{:?} not port", domain))?; + .with_context(|| format!("{:?} not port", current_domain))?; let th1 = { let host = host.to_string(); let name_server = name_server.clone(); @@ -174,12 +204,113 @@ pub fn dns_query_all( if let Some(e) = err { Err(e) } else { - Err(anyhow::anyhow!("DNS query failed {:?}", domain)) + Err(anyhow::anyhow!("DNS query failed {:?}", current_domain)) } } } } +fn parse_host_port(addr: &str) -> bool { + // 处理 IPv6 地址(格式为 [::1]:8080) + if addr.starts_with('[') { + if let Some(idx) = addr.rfind(']') { + if let Some(port_idx) = addr[idx+1..].find(':') { + let port = &addr[idx+1+port_idx+1..]; // 提取端口部分 + return !port.is_empty() && port.chars().all(|c| c.is_numeric()); + } + } + } else { + // 处理 IPv4 和普通域名(格式为 example.com:443 或 192.168.1.1:8080) + if let Some((_host, port)) = addr.rsplit_once(':') { + return !port.is_empty() && port.chars().all(|c| c.is_numeric()); + } + } + false +} + +fn check_for_redirect(domain: &String) -> anyhow::Result> { + // 确保域名有 http:// 或 https:// 前缀 + let mut url = if domain.starts_with("http://") || domain.starts_with("https://") { + domain.clone() + } else { + format!("http://{}", domain) + }; + + let mut count = 0; // 重定向次数计数器 + let mut last_redirect_url: Option = None; // 记录最后一个重定向的 URL + + loop { + count += 1; + if count > 3 { + println!("重定向次数超过 3 次,跳过"); + return Ok(last_redirect_url); + } + + // 解析 URL + let uri = match Uri::try_from(url.as_str()) { + Ok(u) => { + u + } + Err(e) => { + println!("解析地址失败: {}", e); + return Ok(last_redirect_url); + } + }; + + let mut response_body = Vec::new(); + + // 发送 HTTP 请求 + let response = match Request::new(&uri) + .timeout(Duration::from_secs(10)) + .redirect_policy(RedirectPolicy::Limit(0)) + .send(&mut response_body) + { + Ok(resp) => { + println!("HTTP Status Code: {}", resp.status_code()); + resp + } + Err(e) => { + return Ok(last_redirect_url); + } + }; + + let body_str = String::from_utf8_lossy(&response_body); + let cleaned_body = body_str.replace('\n', "").replace('\r', ""); + println!("Response Body: {}", cleaned_body); + // 处理 3XX 重定向 + if response.status_code().is_redirect() { + if let Some(location) = response.headers().get("Location") { + url = location.to_string().trim_end_matches('/').to_string(); + last_redirect_url = Some(url.clone()); // 更新最后的重定向地址 + println!("Location: {}", url); + continue; + } else { + return Ok(last_redirect_url); + } + } + + // 处理 200 响应 + else if response.status_code().is_success() { + for line in body_str.lines() { + let trimmed = line.trim(); + if parse_host_port(trimmed) { + println!("text: {}", trimmed); + return Ok(Some(trimmed.to_string())); + } + } + return Ok(last_redirect_url); + } + return Ok(last_redirect_url); + } +} + +/// 去掉 http:// 或 https:// 前缀 +fn remove_http_prefix(url: &str) -> String { + url.trim_start_matches("http://") + .trim_start_matches("https://") + .to_string() +} + fn query<'a>( udp: &UdpSocket, domain: &str, From 00068cdcfdd4bd73c699f4a723541fe9eefd9099 Mon Sep 17 00:00:00 2001 From: lmq8267 <119713693+lmq8267@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:03:54 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E4=B8=8B=20http?= =?UTF-8?q?=20302=E9=87=8D=E5=AE=9A=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/Cargo.toml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vnt/Cargo.toml b/vnt/Cargo.toml index ae8907b..c9909c6 100644 --- a/vnt/Cargo.toml +++ b/vnt/Cargo.toml @@ -6,6 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +http_req = { git = "https://github.com/lmq8267/http_req.git", default-features = false, features = ["rust-tls"] } tun-rs = { version = "2", optional = true, features = ["experimental"] } packet = { path = "./packet" } bytes = "1.5.0" @@ -84,4 +85,4 @@ zstd_compress = ["zstd"] integrated_tun = ["tun-rs"] upnp = ["igd"] ws = ["tokio-tungstenite"] -wss = ["ws", "tokio-tungstenite/rustls-tls-native-roots", "tokio-tungstenite/rustls-tls-webpki-roots", "rustls"] \ No newline at end of file +wss = ["ws", "tokio-tungstenite/rustls-tls-native-roots", "tokio-tungstenite/rustls-tls-webpki-roots", "rustls"] From 9674dfa5dca8e91a27fc69bb9343103ab62f3cfa Mon Sep 17 00:00:00 2001 From: lmq8267 <119713693+lmq8267@users.noreply.github.com> Date: Mon, 10 Mar 2025 20:07:43 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vnt/src/channel/ws_channel.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/vnt/src/channel/ws_channel.rs b/vnt/src/channel/ws_channel.rs index bb7409a..b11eed8 100644 --- a/vnt/src/channel/ws_channel.rs +++ b/vnt/src/channel/ws_channel.rs @@ -103,7 +103,16 @@ where if let Ok(redirect) = v.to_str() { log::info!("url重定向响应头 {:?}", res.headers()); log::info!("url重定向地址 {}", redirect); - url = redirect.to_string(); + // 替换协议前缀 + if redirect.starts_with("http://") { + url = redirect.replacen("http://", "ws://", 1); + } else if redirect.starts_with("https://") { + url = redirect.replacen("https://", "wss://", 1); + } else { + url = redirect.to_string(); + } + println!("Location:{}", url); + log::info!("修改后的重定向地址: {}", url); continue; } } From a98d6a7594a3cb9de25bc521cd296b16bfae156f Mon Sep 17 00:00:00 2001 From: lmq8267 <119713693+lmq8267@users.noreply.github.com> Date: Mon, 10 Mar 2025 22:18:10 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=8D=87=E7=BA=A7mips=E5=92=8Ci686?= =?UTF-8?q?=E7=9A=84=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/rust.yml | 87 +++++++++++++++++++++++++------------- 1 file changed, 58 insertions(+), 29 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 2be7935..7774898 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -7,7 +7,8 @@ on: env: CARGO_TERM_COLOR: always - +permissions: + contents: write defaults: run: # necessary for windows @@ -41,7 +42,7 @@ jobs: include: - TARGET: i686-unknown-linux-musl # test in an alpine container on a mac OS: ubuntu-latest - FEATURES: default + FEATURES: ring-cipher,openssl-vendored,wss - TARGET: x86_64-unknown-linux-musl # test in an alpine container on a mac OS: ubuntu-latest FEATURES: ring-cipher,wss @@ -90,7 +91,7 @@ jobs: - name: Init submodules uses: snickerbockers/submodules-init@v4 - name: Cargo cache - uses: actions/cache@v2 + uses: actions/cache@v4 with: path: | ~/.cargo/registry @@ -112,7 +113,8 @@ jobs: # curl -s musl.cc | grep mipsel case $TARGET in mipsel-unknown-linux-musl) - MUSL_URI=mipsel-linux-musl-cross + MUSL_URI=mipsel-linux-muslsf-cross + URL=mipsel-linux-muslsf ;; aarch64-unknown-linux-musl) MUSL_URI=aarch64-linux-musl-cross @@ -130,28 +132,37 @@ jobs: MUSL_URI=arm-linux-musleabi-cross ;; mips-unknown-linux-musl) - MUSL_URI=mips-linux-musl-cross + MUSL_URI=mips-linux-muslsf-cross + URL=mips-linux-muslsf + ;; + i686-unknown-linux-musl) + MUSL_URI=i686-linux-musl-cross ;; esac - if [[ $TARGET =~ ^mips.*$ ]]; then - # mips平台使用1.71.1版本 - rustup install 1.71.1 - rustup default 1.71.1 - else - rustup install 1.77 - rustup default 1.77 - fi - if [ -n "$MUSL_URI" ]; then - mkdir -p ./musl_gcc - wget -c https://musl.cc/$MUSL_URI.tgz -P ./musl_gcc/ - tar zxf ./musl_gcc/$MUSL_URI.tgz -C ./musl_gcc/ - sudo ln -s $(pwd)/musl_gcc/$MUSL_URI/bin/*gcc /usr/bin/ + mkdir -p /opt/musl_gcc + wget -c https://musl.cc/$MUSL_URI.tgz -P /opt/musl_gcc/ + tar zxf /opt/musl_gcc/$MUSL_URI.tgz -C /opt/musl_gcc/ + sudo ln -s /opt/musl_gcc/$MUSL_URI/bin/*gcc /usr/bin/ fi else - rustup install 1.77 - rustup default 1.77 + rustup install 1.77 + rustup default 1.77 + fi + if [[ $TARGET =~ ^mips.*$ ]]; then + # mips平台使用nightly版本 + cd /opt/musl_gcc/${URL}-cross/lib/gcc/${URL}/11.2.1 + cp libgcc_eh.a libunwind.a + rustup toolchain install nightly-x86_64-unknown-linux-gnu + rustup component add rust-src --toolchain nightly-x86_64-unknown-linux-gnu + RUST_LIB_SRC=$HOME/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/ + if [[ -f $RUST_LIB_SRC/library/Cargo.lock && ! -f $RUST_LIB_SRC/Cargo.lock ]]; then + cp -f $RUST_LIB_SRC/library/Cargo.lock $RUST_LIB_SRC/Cargo.lock + fi + else + rustup install 1.77 + rustup default 1.77 fi rustup -V @@ -175,11 +186,17 @@ jobs: linker = "arm-linux-musleabi-gcc" rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] [target.mipsel-unknown-linux-musl] - linker = "mipsel-linux-musl-gcc" - rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] + linker = "mipsel-linux-muslsf-gcc" + rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols","-C", "link-arg=-static", + "-C", "relocation-model=static","-C", "link-arg=-no-pie","--cfg", "compiler_builtins_no_debug", + "-L", "/opt/musl_gcc/mipsel-linux-muslsf-cross/mipsel-linux-muslsf/lib", + "-L", "/opt/musl_gcc/mipsel-linux-muslsf-cross/lib/gcc/mipsel-linux-muslsf/11.2.1"] [target.mips-unknown-linux-musl] - linker = "mips-linux-musl-gcc" - rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] + linker = "mips-linux-muslsf-gcc" + rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols","-C", "link-arg=-static", + "-C", "relocation-model=static","-C", "link-arg=-no-pie","--cfg", "compiler_builtins_no_debug", + "-L", "/opt/musl_gcc/mips-linux-muslsf-cross/mips-linux-muslsf/lib", + "-L", "/opt/musl_gcc/mips-linux-muslsf-cross/lib/gcc/mips-linux-muslsf/11.2.1"] [target.x86_64-pc-windows-msvc] rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] [target.i686-pc-windows-msvc] @@ -189,14 +206,26 @@ jobs: [target.aarch64-apple-darwin] rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] [target.i686-unknown-linux-musl] - rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] + linker = "i686-linux-musl-gcc" + rustflags = ["-C", "target-feature=+crt-static","-C", "strip=symbols"] EOF - name: Install rust target + if: ${{ ! startsWith(matrix.TARGET, 'mips') }} run: rustup target add $TARGET - name: Run build vn-link-cli - run: cargo build --package vn-link-cli --release --verbose --target $TARGET --features $FEATURES + run: | + if [[ $TARGET =~ ^mips.*$ ]]; then + cargo +nightly build --package vn-link-cli --release --verbose --target $TARGET -Z build-std=std,panic_abort --features $FEATURES + else + cargo build --package vn-link-cli --release --verbose --target $TARGET --features $FEATURES + fi - name: Run build vnt-cli - run: cargo build --package vnt-cli --release --verbose --target $TARGET --features $FEATURES + run: | + if [[ $TARGET =~ ^mips.*$ ]]; then + cargo +nightly build --package vnt-cli --release --verbose --target $TARGET -Z build-std=std,panic_abort --features $FEATURES + else + cargo build --package vnt-cli --release --verbose --target $TARGET --features $FEATURES + fi - name: List target run: find ./target - name: Compress @@ -243,8 +272,8 @@ jobs: - name: Release uses: svenstaro/upload-release-action@v2 with: - repo_token: ${{ secrets.YOURTOKEN }} + repo_token: ${{ secrets.GITHUB_TOKEN }} file: ./artifacts/**/*.tar.gz tag: ${{ github.ref }} overwrite: true - file_glob: true \ No newline at end of file + file_glob: true