mirror of
https://github.com/eduardogsilva/wireguard_webadmin.git
synced 2026-11-27 07:06:03 +01:00
Handle non-JSON responses from the primary server in the cluster agent
This commit is contained in:
@@ -253,6 +253,30 @@ class ClusterNodeAgent:
|
|||||||
logger.error('Request to %s failed: %s', path, error)
|
logger.error('Request to %s failed: %s', path, error)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def response_json(response, path):
|
||||||
|
"""Decoded JSON object of a response, or None when the primary server did not answer with one."""
|
||||||
|
try:
|
||||||
|
data = response.json()
|
||||||
|
except ValueError:
|
||||||
|
data = None
|
||||||
|
if isinstance(data, dict):
|
||||||
|
return data
|
||||||
|
content_type = response.headers.get('Content-Type', 'no content type')
|
||||||
|
if response.history:
|
||||||
|
logger.error(
|
||||||
|
'Request to %s was redirected to %s (HTTP %s, %s) instead of answering with JSON. If the App Gateway '
|
||||||
|
'protects the WireGuard WebAdmin of the primary server, add a route for /api/cluster/ with a public '
|
||||||
|
'policy to it.', path, response.url, response.status_code, content_type
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
logger.error(
|
||||||
|
'Request to %s did not return JSON (HTTP %s, %s). Check MASTER_SERVER_ADDRESS and, if the App Gateway '
|
||||||
|
'protects the WireGuard WebAdmin of the primary server, add a route for /api/cluster/ with a public '
|
||||||
|
'policy to it.', path, response.status_code, content_type
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
# Section handlers
|
# Section handlers
|
||||||
|
|
||||||
def apply_firewall(self, section):
|
def apply_firewall(self, section):
|
||||||
@@ -385,19 +409,23 @@ class ClusterNodeAgent:
|
|||||||
response = self.request('GET', 'manifest/')
|
response = self.request('GET', 'manifest/')
|
||||||
if response is None:
|
if response is None:
|
||||||
return True
|
return True
|
||||||
|
if response.status_code not in (200, 400, 403):
|
||||||
|
logger.error('Unexpected manifest response: HTTP %s', response.status_code)
|
||||||
|
return True
|
||||||
|
# Answers that do not come from the cluster API (a login page, a proxy error) never stop the worker
|
||||||
|
data = self.response_json(response, 'manifest/')
|
||||||
|
if data is None:
|
||||||
|
return True
|
||||||
if response.status_code == 403:
|
if response.status_code == 403:
|
||||||
logger.error('Token rejected by the primary server. Stopping.')
|
logger.error('Token rejected by the primary server. Stopping.')
|
||||||
self.shutdown_wireguard()
|
self.shutdown_wireguard()
|
||||||
return False
|
return False
|
||||||
if response.status_code == 400:
|
if response.status_code == 400:
|
||||||
logger.warning('Primary server refused this worker: %s', response.json().get('message', ''))
|
logger.warning('Primary server refused this worker: %s', data.get('message', ''))
|
||||||
self.shutdown_wireguard()
|
self.shutdown_wireguard()
|
||||||
return True
|
return True
|
||||||
if response.status_code != 200:
|
|
||||||
logger.error('Unexpected manifest response: HTTP %s', response.status_code)
|
|
||||||
return True
|
|
||||||
|
|
||||||
manifest = response.json().get('sections', {})
|
manifest = data.get('sections', {})
|
||||||
reports = {}
|
reports = {}
|
||||||
for section_name, section_hash in manifest.items():
|
for section_name, section_hash in manifest.items():
|
||||||
if section_name not in self.SECTION_HANDLERS or self.applied_hashes.get(section_name) == section_hash:
|
if section_name not in self.SECTION_HANDLERS or self.applied_hashes.get(section_name) == section_hash:
|
||||||
@@ -405,7 +433,9 @@ class ClusterNodeAgent:
|
|||||||
section_response = self.request('GET', f'section/{section_name}/')
|
section_response = self.request('GET', f'section/{section_name}/')
|
||||||
if section_response is None or section_response.status_code != 200:
|
if section_response is None or section_response.status_code != 200:
|
||||||
continue
|
continue
|
||||||
section = section_response.json()
|
section = self.response_json(section_response, f'section/{section_name}/')
|
||||||
|
if section is None:
|
||||||
|
continue
|
||||||
try:
|
try:
|
||||||
self.SECTION_HANDLERS[section_name](self, section)
|
self.SECTION_HANDLERS[section_name](self, section)
|
||||||
self.applied_hashes[section_name] = section['hash']
|
self.applied_hashes[section_name] = section['hash']
|
||||||
|
|||||||
Reference in New Issue
Block a user