diff options
-rw-r--r-- | pathod/log.py | 26 | ||||
-rw-r--r-- | pathod/pathoc.py | 135 | ||||
-rw-r--r-- | pathod/pathoc_cmdline.py | 1 | ||||
-rw-r--r-- | pathod/pathod.py | 2 | ||||
-rw-r--r-- | test/pathod/test_log.py | 2 |
5 files changed, 83 insertions, 83 deletions
diff --git a/pathod/log.py b/pathod/log.py index 5bf55de4..1d3ec356 100644 --- a/pathod/log.py +++ b/pathod/log.py @@ -1,17 +1,14 @@ -import datetime +import time import six -from netlib import strutils +from netlib import strutils, human -TIMEFMT = '%d-%m-%y %H:%M:%S' - -def write_raw(fp, lines): +def write_raw(fp, lines, timestamp=True): if fp: - fp.write( - "%s: " % datetime.datetime.now().strftime(TIMEFMT) - ) + if timestamp: + fp.write(human.format_timestamp(time.time())) for i in lines: fp.write(i) fp.write("\n") @@ -20,11 +17,12 @@ def write_raw(fp, lines): class LogCtx(object): - def __init__(self, fp, hex, rfile, wfile): + def __init__(self, fp, hex, timestamp, rfile, wfile): self.lines = [] self.fp = fp self.suppressed = False self.hex = hex + self.timestamp = timestamp self.rfile, self.wfile = rfile, wfile def __enter__(self): @@ -50,7 +48,8 @@ class LogCtx(object): self.fp, [ "\n".join(self.lines), - ] + ], + timestamp = self.timestamp ) if exc_value: six.reraise(exc_type, exc_value, traceback) @@ -71,13 +70,14 @@ class LogCtx(object): class ConnectionLogger: - def __init__(self, fp, hex, rfile, wfile): + def __init__(self, fp, hex, timestamp, rfile, wfile): self.fp = fp self.hex = hex self.rfile, self.wfile = rfile, wfile + self.timestamp = timestamp def ctx(self): - return LogCtx(self.fp, self.hex, self.rfile, self.wfile) + return LogCtx(self.fp, self.hex, self.timestamp, self.rfile, self.wfile) def write(self, lines): - write_raw(self.fp, lines) + write_raw(self.fp, lines, timestamp=self.timestamp) diff --git a/pathod/pathoc.py b/pathod/pathoc.py index 3eb91637..def6cfcf 100644 --- a/pathod/pathoc.py +++ b/pathod/pathoc.py @@ -18,7 +18,7 @@ from netlib.exceptions import HttpException, TcpDisconnect, TcpTimeout, TlsExcep NetlibException from netlib.http import http1, http2 -from . import log, language +from pathod import log, language import logging from netlib.tutils import treq @@ -100,6 +100,7 @@ class WebsocketFrameReader(threading.Thread): self.logger = log.ConnectionLogger( self.logfp, self.hexdump, + False, rfile if showresp else None, None ) @@ -216,7 +217,8 @@ class Pathoc(tcp.TCPClient): self.fp, "HTTP/2 requires ALPN support. " "Please use OpenSSL >= 1.0.2. " - "Pathoc might not be working as expected without ALPN." + "Pathoc might not be working as expected without ALPN.", + timestamp = False ) self.protocol = http2.HTTP2Protocol(self, dump_frames=self.http2_framedump) else: @@ -289,47 +291,43 @@ class Pathoc(tcp.TCPClient): if self.use_http2 and not self.ssl: raise NotImplementedError("HTTP2 without SSL is not supported.") - try: - ret = tcp.TCPClient.connect(self) - if connect_to: - self.http_connect(connect_to) + ret = tcp.TCPClient.connect(self) + if connect_to: + self.http_connect(connect_to) - self.sslinfo = None - if self.ssl: - try: - alpn_protos = [b'http/1.1'] - if self.use_http2: - alpn_protos.append(b'h2') - - self.convert_to_ssl( - sni=self.sni, - cert=self.clientcert, - method=self.ssl_version, - options=self.ssl_options, - cipher_list=self.ciphers, - alpn_protos=alpn_protos - ) - except TlsException as v: - raise PathocError(str(v)) - - self.sslinfo = SSLInfo( - self.connection.get_peer_cert_chain(), - self.get_current_cipher(), - self.get_alpn_proto_negotiated() + self.sslinfo = None + if self.ssl: + try: + alpn_protos = [b'http/1.1'] + if self.use_http2: + alpn_protos.append(b'h2') + + self.convert_to_ssl( + sni=self.sni, + cert=self.clientcert, + method=self.ssl_version, + options=self.ssl_options, + cipher_list=self.ciphers, + alpn_protos=alpn_protos ) - if showssl: - print(str(self.sslinfo), file=fp) + except TlsException as v: + raise PathocError(str(v)) - if self.use_http2: - self.protocol.check_alpn() - if not self.http2_skip_connection_preface: - self.protocol.perform_client_connection_preface() - - if self.timeout: - self.settimeout(self.timeout) - except Exception: - self.close() - raise + self.sslinfo = SSLInfo( + self.connection.get_peer_cert_chain(), + self.get_current_cipher(), + self.get_alpn_proto_negotiated() + ) + if showssl: + print(str(self.sslinfo), file=fp) + + if self.use_http2: + self.protocol.check_alpn() + if not self.http2_skip_connection_preface: + self.protocol.perform_client_connection_preface() + + if self.timeout: + self.settimeout(self.timeout) return ret def stop(self): @@ -372,6 +370,7 @@ class Pathoc(tcp.TCPClient): logger = log.ConnectionLogger( self.fp, self.hexdump, + False, None, self.wfile if self.showreq else None, ) @@ -412,6 +411,7 @@ class Pathoc(tcp.TCPClient): logger = log.ConnectionLogger( self.fp, self.hexdump, + False, self.rfile if self.showresp else None, self.wfile if self.showreq else None, ) @@ -507,39 +507,40 @@ def main(args): # pragma: no cover ) trycount = 0 try: - p.connect(args.connect_to, args.showssl) + with p.connect(args.connect_to, args.showssl): + for spec in playlist: + if args.explain or args.memo: + spec = spec.freeze(p.settings) + if args.memo: + h = hashlib.sha256(spec.spec()).digest() + if h not in memo: + trycount = 0 + memo.add(h) + else: + trycount += 1 + if trycount > args.memolimit: + print("Memo limit exceeded...", file=sys.stderr) + return + else: + continue + try: + ret = p.request(spec) + if ret and args.oneshot: + return + # We consume the queue when we can, so it doesn't build up. + for i_ in p.wait(timeout=0, finish=False): + pass + except NetlibException: + break + for i_ in p.wait(timeout=0.01, finish=True): + pass except TcpException as v: print(str(v), file=sys.stderr) continue except PathocError as v: print(str(v), file=sys.stderr) sys.exit(1) - for spec in playlist: - if args.explain or args.memo: - spec = spec.freeze(p.settings) - if args.memo: - h = hashlib.sha256(spec.spec()).digest() - if h not in memo: - trycount = 0 - memo.add(h) - else: - trycount += 1 - if trycount > args.memolimit: - print("Memo limit exceeded...", file=sys.stderr) - return - else: - continue - try: - ret = p.request(spec) - if ret and args.oneshot: - return - # We consume the queue when we can, so it doesn't build up. - for i_ in p.wait(timeout=0, finish=False): - pass - except NetlibException: - break - for i_ in p.wait(timeout=0.01, finish=True): - pass + except KeyboardInterrupt: pass if p: diff --git a/pathod/pathoc_cmdline.py b/pathod/pathoc_cmdline.py index a9603c40..1f438cd7 100644 --- a/pathod/pathoc_cmdline.py +++ b/pathod/pathoc_cmdline.py @@ -218,7 +218,6 @@ def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr): print(v.marked(), file=stderr) sys.exit(1) args.requests = reqs - return args diff --git a/pathod/pathod.py b/pathod/pathod.py index ed35a92e..d1cc9980 100644 --- a/pathod/pathod.py +++ b/pathod/pathod.py @@ -266,7 +266,7 @@ class PathodHandler(tcp.BaseHandler): lr = self.rfile if self.server.logreq else None lw = self.wfile if self.server.logresp else None - logger = log.ConnectionLogger(self.logfp, self.server.hexdump, lr, lw) + logger = log.ConnectionLogger(self.logfp, self.server.hexdump, True, lr, lw) self.settings.protocol = self.protocol diff --git a/test/pathod/test_log.py b/test/pathod/test_log.py index 29801eb3..0cd5b3b0 100644 --- a/test/pathod/test_log.py +++ b/test/pathod/test_log.py @@ -16,7 +16,7 @@ class DummyIO(six.StringIO): def test_disconnect(): outf = DummyIO() rw = DummyIO() - l = log.ConnectionLogger(outf, False, rw, rw) + l = log.ConnectionLogger(outf, False, True, rw, rw) try: with l.ctx() as lg: lg("Test") |