From 962a414327d93b604a59a4b8c8582d359745009d Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 24 Oct 2014 14:01:34 +1300 Subject: Fix tests, re-add scripts --- libpathod/pathoc.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 938dbeba..ba831fc6 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -1,4 +1,5 @@ -import sys, os +import sys +import os from netlib import tcp, http, certutils import netlib.utils import language, utils @@ -173,7 +174,7 @@ class Pathoc(tcp.TCPClient): print >> fp, "%s=%s"%cn, print >> fp print >> fp, "\tVersion: %s"%i.get_version() - print >> fp, "\tValidity: %s - %s"%(i.get_notBefore(),i.get_notAfter()) + print >> fp, "\tValidity: %s - %s"%(i.get_notBefore(),i.get_notAfter()) print >> fp, "\tSerial: %s"%i.get_serial_number() print >> fp, "\tAlgorithm: %s"%i.get_signature_algorithm() pk = i.get_pubkey() -- cgit v1.2.3 From cd481fdd14a96afeb61ce5d2b8d899f24af1051f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Fri, 24 Oct 2014 17:12:54 +1300 Subject: Refactor and whitespace --- libpathod/pathoc.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index ba831fc6..9ff03eca 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -191,4 +191,37 @@ class Pathoc(tcp.TCPClient): return True - +def main(args): + try: + for i in range(args.repeat): + p = Pathoc( + (args.host, args.port), + ssl=args.ssl, + sni=args.sni, + sslversion=args.sslversion, + clientcert=args.clientcert, + ciphers=args.ciphers + ) + try: + p.connect(args.connect_to) + except (tcp.NetLibError, PathocError), v: + print >> sys.stderr, str(v) + sys.exit(1) + if args.timeout: + p.settimeout(args.timeout) + for spec in args.request: + ret = p.print_request( + spec, + showreq=args.showreq, + showresp=args.showresp, + explain=args.explain, + showssl=args.showssl, + hexdump=args.hexdump, + ignorecodes=args.ignorecodes, + ignoretimeout=args.ignoretimeout + ) + sys.stdout.flush() + if ret and args.oneshot: + sys.exit(0) + except KeyboardInterrupt: + pass -- cgit v1.2.3 From 5aadf92767614b7bd8e2ef677085410ac359e5e8 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 08:18:39 +1300 Subject: Nicer way to specify patterns read for file - just use a path --- libpathod/pathoc.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 9ff03eca..e534bba5 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -80,7 +80,7 @@ class Pathoc(tcp.TCPClient): May raise language.ParseException, netlib.http.HttpError or language.FileAccessDenied. """ - r = language.parse_request(self.settings, spec) + r = language.parse_request(spec) language.serve(r, self.wfile, self.settings, self.address.host) self.wfile.flush() ret = list(http.read_response(self.rfile, r.method.string(), None)) @@ -115,7 +115,7 @@ class Pathoc(tcp.TCPClient): Returns True if we have a non-ignored response. """ try: - r = language.parse_request(self.settings, spec) + r = language.parse_request(spec) except language.ParseException, v: print >> fp, "Error parsing request spec: %s"%v.msg print >> fp, v.marked() -- cgit v1.2.3 From 00d0ee5ad56d8243b1e9bfffec9a941e11359d2c Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 15:30:54 +1300 Subject: Parse patterns eagerly on instantiation --- libpathod/pathoc.py | 81 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 30 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index e534bba5..ae1e98cf 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -2,10 +2,14 @@ import sys import os from netlib import tcp, http, certutils import netlib.utils -import language, utils + +import language +import utils import OpenSSL.crypto -class PathocError(Exception): pass + +class PathocError(Exception): + pass class SSLInfo: @@ -24,7 +28,14 @@ class Response: class Pathoc(tcp.TCPClient): - def __init__(self, address, ssl=None, sni=None, sslversion=4, clientcert=None, ciphers=None): + def __init__( + self, + address, + ssl=None, + sni=None, + sslversion=4, + clientcert=None, + ciphers=None): tcp.TCPClient.__init__(self, address) self.settings = dict( staticdir = os.getcwd(), @@ -37,9 +48,9 @@ class Pathoc(tcp.TCPClient): def http_connect(self, connect_to): self.wfile.write( - 'CONNECT %s:%s HTTP/1.1\r\n'%tuple(connect_to) + - '\r\n' - ) + 'CONNECT %s:%s HTTP/1.1\r\n'%tuple(connect_to) + + '\r\n' + ) self.wfile.flush() l = self.rfile.readline() if not l: @@ -61,17 +72,17 @@ class Pathoc(tcp.TCPClient): if self.ssl: try: self.convert_to_ssl( - sni=self.sni, - cert=self.clientcert, - method=self.sslversion, - cipher_list = self.ciphers - ) + sni=self.sni, + cert=self.clientcert, + method=self.sslversion, + cipher_list = self.ciphers + ) except tcp.NetLibError, v: raise PathocError(str(v)) self.sslinfo = SSLInfo( - self.connection.get_peer_cert_chain(), - self.get_current_cipher() - ) + self.connection.get_peer_cert_chain(), + self.get_current_cipher() + ) def request(self, spec): """ @@ -88,7 +99,9 @@ class Pathoc(tcp.TCPClient): return Response(*ret) def _show_summary(self, fp, httpversion, code, msg, headers, content): - print >> fp, "<< %s %s: %s bytes"%(code, utils.xrepr(msg), len(content)) + print >> fp, "<< %s %s: %s bytes"%( + code, utils.xrepr(msg), len(content) + ) def _show(self, fp, header, data, hexdump): if hexdump: @@ -99,7 +112,18 @@ class Pathoc(tcp.TCPClient): print >> fp, "%s (unprintables escaped):"%header print >> fp, netlib.utils.cleanBin(data) - def print_request(self, spec, showreq, showresp, explain, showssl, hexdump, ignorecodes, ignoretimeout, fp=sys.stdout): + def print_request( + self, + r, + showreq, + showresp, + explain, + showssl, + hexdump, + ignorecodes, + ignoretimeout, + fp=sys.stdout + ): """ Performs a series of requests, and prints results to the specified file descriptor. @@ -114,16 +138,6 @@ class Pathoc(tcp.TCPClient): Returns True if we have a non-ignored response. """ - try: - r = language.parse_request(spec) - except language.ParseException, v: - print >> fp, "Error parsing request spec: %s"%v.msg - print >> fp, v.marked() - return - except language.FileAccessDenied, v: - print >> fp, "File access error: %s"%v - return - if explain: r = r.freeze(self.settings, self.address.host) @@ -133,7 +147,12 @@ class Pathoc(tcp.TCPClient): if showresp: self.rfile.start_log() try: - req = language.serve(r, self.wfile, self.settings, self.address.host) + req = language.serve( + r, + self.wfile, + self.settings, + self.address.host + ) self.wfile.flush() resp = http.read_response(self.rfile, r.method.string(), None) except http.HttpError, v: @@ -174,13 +193,15 @@ class Pathoc(tcp.TCPClient): print >> fp, "%s=%s"%cn, print >> fp print >> fp, "\tVersion: %s"%i.get_version() - print >> fp, "\tValidity: %s - %s"%(i.get_notBefore(),i.get_notAfter()) + print >> fp, "\tValidity: %s - %s"%( + i.get_notBefore(), i.get_notAfter() + ) print >> fp, "\tSerial: %s"%i.get_serial_number() print >> fp, "\tAlgorithm: %s"%i.get_signature_algorithm() pk = i.get_pubkey() types = { - OpenSSL.crypto.TYPE_RSA: "RSA", - OpenSSL.crypto.TYPE_DSA: "DSA" + OpenSSL.crypto.TYPE_RSA: "RSA", + OpenSSL.crypto.TYPE_DSA: "DSA" } t = types.get(pk.type(), "Uknown") print >> fp, "\tPubkey: %s bit %s"%(pk.bits(), t) -- cgit v1.2.3 From 6d8431ab3e96568b3579a85e680371fd20c961aa Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 16:20:23 +1300 Subject: Allow specification of multiple patterns from file and on command line --- libpathod/pathoc.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index ae1e98cf..b9b202eb 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -18,8 +18,17 @@ class SSLInfo: class Response: - def __init__(self, httpversion, status_code, msg, headers, content, sslinfo): - self.httpversion, self.status_code, self.msg = httpversion, status_code, msg + def __init__( + self, + httpversion, + status_code, + msg, + headers, + content, + sslinfo + ): + self.httpversion, self.status_code = httpversion, status_code + self.msg = msg self.headers, self.content = headers, content self.sslinfo = sslinfo @@ -91,7 +100,7 @@ class Pathoc(tcp.TCPClient): May raise language.ParseException, netlib.http.HttpError or language.FileAccessDenied. """ - r = language.parse_request(spec) + r = language.parse_requests(spec)[0] language.serve(r, self.wfile, self.settings, self.address.host) self.wfile.flush() ret = list(http.read_response(self.rfile, r.method.string(), None)) -- cgit v1.2.3 From ac4e9f8cb5a9541dcff0a86efdb04357b9bc027f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 16:43:01 +1300 Subject: Rationalize pathoc arguments, add -r flag to randomly select a request from the provided specs --- libpathod/pathoc.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index b9b202eb..c85c207a 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -1,5 +1,6 @@ import sys import os +import random from netlib import tcp, http, certutils import netlib.utils @@ -189,7 +190,7 @@ class Pathoc(tcp.TCPClient): if resp: self._show_summary(fp, *resp) - if self.sslinfo: + if showssl and self.sslinfo: print >> fp, "Cipher: %s, %s bit, %s"%self.sslinfo.cipher print >> fp, "SSL certificate chain:\n" for i in self.sslinfo.certchain: @@ -239,7 +240,11 @@ def main(args): sys.exit(1) if args.timeout: p.settimeout(args.timeout) - for spec in args.request: + if args.random: + playlist = [random.choice(args.requests)] + else: + playlist = args.requests + for spec in playlist: ret = p.print_request( spec, showreq=args.showreq, -- cgit v1.2.3 From fc4f9a1c7a0734a190b99265ca50d72014173859 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 17:58:59 +1300 Subject: pathoc -n 0 repeats forever --- libpathod/pathoc.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index c85c207a..02d0c06d 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -224,7 +224,9 @@ class Pathoc(tcp.TCPClient): def main(args): try: - for i in range(args.repeat): + cnt = 0 + while 1: + cnt += 1 p = Pathoc( (args.host, args.port), ssl=args.ssl, @@ -258,5 +260,7 @@ def main(args): sys.stdout.flush() if ret and args.oneshot: sys.exit(0) + if cnt == args.repeat: + break except KeyboardInterrupt: pass -- cgit v1.2.3 From c00ae41486de06192865f8539da0f00985a16a90 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sat, 25 Oct 2014 19:50:48 +1300 Subject: Add a memoize argument to prevent playing the same pattern twice Also remove addition of Date header, which makes this non-deterministic --- libpathod/pathoc.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 02d0c06d..0ff02b01 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -1,5 +1,6 @@ import sys import os +import hashlib import random from netlib import tcp, http, certutils import netlib.utils @@ -148,9 +149,6 @@ class Pathoc(tcp.TCPClient): Returns True if we have a non-ignored response. """ - if explain: - r = r.freeze(self.settings, self.address.host) - resp, req = None, None if showreq: self.wfile.start_log() @@ -223,10 +221,15 @@ class Pathoc(tcp.TCPClient): def main(args): + memo = set([]) try: cnt = 0 while 1: cnt += 1 + if args.random: + playlist = [random.choice(args.requests)] + else: + playlist = args.requests p = Pathoc( (args.host, args.port), ssl=args.ssl, @@ -235,6 +238,21 @@ def main(args): clientcert=args.clientcert, ciphers=args.ciphers ) + if args.explain or args.memo: + playlist = [ + i.freeze(p.settings, p.address.host) for i in playlist + ] + if args.memo: + newlist = [] + for spec in playlist: + h = hashlib.sha256(spec.spec()).digest() + if h not in memo: + memo.add(h) + newlist.append(spec) + playlist = newlist + if not playlist: + continue + try: p.connect(args.connect_to) except (tcp.NetLibError, PathocError), v: @@ -242,10 +260,6 @@ def main(args): sys.exit(1) if args.timeout: p.settimeout(args.timeout) - if args.random: - playlist = [random.choice(args.requests)] - else: - playlist = args.requests for spec in playlist: ret = p.print_request( spec, -- cgit v1.2.3 From 956149c126c0500aa67604996ca310824b2f23a2 Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 26 Oct 2014 18:16:47 +1300 Subject: --memo-limit - limit failed attempts to find new generated requests to N --- libpathod/pathoc.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'libpathod/pathoc.py') diff --git a/libpathod/pathoc.py b/libpathod/pathoc.py index 0ff02b01..e7aff520 100644 --- a/libpathod/pathoc.py +++ b/libpathod/pathoc.py @@ -222,9 +222,14 @@ class Pathoc(tcp.TCPClient): def main(args): memo = set([]) + trycount = 0 try: cnt = 0 while 1: + if trycount > args.memolimit: + print >> sys.stderr, "Memo limit exceeded..." + return + cnt += 1 if args.random: playlist = [random.choice(args.requests)] @@ -251,8 +256,10 @@ def main(args): newlist.append(spec) playlist = newlist if not playlist: + trycount += 1 continue + trycount = 0 try: p.connect(args.connect_to) except (tcp.NetLibError, PathocError), v: -- cgit v1.2.3