2021-03-01 15:47:06 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2021-03-01 15:59:38 +00:00
|
|
|
import traceback, os, asyncio, requests, json, discord
|
|
|
|
from bot_utils import setup, send_msg
|
2021-03-01 15:47:06 +00:00
|
|
|
|
2021-03-01 16:13:00 +00:00
|
|
|
bot_token = setup()
|
|
|
|
client = discord.Client()
|
|
|
|
|
2021-03-01 15:47:06 +00:00
|
|
|
AIRTABLE_TABLE = "app89plJvA9EqTJEc"
|
|
|
|
AIRTABLE_FIELD = "Link"
|
2021-03-01 16:11:12 +00:00
|
|
|
AIRTABLE_API_KEY = os.getenv('AIRTABLE_API_KEY')
|
2021-03-01 15:47:06 +00:00
|
|
|
|
|
|
|
async def block_skylinks_from_airtable():
|
2021-03-01 16:08:10 +00:00
|
|
|
print("Pulling blocked skylinks from airtable via api integration")
|
2021-03-01 15:47:06 +00:00
|
|
|
headers = { "Authorization": "Bearer " + AIRTABLE_API_KEY }
|
|
|
|
airtable = requests.get(
|
|
|
|
"https://api.airtable.com/v0/" + AIRTABLE_TABLE + "/Table%201?fields%5B%5D=" + AIRTABLE_FIELD, headers=headers
|
2021-03-01 16:19:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if airtable.status_code != 200:
|
2021-03-01 16:20:42 +00:00
|
|
|
message = "Airtable blocklist integration responded with code " + str(airtable.status_code) + ": " + (airtable.text or "empty response")
|
2021-03-01 16:22:43 +00:00
|
|
|
return print(message) or await send_msg(client, message, force_notify=True)
|
2021-03-01 16:19:13 +00:00
|
|
|
|
|
|
|
skylinks = [entry['fields'][AIRTABLE_FIELD] for entry in airtable.json()['records']]
|
2021-03-01 16:56:30 +00:00
|
|
|
|
|
|
|
if len(skylinks) == 0:
|
|
|
|
return print("Airtable returned 0 skylinks to block - make sure your table configuration is correct")
|
|
|
|
else:
|
|
|
|
print("Airtable returned " + str(len(skylinks)) + " skylinks to block")
|
2021-03-01 15:47:06 +00:00
|
|
|
|
|
|
|
apipassword = os.popen('docker exec sia cat /sia-data/apipassword').read().strip()
|
|
|
|
ipaddress = os.popen('docker inspect -f \'{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}\' sia').read().strip()
|
|
|
|
|
2021-03-01 16:08:10 +00:00
|
|
|
print("Sending blocklist request to siad")
|
2021-03-01 15:47:06 +00:00
|
|
|
headers = { 'user-agent': 'Sia-Agent' }
|
|
|
|
auth = ('', apipassword)
|
|
|
|
data = json.dumps({ 'add': skylinks })
|
|
|
|
response = requests.post('http://' + ipaddress + ':9980/skynet/blocklist', auth = auth, headers = headers, data = data)
|
|
|
|
|
2021-03-01 16:08:10 +00:00
|
|
|
if response.status_code == 204:
|
2021-03-01 16:56:30 +00:00
|
|
|
print("Skylinks successfully added to siad blocklist")
|
2021-03-01 16:08:10 +00:00
|
|
|
else:
|
|
|
|
message = "Siad blocklist endpoint responded with code " + str(response.status_code) + ": " + (response.text or "empty response")
|
2021-03-01 17:39:43 +00:00
|
|
|
return print(message) or await send_msg(client, message, force_notify=True)
|
2021-03-01 15:47:06 +00:00
|
|
|
|
2021-03-01 17:26:38 +00:00
|
|
|
print("Purging nginx cache containing blocked skylinks")
|
2021-03-01 17:39:43 +00:00
|
|
|
cached_files_command = '/usr/bin/find /data/nginx/cache/ -type f | /usr/bin/xargs --no-run-if-empty -n1000 /bin/grep -El \'^KEY: .*(' + '|'.join(skylinks) + ')\''
|
|
|
|
cached_files_count = int(os.popen('docker exec -it nginx bash -c "' + cached_files_command + ' | wc -l"').read().strip())
|
|
|
|
|
|
|
|
if cached_files_count == 0:
|
|
|
|
return print("No nginx cached files matching blocked skylinks were found")
|
|
|
|
|
|
|
|
os.popen('docker exec -it nginx bash -c "' + cached_files_command + ' | xargs rm"')
|
|
|
|
message = "Purged " + str(cached_files_count) + " blocklisted files from nginx cache")
|
|
|
|
return print(message) or await send_msg(client, message)
|
2021-03-01 16:56:30 +00:00
|
|
|
|
2021-03-01 15:47:06 +00:00
|
|
|
async def exit_after(delay):
|
|
|
|
await asyncio.sleep(delay)
|
|
|
|
os._exit(0)
|
|
|
|
|
2021-03-01 15:59:38 +00:00
|
|
|
@client.event
|
2021-03-01 15:47:06 +00:00
|
|
|
async def on_ready():
|
2021-03-01 15:59:38 +00:00
|
|
|
try:
|
|
|
|
await block_skylinks_from_airtable()
|
|
|
|
except: # catch all exceptions
|
2021-03-01 16:22:43 +00:00
|
|
|
await send_msg(client, "```\n{}\n```".format(traceback.format_exc()), force_notify=True)
|
2021-03-01 15:47:06 +00:00
|
|
|
asyncio.create_task(exit_after(3))
|
|
|
|
|
2021-03-01 16:09:18 +00:00
|
|
|
client.run(bot_token)
|
|
|
|
|
2021-03-01 15:59:38 +00:00
|
|
|
# asyncio.run(on_ready())
|
2021-03-01 15:47:06 +00:00
|
|
|
|
|
|
|
# --- BASH EQUIVALENT
|
|
|
|
# skylinks=$(curl "https://api.airtable.com/v0/${AIRTABLE_TABLE}/Table%201?fields%5B%5D=Link" -H "Authorization: Bearer ${AIRTABLE_KEY}" | python3 -c "import sys, json; print('[\"' + '\",\"'.join([entry['fields']['Link'] for entry in json.load(sys.stdin)['records']]) + '\"]')")
|
|
|
|
# apipassword=$(docker exec sia cat /sia-data/apipassword)
|
|
|
|
# ipaddress=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sia)
|
|
|
|
# curl -A "Sia-Agent" --user "":"${apipassword}" --data "{\"add\" : ${skylinks}}" "${ipaddress}:9980/skynet/blocklist"
|