MCPcopy Create free account
hub / github.com/breakerspace/weaponizing-censors / process_file

Function process_file

process_sorted.py:19–83  ·  view source on GitHub ↗

Processes given file

(args)

Source from the content-addressed store, hash-verified

17
18
19def process_file(args):
20 """
21 Processes given file
22 """
23 info = {}
24 info["total_bytes_from_all_ips"] = 0
25 info["total_packets_from_all_ips"] = 0
26 info["total_bytes_from_amplifying_ips"] = 0
27 info["total_packets_from_amplifying_ips"] = 0
28 info["total_ips"] = 0
29 info["total_amplifying_ips"] = 0
30 flags = {}
31 size = args["size"]
32 to_analyze = args["file"]
33 print("Calculating total length of file to analyze:")
34 length = get_len(to_analyze)
35 print("%d total packets to analyze." % length)
36 pbar = tqdm.tqdm(total=length, leave=False)
37 d = args["delimeter"]
38 with open(to_analyze, "r") as fd:
39 line = fd.readline()
40 ip, length, _, _, _ = line.split(d)
41 out_file = to_analyze.replace(".csv", "") + "_total_by_ip.txt"
42 with open(out_file, "w") as out_fd:
43 last_ip = ip
44 total_len = 0
45 total_packets = 0
46 while line:
47 pbar.update(1)
48 ip, length, _, pktflags, _ = line.split(d)
49 if ip == "addr" or length == "len": #skip csv format line
50 line = fd.readline()
51 continue
52 if pktflags not in flags:
53 flags[pktflags] = 0
54 flags[pktflags] += 1
55 if ip == last_ip: #encounter same ip, update totals accordingly
56 total_len += int(length)
57 total_packets += 1
58 else: #encounter new ip, write info for last ip and start count for new ip
59 out_fd.write("%d %s %d\n" % (total_len, last_ip, total_packets))
60 info["total_bytes_from_all_ips"] += total_len
61 info["total_packets_from_all_ips"] += total_packets
62 info["total_ips"] += 1
63 if total_len > size:
64 info["total_bytes_from_amplifying_ips"] += total_len
65 info["total_packets_from_amplifying_ips"] += total_packets
66 info["total_amplifying_ips"] += 1
67 total_len = int(length)
68 total_packets = 1
69 last_ip = ip
70 line = fd.readline()
71 if ip != "saddr":
72 out_fd.write("%d %s %d\n" % (total_len, ip, total_packets))
73 info["total_bytes_from_all_ips"] += total_len
74 info["total_packets_from_all_ips"] += total_packets
75 info["total_ips"] += 1
76 if total_len > size:

Callers 1

process_sorted.pyFile · 0.85

Calls 1

get_lenFunction · 0.85

Tested by

no test coverage detected