Example #1
0
def show_route(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    bgpmap = request_type.endswith("bgpmap")

    mask = ""
    if len(expression.split("/")) == 2:
        expression, mask = (expression.split("/"))

    if not mask and proto == "ipv4":
        mask = "32"
    if not mask and proto == "ipv6":
        mask = "128"
    if not mask_is_valid(mask):
        return error_page("mask %s is invalid" % mask)

    if proto == "ipv6" and not ipv6_is_valid(expression):
        try:
            expression = resolve(expression, "AAAA")
        except:
            return error_page("%s is unresolvable or invalid for %s" % (expression, proto))
    if proto == "ipv4" and not ipv4_is_valid(expression):
        try:
            expression = resolve(expression, "A")
        except:
            return error_page("%s is unresolvable or invalid for %s" % (expression, proto))

    if mask:
        expression += "/" + mask

    detail = {}
    errors = []
    for host in hosts.split("+"):
        ret, res = proxy_command(host, proto, request_type, expression)
        res = res.split("\n")

        if ret is False:
            errors.append("%s" % res)
            continue

        if len(res) <= 1:
            errors.append("%s: proxy command failed with error, %s" % (host, "\n".join(res)))
            continue

        if bgpmap:
            detail[host] = build_as_tree_from_raw_proxy_ouput(host, proto, res)
        else:
            detail[host] = add_links(res)

    if bgpmap:
        detail = json.dumps(detail)

    return render_template((bgpmap and 'bgpmap.html' or 'route.html'), detail=detail, command=request_type, expression=expression, errors=errors)
Example #2
0
def show_route(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    bgpmap = request_type.endswith("bgpmap")

    all = (request_type.endswith("detail") and " all" or "")
    if bgpmap:
        all = " all"

    if request_type.startswith("adv"):
        command = "show route " + expression.strip()
        if bgpmap and not command.endswith("all"):
            command = command + " all"
    elif request_type.startswith("where"):
        command = "show route where net ~ [ " + expression + " ]" + all
    else:
        mask = ""
        if len(expression.split("/")) == 2:
            expression, mask = (expression.split("/"))

        if not mask and proto == "ipv4":
            mask = "32"
        if not mask and proto == "ipv6":
            mask = "128"
        if not mask_is_valid(mask):
            return error_page("mask %s is invalid" % mask)

        if proto == "ipv6" and not ipv6_is_valid(expression):
            try:
                expression = resolve(expression, "AAAA")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (expression, proto))
        if proto == "ipv4" and not ipv4_is_valid(expression):
            try:
                expression = resolve(expression, "A")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (expression, proto))

        if mask:
            expression += "/" + mask

        command = "show route for " + expression + all

    detail = {}
    errors = []
    for host in hosts.split("+"):
        ret, res = bird_command(host, proto, command)
        res = res.split("\n")

        if ret is False:
            errors.append("%s" % res)
            continue

        if len(res) <= 1:
            errors.append("%s: bird command failed with error, %s" % (host, "\n".join(res)))
            continue

        if bgpmap:
            detail[host] = build_as_tree_from_raw_bird_ouput(host, proto, res)
        else:
            detail[host] = add_links(res)

    if bgpmap:
        detail = json.dumps(detail)

    return render_template((bgpmap and 'bgpmap.html' or 'route.html'), detail=detail, command=command, expression=expression, errors=errors)
