@@ -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