Craft anchor is now specified as a regex

Unifies this with anchor points.
This commit is contained in:
Aldo Cortesi 2015-06-04 19:09:38 +12:00
parent c5992b8d23
commit ae9f470d2b
4 changed files with 27 additions and 15 deletions

View File

@ -8,6 +8,7 @@ from netlib import http_uastrings
from . import pathoc, pathod, version, utils, language from . import pathoc, pathod, version, utils, language
def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr): def args_pathoc(argv, stdout=sys.stdout, stderr=sys.stderr):
preparser = argparse.ArgumentParser(add_help=False) preparser = argparse.ArgumentParser(add_help=False)
preparser.add_argument( preparser.add_argument(
@ -234,13 +235,17 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
action="append", action="append",
metavar="ANCHOR", metavar="ANCHOR",
help=""" help="""
Add an anchor. Specified as a string with the form pattern=pagespec, or Add an anchor. Specified as a string with the form
pattern=filepath pattern=spec or pattern=filepath, where pattern is a regular
expression.
""" """
) )
parser.add_argument( parser.add_argument(
"-c", dest='craftanchor', default="/p", type=str, "-c", dest='craftanchor', default=pathod.DEFAULT_ANCHOR, type=str,
help='Anchorpoint for URL crafting commands. (/p)' help="""
Regular expression specifying anchor point for URL crafting
commands. (%s)
"""%pathod.DEFAULT_ANCHOR
) )
parser.add_argument( parser.add_argument(
"--confdir", "--confdir",
@ -394,6 +399,13 @@ def args_pathod(argv, stdout=sys.stdout, stderr=sys.stderr):
return parser.error(v) return parser.error(v)
args.sizelimit = sizelimit args.sizelimit = sizelimit
try:
args.craftanchor = re.compile(args.craftanchor)
except re.error:
return parser.error(
"Invalid regex in craft anchor: %s" % args.craftanchor
)
anchors = [] anchors = []
for patt, spec in args.anchors: for patt, spec in args.anchors:
if os.path.isfile(spec): if os.path.isfile(spec):

View File

@ -4,6 +4,8 @@ import os
import sys import sys
import threading import threading
import urllib import urllib
import re
from netlib import tcp, http, wsgi, certutils, websockets from netlib import tcp, http, wsgi, certutils, websockets
from . import version, app, language, utils, log from . import version, app, language, utils, log
@ -15,6 +17,7 @@ DEFAULT_CERT_DOMAIN = "pathod.net"
CONFDIR = "~/.mitmproxy" CONFDIR = "~/.mitmproxy"
CERTSTORE_BASENAME = "mitmproxy" CERTSTORE_BASENAME = "mitmproxy"
CA_CERT_NAME = "mitmproxy-ca.pem" CA_CERT_NAME = "mitmproxy-ca.pem"
DEFAULT_ANCHOR = r"/p/?"
logger = logging.getLogger('pathod') logger = logging.getLogger('pathod')
@ -246,10 +249,9 @@ class PathodHandler(tcp.BaseHandler):
nexthandler, retlog["response"] = self.serve_crafted(i[1]) nexthandler, retlog["response"] = self.serve_crafted(i[1])
return nexthandler, retlog return nexthandler, retlog
if not self.server.nocraft and utils.matchpath( m = utils.MemBool()
path, if m(self.server.craftanchor.match(path)):
self.server.craftanchor): spec = urllib.unquote(path)[len(m.v.group()):]
spec = urllib.unquote(path)[len(self.server.craftanchor) + 1:]
websocket_key = websockets.check_client_handshake(headers) websocket_key = websockets.check_client_handshake(headers)
self.settings.websocket_key = websocket_key self.settings.websocket_key = websocket_key
if websocket_key and not spec: if websocket_key and not spec:
@ -323,7 +325,7 @@ class Pathod(tcp.TCPServer):
addr, addr,
ssl=False, ssl=False,
ssloptions=None, ssloptions=None,
craftanchor="/p", craftanchor=re.compile(DEFAULT_ANCHOR),
staticdir=None, staticdir=None,
anchors=(), anchors=(),
sizelimit=None, sizelimit=None,
@ -380,6 +382,8 @@ class Pathod(tcp.TCPServer):
""" """
A policy check that verifies the request size is withing limits. A policy check that verifies the request size is withing limits.
""" """
if self.nocraft:
return "Crafting disabled.", None
try: try:
req = req.resolve(settings) req = req.resolve(settings)
l = req.maximum_length(settings) l = req.maximum_length(settings)

View File

@ -1,8 +1,9 @@
import threading import threading
import Queue import Queue
import requests import requests
import requests.packages.urllib3 import requests.packages.urllib3
import pathod from . import pathod
requests.packages.urllib3.disable_warnings() requests.packages.urllib3.disable_warnings()

View File

@ -127,8 +127,3 @@ def daemonize(stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'): # pra
os.dup2(si.fileno(), sys.stdin.fileno()) os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno()) os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno()) os.dup2(se.fileno(), sys.stderr.fileno())
def matchpath(path, spec):
if path == spec or path.startswith(spec + "/"):
return True