Skip to content
Merged
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
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ deploy: ## deploy to cf workers
.PHONY: dev
dev: ## run the project locally
@ npx wrangler dev -c .wrangler.dev.toml

lint:
cargo clippy --all-targets --all-features -- -D warnings
5 changes: 2 additions & 3 deletions src/proxy/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use regex::Regex;

static REGISTRY: &str = "registry-1.docker.io";


fn replace_host(content: &mut str, src: &str, dest: &str) -> Result<String> {

let re = Regex::new(r#"(?P<attr>src|href)(?P<eq>=)(?P<quote>['"]?)(?P<url>(//|https://))"#)
Expand Down Expand Up @@ -112,7 +111,6 @@ pub async fn handler(mut req: Request, uri: Url, dst_host: &str) -> Result<Respo
}
}
(401, "www-authenticate") => value.replace("https://", &format!("https://{}/", my_host)),
(_, "set-cookie") => value.replace(dst_host, &my_host),
_ => value,
};
resp_header.set(&key, &new_value)?;
Expand All @@ -121,9 +119,10 @@ pub async fn handler(mut req: Request, uri: Url, dst_host: &str) -> Result<Respo
let _ = resp_header.set("access-control-allow-origin", "*");
if let Some(s) = resp_header.get("content-type")? {
if s.contains("text/html") {
let _ = resp_header.delete("content-encoding");
let _ = resp_header.set("set-cookie", format!("{}={}; Path=/; Max-Age=3600", COOKIE_HOST_KEY, dst_host).as_str());
let mut body = response.text().await?;
let newbody = replace_host(&mut body, dst_host, &my_host)?;
let _ = resp_header.delete("content-encoding");
let resp = Response::builder()
.with_headers(resp_header)
.with_status(status)
Expand Down
43 changes: 11 additions & 32 deletions src/proxy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,31 +204,21 @@ pub async fn handler(req: Request, cx: RouteContext<()>) -> Result<Response> {
.and_then(|cookie| get_cookie_by_name(&cookie, COOKIE_HOST_KEY));

let (mut domain, port, mut path) = parse_path(&origin_path);
// when not resolve, will try find domain by cookie.
let mut notresolve= true;
// when only domain, will update cookie in response.
let mut onlydomain = false;
let scheme = "https";

match domain {
Some(d) if d.contains('.') => {
if (dns::is_cf_address(&Address::Domain(d)).await).is_ok() {
notresolve = false;
if path.is_none() || path.as_ref().unwrap().len()<2 {
onlydomain = true;
}
}
},
_ => {},
}

match (notresolve, &cookie_host) {
(true, Some(host)) => {
// when not resolve, will try find domain by cookie.
let resolve = match domain {
Some(d) => d.contains('.') && dns::is_cf_address(&Address::Domain(d)).await.is_ok(),
_ => false,
};

match (resolve, &cookie_host) {
(false, Some(host)) => {
domain = Some(host.as_ref());
path = Some(origin_path.as_str());
}
(true, None) => return Response::error("Not Found", 404),
(false, _) => {},
(false, None) => return Response::error("Not Found", 404),
(true, _) => {},
}

let host = domain.unwrap();
Expand All @@ -250,18 +240,7 @@ pub async fn handler(req: Request, cx: RouteContext<()>) -> Result<Response> {
.join("&")
.as_str());
}
let mut resp = api::handler(req, Url::parse(&url)?, host).await?;
match resp.headers().get("content-type")? {
Some(s) if s.contains("text/html") => {
if onlydomain {
console_debug!("set cookie domain: {:?}", host);
let _ = resp.headers_mut()
.set("set-cookie", format!("{}={}; Path=/; Max-Age=3600", COOKIE_HOST_KEY, host).as_str());
}
}
_ => {}
}
Ok(resp)
api::handler(req, Url::parse(&url)?, host).await
}
}
}
Expand Down
Loading