Skip to content

Commit

Permalink
Fix hostname issues in CDN
Browse files Browse the repository at this point in the history
  • Loading branch information
ssrlive committed Jun 12, 2024
1 parent bfbac40 commit 8255432
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,7 @@ pub(crate) async fn create_ws_stream<S: AsyncRead + AsyncWrite + Unpin>(

let b64_dst = dst_addr.as_ref().map(|dst_addr| addess_to_b64str(dst_addr, false));

let server_ip = client.server_ip_addr.ok_or("server ip addr")?.to_string();
let uri = format!("ws://{}/{}/", server_ip, tunnel_path);
let uri = format!("ws://{}/{}/", client.server_host, tunnel_path);

let uri = WeirdUri::new(&uri, b64_dst, udp_tunnel, client.client_id.clone());

Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,3 @@ pub(crate) fn b64str_to_address(s: &str, url_safe: bool) -> Result<Address> {
};
Address::try_from(&buf[..]).map_err(|e| e.into())
}

pub(crate) fn combine_addr_and_port(addr: &str, port: u16) -> String {
if addr.contains(':') {
format!("[{}]:{}", addr, port)
} else {
format!("{}:{}", addr, port)
}
}
15 changes: 13 additions & 2 deletions src/weirduri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,23 @@ impl WeirdUri {
}
}

fn combine_addr_and_port(addr: &url::Host<&str>, port: Option<u16>) -> String {
match port {
None => addr.to_string(),
Some(port) => match addr {
url::Host::Domain(domain) => format!("{}:{}", domain, port),
url::Host::Ipv4(ip) => format!("{}:{}", ip, port),
url::Host::Ipv6(ip) => format!("[{}]:{}", ip, port),
},
}
}

impl IntoClientRequest for WeirdUri {
fn into_client_request(self) -> Result<Request> {
let uri = url::Url::parse(&self.uri).map_err(|_| Error::Url(UrlError::NoPathOrQuery))?;

let host = uri.host_str().ok_or(Error::Url(UrlError::EmptyHostName))?;
let host = crate::combine_addr_and_port(host, uri.port().unwrap_or(80));
let host = uri.host().ok_or(Error::from(UrlError::EmptyHostName))?;
let host = combine_addr_and_port(&host, uri.port());

let mut builder = Request::builder()
.method("GET")
Expand Down

0 comments on commit 8255432

Please sign in to comment.