Example #3
0
def bird():
    check_accesslist()

    router_type = app.config.get("ROUTER_TYPE", "bird")
    if router_type == "bird":
        from bird import BirdServer
        if request.path == "/proxy": b = BirdServer(file="/var/run/bird.ctl")
        elif request.path == "/proxy6": b = BirdServer(file="/var/run/bird6.ctl")
        else: return "No bird server selected"
    else: return "Router %s is not available" % (router_type)

    cmd = request.args.get("cmd","")
    cmd = unquote(cmd)

    query = request.args.get("q","")
    query = unquote(query)

    proto = request.args.get("ip","ipv4")
    proto = unquote(proto)

    bgpmap = cmd.endswith("bgpmap")

    all = (cmd.endswith("detail") and " all" or "")
    if bgpmap:
        all = " all"

    if cmd.startswith("adv"):
        command = "show route " + query.strip()
        if bgpmap and not command.endswith("all"):
            command = command + " all"
    elif cmd.startswith("where"):
        command = "show route where net ~ [ " + query + " ]" + all
    else:
        mask = ""
        if len(query.split("/")) == 2:
            query, mask = (query.split("/"))

        if not mask and proto == "ipv4":
            mask = "32"
        if not mask and proto == "ipv6":
            mask = "128"
        if not mask_is_valid(mask):
            return error_page("mask %s is invalid" % mask)

        if proto == "ipv6" and not ipv6_is_valid(query):
            try:
                query = resolve(query, "AAAA")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (query, proto))
        if proto == "ipv4" and not ipv4_is_valid(query):
            try:
                query = resolve(query, "A")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (query, proto))

        if mask:
            query += "/" + mask

        command = "show route for " + query + all

    status, result = b.cmd(command)
    b.close()
    # FIXME: use status
    return result
Example #4
0
def show_route(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    bgpmap = request_type.endswith("bgpmap")

    all = (request_type.endswith("detail") and " all" or "")
    if bgpmap:
        all = " all"

    if request_type.startswith("adv"):
        command = "show route " + expression.strip()
        if bgpmap and not command.endswith("all"):
            command = command + " all"
    elif request_type.startswith("where"):
        command = "show route where net ~ [ " + expression + " ]" + all
    else:
        mask = ""
        if len(expression.split("/")) == 2:
            expression, mask = (expression.split("/"))

        if not mask and proto == "ipv4":
            mask = "32"
        if not mask and proto == "ipv6":
            mask = "128"
        if not mask_is_valid(mask):
            return error_page("mask %s is invalid" % mask)

        if proto == "ipv6" and not ipv6_is_valid(expression):
            try:
                expression = resolve(expression, "AAAA")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (expression, proto))
        if proto == "ipv4" and not ipv4_is_valid(expression):
            try:
                expression = resolve(expression, "A")
            except:
                return error_page("%s is unresolvable or invalid for %s" % (expression, proto))

        if mask:
            expression += "/" + mask

        command = "show route for " + expression + all

    detail = {}
    errors = []
    for host in hosts.split("+"):
        ret, res = bird_command(host, proto, command)
        res = res.split("\n")

        if ret is False:
            errors.append("%s" % res)
            continue

        if len(res) <= 1:
            errors.append("%s: bird command failed with error, %s" % (host, "\n".join(res)))
            continue

        if bgpmap:
            detail[host] = build_as_tree_from_raw_bird_ouput(host, proto, res)
        else:
            detail[host] = add_links(res)

    if bgpmap:
        detail = base64.b64encode(json.dumps(detail))

    return render_template((bgpmap and 'bgpmap.html' or 'route.html'), detail=detail, command=command, expression=expression, errors=errors)
