fix(remote): send the typed --proxy to the server when it resolved to loopback

dl and search keep the typed --proxy value, so --proxy controld:ca --remote sends the server the Control D resolver, not this machine's forwarder. The Control D log names the location and masks the resolver ID in place of 127.0.0.1.
This commit is contained in:
imSp4rky
2026-10-21 11:03:10 -06:00
parent f5bd24b5e9
commit e5c339934c
6 changed files with 45 additions and 6 deletions
+14
View File
@@ -16,6 +16,7 @@ import requests
from requests.adapters import HTTPAdapter
from unshackle.core.proxies.proxy import Proxy
from unshackle.core.utils.redact import REDACTED
log = logging.getLogger("ControlD")
@@ -261,6 +262,7 @@ class LocalProxy(socketserver.ThreadingTCPServer):
# process-wide across provider instances; separate processes share only the account's unshackle-<region> names
FORWARDERS: dict[str, LocalProxy] = {}
REGIONS: dict[str, str] = {} # profile -> region, oldest claim first
LOCATIONS: dict[str, str] = {} # profile -> the location code it redirects through
STATE = threading.Lock()
@@ -337,6 +339,7 @@ class ControlD(Proxy):
self.configured = {x["profile"]: resolver_id(x["resolver"]) for x in pairs}
self.max_profiles = max_profiles
self.resolvers = dict(self.configured)
self.last_profile: Optional[str] = None
self.catalogue = self.get_locations()
@@ -459,6 +462,8 @@ class ControlD(Proxy):
server.resolver.cache.clear()
REGIONS.pop(profile, None)
REGIONS[profile] = query
LOCATIONS[profile] = location
self.last_profile = profile
return resolver
def get_proxy(self, query: str) -> Optional[str]:
@@ -467,6 +472,15 @@ class ControlD(Proxy):
resolver = self.claim(query.lower())
return forwarder(resolver).uri if resolver else None
def last_connection_display(self) -> Optional[str]:
"""Name the location of the last claim, with the resolver ID masked, in place of the loopback URI."""
location = LOCATIONS.get(self.last_profile or "")
if not location:
return None
city = next((x.get("city") for x in self.catalogue if x["PK"] == location), None)
label = f"{location} - {city}" if city else location
return f"({label}): {self.resolvers[self.last_profile][:4]}{REDACTED}@dns.controld.com"
def get_remote_proxy(self, query: str) -> Optional[str]:
"""Get the `controld://` proxy URI for this region, for a server on another machine."""
with STATE:
+9 -2
View File
@@ -920,6 +920,12 @@ def resolve_remote_proxy_arg(proxy_arg: str, providers: Optional[list] = None) -
raise click.ClickException(str(e))
def server_proxy_arg(params: Dict[str, Any]) -> str:
"""The --proxy value to resolve for a server: the one typed when it resolved to a proxy on this machine."""
proxy = params["proxy"]
return (params.get("proxy_arg") or proxy) if is_loopback(proxy) else proxy
_CACHE_HEX_ID_RE = re.compile(r"[0-9a-f]{32,}", re.IGNORECASE)
@@ -1159,7 +1165,8 @@ class RemoteService:
if params.get("no_proxy"):
data["no_proxy"] = True
elif params.get("proxy"):
data["proxy"] = resolve_remote_proxy_arg(params["proxy"], getattr(self.ctx.obj, "proxy_providers", None))
providers = getattr(self.ctx.obj, "proxy_providers", None)
data["proxy"] = resolve_remote_proxy_arg(server_proxy_arg(params), providers)
self.require_reachable_proxy(data["proxy"])
response = self.client.post("/api/search", data)
self.save_returned_cache(response.get("cache") or {}, profile)
@@ -1226,7 +1233,7 @@ class RemoteService:
"rejects the remote session"
)
elif not no_proxy and proxy:
resolved_proxy = resolve_remote_proxy_arg(proxy, providers)
resolved_proxy = resolve_remote_proxy_arg(server_proxy_arg(self.ctx.parent.params), providers)
if resolved_proxy:
create_data["proxy"] = resolved_proxy
query = (self.ctx.parent.params.get("proxy_query") if self.ctx.parent else None) or proxy