From 1a767555946b637256951b78e9495f6eb8295c62 Mon Sep 17 00:00:00 2001 From: Thomas Mangin Date: Fri, 25 Oct 2024 16:00:31 +0100 Subject: [PATCH] ruff'ing the etc folder --- etc/exabgp/example-api-program.run | 39 ++++++++++++----- etc/exabgp/run/api-api.receive.run | 4 +- etc/exabgp/run/api-attributes-path.run | 4 +- etc/exabgp/run/api-blocklist.run | 59 +++++++++++++------------- etc/exabgp/run/api-flow-merge.run | 1 - etc/exabgp/run/api-rr.run | 12 +++--- etc/exabgp/run/stderr-1.py | 2 +- etc/exabgp/run/syslog-1.py | 6 +-- etc/exabgp/run/watchdog.run | 4 +- 9 files changed, 74 insertions(+), 57 deletions(-) diff --git a/etc/exabgp/example-api-program.run b/etc/exabgp/example-api-program.run index ce94ce23a..7878e34f8 100755 --- a/etc/exabgp/example-api-program.run +++ b/etc/exabgp/example-api-program.run @@ -37,7 +37,12 @@ errno_fatal = set( ) ) -errno_unavailable = set((errno.ECONNREFUSED, errno.EHOSTUNREACH,)) +errno_unavailable = set( + ( + errno.ECONNREFUSED, + errno.EHOSTUNREACH, + ) +) def asynchronous(fd): @@ -57,7 +62,7 @@ def sync(fd): if not asynchronous(sys.stdin): - sys.stderr.write("could not set stdin/stdout non blocking\n") + sys.stderr.write('could not set stdin/stdout non blocking\n') sys.stderr.flush() sys.exit(1) @@ -73,15 +78,15 @@ def _reader(): yield '' continue elif exc.args[0] in errno_fatal: - sys.stderr.write("fatal error while reading on stdin : %s" % str(exc)) + sys.stderr.write('fatal error while reading on stdin : %s' % str(exc)) sys.exit(1) else: - sys.stderr.write("unknown error while reading on stdin : %s" % str(exc)) + sys.stderr.write('unknown error while reading on stdin : %s' % str(exc)) sys.exit(1) if not data: # we lost the pipe - sys.stderr.write("the read pipe was closed by the other side : %s" % str(exc)) + sys.stderr.write('the read pipe was closed by the other side : %s' % str(exc)) sys.exit(1) received += data @@ -95,7 +100,12 @@ def _reader(): reader = _reader().next -def write(data='', memory=['',]): +def write( + data='', + memory=[ + '', + ], +): left = memory.pop() left += data try: @@ -109,11 +119,11 @@ def write(data='', memory=['',]): if exc.args[0] in errno_block: return not not left elif exc.args[0] in errno_fatal: - sys.stderr.write("fatal error while writing on stdout : %s" % str(exc)) + sys.stderr.write('fatal error while writing on stdout : %s' % str(exc)) sys.stderr.flush() sys.exit(1) else: - sys.stderr.write("unknown error while writing on stdout : %s" % str(exc)) + sys.stderr.write('unknown error while writing on stdout : %s' % str(exc)) sys.stderr.flush() sys.exit(1) @@ -122,18 +132,25 @@ def write(data='', memory=['',]): def read(timeout): try: - r, w, x = select.select([sys.stdin], [], [sys.stdin,], timeout) # pylint: disable=W0612 + r, w, x = select.select( + [sys.stdin], + [], + [ + sys.stdin, + ], + timeout, + ) # pylint: disable=W0612 except IOError as exc: if exc.args[0] in errno_block: return '' elif exc.args[0] in errno_fatal: # this may not send anything ... - sys.stderr.write("fatal error during select : %s" % str(exc)) + sys.stderr.write('fatal error during select : %s' % str(exc)) sys.stderr.flush() sys.exit(1) else: # this may not send anything ... - sys.stderr.write("unexpected error during select : %s" % str(exc)) + sys.stderr.write('unexpected error during select : %s' % str(exc)) sys.stderr.flush() sys.exit(1) diff --git a/etc/exabgp/run/api-api.receive.run b/etc/exabgp/run/api-api.receive.run index 8f9aff0aa..9936a6ace 100755 --- a/etc/exabgp/run/api-api.receive.run +++ b/etc/exabgp/run/api-api.receive.run @@ -25,12 +25,12 @@ while os.getppid() != 1: sys.stderr.write(' (ok) api.receive 0.0.0.0/32 received\n%s\n' % line) sys.stdout.flush() time.sleep(2) # make sure the other process is faster if it gets any data - sys.stdout.write("announce route 6.6.6.0/24 next-hop 1.1.1.1\n") + sys.stdout.write('announce route 6.6.6.0/24 next-hop 1.1.1.1\n') sys.stdout.flush() else: sys.stderr.write(' (failure) api.receive received unexpected data:\n%s' % line) sys.stderr.flush() - sys.stdout.write("announce route 6.6.6.0/24 next-hop 9.9.9.9\n") + sys.stdout.write('announce route 6.6.6.0/24 next-hop 9.9.9.9\n') sys.stdout.flush() time.sleep(3) sys.exit(1) diff --git a/etc/exabgp/run/api-attributes-path.run b/etc/exabgp/run/api-attributes-path.run index fa5a7e025..17861c144 100755 --- a/etc/exabgp/run/api-attributes-path.run +++ b/etc/exabgp/run/api-attributes-path.run @@ -5,8 +5,8 @@ import sys import time messages = [ - 'neighbor 127.0.0.1 announce attributes path-information 1.2.3.4 next-hop 10.11.12.13 community [14:15] local-preference 16 nlri 16.17.18.19/32 20.21.22.23/32', - 'neighbor 127.0.0.1 announce attributes path-information 4.3.2.1 next-hop 10.11.12.13 community [14:15] local-preference 16 nlri 16.17.18.19/32 20.21.22.23/32', + 'neighbor 127.0.0.1 announce attributes path-information 1.2.3.4 next-hop 10.11.12.13 community [14:15] local-preference 16 nlri 16.17.18.19/32 20.21.22.23/32', + 'neighbor 127.0.0.1 announce attributes path-information 4.3.2.1 next-hop 10.11.12.13 community [14:15] local-preference 16 nlri 16.17.18.19/32 20.21.22.23/32', ] while messages: diff --git a/etc/exabgp/run/api-blocklist.run b/etc/exabgp/run/api-blocklist.run index de2a63595..e8c4632d0 100755 --- a/etc/exabgp/run/api-blocklist.run +++ b/etc/exabgp/run/api-blocklist.run @@ -1,8 +1,7 @@ #!/usr/bin/python3 # encoding utf-8 -""" -""" +""" """ import os import sys @@ -26,9 +25,9 @@ import urllib.parse # (no-advertise may also be set in the router route-map (belt and suspenders)) # -delay = 600 -specs4 = ' next-hop 192.0.2.1 community [65535:666 no-advertise]' -specs6 = ' next-hop 100::1 community [65535:666 no-advertise]' +delay = 600 +specs4 = ' next-hop 192.0.2.1 community [65535:666 no-advertise]' +specs6 = ' next-hop 100::1 community [65535:666 no-advertise]' # # Blocklists mostly currated from: @@ -42,15 +41,16 @@ specs6 = ' next-hop 100::1 community [65535:666 no-advertise]' # blocklists = [ - { 'url': 'https://www.spamhaus.org/drop/drop.txt', 'refresh': 7200 }, - { 'url': 'https://www.spamhaus.org/drop/edrop.txt', 'refresh': 7200 }, - { 'url': 'https://www.spamhaus.org/drop/dropv6.txt', 'refresh': 7200 }, - { 'url': 'https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt', 'refresh': 7200 }, - { 'url': 'https://blocklist.greensnow.co/greensnow.txt', 'refresh': 7200 }, - { 'url': 'https://www.darklist.de/raw.php', 'refresh': 7200 }, - { 'url': 'https://sigs.interserver.net/ipslim.txt', 'refresh': 7200 }, - { 'url': 'https://api.blocklist.de/getlast.php?time=3600', 'refresh': 3600 } - ] + {'url': 'https://www.spamhaus.org/drop/drop.txt', 'refresh': 7200}, + {'url': 'https://www.spamhaus.org/drop/edrop.txt', 'refresh': 7200}, + {'url': 'https://www.spamhaus.org/drop/dropv6.txt', 'refresh': 7200}, + {'url': 'https://rules.emergingthreats.net/fwrules/emerging-Block-IPs.txt', 'refresh': 7200}, + {'url': 'https://blocklist.greensnow.co/greensnow.txt', 'refresh': 7200}, + {'url': 'https://www.darklist.de/raw.php', 'refresh': 7200}, + {'url': 'https://sigs.interserver.net/ipslim.txt', 'refresh': 7200}, + {'url': 'https://api.blocklist.de/getlast.php?time=3600', 'refresh': 3600}, +] + def requestsGet(url): r_session = requests.session() @@ -59,6 +59,7 @@ def requestsGet(url): r.raise_for_status() return r + def lineFilter(line): if not line: return None @@ -67,23 +68,23 @@ def lineFilter(line): return None if l.startswith('#'): return None - return (l.split(' ')[0].split(';')[0].split('#')[0].strip()) + return l.split(' ')[0].split(';')[0].split('#')[0].strip() -class blocklistThread(object): +class blocklistThread(object): def __init__(self, url=None, refresh=86400): try: - refresh = int(refresh) + refresh = int(refresh) except ValueError: - raise ValueError('{} is not a valid refresh time interval'.format(refresh)) + raise ValueError('{} is not a valid refresh time interval'.format(refresh)) if refresh < 60: - raise ValueError('{} is not a valid refresh interval of at least 60 seconds'.format(refresh)) + raise ValueError('{} is not a valid refresh interval of at least 60 seconds'.format(refresh)) try: result = urllib.parse.urlparse(url) except ValueError: - raise ValueError('{} is not a valid url'.format(url)) - if not all ([result.scheme, result.netloc]): - raise ValueError('{} is not a valid url'.format(url)) + raise ValueError('{} is not a valid url'.format(url)) + if not all([result.scheme, result.netloc]): + raise ValueError('{} is not a valid url'.format(url)) self._prefixes = [] self._valid = False self._url = url @@ -101,7 +102,7 @@ class blocklistThread(object): def getPrefixes(self): # Give our thread a chance to get data once count = 0 - while ((not self._valid) and (count < 12)): + while (not self._valid) and (count < 12): count = count + 1 time.sleep(5) self._valid = True @@ -137,8 +138,8 @@ class blocklistThread(object): # We add in a little jitter to assist source site load time.sleep(refresh + random.randint(-300, 300)) -class responseThread(object): +class responseThread(object): def __init__(self): thread = threading.Thread(target=self.run, args=()) thread.daemon = True @@ -159,20 +160,20 @@ class responseThread(object): sys.stderr.write('error {} reading from stdin.\n'.format(e.errno)) sys.stderr.flush() else: - if (line == 'shutdown'): + if line == 'shutdown': sys.stderr.write('shutdown request received, terminating process.\n') sys.stderr.flush() os._exit(1) - if (line != 'done'): + if line != 'done': sys.stderr.write('unexpected response {} received.\n'.format(line)) sys.stderr.flush() + # # Start at the start # if __name__ == '__main__': - # Start our blocklist retrival threads blocklistThreads = [] @@ -193,7 +194,6 @@ if __name__ == '__main__': currentBlocklist = dict() while True: - newBlocklist = dict() for blt in blocklistThreads: for prefix in blt.getPrefixes(): @@ -223,5 +223,4 @@ if __name__ == '__main__': except KeyboardInterrupt: sys.stderr.write('\nshutting down due to user request\n') sys.stderr.flush() - os._exit(1) - + os._exit(1) diff --git a/etc/exabgp/run/api-flow-merge.run b/etc/exabgp/run/api-flow-merge.run index caf3a81f7..8a5eab790 100755 --- a/etc/exabgp/run/api-flow-merge.run +++ b/etc/exabgp/run/api-flow-merge.run @@ -15,7 +15,6 @@ messages = [ 'announce flow route { match { source 8.8.8.8/32; } then { discard; } scope { interface-set [ non-transitive:input:3405770241:1 transitive:output:254:254 ]; } }', 'announce flow route { match { source 9.9.9.9/32; } then { discard; } scope { interface-set [ non-transitive:input:3405770241:1 transitive:output:254:254 ]; } }', 'announce flow route { match { source 10.10.10.10/32; } scope { interface-set [ transitive:input-output:1234:10 transitive:input:1234:10 transitive:output:0:0]; } then { discard; } }', - 'announce flow route destination 133.130.1.219/32 discard interface-set [ transitive:input:1234:10]', 'announce flow route destination 133.130.1.19/32 interface-set [ transitive:input:1234:10 ] discard', ] diff --git a/etc/exabgp/run/api-rr.run b/etc/exabgp/run/api-rr.run index 8412c68e9..e2fe09dbd 100755 --- a/etc/exabgp/run/api-rr.run +++ b/etc/exabgp/run/api-rr.run @@ -4,6 +4,7 @@ import os import sys import time + def check_ok(): line = sys.stdin.readline().strip() if line != 'done': @@ -11,36 +12,37 @@ def check_ok(): sys.stdout.write('announce route 255.255.255.255/32 next-hop 255.255.255.255\n') sys.stdout.flush() + try: time.sleep(0.2) sys.stdout.write('announce route 192.168.0.0/32 next-hop 10.0.0.0\n') sys.stdout.flush() time.sleep(0.2) - check_ok() + check_ok() sys.stdout.write('announce route 192.168.0.1/32 next-hop 10.0.0.1\n') sys.stdout.flush() time.sleep(0.2) - check_ok() + check_ok() sys.stdout.write('announce route-refresh ipv4 unicast\n') sys.stdout.flush() time.sleep(0.2) - check_ok() + check_ok() valid = True rr = sys.stdin.readline() if '"nlri": "192.168.0.0/32"' not in rr: valid = False - sys.stdout.write("debug " + rr + '\n') + sys.stdout.write('debug ' + rr + '\n') sys.stdout.flush() rr = sys.stdin.readline() if '"nlri": "192.168.0.1/32"' not in rr: valid = False - sys.stdout.write("debug " + rr + '\n') + sys.stdout.write('debug ' + rr + '\n') sys.stdout.flush() if valid: diff --git a/etc/exabgp/run/stderr-1.py b/etc/exabgp/run/stderr-1.py index 125c3ef3c..ef103efc2 100755 --- a/etc/exabgp/run/stderr-1.py +++ b/etc/exabgp/run/stderr-1.py @@ -18,7 +18,7 @@ def _prefixed(level, message): try: line = sys.stdin.readline().strip() sys.stdout.flush() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/etc/exabgp/run/syslog-1.py b/etc/exabgp/run/syslog-1.py index 636ee29b0..a16398127 100755 --- a/etc/exabgp/run/syslog-1.py +++ b/etc/exabgp/run/syslog-1.py @@ -8,10 +8,10 @@ def _prefixed(level, message): now = time.strftime('%a, %d %b %Y %H:%M:%S', time.localtime()) - return "%s %-8s %-6d %s" % (now, level, os.getpid(), message) + return '%s %-8s %-6d %s' % (now, level, os.getpid(), message) -syslog.openlog("ExaBGP") +syslog.openlog('ExaBGP') # When the parent dies we are seeing continual newlines, so we only access so many before stopping counter = 0 @@ -19,7 +19,7 @@ def _prefixed(level, message): while os.getppid() != 1: try: line = sys.stdin.readline().strip() - if line == "": + if line == '': counter += 1 if counter > 100: break diff --git a/etc/exabgp/run/watchdog.run b/etc/exabgp/run/watchdog.run index 6d5203b91..aa3a0805d 100755 --- a/etc/exabgp/run/watchdog.run +++ b/etc/exabgp/run/watchdog.run @@ -7,10 +7,10 @@ import time time.sleep(1) # time for configuration routes to pass while os.getppid() != 1: - sys.stdout.write("announce watchdog dnsr\n") + sys.stdout.write('announce watchdog dnsr\n') sys.stdout.flush() time.sleep(0.2) - sys.stdout.write("withdraw watchdog dnsr\n") + sys.stdout.write('withdraw watchdog dnsr\n') sys.stdout.flush() time.sleep(0.2)