Example #5
0
def show_route(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    bgpmap = request_type.endswith("bgpmap")

    all = (request_type.endswith("detail") and " all" or "")
    if bgpmap:
        all = " all"

    if request_type.startswith("adv"):
        command = "show route " + expression.strip()
        if bgpmap and not command.endswith("all"):
            command = command + " all"
    elif request_type.startswith("where"):
        command = "show route where net ~ [ " + expression + " ]" + all
    else:
        mask = ""
        if len(expression.split("/")) == 2:
            expression, mask = (expression.split("/"))

        if app.config.get("UNIFIED_DAEMON", False):
            if not ip_is_valid(expression):
                try:
                    expression = resolve_any(expression)
                except:
                    return error_page("%s is unresolvable" % expression)

            if not mask and ipv4_is_valid(expression):
                mask = "32"
            if not mask and ipv6_is_valid(expression):
                mask = "128"
            if not mask_is_valid(mask):
                return error_page("mask %s is invalid" % mask)
        else:
            if not mask and proto == "ipv4":
                mask = "32"
            if not mask and proto == "ipv6":
                mask = "128"
            if not mask_is_valid(mask):
                return error_page("mask %s is invalid" % mask)

            if proto == "ipv6" and not ipv6_is_valid(expression):
                try:
                    expression = resolve(expression, "AAAA")
                except:
                    return error_page("%s is unresolvable or invalid for %s" %
                                      (expression, proto))
            if proto == "ipv4" and not ipv4_is_valid(expression):
                try:
                    expression = resolve(expression, "A")
                except:
                    return error_page("%s is unresolvable or invalid for %s" %
                                      (expression, proto))

        if mask:
            expression += "/" + mask

        command = "show route for " + expression + all

    detail = {}
    errors = []

    hosts = hosts.split("+")
    if hosts == ["all"]:
        hosts = list(app.config["PROXY"].keys())
    allhosts = hosts[:]
    for host in allhosts:
        ret, res = bird_command(host, proto, command)
        res = res.split("\n")

        if ret is False:
            errors.append("%s" % res)
            continue

        if len(res) <= 1:
            errors.append("%s: bird command failed with error, %s" %
                          (host, "\n".join(res)))
            continue

        if bgpmap:
            detail[host] = build_as_tree_from_raw_bird_ouput(host, proto, res)
            #for internal routes via hosts not selected
            #add them to the list, but only show preferred route
            if host not in hosts:
                detail[host] = detail[host][:1]
            for path in detail[host]:
                if len(path) == 2:
                    if (path[1] not in allhosts) and (path[1]
                                                      in app.config["PROXY"]):
                        allhosts.append(path[1])

        else:
            detail[host] = add_links(res)

    if bgpmap:
        img = render_img(detail)
        return render_template('bgpmap.html',
                               img=img,
                               command=command,
                               expression=expression,
                               errors=errors)
    else:
        return render_template('route.html',
                               detail=detail,
                               command=command,
                               expression=expression,
                               errors=errors)
Example #6
0
def show_route(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    bgpmap = request_type.endswith("bgpmap")

    mask = ""
    if len(expression.split("/")) == 2:
        expression, mask = (expression.split("/"))

    if not mask and proto == "ipv4":
        mask = "32"
    if not mask and proto == "ipv6":
        mask = "128"
    if not mask_is_valid(mask):
        return error_page("mask %s is invalid" % mask)

    if proto == "ipv6" and not ipv6_is_valid(expression):
        try:
            expression = resolve(expression, "AAAA")
        except:
            return error_page("%s is unresolvable or invalid for %s" %
                              (expression, proto))
    if proto == "ipv4" and not ipv4_is_valid(expression):
        try:
            expression = resolve(expression, "A")
        except:
            return error_page("%s is unresolvable or invalid for %s" %
                              (expression, proto))

    if mask:
        expression += "/" + mask

    detail = {}
    errors = []
    for host in hosts.split("+"):
        ret, res = proxy_command(host, proto, request_type, expression)
        res = res.split("\n")

        if ret is False:
            errors.append("%s" % res)
            continue

        if len(res) <= 1:
            errors.append("%s: proxy command failed with error, %s" %
                          (host, "\n".join(res)))
            continue

        if bgpmap:
            detail[host] = build_as_tree_from_raw_proxy_ouput(host, proto, res)
        else:
            detail[host] = add_links(res)

    if bgpmap:
        detail = json.dumps(detail)

    return render_template((bgpmap and 'bgpmap.html' or 'route.html'),
                           detail=detail,
                           command=request_type,
                           expression=expression,
                           errors=errors)