Files
rengine/shared/services/proxy_resolve.py
T
Yogesh Ojha a548afffa2 feat(settings): tables that save on change, Users, sheets on their pages
Settings has six tabs: General, API keys, Proxies, Notifications, AI and
Users. Each tab is one card whose controls save on change; list tabs are
tables with a row menu and their Add action in the tab row. A non-admin
sees General and AI, read-only.

Out-of-band testing is the Callback server sheet on the Arsenal Nuclei
tab, with a Need a callback count that filters the library to the same
rows. Bounty Hub settings are a sheet on the Bounty Hub page. The old
settings routes redirect to both.

- API keys store the last test result on the row and clear it when the
  key changes. Providers report whether a test exists. The usage count
  is gone: only ViewDNS ever incremented it.
- Notification channels store the last delivery, written by the
  notifier on its own connection. A channel offers the seven event
  types the product emits, program changes on by default, and levels
  named All events, Warning and critical, Critical only.
- A default proxy applies to a scan launched without a context and is
  preselected for new contexts. A pool hands each scan one endpoint,
  proxy URLs bracket IPv6 hosts, and deleting a proxy clears it from
  its contexts.
- Users can be added with a role, disabled and promoted. An account
  cannot change itself, and one that owns records answers 409.
- Retention windows are validated and include keep forever. The
  instance name cannot be blank and names every page title.
- The AI list drops the two switches nothing read; each feature states
  what it sends. The out-of-band Off help says the checks are skipped.

Connectors, Bounty Hub and Remote control lose their extra page
padding. Migration 5d2e9a8c1b47 adds the test and delivery columns and
drops proxies.mode.

Claude-Session: https://claude.ai/code/session_01RVAHQxZ7JAkoEiTCDiHYue
2026-09-27 10:01:19 +05:30

76 lines
2.3 KiB
Python

import json
import secrets
from urllib.parse import urlsplit
from sqlalchemy import select
from shared.models.proxy import Proxy, ProxyEndpoint
from shared.utils.crypto import decrypt_stored
from shared.utils.net import host_port
def load_proxy_endpoints(proxy: Proxy) -> list[ProxyEndpoint]:
raw = decrypt_stored(proxy.endpoints_encrypted or "", label=f"Proxy {proxy.name!r}")
if not raw:
return []
return [ProxyEndpoint(**e) for e in json.loads(raw)]
SOCKS_SCHEMES = frozenset({"socks5", "socks5h"})
def is_socks5(proxy_url: str | None) -> bool:
"""Whether a tool that speaks only socks5 can carry this proxy."""
if not proxy_url:
return False
raw = proxy_url if "://" in proxy_url else f"socks5://{proxy_url}"
return urlsplit(raw).scheme.lower() in SOCKS_SCHEMES
def proxy_env(proxy_url: str | None) -> dict[str, str] | None:
"""Proxy env vars honoured by Go HTTP clients that take no proxy flag."""
if not proxy_url:
return None
return {
"HTTP_PROXY": proxy_url,
"HTTPS_PROXY": proxy_url,
"ALL_PROXY": proxy_url,
}
def build_proxy_url(ep: ProxyEndpoint) -> str:
authority = host_port(ep.host, ep.port)
if ep.username:
cred = f"{ep.username}:{ep.password}@" if ep.password else f"{ep.username}@"
return f"{ep.scheme}://{cred}{authority}"
return f"{ep.scheme}://{authority}"
def resolve_proxy_url(proxy: Proxy | None) -> str | None:
"""One endpoint per scan, drawn from the pool when the proxy holds several."""
if proxy is None or not proxy.is_active:
return None
endpoints = load_proxy_endpoints(proxy)
if not endpoints:
return None
return build_proxy_url(secrets.choice(endpoints))
def default_proxy(session) -> Proxy | None:
return (
session.execute(
select(Proxy).where(Proxy.is_default.is_(True), Proxy.is_active.is_(True))
)
.scalars()
.first()
)
def scan_proxy_url(session, context) -> str | None:
"""The context's proxy, or the default proxy for a scan without a context."""
if context is None:
return resolve_proxy_url(default_proxy(session))
if context.proxy_id is None:
return None
return resolve_proxy_url(session.get(Proxy, context.proxy_id))