commit 5766118620a45d9df9731e1c00f7538a001dd3d4
Author: Damian Johnson <atagar(a)torproject.org>
Date: Thu Oct 26 10:55:19 2017 -0700
Use stem's address caching
Now that stem can cache our address we can drop our our_address() helper.
---
nyx/__init__.py | 25 +++----------------------
nyx/panel/connection.py | 6 +++---
nyx/panel/header.py | 2 +-
nyx/tracker.py | 4 ++--
test/panel/connection.py | 8 ++++----
test/panel/header.py | 2 +-
6 files changed, 14 insertions(+), 33 deletions(-)
diff --git a/nyx/__init__.py b/nyx/__init__.py
index 5947294..7c61651 100644
--- a/nyx/__init__.py
+++ b/nyx/__init__.py
@@ -9,7 +9,6 @@ Tor curses monitoring application.
nyx_interface - nyx interface singleton
tor_controller - tor connection singleton
cache - provides our application cache
- our_address - provides our ip address
show_message - shows a message to the user
input_prompt - prompts the user for text input
@@ -105,9 +104,9 @@ CACHE = None
CHROOT = None
BASE_DIR = os.path.sep.join(__file__.split(os.path.sep)[:-1])
-CACHED_ADDRESS = None
-ADDRESS_FETCHED = None
-ADDRESS_FETCH_RATE = 60
+# our address infrequently changes so we can cache it for a while
+
+stem.control.CACHE_ADDRESS_FOR = 30
# disable trace level messages about cache hits
@@ -251,24 +250,6 @@ def cache():
return CACHE
-def our_address(default = None):
- """
- Provides our ip address.
-
- :param str default: response if address is unavailable
-
- :returns: **str** with our address
- """
-
- global CACHED_ADDRESS, ADDRESS_FETCHED
-
- if ADDRESS_FETCHED is None or (time.time() - ADDRESS_FETCHED) > ADDRESS_FETCH_RATE:
- CACHED_ADDRESS = tor_controller().get_info('address', None)
- ADDRESS_FETCHED = time.time()
-
- return CACHED_ADDRESS if CACHED_ADDRESS is not None else default
-
-
def show_message(message = None, *attr, **kwargs):
"""
Shows a message in our header.
diff --git a/nyx/panel/connection.py b/nyx/panel/connection.py
index 1a090ac..fc586b6 100644
--- a/nyx/panel/connection.py
+++ b/nyx/panel/connection.py
@@ -608,10 +608,10 @@ def _draw_line(subwindow, x, y, line, is_selected, width, current_time):
def _draw_address_column(subwindow, x, y, line, attr):
+ src = tor_controller().get_info('address', line.connection.local_address)
+
if line.line_type == LineType.CONNECTION:
- src = '%s:%s' % (nyx.our_address(line.connection.local_address), line.connection.local_port)
- else:
- src = nyx.our_address(line.connection.local_address)
+ src = '%s:%s' % (src, line.connection.local_port)
if line.line_type == LineType.CIRCUIT_HEADER and line.circuit.status != 'BUILT':
dst = 'Building...'
diff --git a/nyx/panel/header.py b/nyx/panel/header.py
index 0820a21..b1cbfbf 100644
--- a/nyx/panel/header.py
+++ b/nyx/panel/header.py
@@ -268,7 +268,7 @@ class Sampling(object):
'version': str(controller.get_version('Unknown')).split()[0],
'version_status': controller.get_info('status/version/current', 'Unknown'),
- 'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else nyx.our_address('Unknown'),
+ 'address': or_listeners[0][0] if (or_listeners and or_listeners[0][0] != '0.0.0.0') else controller.get_info('address', 'Unknown'),
'or_port': or_listeners[0][1] if or_listeners else '',
'dir_port': controller.get_conf('DirPort', '0'),
'control_port': str(control_listeners[0][1]) if control_listeners else None,
diff --git a/nyx/tracker.py b/nyx/tracker.py
index fb41f9e..fa2381e 100644
--- a/nyx/tracker.py
+++ b/nyx/tracker.py
@@ -903,7 +903,7 @@ class ConsensusTracker(object):
controller = tor_controller()
- if nyx.our_address() == address:
+ if controller.get_info('address', None) == address:
fingerprint = controller.get_info('fingerprint', None)
ports = controller.get_ports(stem.control.Listener.OR, None)
@@ -924,7 +924,7 @@ class ConsensusTracker(object):
controller = tor_controller()
if fingerprint == controller.get_info('fingerprint', None):
- my_address = nyx.our_address()
+ my_address = controller.get_info('address', None)
my_or_ports = controller.get_ports(stem.control.Listener.OR, [])
if my_address and len(my_or_ports) == 1:
diff --git a/test/panel/connection.py b/test/panel/connection.py
index c95f31a..f075f77 100644
--- a/test/panel/connection.py
+++ b/test/panel/connection.py
@@ -195,9 +195,9 @@ class TestConnectionPanel(unittest.TestCase):
@require_curses
@patch('nyx.panel.connection.tor_controller')
- @patch('nyx.our_address', Mock(return_value = '82.121.9.9'))
def test_draw_line(self, tor_controller_mock):
tor_controller_mock().is_geoip_unavailable.return_value = False
+ tor_controller_mock().get_info.return_value = '82.121.9.9'
test_data = ((
line(),
@@ -213,7 +213,7 @@ class TestConnectionPanel(unittest.TestCase):
' | 82.121.9.9 2 / Middle',
), (
line(line_type = LineType.CIRCUIT, fingerprint = 'E0BD57A11F00041A9789577C53A1B784473669E4'),
- ' +- 82.121.9.9 3 / Exit',
+ ' +- 82.121.9.9 3 / End',
))
for test_line, expected in test_data:
@@ -222,9 +222,9 @@ class TestConnectionPanel(unittest.TestCase):
@require_curses
@patch('nyx.panel.connection.tor_controller')
- @patch('nyx.our_address', Mock(return_value = '82.121.9.9'))
def test_draw_address_column(self, tor_controller_mock):
tor_controller_mock().is_geoip_unavailable.return_value = False
+ tor_controller_mock().get_info.return_value = '82.121.9.9'
test_data = ((
line(),
@@ -282,7 +282,7 @@ class TestConnectionPanel(unittest.TestCase):
test_data = {
'1F43EE37A0670301AD9CB555D94AFEC2C89FDE86': ' 1 / Guard',
'B6D83EC2D9E18B0A7A33428F8CFA9C536769E209': ' 2 / Middle',
- 'E0BD57A11F00041A9789577C53A1B784473669E4': ' 3 / Exit',
+ 'E0BD57A11F00041A9789577C53A1B784473669E4': ' 3 / End',
}
for fp, expected in test_data.items():
diff --git a/test/panel/header.py b/test/panel/header.py
index 73259da..1b07799 100644
--- a/test/panel/header.py
+++ b/test/panel/header.py
@@ -87,7 +87,6 @@ class TestHeaderPanel(unittest.TestCase):
@patch('time.time', Mock(return_value = 1234.5))
@patch('os.times', Mock(return_value = (0.08, 0.03, 0.0, 0.0, 18759021.31)))
@patch('os.uname', Mock(return_value = ('Linux', 'odin', '3.5.0-54-generic', '#81~precise1-Ubuntu SMP Tue Jul 15 04:05:58 UTC 2014', 'i686')))
- @patch('nyx.our_address', Mock(return_value = '174.21.17.28'))
@patch('stem.util.system.start_time', Mock(return_value = 5678))
@patch('stem.util.proc.file_descriptors_used', Mock(return_value = 89))
def test_sample(self, consensus_tracker_mock, resource_tracker_mock, tor_controller_mock):
@@ -100,6 +99,7 @@ class TestHeaderPanel(unittest.TestCase):
tor_controller_mock().get_pid.return_value = '123'
tor_controller_mock().get_info.side_effect = lambda param, default = None: {
+ 'address': '174.21.17.28',
'fingerprint': '1A94D1A794FCB2F8B6CBC179EF8FDD4008A98D3B',
'status/version/current': 'recommended',
'process/descriptor-limit': 678,