Skip to content

Commit 68adf78

Browse files
authored
update (#2)
* update readme * update readme and config * tj configurable * update action * handle 401 and 30x code, keep cf-connecting-ip header * update readme
1 parent 835666c commit 68adf78

3 files changed

Lines changed: 35 additions & 5 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# tul [English](README.md) | [中文](README_zh.md)
1+
# tul
2+
3+
[English](README.md) | [中文](README_zh.md)
24

35
A lightweight Cloudflare Worker proxy written in Rust/WASM.
46

README_zh.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# tul [English](README.md) | [中文](README_zh.md)
1+
# tul
2+
3+
[English](README.md) | [中文](README_zh.md)
24

35
一个轻量级的基于 Cloudflare Worker 代理,使用 Rust/WASM 编写。
46

src/proxy/api.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ async fn get_hop_headers() -> HashSet<String> {
3030
headers.insert("x-forwarded-server".to_string());
3131

3232
// Cloudflare headers
33-
headers.insert("cf-connecting-ip".to_string());
33+
//headers.insert("cf-connecting-ip".to_string()); // use this otherwise visit cf-cdn blocked.
3434
headers.insert("cf-ray".to_string());
3535
headers.insert("cf-ipcountry".to_string());
3636
headers.insert("cf-request-id".to_string());
@@ -56,6 +56,8 @@ pub async fn handler(mut req: Request, uri: Uri) -> Result<Response> {
5656
get_hop_headers().await
5757
}).await;
5858

59+
let myhost = req.headers().get("host")?.ok_or("Host header not found")?;
60+
5961
let mut request_builder = client.request(method, uri.to_string());
6062
for (key, value) in req.headers().entries() {
6163
if hops.contains(key.as_str()) {
@@ -75,7 +77,7 @@ pub async fn handler(mut req: Request, uri: Uri) -> Result<Response> {
7577
Ok(response) => {
7678
let status = response.status().as_u16();
7779
let headers = Headers::new();
78-
80+
7981
for (key, value) in response.headers().iter() {
8082
if let Ok(value) = value.to_str() {
8183
if hops.contains(key.as_str()) {
@@ -85,7 +87,31 @@ pub async fn handler(mut req: Request, uri: Uri) -> Result<Response> {
8587
if key == "content-encoding" {
8688
continue;
8789
}
88-
headers.append(key.as_str(), value);
90+
match status {
91+
301 | 302 | 303 | 307 | 308 => {
92+
if key == "location" {
93+
if value.starts_with('/') {
94+
let modified_value = format!("/{}{value}",uri.host().unwrap());
95+
headers.append(key.as_str(), &modified_value)?;
96+
continue;
97+
}
98+
if value.starts_with("https://") {
99+
let modified_value = value.replace("https://", &format!("https://{}/)",myhost.as_str()));
100+
headers.append(key.as_str(), &modified_value)?;
101+
continue;
102+
}
103+
}
104+
}
105+
401 => {
106+
if key == "www-authenticate" {
107+
let modified_value = value.replace("https://", &format!("https://{}/",myhost.as_str()));
108+
headers.append(key.as_str(), &modified_value)?;
109+
continue;
110+
}
111+
}
112+
_ => {}
113+
}
114+
headers.append(key.as_str(), value)?;
89115
}
90116
}
91117

0 commit comments

Comments
 (0)