Skip to content

Commit

Permalink
Merge branch 'issue2979' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolargo committed Oct 19, 2024
2 parents d39d7c7 + 315678b commit 5f3350e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
15 changes: 10 additions & 5 deletions glances/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@
import sys
import time

from defusedxml import xmlrpc

from glances import __version__
from glances.globals import Fault, ProtocolError, ServerProxy, Transport, json_loads
from glances.globals import json_loads
from glances.logger import logger
from glances.outputs.glances_curses import GlancesCursesClient
from glances.stats_client import GlancesStatsClient
from glances.timer import Counter

# Correct issue #1025 by monkey path the xmlrpc lib
xmlrpc.monkey_patch()


class GlancesClientTransport(Transport):
class GlancesClientTransport(xmlrpc.xmlrpc_client.Transport):
"""This class overwrite the default XML-RPC transport and manage timeout."""

def set_timeout(self, timeout):
Expand Down Expand Up @@ -57,7 +62,7 @@ def __init__(self, config=None, args=None, timeout=7, return_to_browser=False):
# Configure the server timeout
transport.set_timeout(timeout)
try:
self.client = ServerProxy(self.uri, transport=transport)
self.client = xmlrpc.xmlrpc_client.ServerProxy(self.uri, transport=transport)
except Exception as e:
self.log_and_exit(f"Client couldn't create socket {self.uri}: {e}")

Expand Down Expand Up @@ -101,7 +106,7 @@ def _login_glances(self):
print(fall_back_msg)
else:
logger.info(fall_back_msg)
except ProtocolError as err:
except xmlrpc.xmlrpc_client.ProtocolError as err:
# Other errors
msg = f"Connection to server {self.uri} failed"
if err.errcode == 401:
Expand Down Expand Up @@ -197,7 +202,7 @@ def update_glances(self):
except OSError:
# Client cannot get server stats
return "Disconnected"
except Fault:
except xmlrpc.xmlrpc_client.Fault:
# Client cannot get server stats (issue #375)
return "Disconnected"
else:
Expand Down
13 changes: 9 additions & 4 deletions glances/client_browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@

import threading

from defusedxml import xmlrpc

from glances.autodiscover import GlancesAutoDiscoverServer
from glances.client import GlancesClient, GlancesClientTransport
from glances.globals import Fault, ProtocolError, ServerProxy, json_loads
from glances.globals import json_loads
from glances.logger import LOG_FILENAME, logger
from glances.outputs.glances_curses_browser import GlancesCursesBrowser
from glances.password_list import GlancesPasswordList as GlancesPassword
from glances.static_list import GlancesStaticServer

# Correct issue #1025 by monkey path the xmlrpc lib
xmlrpc.monkey_patch()


class GlancesClientBrowser:
"""This class creates and manages the TCP client browser (servers list)."""
Expand Down Expand Up @@ -86,7 +91,7 @@ def __update_stats(self, server):

# Get common stats from Glances server
try:
s = ServerProxy(uri, transport=t)
s = xmlrpc.xmlrpc_client.ServerProxy(uri, transport=t)
except Exception as e:
logger.warning(f"Client browser couldn't create socket ({e})")
return server
Expand All @@ -107,12 +112,12 @@ def __update_stats(self, server):
if 'key' in column:
d_json = d_json.get(column['key'])
server[server_key + '_decoration'] = d_json[column['field']]['decoration']
except (KeyError, IndexError, Fault) as e:
except (KeyError, IndexError, xmlrpc.xmlrpc_client.Fault) as e:
logger.debug(f"Error while grabbing stats form server ({e})")
except OSError as e:
logger.debug(f"Error while grabbing stats form server ({e})")
server['status'] = 'OFFLINE'
except ProtocolError as e:
except xmlrpc.xmlrpc_client.ProtocolError as e:
if e.errcode == 401:
# Error 401 (Authentication failed)
# Password is not the good one...
Expand Down
6 changes: 0 additions & 6 deletions glances/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
from urllib.parse import urlparse
from urllib.request import Request, urlopen
from xmlrpc.client import Fault, ProtocolError, Server, ServerProxy, Transport
from xmlrpc.server import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer

from defusedxml.xmlrpc import monkey_patch

# Correct issue #1025 by monkey path the xmlrpc lib
monkey_patch()

# Prefer faster libs for JSON (de)serialization
# Preference Order: orjson > ujson > json (builtin)
Expand Down
12 changes: 8 additions & 4 deletions glances/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
import sys
from base64 import b64decode

from defusedxml import xmlrpc

from glances import __version__
from glances.autodiscover import GlancesAutoDiscoverClient
from glances.globals import SimpleXMLRPCRequestHandler, SimpleXMLRPCServer
from glances.logger import logger
from glances.stats_server import GlancesStatsServer
from glances.timer import Timer

# Correct issue #1025 by monkey path the xmlrpc lib
xmlrpc.monkey_patch()


class GlancesXMLRPCHandler(SimpleXMLRPCRequestHandler):
class GlancesXMLRPCHandler(xmlrpc.xmlrpc_server.SimpleXMLRPCRequestHandler):
"""Main XML-RPC handler."""

rpc_paths = ('/RPC2',)
Expand Down Expand Up @@ -71,7 +75,7 @@ def check_user(self, username, password):
return False

def parse_request(self):
if SimpleXMLRPCRequestHandler.parse_request(self):
if xmlrpc.xmlrpc_server.SimpleXMLRPCRequestHandler.parse_request(self):
# Next we authenticate
if self.authenticate(self.headers):
return True
Expand All @@ -84,7 +88,7 @@ def log_message(self, log_format, *args):
pass


class GlancesXMLRPCServer(SimpleXMLRPCServer):
class GlancesXMLRPCServer(xmlrpc.xmlrpc_server.SimpleXMLRPCServer):
"""Init a SimpleXMLRPCServer instance (IPv6-ready)."""

finished = False
Expand Down

0 comments on commit 5f3350e

Please sign in to comment.