Skip to content

Commit

Permalink
ruff'ing the etc folder
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-mangin committed Oct 25, 2024
1 parent 9855ee8 commit 1a76755
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 57 deletions.
39 changes: 28 additions & 11 deletions etc/exabgp/example-api-program.run
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ errno_fatal = set(
)
)

errno_unavailable = set((errno.ECONNREFUSED, errno.EHOSTUNREACH,))
errno_unavailable = set(
(
errno.ECONNREFUSED,
errno.EHOSTUNREACH,
)
)


def asynchronous(fd):
Expand All @@ -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)

Expand All @@ -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
Expand All @@ -95,7 +100,12 @@ def _reader():
reader = _reader().next


def write(data='', memory=['',]):
def write(
data='',
memory=[
'',
],
):
left = memory.pop()
left += data
try:
Expand All @@ -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)

Expand All @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions etc/exabgp/run/api-api.receive.run
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions etc/exabgp/run/api-attributes-path.run
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
59 changes: 29 additions & 30 deletions etc/exabgp/run/api-blocklist.run
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#!/usr/bin/python3
# encoding utf-8

"""
"""
""" """

import os
import sys
Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -59,6 +59,7 @@ def requestsGet(url):
r.raise_for_status()
return r


def lineFilter(line):
if not line:
return None
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = []
Expand All @@ -193,7 +194,6 @@ if __name__ == '__main__':
currentBlocklist = dict()

while True:

newBlocklist = dict()
for blt in blocklistThreads:
for prefix in blt.getPrefixes():
Expand Down Expand Up @@ -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)
1 change: 0 additions & 1 deletion etc/exabgp/run/api-flow-merge.run
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]
Expand Down
12 changes: 7 additions & 5 deletions etc/exabgp/run/api-rr.run
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,45 @@ import os
import sys
import time


def check_ok():
line = sys.stdin.readline().strip()
if line != 'done':
sys.stdout.write(f'debug expected "done" not "{line}"\n')
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:
Expand Down
2 changes: 1 addition & 1 deletion etc/exabgp/run/stderr-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions etc/exabgp/run/syslog-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

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

while os.getppid() != 1:
try:
line = sys.stdin.readline().strip()
if line == "":
if line == '':
counter += 1
if counter > 100:
break
Expand Down
4 changes: 2 additions & 2 deletions etc/exabgp/run/watchdog.run
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit 1a76755

Please sign in to comment.