Mirrowel commited on
Commit
65fe549
·
1 Parent(s): 48b6b15

feat(quota-viewer): ✨ add intelligent URL scheme detection for local and private hosts

Browse files

Previous implementation used https for any host containing a dot, which incorrectly applied https to private IP addresses. The new logic properly detects local hosts and private networks (localhost, 127.0.0.1, 192.168.x.x, 10.x.x.x, 172.16-31.x.x) and uses http for them while maintaining https for external domains and port 443.

Files changed (1) hide show
  1. src/proxy_app/quota_viewer.py +32 -3
src/proxy_app/quota_viewer.py CHANGED
@@ -84,6 +84,36 @@ def create_progress_bar(percent: Optional[int], width: int = 10) -> str:
84
  return "▓" * filled + "░" * (width - filled)
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  def format_cooldown(seconds: int) -> str:
88
  """Format cooldown seconds as human-readable string."""
89
  if seconds < 60:
@@ -131,8 +161,7 @@ class QuotaViewer:
131
  return "http://127.0.0.1:8000"
132
  host = self.current_remote.get("host", "127.0.0.1")
133
  port = self.current_remote.get("port", 8000)
134
- # Use https if port is 443 or host looks like a domain
135
- scheme = "https" if port == 443 or "." in host else "http"
136
  return f"{scheme}://{host}:{port}"
137
 
138
  def check_connection(
@@ -150,7 +179,7 @@ class QuotaViewer:
150
  """
151
  host = remote.get("host", "127.0.0.1")
152
  port = remote.get("port", 8000)
153
- scheme = "https" if port == 443 or "." in host else "http"
154
  url = f"{scheme}://{host}:{port}/"
155
 
156
  headers = {}
 
84
  return "▓" * filled + "░" * (width - filled)
85
 
86
 
87
+ def is_local_host(host: str) -> bool:
88
+ """Check if host is a local/private address (should use http, not https)."""
89
+ if host in ("localhost", "127.0.0.1", "::1"):
90
+ return True
91
+ # Private IP ranges
92
+ if host.startswith("192.168.") or host.startswith("10."):
93
+ return True
94
+ if host.startswith("172."):
95
+ # 172.16.0.0 - 172.31.255.255
96
+ try:
97
+ second_octet = int(host.split(".")[1])
98
+ if 16 <= second_octet <= 31:
99
+ return True
100
+ except (ValueError, IndexError):
101
+ pass
102
+ return False
103
+
104
+
105
+ def get_scheme_for_host(host: str, port: int) -> str:
106
+ """Determine http or https scheme based on host and port."""
107
+ if port == 443:
108
+ return "https"
109
+ if is_local_host(host):
110
+ return "http"
111
+ # For external domains, default to https
112
+ if "." in host:
113
+ return "https"
114
+ return "http"
115
+
116
+
117
  def format_cooldown(seconds: int) -> str:
118
  """Format cooldown seconds as human-readable string."""
119
  if seconds < 60:
 
161
  return "http://127.0.0.1:8000"
162
  host = self.current_remote.get("host", "127.0.0.1")
163
  port = self.current_remote.get("port", 8000)
164
+ scheme = get_scheme_for_host(host, port)
 
165
  return f"{scheme}://{host}:{port}"
166
 
167
  def check_connection(
 
179
  """
180
  host = remote.get("host", "127.0.0.1")
181
  port = remote.get("port", 8000)
182
+ scheme = get_scheme_for_host(host, port)
183
  url = f"{scheme}://{host}:{port}/"
184
 
185
  headers = {}