Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sourcery Starbot ⭐ refactored ancker010/exabgp-flowspec-generator #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions flowspec-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,26 @@

# Function to generate a random IPv4 address within the given prefix.
def genipv4():
subnet = IPv4Network("192.168.0.0/16")
subnet = IPv4Network("192.168.0.0/16")
bits = getrandbits(subnet.max_prefixlen - subnet.prefixlen)
addr = IPv4Address(subnet.network_address + bits)
addr_str = str(addr)
return addr_str
return str(addr)
Comment on lines -17 to +20
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function genipv4 refactored with the following changes:

  • Inline variable that is only used once


def genipv6():
subnet6 = IPv6Network("2001:DB8:beef::/64")
bits6 = getrandbits(subnet6.max_prefixlen - subnet6.prefixlen)
addr6 = IPv6Address(subnet6.network_address + bits6)
addr_str6 = str(addr6)
return addr_str6
return str(addr6)
Comment on lines -27 to +26
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function genipv6 refactored with the following changes:

  • Inline variable that is only used once



# Function to generate a list of flowspec rules based on random data.
def genflows():
flow_types = []
flow_types.append(
"announce flow route { match { source " + genipv4() + "/32; destination 192.168.255.10/32; destination-port =" + str(random.randint(1024, 65530)) + "; protocol tcp; } then { rate-limit " + str(random.randint(9600, 51200)) + "; } }")
flow_types = [
"announce flow route { match { source " + genipv4() +
"/32; destination 192.168.255.10/32; destination-port =" + str(
random.randint(1024, 65530)) + "; protocol tcp; } then { rate-limit " +
str(random.randint(9600, 51200)) + "; } }"
]
Comment on lines -33 to +36
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function genflows refactored with the following changes:

  • Merge append into list assignment

flow_types.append("announce flow route { match { source " + genipv4() + "/32; } then { discard; } }")
flow_types.append("announce flow route { match { source " + genipv4() + "/32; } then { redirect 666:666; } }")
flow_types.append("announce flow route { match { destination " + genipv4() + "/32; } then { discard; } }")
Expand All @@ -45,9 +46,12 @@ def genflows():
return flow_types

def genflows6():
flow_types6 = []
flow_types6.append(
"announce flow route { match { source " + genipv6() + "/128; destination 2001:DB8:beef:cafe::1/128; destination-port =" + str(random.randint(1024, 65530)) + "; protocol tcp; } then { rate-limit " + str(random.randint(9600, 51200)) + "; } }")
flow_types6 = [
"announce flow route { match { source " + genipv6() +
"/128; destination 2001:DB8:beef:cafe::1/128; destination-port =" + str(
random.randint(1024, 65530)) + "; protocol tcp; } then { rate-limit " +
str(random.randint(9600, 51200)) + "; } }"
]
Comment on lines -48 to +54
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function genflows6 refactored with the following changes:

  • Merge append into list assignment

flow_types6.append("announce flow route { match { source " + genipv6() + "/128; } then { discard; } }")
flow_types6.append("announce flow route { match { source " + genipv6() + "/128; } then { redirect 666:666; } }")
flow_types6.append("announce flow route { match { destination " + genipv6() + "/128; } then { discard; } }")
Expand All @@ -63,11 +67,11 @@ def genflows6():

# Append a randomly chosen item from the flow_types list to the messages list to be sent to exabgp.
# This is probably overly complicated, but it does what I want it to do.
for x in range(0, count):
for _ in range(count):
flows = genflows()
messages.append(random.choice(flows))

for x in range(0, count):
for x in range(count):
Comment on lines -66 to +74
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines 66-70 refactored with the following changes:

  • Replace range(0, x) with range(x)
  • Replace unused for index with underscore

flows6 = genflows6()
messages.append(random.choice(flows6))

Expand Down