Skip to content

Commit

Permalink
Misc cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch committed Feb 25, 2025
1 parent a95d97a commit b9dfb1d
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion netbox/extras/dashboard/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ def get_feed(self):
response = requests.get(
url=self.config['feed_url'],
headers={'User-Agent': f'NetBox/{settings.RELEASE.version}'},
proxies=resolve_proxies(url=self.config['feed_url']),
proxies=resolve_proxies(url=self.config['feed_url'], context={'client': self}),
timeout=3
)
response.raise_for_status()
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/webhooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def send_webhook(event_rule, model_name, event_type, data, timestamp, username,
session.verify = webhook.ssl_verification
if webhook.ca_file_path:
session.verify = webhook.ca_file_path
proxies = resolve_proxies(url=url, context={'webhook': webhook})
proxies = resolve_proxies(url=url, context={'client': webhook})
response = session.send(prepared_request, proxies=proxies)

if 200 <= response.status_code <= 299:
Expand Down
4 changes: 2 additions & 2 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
FIELD_CHOICES = getattr(configuration, 'FIELD_CHOICES', {})
FILE_UPLOAD_MAX_MEMORY_SIZE = getattr(configuration, 'FILE_UPLOAD_MAX_MEMORY_SIZE', 2621440)
GRAPHQL_MAX_ALIASES = getattr(configuration, 'GRAPHQL_MAX_ALIASES', 10)
HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', None)
HTTP_PROXIES = getattr(configuration, 'HTTP_PROXIES', {})
INTERNAL_IPS = getattr(configuration, 'INTERNAL_IPS', ('127.0.0.1', '::1'))
ISOLATED_DEPLOYMENT = getattr(configuration, 'ISOLATED_DEPLOYMENT', False)
JINJA2_FILTERS = getattr(configuration, 'JINJA2_FILTERS', {})
Expand Down Expand Up @@ -209,7 +209,7 @@
try:
import_string(path)
except ImportError:
raise ImproperlyConfigured(f"Invalid proxy router path: {path}")
raise ImproperlyConfigured(f"Invalid path in PROXY_ROUTERS: {path}")


#
Expand Down
11 changes: 10 additions & 1 deletion netbox/utilities/proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def _get_protocol_from_url(url):
return urlparse(url).scheme

def route(self, url=None, protocol=None, context=None):
"""
Returns the appropriate proxy given a URL or protocol. Arbitrary context data may also be passed where
available.
Args:
url: The specific request URL for which the proxy will be used (if known)
protocol: The protocol in use (e.g. http or https) (if known)
context: Additional context to aid in proxy selection. May include e.g. the requesting client.
"""
if url and protocol is None:
protocol = self._get_protocol_from_url(url)
if protocol and protocol in settings.HTTP_PROXIES:
Expand All @@ -42,5 +51,5 @@ def resolve_proxies(url=None, protocol=None, context=None):

for item in settings.PROXY_ROUTERS:
router = import_string(item) if type(item) is str else item
if proxies := router.route(url=url, protocol=protocol, context=context):
if proxies := router().route(url=url, protocol=protocol, context=context):
return proxies

0 comments on commit b9dfb1d

Please sign in to comment.