Improve check-instance-ban script

* Added user-agent and more headers
* Design has changed from: 1) search acct on instance-to-check 2) check API lookup of instance.
main
Bofh 5 months ago
parent 5d0dc51505
commit 85f3fc1e64

@ -14,25 +14,27 @@ def main():
if len(sys.argv) > 1:
DOMAIN = sys.argv[1]
res = http_get('https://api.joinmastodon.org/servers?language=en&category=&region=&ownership=&registrations=')
res = http_get('https://api.joinmastodon.org/servers?language=&category=&region=&ownership=&registrations=')
res = json.loads(res.text)
domains = [it['domain'] for it in res]
domains = sorted(set(domains))
acct = get_admin_account(DOMAIN)
if acct == False:
return 1
failed_servers = 0
banned_by = []
for it in res:
for domain in domains:
print('checking "{}" has banned "{}"... '\
.format(it['domain'], DOMAIN))
acct = get_admin_account(it['domain'])
if acct == False:
failed_servers += 1
continue
r = http_get('https://'+DOMAIN+'/'+acct, True)
.format(domain, DOMAIN))
r = http_get('https://{}/api/v1/accounts/lookup?acct={}'\
.format(domain, acct), True)
if r == False:
failed_servers += 1
continue
if r.status_code == 404:
banned_by.append(it['domain'])
banned_by.append(domain)
print('YES')
else:
print('NO')
@ -41,8 +43,9 @@ def main():
print('Instance: '+DOMAIN)
print('Banned by: '+json.dumps(banned_by))
print('Servers count: total={} | failed={} | used_in_percentage={}'.format(len(res), failed_servers, len(res)-failed_servers))
print('Percentage of bans: '+str( (len(banned_by) * 100) / (len(res)-failed_servers) )+'% has banned the instance')
print('Percentage of bans: '+str(round( (len(banned_by) * 100) / (len(res)-failed_servers), 2))+'% has banned the instance')
print()
return 0
def http_get(url, show=False):
try:
@ -56,7 +59,24 @@ def http_get(url, show=False):
return cache_read(fl)
if show:
print(' HTTP: '+url)
r = requests.get(url, timeout=5)
_headers = """
User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:102.0) Gecko/20100101 Firefox/102.0
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Pragma: no-cache
Cache-Control: no-cache
"""
headers = {}
for l in _headers.strip().splitlines():
l = l.strip()
if l == '':
continue
k = l[:l.index(':')]
v = l[l.index(':')+1:].strip()
headers[k] = v
r = requests.get(url, headers=headers, timeout=5)
cache_write(fl, r)
return r
except requests.exceptions.ConnectionError:
@ -80,7 +100,7 @@ def get_admin_account(domain):
if r == False:
return False
js = json.loads(r.text)
return '@'+js['contact_account']['username']+'@'+domain
return js['contact_account']['username']+'@'+domain
def md5(s):
return hashlib.md5(s.encode('utf8')).hexdigest()
@ -100,4 +120,4 @@ def writef(f,c,t='w'):
w.close()
if __name__ == '__main__':
main()
sys.exit(main())

Loading…
Cancel
Save