def MikroTik_TOR_nodes_update(submenu='demo'):
# Only one fetch per 30 minutes allowed
# by the author of list!
# Skip IPv6 addresses.
ROUTER_IP = '192.168.88.1'
USER = 'admin'
PWD = 'PaSsW0rD'
# Exit nodes only:
URL = 'https://www.dan.me.uk/torlist/?exit'
LIST_NAME = 'TOR_NODES'
ans = dialog([
'Get a new list?'
, 'Use previous'
, 'Cancel'
])
if ans == 1000:
ips = page_get(URL)
if isinstance(ips, Exception):
dialog('Failed to download new list'
, content=repr(ips))
return
elif 'Umm...' in ips:
dialog('Failed to download new list'
, content=ips)
return
else:
var_set('tor nodes', ips)
elif ans == 1001:
ips = var_get('tor nodes')
else:
return
# skip ipv6:
ips = [ip for ip in ips.split() if not ':' in ip]
cmd = []
for ip in ips:
cmd.append([
'/ip/firewall/address-list/add'
, '=list=' + LIST_NAME
, '=address=' + ip
])
# remove old nodes:
status, data = routeros_find_send(
cmd_find = [
'/ip/firewall/address-list/print'
, '?list=' + LIST_NAME
]
, cmd_send = ['/ip/firewall/address-list/remove']
, device_ip=ROUTER_IP
, device_user=USER
, device_pwd=PWD
)
if not status:
dialog('Error when removing old nodes'
, content=data)
return
status, data = routeros_send(
cmd=cmd
, device_ip=ROUTER_IP
, device_user=USER
, device_pwd=PWD
, info=True
)
if status:
dialog(f'Done: {len(ips)} nodes')
else:
dialog('Error when adding new nodes'
, content=data)