@@ -296,7 +296,10 @@ async def connect(self) -> None:
296296 async with ReTunnelAPIClient (api_url ) as api :
297297 try :
298298 # First try to reactivate the existing token
299- result = await api .reactivate_token (old_token )
299+ if old_token :
300+ result = await api .reactivate_token (old_token )
301+ else :
302+ result = await api .register_user ()
300303 self .auth_token = result .get ("auth_token" )
301304 if self .auth_token :
302305 await config_manager .set_auth_token (
@@ -610,12 +613,18 @@ async def _handle_proxy_connection(
610613
611614 if not tunnel :
612615 self .logger .error (f"No tunnel found for URL: { url } " )
613- self .logger .error (f"Available tunnels: { list (self .tunnels .keys ())} " )
616+ self .logger .error (
617+ f"Available tunnels: { list (self .tunnels .keys ())} "
618+ )
614619 for tid , t in self .tunnels .items ():
615- self .logger .error (f" Tunnel { tid } : tunnel_id={ t .tunnel_id } , url={ t .url } " )
620+ self .logger .error (
621+ f" Tunnel { tid } : tunnel_id={ t .tunnel_id } , url={ t .url } "
622+ )
616623 break
617624 else :
618- self .logger .info (f"Found tunnel for URL { url } : tunnel_id={ tunnel .tunnel_id } , tunnel.url={ tunnel .url } " )
625+ self .logger .info (
626+ f"Found tunnel for URL { url } : tunnel_id={ tunnel .tunnel_id } , tunnel.url={ tunnel .url } "
627+ )
619628
620629 # Connect to local service
621630 local_reader , local_writer = await asyncio .open_connection (
@@ -636,8 +645,10 @@ async def _handle_proxy_connection(
636645 query = http_req .get ("query" , "" )
637646 headers = http_req .get ("headers" , {})
638647 body = http_req .get ("body" , b"" )
639-
640- self .logger .info (f"Incoming request: { method } { path } { '?' + query if query else '' } " )
648+
649+ self .logger .info (
650+ f"Incoming request: { method } { path } { '?' + query if query else '' } "
651+ )
641652
642653 # Build request line
643654 if query :
@@ -736,13 +747,19 @@ async def _handle_proxy_connection(
736747 break
737748
738749 # Log response details for debugging
739- self .logger .debug (f"Response status: { status_code } " )
740- self .logger .debug (f"Response headers: { response_headers } " )
750+ self .logger .debug (
751+ f"Response status: { status_code } "
752+ )
753+ self .logger .debug (
754+ f"Response headers: { response_headers } "
755+ )
741756
742757 # Handle redirects (301, 302, 303, 307, 308)
743758 if status_code in [301 , 302 , 303 , 307 , 308 ]:
744- self .logger .info (f"Processing redirect with status { status_code } " )
745-
759+ self .logger .info (
760+ f"Processing redirect with status { status_code } "
761+ )
762+
746763 # Check for Location header (case-insensitive)
747764 location_key = None
748765 location_value = None
@@ -751,41 +768,76 @@ async def _handle_proxy_connection(
751768 location_key = key
752769 location_value = value
753770 break
754-
755- if location_value and tunnel :
756- self .logger .info (f"Original Location header: { location_value } " )
757-
771+
772+ if location_value and tunnel and location_key :
773+ self .logger .info (
774+ f"Original Location header: { location_value } "
775+ )
776+
758777 # Parse the location URL
759778 if location_value .startswith ("/" ):
760779 # Relative URL - prepend tunnel URL
761780 # Remove trailing slash from tunnel URL if present
762781 base_url = tunnel .url .rstrip ("/" )
763- new_location = f"{ base_url } { location_value } "
764- response_headers [location_key ] = new_location
765- self .logger .info (f"Rewritten relative redirect to: { new_location } " )
766- elif location_value .startswith ("http://localhost" ) or location_value .startswith ("http://127.0.0.1" ) or location_value .startswith ("https://localhost" ) or location_value .startswith ("https://127.0.0.1" ):
782+ new_location = (
783+ f"{ base_url } { location_value } "
784+ )
785+ response_headers [location_key ] = (
786+ new_location
787+ )
788+ self .logger .info (
789+ f"Rewritten relative redirect to: { new_location } "
790+ )
791+ elif (
792+ location_value .startswith (
793+ "http://localhost"
794+ )
795+ or location_value .startswith (
796+ "http://127.0.0.1"
797+ )
798+ or location_value .startswith (
799+ "https://localhost"
800+ )
801+ or location_value .startswith (
802+ "https://127.0.0.1"
803+ )
804+ ):
767805 # Absolute URL pointing to localhost (with or without port)
768806 # Extract the path and query
769807 try :
770808 parsed = urlparse (location_value )
771809 # Reconstruct with tunnel URL
772- tunnel_parsed = urlparse (tunnel .url )
773- new_location = urlunparse ((
774- tunnel_parsed .scheme ,
775- tunnel_parsed .netloc ,
776- parsed .path ,
777- parsed .params ,
778- parsed .query ,
779- parsed .fragment
780- ))
781- response_headers [location_key ] = new_location
782- self .logger .info (f"Rewritten absolute localhost redirect to: { new_location } " )
810+ tunnel_parsed = urlparse (
811+ tunnel .url
812+ )
813+ new_location = urlunparse (
814+ (
815+ tunnel_parsed .scheme ,
816+ tunnel_parsed .netloc ,
817+ parsed .path ,
818+ parsed .params ,
819+ parsed .query ,
820+ parsed .fragment ,
821+ )
822+ )
823+ response_headers [location_key ] = (
824+ new_location
825+ )
826+ self .logger .info (
827+ f"Rewritten absolute localhost redirect to: { new_location } "
828+ )
783829 except Exception as e :
784- self .logger .error (f"Error parsing redirect URL: { e } " )
830+ self .logger .error (
831+ f"Error parsing redirect URL: { e } "
832+ )
785833 else :
786- self .logger .info (f"Not rewriting external redirect: { location_value } " )
834+ self .logger .info (
835+ f"Not rewriting external redirect: { location_value } "
836+ )
787837 else :
788- self .logger .warning (f"Redirect response but no Location header found. Headers: { response_headers } " )
838+ self .logger .warning (
839+ f"Redirect response but no Location header found. Headers: { response_headers } "
840+ )
789841
790842 # Send response back
791843 response_meta = {
0 commit comments