示例#1
0
文件: lg.py 项目: lhaus/bird-lg
def traceroute(hosts, proto="ipv4"):
    q = get_query()

    if not q:
        abort(400)

    set_session("traceroute", hosts, proto, q)

    if app.config.get("UNIFIED_DAEMON", False):
        if not ip_is_valid(q):
            try:
                if app.config.get("UNIFIED_TRACEROUTE_IPV6", True):
                    q = resolve_any(q)
                else:
                    q = resolve(q, "A")
            except:
                return error_page("%s is unresolvable" % q)
        if ipv6_is_valid(q):
            proto = "ipv6"
        else:
            proto = "ipv4"
    else:
        if proto == "ipv6" and not ipv6_is_valid(q):
            try:
                q = resolve(q, "AAAA")
            except:
                return error_page("%s is unresolvable or invalid for %s" %
                                  (q, proto))
        if proto == "ipv4" and not ipv4_is_valid(q):
            try:
                q = resolve(q, "A")
            except:
                return error_page("%s is unresolvable or invalid for %s" %
                                  (q, proto))

    errors = []
    infos = {}
    hosts = hosts.split("+")
    if hosts == ["all"]:
        hosts = app.config["PROXY"].keys()
    for host in hosts:
        status, resultat = bird_proxy(host, proto, "traceroute", q)
        if status is False:
            errors.append("%s" % resultat)
            continue

        infos[host] = add_links(resultat)
    return render_template('traceroute.html', infos=infos, errors=errors)
示例#2
0
文件: lg.py 项目: rodecker/bird-lg
def traceroute(hosts, proto):
    q = get_query()

    if not q:
        abort(400)

    set_session("traceroute", hosts, proto, q)

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

    errors = []
    infos = {}
    for host in hosts.split("+"):
        status, resultat = bird_proxy(host, proto, "traceroute", q)
        if status is False:
            errors.append("%s" % resultat)
            continue


        infos[host] = add_links(resultat)
    return render_template('traceroute.html', infos=infos, errors=errors)
示例#3
0
def traceroute(hosts, proto):
    q = get_query()

    if not q:
        abort(400)

    set_session("traceroute", hosts, proto, q)

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

    errors = []
    infos = {}
    for host in hosts.split("+"):
        status, resultat = bird_proxy(host, proto, "traceroute", q)
        if status is False:
            errors.append("%s" % resultat)
            continue

        infos[host] = add_links(resultat)
    return render_template('traceroute.html', infos=infos, errors=errors)
示例#4
0
文件: lg.py 项目: Magicking/bird-lg
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)
示例#5
0
文件: lg.py 项目: xddxdd/bird-lg
def traceroute(hosts, proto):
    q = get_query()

    if not q:
        abort(400)

    set_session("traceroute", hosts, proto, q)

    if app.config.get("UNIFIED_DAEMON", False):
        if not ip_is_valid(q):
            try:
                q = resolve_any(q)
            except:
                return error_page("%s is unresolvable" % q)
    else:
        if proto == "ipv6" and not ipv6_is_valid(q):
            try:
                q = resolve(q, "AAAA")
            except:
                return error_page("%s is unresolvable or invalid for %s" %
                                  (q, proto))
        if proto == "ipv4" and not ipv4_is_valid(q):
            try:
                q = resolve(q, "A")
            except:
                return error_page("%s is unresolvable or invalid for %s" %
                                  (q, proto))

    errors = []
    infos = {}

    hosts_l = hosts.split("+")
    reqs = [
        grequests.get(bird_url(host, proto, "traceroute", q), timeout=20)
        for host in hosts.split("+")
    ]
    rets = grequests.map(reqs)

    for i in range(len(hosts_l)):
        host = hosts_l[i]

        if not rets[i]:
            errors.append("%s: request failed" % host)
            continue

        res = rets[i].text.split("\n")

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

        infos[host] = add_links(res)
    return render_template('traceroute.html', infos=infos, errors=errors)
示例#6
0
def show_route_api(request_type, hosts, proto):
    expression = get_query()
    if not expression:
        abort(400)

    set_session(request_type, hosts, proto, expression)

    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

        detail[host] = add_json(res)

    return json.dumps(detail)
示例#7
0
def traceroute(hosts, proto):
    q = request.args.get('q', '').strip()
    if not q:
        abort(400)

    set_session("traceroute", hosts, proto, q)

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

    infos = {}
    for host in hosts.split("+"):
        status, resultat = bird_proxy(host, proto, "traceroute", q)
        infos[host] = add_links(resultat)
    return render_template('traceroute.html', infos=infos)
示例#8
0
文件: lg.py 项目: rodecker/bird-lg
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)
示例#9
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)
示例#10
0
文件: lg.py 项目: xddxdd/bird-lg
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_l = hosts.split("+")
    reqs = [
        grequests.get(bird_url(host, proto, "bird", command), timeout=20)
        for host in hosts.split("+")
    ]
    rets = grequests.map(reqs)

    for i in range(len(hosts_l)):
        host = hosts_l[i]

        if not rets[i]:
            errors.append("%s: request failed" % host)
            continue

        res = rets[i].text.split("\n")

        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)
示例#11
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
示例#12
0
文件: lg.py 项目: petabyteboy/bird-lg
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)
示例#13
0
文件: lg.py 项目: Magicking/bird-lg
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)