From 91e5a4a4b5bf1beb083afb0731294cfeaca62944 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Mon, 2 Mar 2015 18:30:46 -0300 Subject: #487 added microsecond support to format_timestamp and used in FlowDetailView. Still WIP. --- libmproxy/console/flowdetailview.py | 10 +++++----- libmproxy/utils.py | 19 ++++++++++++++----- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index 51ae8da6..d0326f1a 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -40,8 +40,8 @@ class FlowDetailsView(urwid.ListBox): sc = self.flow.server_conn parts = [ ["Address", "%s:%s" % sc.address()], - ["Start time", utils.format_timestamp(sc.timestamp_start)], - ["End time", utils.format_timestamp(sc.timestamp_end) if sc.timestamp_end else "active"], + ["Start time", utils.format_timestamp(sc.timestamp_start, True)], + ["End time", utils.format_timestamp(sc.timestamp_end, True) if sc.timestamp_end else "active"], ] text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) @@ -84,10 +84,10 @@ class FlowDetailsView(urwid.ListBox): cc = self.flow.client_conn parts = [ ["Address", "%s:%s" % cc.address()], - ["Start time", utils.format_timestamp(cc.timestamp_start)], + ["Start time", utils.format_timestamp(cc.timestamp_start, True)], # ["Requests", "%s"%cc.requestcount], - ["End time", utils.format_timestamp(cc.timestamp_end) if cc.timestamp_end else "active"], + ["End time", utils.format_timestamp(cc.timestamp_end, True) if cc.timestamp_end else "active"], ] text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) - + return text diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 76e99c34..a50be7fc 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -10,11 +10,20 @@ def timestamp(): return time.time() -def format_timestamp(s): - s = time.localtime(s) - d = datetime.datetime.fromtimestamp(time.mktime(s)) - return d.strftime("%Y-%m-%d %H:%M:%S") - +def format_timestamp(s, include_fractions = False): + if type(s) is not float: + s = time.localtime(s) + d = datetime.datetime.fromtimestamp(time.mktime(s)) + else: + # s is a timestamp(float) can be converted to a datetime without lossing microsecs + d = datetime.datetime.fromtimestamp(s) + if not include_fractions: + format = "%Y-%m-%d %H:%M:%S" + else: + # this will show 3 miliseconds digits (but will truncate and not round the 4th significant digit + # so 600999 microseconds will be 600ms and not 601ms) + format = "%Y-%m-%d %H:%M:%S."+str(d.microsecond/1000) + return d.strftime(format) def isBin(s): """ -- cgit v1.2.3 From 58dba3f49083288e19f6e9edb7cc60e88dd446e3 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Mon, 2 Mar 2015 23:22:44 -0300 Subject: fixed formatting and added a 'test' (sort of) --- libmproxy/console/flowdetailview.py | 8 ++++---- libmproxy/utils.py | 22 ++++++++-------------- test/test_utils.py | 2 ++ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index d0326f1a..e1339d77 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -40,8 +40,8 @@ class FlowDetailsView(urwid.ListBox): sc = self.flow.server_conn parts = [ ["Address", "%s:%s" % sc.address()], - ["Start time", utils.format_timestamp(sc.timestamp_start, True)], - ["End time", utils.format_timestamp(sc.timestamp_end, True) if sc.timestamp_end else "active"], + ["Start time", utils.format_timestamp_with_milli(sc.timestamp_start)], + ["End time", utils.format_timestamp_with_milli(sc.timestamp_end) if sc.timestamp_end else "active"], ] text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) @@ -84,9 +84,9 @@ class FlowDetailsView(urwid.ListBox): cc = self.flow.client_conn parts = [ ["Address", "%s:%s" % cc.address()], - ["Start time", utils.format_timestamp(cc.timestamp_start, True)], + ["Start time", utils.format_timestamp_with_milli(cc.timestamp_start)], # ["Requests", "%s"%cc.requestcount], - ["End time", utils.format_timestamp(cc.timestamp_end, True) if cc.timestamp_end else "active"], + ["End time", utils.format_timestamp_with_milli(cc.timestamp_end) if cc.timestamp_end else "active"], ] text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) diff --git a/libmproxy/utils.py b/libmproxy/utils.py index a50be7fc..e608c8a3 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -11,20 +11,14 @@ def timestamp(): def format_timestamp(s, include_fractions = False): - if type(s) is not float: - s = time.localtime(s) - d = datetime.datetime.fromtimestamp(time.mktime(s)) - else: - # s is a timestamp(float) can be converted to a datetime without lossing microsecs - d = datetime.datetime.fromtimestamp(s) - if not include_fractions: - format = "%Y-%m-%d %H:%M:%S" - else: - # this will show 3 miliseconds digits (but will truncate and not round the 4th significant digit - # so 600999 microseconds will be 600ms and not 601ms) - format = "%Y-%m-%d %H:%M:%S."+str(d.microsecond/1000) - return d.strftime(format) - + s = time.localtime(s) + d = datetime.datetime.fromtimestamp(time.mktime(s)) + return d.strftime("%Y-%m-%d %H:%M:%S") + +def format_timestamp_with_milli(s): + d = datetime.datetime.fromtimestamp(s) + return d.strftime("%Y-%m-%d %H:%M:%S.")+str(d.microsecond/1000).zfill(3) + def isBin(s): """ Does this string have any non-ASCII characters? diff --git a/test/test_utils.py b/test/test_utils.py index 45bfb4f7..78d1c072 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -8,6 +8,8 @@ utils.CERT_SLEEP_TIME = 0 def test_format_timestamp(): assert utils.format_timestamp(utils.timestamp()) +def test_format_timestamp_with_milli(): + assert utils.format_timestamp_with_milli(utils.timestamp()) def test_isBin(): assert not utils.isBin("testing\n\r") -- cgit v1.2.3 From f514eacd7a7aca1eb406cab79c92f730f5143fc4 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Tue, 3 Mar 2015 00:44:31 -0300 Subject: added timing information to FlowDetailView --- libmproxy/console/flowdetailview.py | 40 +++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index e1339d77..301be9b8 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -35,14 +35,37 @@ class FlowDetailsView(urwid.ListBox): title = urwid.AttrWrap(title, "heading") text.append(title) - if self.flow.server_conn: + cc = self.flow.client_conn + sc = self.flow.server_conn + req = self.flow.request; + resp = self.flow.response; + + timing_parts = [] + if cc: + timing_parts.append(["Client conn. established", utils.format_timestamp_with_milli(cc.timestamp_start) if cc.timestamp_start else "active"]) + if sc: + timing_parts.append(["Server conn. initiated", utils.format_timestamp_with_milli(sc.timestamp_start)]) + timing_parts.append(["Server conn. TCP handshake", utils.format_timestamp_with_milli(sc.timestamp_tcp_setup) if sc.timestamp_tcp_setup else "active"]) + if sc.ssl_established: + timing_parts.append(["Server conn. SSL handshake", utils.format_timestamp_with_milli(sc.timestamp_ssl_setup) if sc.timestamp_ssl_setup else "active"]) + if cc: + if sc.ssl_established: + timing_parts.append(["Client conn. SSL handshake", utils.format_timestamp_with_milli(cc.timestamp_ssl_setup) if cc.timestamp_ssl_setup else "active"]) + + timing_parts.append(["First request byte", utils.format_timestamp_with_milli(req.timestamp_start)]) + timing_parts.append(["Request complete", utils.format_timestamp_with_milli(req.timestamp_end) if req.timestamp_end else "active"]) + + if resp: + timing_parts.append(["First response byte", utils.format_timestamp_with_milli(resp.timestamp_start)]) + timing_parts.append(["response complete", utils.format_timestamp_with_milli(resp.timestamp_end) if resp.timestamp_end else "active"]) + + + if sc: text.append(urwid.Text([("head", "Server Connection:")])) - sc = self.flow.server_conn parts = [ ["Address", "%s:%s" % sc.address()], - ["Start time", utils.format_timestamp_with_milli(sc.timestamp_start)], - ["End time", utils.format_timestamp_with_milli(sc.timestamp_end) if sc.timestamp_end else "active"], ] + text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) c = self.flow.server_conn.cert @@ -79,15 +102,16 @@ class FlowDetailsView(urwid.ListBox): ) text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) - if self.flow.client_conn: + if cc: text.append(urwid.Text([("head", "Client Connection:")])) - cc = self.flow.client_conn + parts = [ ["Address", "%s:%s" % cc.address()], - ["Start time", utils.format_timestamp_with_milli(cc.timestamp_start)], # ["Requests", "%s"%cc.requestcount], - ["End time", utils.format_timestamp_with_milli(cc.timestamp_end) if cc.timestamp_end else "active"], ] + text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) + text.append(urwid.Text([("head", "Timing:")])) + text.extend(common.format_keyvals(timing_parts, key="key", val="text", indent=4)) return text -- cgit v1.2.3 From 8a672b7955a4a1bc3268755022afd2ca0e05e283 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Tue, 3 Mar 2015 00:55:23 -0300 Subject: minor refactor --- libmproxy/console/flowdetailview.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index 301be9b8..8f1f013e 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -48,9 +48,9 @@ class FlowDetailsView(urwid.ListBox): timing_parts.append(["Server conn. TCP handshake", utils.format_timestamp_with_milli(sc.timestamp_tcp_setup) if sc.timestamp_tcp_setup else "active"]) if sc.ssl_established: timing_parts.append(["Server conn. SSL handshake", utils.format_timestamp_with_milli(sc.timestamp_ssl_setup) if sc.timestamp_ssl_setup else "active"]) - if cc: - if sc.ssl_established: - timing_parts.append(["Client conn. SSL handshake", utils.format_timestamp_with_milli(cc.timestamp_ssl_setup) if cc.timestamp_ssl_setup else "active"]) + + if cc and sc.ssl_established: + timing_parts.append(["Client conn. SSL handshake", utils.format_timestamp_with_milli(cc.timestamp_ssl_setup) if cc.timestamp_ssl_setup else "active"]) timing_parts.append(["First request byte", utils.format_timestamp_with_milli(req.timestamp_start)]) timing_parts.append(["Request complete", utils.format_timestamp_with_milli(req.timestamp_end) if req.timestamp_end else "active"]) @@ -59,7 +59,6 @@ class FlowDetailsView(urwid.ListBox): timing_parts.append(["First response byte", utils.format_timestamp_with_milli(resp.timestamp_start)]) timing_parts.append(["response complete", utils.format_timestamp_with_milli(resp.timestamp_end) if resp.timestamp_end else "active"]) - if sc: text.append(urwid.Text([("head", "Server Connection:")])) parts = [ @@ -68,7 +67,7 @@ class FlowDetailsView(urwid.ListBox): text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) - c = self.flow.server_conn.cert + c = sc.cert if c: text.append(urwid.Text([("head", "Server Certificate:")])) parts = [ -- cgit v1.2.3 From 485003511b0eaf61cca38a889ec5a888937a142b Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Tue, 3 Mar 2015 17:49:47 -0300 Subject: removed unused parameter --- libmproxy/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libmproxy/utils.py b/libmproxy/utils.py index e608c8a3..e2306c11 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -10,7 +10,7 @@ def timestamp(): return time.time() -def format_timestamp(s, include_fractions = False): +def format_timestamp(s): s = time.localtime(s) d = datetime.datetime.fromtimestamp(time.mktime(s)) return d.strftime("%Y-%m-%d %H:%M:%S") -- cgit v1.2.3 From b49d573c8ba2aac2f2ec9082c9982cc097a094b9 Mon Sep 17 00:00:00 2001 From: Marcelo Glezer Date: Tue, 3 Mar 2015 18:38:16 -0300 Subject: sorted timing information by timestamp --- libmproxy/console/flowdetailview.py | 39 +++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index 8f1f013e..1299443d 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -40,25 +40,6 @@ class FlowDetailsView(urwid.ListBox): req = self.flow.request; resp = self.flow.response; - timing_parts = [] - if cc: - timing_parts.append(["Client conn. established", utils.format_timestamp_with_milli(cc.timestamp_start) if cc.timestamp_start else "active"]) - if sc: - timing_parts.append(["Server conn. initiated", utils.format_timestamp_with_milli(sc.timestamp_start)]) - timing_parts.append(["Server conn. TCP handshake", utils.format_timestamp_with_milli(sc.timestamp_tcp_setup) if sc.timestamp_tcp_setup else "active"]) - if sc.ssl_established: - timing_parts.append(["Server conn. SSL handshake", utils.format_timestamp_with_milli(sc.timestamp_ssl_setup) if sc.timestamp_ssl_setup else "active"]) - - if cc and sc.ssl_established: - timing_parts.append(["Client conn. SSL handshake", utils.format_timestamp_with_milli(cc.timestamp_ssl_setup) if cc.timestamp_ssl_setup else "active"]) - - timing_parts.append(["First request byte", utils.format_timestamp_with_milli(req.timestamp_start)]) - timing_parts.append(["Request complete", utils.format_timestamp_with_milli(req.timestamp_end) if req.timestamp_end else "active"]) - - if resp: - timing_parts.append(["First response byte", utils.format_timestamp_with_milli(resp.timestamp_start)]) - timing_parts.append(["response complete", utils.format_timestamp_with_milli(resp.timestamp_end) if resp.timestamp_end else "active"]) - if sc: text.append(urwid.Text([("head", "Server Connection:")])) parts = [ @@ -110,7 +91,23 @@ class FlowDetailsView(urwid.ListBox): ] text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) - + + parts = [] + + parts.append(["Client conn. established", utils.format_timestamp_with_milli(cc.timestamp_start) if (cc and cc.timestamp_start) else "active"]) + parts.append(["Server conn. initiated", utils.format_timestamp_with_milli(sc.timestamp_start) if sc else "active" ]) + parts.append(["Server conn. TCP handshake", utils.format_timestamp_with_milli(sc.timestamp_tcp_setup) if (sc and sc.timestamp_tcp_setup) else "active"]) + if sc.ssl_established: + parts.append(["Server conn. SSL handshake", utils.format_timestamp_with_milli(sc.timestamp_ssl_setup) if sc.timestamp_ssl_setup else "active"]) + parts.append(["Client conn. SSL handshake", utils.format_timestamp_with_milli(cc.timestamp_ssl_setup) if (cc and cc.timestamp_ssl_setup) else "active"]) + parts.append(["First request byte", utils.format_timestamp_with_milli(req.timestamp_start)]) + parts.append(["Request complete", utils.format_timestamp_with_milli(req.timestamp_end) if req.timestamp_end else "active"]) + parts.append(["First response byte", utils.format_timestamp_with_milli(resp.timestamp_start) if resp else "active"]) + parts.append(["Response complete", utils.format_timestamp_with_milli(resp.timestamp_end) if (resp and resp.timestamp_end) else "active"]) + + # sort operations by timestamp + parts = sorted(parts, key=lambda p: p[1]) + text.append(urwid.Text([("head", "Timing:")])) - text.extend(common.format_keyvals(timing_parts, key="key", val="text", indent=4)) + text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) return text -- cgit v1.2.3 From 48023db59e048da20484ca631d41aa524425f5dd Mon Sep 17 00:00:00 2001 From: Tarashish Mishra Date: Wed, 4 Mar 2015 22:32:01 +0530 Subject: Minor refactor to PR #496 --- libmproxy/console/flowdetailview.py | 10 +++++----- libmproxy/utils.py | 14 ++++++++------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libmproxy/console/flowdetailview.py b/libmproxy/console/flowdetailview.py index 1299443d..4164c416 100644 --- a/libmproxy/console/flowdetailview.py +++ b/libmproxy/console/flowdetailview.py @@ -37,15 +37,15 @@ class FlowDetailsView(urwid.ListBox): cc = self.flow.client_conn sc = self.flow.server_conn - req = self.flow.request; - resp = self.flow.response; + req = self.flow.request + resp = self.flow.response if sc: text.append(urwid.Text([("head", "Server Connection:")])) parts = [ ["Address", "%s:%s" % sc.address()], ] - + text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) c = sc.cert @@ -89,7 +89,7 @@ class FlowDetailsView(urwid.ListBox): ["Address", "%s:%s" % cc.address()], # ["Requests", "%s"%cc.requestcount], ] - + text.extend(common.format_keyvals(parts, key="key", val="text", indent=4)) parts = [] @@ -105,7 +105,7 @@ class FlowDetailsView(urwid.ListBox): parts.append(["First response byte", utils.format_timestamp_with_milli(resp.timestamp_start) if resp else "active"]) parts.append(["Response complete", utils.format_timestamp_with_milli(resp.timestamp_end) if (resp and resp.timestamp_end) else "active"]) - # sort operations by timestamp + # sort operations by timestamp parts = sorted(parts, key=lambda p: p[1]) text.append(urwid.Text([("head", "Timing:")])) diff --git a/libmproxy/utils.py b/libmproxy/utils.py index e2306c11..51f2dc26 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -15,10 +15,12 @@ def format_timestamp(s): d = datetime.datetime.fromtimestamp(time.mktime(s)) return d.strftime("%Y-%m-%d %H:%M:%S") + def format_timestamp_with_milli(s): d = datetime.datetime.fromtimestamp(s) - return d.strftime("%Y-%m-%d %H:%M:%S.")+str(d.microsecond/1000).zfill(3) - + return d.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] + + def isBin(s): """ Does this string have any non-ASCII characters? @@ -84,15 +86,15 @@ def pretty_size(size): def pretty_duration(secs): formatters = [ - (100, "{:.0f}s"), - (10, "{:2.1f}s"), - (1, "{:1.2f}s"), + (100, "{:.0f}s"), + (10, "{:2.1f}s"), + (1, "{:1.2f}s"), ] for limit, formatter in formatters: if secs >= limit: return formatter.format(secs) - #less than 1 sec + #less than 1 sec return "{:.0f}ms".format(secs*1000) class Data: -- cgit v1.2.3