From e63e1213547ac057fa3864c16a08eea2b18d6b58 Mon Sep 17 00:00:00 2001 From: Brian May Date: Mon, 14 Dec 2015 09:21:15 +1100 Subject: [PATCH] Print PF rules used. Also support multiline debug output better. --- sshuttle/helpers.py | 10 +++++++++- sshuttle/methods/pf.py | 3 ++- sshuttle/tests/test_helpers.py | 16 ++++++++++++++++ sshuttle/tests/test_methods_pf.py | 1 + 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/sshuttle/helpers.py b/sshuttle/helpers.py index b23e030..4e80e33 100644 --- a/sshuttle/helpers.py +++ b/sshuttle/helpers.py @@ -7,9 +7,17 @@ verbose = 0 def log(s): + global logprefix try: sys.stdout.flush() - sys.stderr.write(logprefix + s) + if s.find("\n") != -1: + prefix = logprefix + s = s.rstrip("\n") + for line in s.split("\n"): + sys.stderr.write(prefix + line + "\n") + prefix = "---> " + else: + sys.stderr.write(logprefix + s) sys.stderr.flush() except IOError: # this could happen if stderr gets forcibly disconnected, eg. because diff --git a/sshuttle/methods/pf.py b/sshuttle/methods/pf.py index b3f540e..276571a 100644 --- a/sshuttle/methods/pf.py +++ b/sshuttle/methods/pf.py @@ -7,7 +7,7 @@ import subprocess as ssubprocess from fcntl import ioctl from ctypes import c_char, c_uint8, c_uint16, c_uint32, Union, Structure, \ sizeof, addressof, memmove -from sshuttle.helpers import debug1, debug2, Fatal, family_to_string +from sshuttle.helpers import debug1, debug2, debug3, Fatal, family_to_string from sshuttle.methods import BaseMethod @@ -215,6 +215,7 @@ class Method(BaseMethod): rules = b'\n'.join(tables + translating_rules + filtering_rules) \ + b'\n' assert isinstance(rules, bytes) + debug3("rules:\n" + rules.decode("ASCII")) pf_status = pfctl('-s all')[0] if b'\nrdr-anchor "sshuttle" all\n' not in pf_status: diff --git a/sshuttle/tests/test_helpers.py b/sshuttle/tests/test_helpers.py index 3017b19..2c111ff 100644 --- a/sshuttle/tests/test_helpers.py +++ b/sshuttle/tests/test_helpers.py @@ -11,12 +11,28 @@ import sshuttle.helpers @patch('sshuttle.helpers.sys.stderr') def test_log(mock_stderr, mock_stdout): sshuttle.helpers.log("message") + sshuttle.helpers.log("message 1\n") + sshuttle.helpers.log("message 2\nline2\nline3\n") + sshuttle.helpers.log("message 3\nline2\nline3") assert mock_stdout.mock_calls == [ call.flush(), + call.flush(), + call.flush(), + call.flush(), ] assert mock_stderr.mock_calls == [ call.write('prefix: message'), call.flush(), + call.write('prefix: message 1\n'), + call.flush(), + call.write('prefix: message 2\n'), + call.write('---> line2\n'), + call.write('---> line3\n'), + call.flush(), + call.write('prefix: message 3\n'), + call.write('---> line2\n'), + call.write('---> line3\n'), + call.flush(), ] diff --git a/sshuttle/tests/test_methods_pf.py b/sshuttle/tests/test_methods_pf.py index 33fd164..fb25f3a 100644 --- a/sshuttle/tests/test_methods_pf.py +++ b/sshuttle/tests/test_methods_pf.py @@ -104,6 +104,7 @@ def pfctl(args, stdin=None): return None +@patch('sshuttle.helpers.verbose', new=3) @patch('sshuttle.methods.pf.sys.platform', 'darwin') @patch('sshuttle.methods.pf.pfctl') @patch('sshuttle.methods.pf.ioctl')