def simplify_route(cur_route, user, srv, hops_folder):
    updated_route = {}

    if not is_ip(user):
        user_ip = host2ip(user)
    else:
        user_ip = user
    if not is_ip(srv):
        srv_ip = host2ip(srv)
    else:
        srv_ip = srv

    updated_id = 0
    hop_ids = sorted(cur_route.keys(), key=int)
    first_hop_id = hop_ids[0]
    if user_ip != cur_route[first_hop_id]['IP']:
        start_hop_id = 0
    else:
        start_hop_id = 1
    updated_route[updated_id] = user_ip
    updated_id += 1

    for hop_id in hop_ids[start_hop_id:]:
        cur_hop_ip = cur_route[hop_id]['IP']
        if (cur_hop_ip == "*") or is_reserved(cur_hop_ip):
            continue
        else:
            updated_route[updated_id] = cur_route[hop_id]['IP']
            updated_id += 1

    last_hop_id = hop_ids[-1]
    if cur_route[last_hop_id]['IP'] != srv_ip:
        updated_route[updated_id] = srv_ip

    return updated_route
def route2graph(user_routes, hops_folder, route_status=None):
    users = user_routes.keys()
    route_graph = nx.Graph()
    srv_nodes = []
    user_nodes = []
    for user in users:
        cur_user_routes = user_routes[user]
        if is_ip(user):
            user_ip = user
        else:
            user_ip = host2ip(user)
        if user_ip not in user_nodes:
            user_nodes.append(user_ip)

        for srv in cur_user_routes.keys():
            if route_status:
                if srv not in route_status[user].keys():
                    continue

            # print user, srv
            if is_ip(srv):
                srv_ip = srv
            else:
                srv_ip = host2ip(srv)

            if srv_ip not in srv_nodes:
                srv_nodes.append(srv_ip)

            cur_route = cur_user_routes[srv]
            cur_clean_route = simplify_route(cur_route, user, srv, hops_folder)
            print cur_clean_route
            route_ids = sorted(cur_clean_route.keys(), key=int)
            pre_node = cur_clean_route[route_ids[0]]
            route_graph.add_node(pre_node, node_shape='^', node_size=200, node_color='m')
            for cur_id in route_ids[1:]:
                cur_node = cur_clean_route[cur_id]
                if cur_node == srv_ip:
                    route_graph.add_node(cur_node, node_shape='s', node_size=300, node_color='b')
                else:
                    route_graph.add_node(cur_node, node_shape='o', node_size=50, node_color='grey')

                cur_edge = sorted([pre_node, cur_node])
                route_graph.add_edge(cur_edge[0], cur_edge[1], edge_color='k')
                pre_node = cur_node

    return route_graph, srv_nodes, user_nodes
def get_node_ips(inputList, outputFolder, outputName):
    with open(inputList, 'rb') as f:
        nodes = {}
        for node in f.readlines():
            curNode = node.rstrip()
            nodeIP = host2ip(curNode)
            if nodeIP != '*':
                nodes[curNode] = nodeIP
                ## print curNode, nodeIP
        writeJson(outputFolder, outputName, nodes)
def color_graph(route_graph, routes, users_status, hops_folder, anomaly_user=None):
    node_status = {}
    for user in routes.keys():
        cur_user_routes = routes[user]
        for srv in cur_user_routes.keys():
            print "Color route from ", user, " to server:", srv
            cur_route = cur_user_routes[srv]
            cur_clean_route = simplify_route(cur_route, user, srv, hops_folder)
            if srv not in users_status[user].keys():
                continue
            cur_route_status = users_status[user][srv]

            route_ids = sorted(cur_clean_route.keys(), key=int)

            '''
            client_node = cur_clean_route[route_ids[0]]
            if client_node in node_status.keys():
                node_status[client_node] = cur_route_status & node_status[client_node]
            else:
                node_status[client_node] = cur_route_status
            '''

            for cur_id in route_ids:
                cur_node = cur_clean_route[cur_id]
                if cur_node in node_status.keys():
                    node_status[cur_node] = cur_route_status | node_status[cur_node]
                else:
                    node_status[cur_node] = cur_route_status

    node_colors = {}
    for node in node_status.keys():
        if node_status[node]:
            node_colors[node] = 'g'
        else:
            node_colors[node] = 'r'
            print node

    if anomaly_user:
        anomaly_user_ip = host2ip(anomaly_user)
        node_colors[anomaly_user_ip] = 'r'

    edge_colors = {}
    for (u, v) in route_graph.edges():
        cur_edge = (u, v)
        if node_status[u] and node_status[v]:
            edge_colors[cur_edge] = 'g'
        else:
            edge_colors[cur_edge] = 'r'

    nx.set_node_attributes(route_graph, "node_color", node_colors)
    nx.set_edge_attributes(route_graph, "edge_color", edge_colors)

    return route_graph
def read_user_info(user_name):
    default_user_path = "./clientsInfo/nodes/"
    fileName = default_user_path + user_name + ".json"
    if os.path.exists(fileName):
        user_info = json.load(open(fileName))
    else:
        user_ip = host2ip(user_name)
        if is_ip(user_ip):
            user_info = ipinfo(user_ip)
            writeJson(default_user_path, user_name, user_info)
        else:
            user_info = {}
    return user_info
Esempio n. 6
0
def read_hop_info(hopinfo_path, hop_ip):
    default_hop_path = hopinfo_path + hop_ip + ".json"
    if os.path.exists(default_hop_path):
        try:
            hop_info = json.load(open(default_hop_path))
        except:
            os.remove(default_hop_path)
            if is_ip(hop_ip):
                hop_info = ipinfo(hop_ip)
                save_ipinfo(hopinfo_path, hop_info)
            else:
                hop_info = {}
    else:
        if not is_ip(hop_ip):
            hop_ip = host2ip(hop_ip)

        if is_ip(hop_ip):
            hop_info = ipinfo(hop_ip)
            save_ipinfo(hopinfo_path, hop_info)
        else:
            hop_info = {}
    return hop_info
def load_all_hops(trFolder):
    all_hops = {}

    total_hops = 0

    ## Get all traceroute files
    tr_files = glob.glob(trFolder + "*")

    for cur_tr in tr_files:
        print cur_tr
        tr_file_name = ntpath.basename(cur_tr)
        cur_user = tr_file_name.split('@')[1]
        if cur_user not in all_hops.keys():
            all_hops[cur_user] = {}

        srvs_hops = json.load(open(cur_tr))
        for srv in srvs_hops.keys():
            for hop_id in srvs_hops[srv].keys():
                cur_hop_name = srvs_hops[srv][hop_id]['Addr']
                if is_ip(cur_hop_name):
                    cur_hop_ip = cur_hop_name
                elif cur_hop_name == "*":
                    cur_hop_ip = "*"
                else:
                    cur_hop_ip = host2ip(cur_hop_name)

                if cur_hop_ip == "*":
                    continue
                elif is_reserved(cur_hop_ip):
                    continue

                if cur_hop_ip not in all_hops[cur_user].keys():
                    all_hops[cur_user][cur_hop_ip] = cur_hop_name
                    total_hops += 1

    print "Total number of hops: ", total_hops
    return all_hops