2021-05-14 10:39:26 +02:00
|
|
|
import time
|
|
|
|
from datetime import datetime
|
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import sys
|
|
|
|
import os.path
|
|
|
|
import psycopg2
|
2022-03-13 18:06:07 +01:00
|
|
|
import requests
|
|
|
|
import urllib3
|
2021-05-14 10:39:26 +02:00
|
|
|
import socket
|
2022-03-13 18:06:07 +01:00
|
|
|
import ray
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
ray.init(num_cpus = 32) # Specify this system CPUs.
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
apis = ['/api/v1/instance?',
|
|
|
|
'/nodeinfo/2.0?',
|
|
|
|
'/nodeinfo/2.0.json?',
|
|
|
|
'/main/nodeinfo/2.0?',
|
|
|
|
'/api/statusnet/config?',
|
|
|
|
'/api/nodeinfo/2.0.json?',
|
|
|
|
'/api/nodeinfo?',
|
|
|
|
'/wp-json/nodeinfo/2.0?',
|
|
|
|
'/api/v1/instance/nodeinfo/2.0?',
|
|
|
|
'/.well-known/x-nodeinfo2?'
|
|
|
|
]
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
def is_json(myjson):
|
|
|
|
|
|
|
|
try:
|
|
|
|
json_object = json.loads(myjson)
|
|
|
|
except ValueError as e:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def write_api(server, software, users, alive, api, soft_version):
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
fediverse_db, fediverse_db_user = get_db_config()
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
insert_sql = "INSERT INTO fediverse(server, updated_at, software, users, alive, users_api, version) VALUES(%s,%s,%s,%s,%s,%s,%s) ON CONFLICT DO NOTHING"
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
conn = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
conn = psycopg2.connect(database=fediverse_db, user=fediverse_db_user, password="", host="/var/run/postgresql", port="5432")
|
2021-05-14 10:39:26 +02:00
|
|
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
print(f'Writing {server} nodeinfo data...')
|
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
cur.execute(insert_sql, (server, now, software, users, alive, api, soft_version))
|
|
|
|
|
2021-05-14 13:44:19 +02:00
|
|
|
cur.execute(
|
|
|
|
"UPDATE fediverse SET updated_at=(%s), software=(%s), users=(%s), alive=(%s), users_api=(%s), version=(%s) where server=(%s)",
|
2021-10-26 13:38:12 +02:00
|
|
|
(now, software, users, alive, api, soft_version, server)
|
|
|
|
)
|
2021-05-14 10:39:26 +02:00
|
|
|
|
|
|
|
cur.execute("UPDATE world SET checked='t' where server=(%s)", (server,))
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
|
|
|
|
|
|
|
print(error)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
if conn is not None:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
conn.close()
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
@ray.remote
|
|
|
|
def getsoft(server):
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if server.find(".") == -1:
|
|
|
|
return
|
|
|
|
if server.find("@") != -1:
|
|
|
|
return
|
|
|
|
if server.find("/") != -1:
|
|
|
|
return
|
|
|
|
if server.find(":") != -1:
|
|
|
|
return
|
|
|
|
|
|
|
|
soft = ''
|
|
|
|
|
|
|
|
is_nodeinfo = False
|
|
|
|
|
|
|
|
url = 'https://' + server
|
|
|
|
|
|
|
|
user_agent = {'User-agent': "fediverse's stats (fediverse@mastodont.cat)"}
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
try:
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
response = requests.get(url + '/.well-known/nodeinfo', headers = user_agent, timeout=3)
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if response.status_code == 200:
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
try:
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
response_json = response.json()
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
nodeinfo = response_json['links'][0]['href'].replace(f'https://{server}','')
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
try:
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
nodeinfo_data = requests.get(url + nodeinfo, headers = user_agent, timeout=3)
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if nodeinfo_data.status_code == 200:
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
nodeinfo_json = nodeinfo_data.json()
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
is_nodeinfo = True
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
else:
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
print(f"Server {server}'s nodeinfo not responding: error code {nodeinfo_data.status_code}")
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
print(f'Server {server} not responding: error code {response.status_code}')
|
|
|
|
print('*********************************************************************')
|
|
|
|
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
|
|
|
|
for api in apis:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
response = requests.get(url + api, headers = user_agent, timeout=3)
|
|
|
|
|
|
|
|
if is_json(response.text):
|
|
|
|
|
|
|
|
nodeinfo_json = response.json()
|
|
|
|
|
|
|
|
if 'software' in nodeinfo_json:
|
|
|
|
|
|
|
|
nodeinfo = api
|
|
|
|
|
|
|
|
is_nodeinfo = True
|
|
|
|
|
|
|
|
break
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
elif 'title' in nodeinfo_json:
|
|
|
|
|
|
|
|
if nodeinfo_json['title'] == 'Zap':
|
|
|
|
|
|
|
|
nodeinfo = api
|
|
|
|
|
|
|
|
is_nodeinfo = True
|
|
|
|
|
|
|
|
soft = 'zap'
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
elif 'version' in nodeinfo_json:
|
|
|
|
|
|
|
|
nodeinfo = api
|
|
|
|
|
|
|
|
is_nodeinfo = True
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
except:
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
pass
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except requests.exceptions.SSLError as errssl:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except requests.exceptions.HTTPError as errh:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except requests.exceptions.ConnectionError as errc:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except requests.exceptions.ReadTimeout as to_err:
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except requests.exceptions.TooManyRedirects as tmr_err:
|
|
|
|
|
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except urllib3.exceptions.LocationParseError as lp_err:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
except requests.exceptions.InvalidURL as iu_err:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
pass
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
else:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if is_nodeinfo:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if nodeinfo != '/api/v1/instance?':
|
2022-03-02 22:26:05 +01:00
|
|
|
|
|
|
|
if nodeinfo != '/.well-known/x-nodeinfo2?':
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
soft = nodeinfo_json['software']['name']
|
|
|
|
soft = soft.lower()
|
|
|
|
soft_version = nodeinfo_json['software']['version']
|
|
|
|
users = nodeinfo_json['usage']['users']['total']
|
|
|
|
if users > 1000000:
|
2021-10-26 13:38:12 +02:00
|
|
|
return
|
2022-03-02 22:26:05 +01:00
|
|
|
alive = True
|
|
|
|
|
|
|
|
write_api(server, soft, users, alive, nodeinfo, soft_version)
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
print(f"Server {server} ({soft} {soft_version}) is alive!")
|
2022-03-02 22:26:05 +01:00
|
|
|
print('*********************************************************************')
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2021-05-14 13:44:19 +02:00
|
|
|
return
|
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
except:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
soft = nodeinfo_json['server']['software']
|
|
|
|
soft = soft.lower()
|
|
|
|
soft_version = nodeinfo_json['server']['version']
|
|
|
|
users = nodeinfo_json['usage']['users']['total']
|
|
|
|
if users > 1000000:
|
|
|
|
return
|
|
|
|
alive = True
|
|
|
|
|
|
|
|
if soft == 'socialhome':
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
write_api(server, soft, users, alive, nodeinfo, soft_version)
|
2022-03-02 22:26:05 +01:00
|
|
|
|
|
|
|
print('*********************************************************************')
|
2022-03-13 18:06:07 +01:00
|
|
|
print(f"Server {serve}r ({soft} {soft_version}) is alive!")
|
2022-03-02 22:26:05 +01:00
|
|
|
print('*********************************************************************')
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
if soft == '' and nodeinfo == "/api/v1/instance?":
|
2022-03-02 22:26:05 +01:00
|
|
|
|
|
|
|
soft = 'mastodon'
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
try:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
users = nodeinfo_json['stats']['user_count']
|
|
|
|
|
|
|
|
if users > 1000000:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
return
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except:
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
users = 0
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
try:
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
soft_version = nodeinfo_json['version']
|
2021-10-26 13:38:12 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
except:
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
soft_version = 'unknown'
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
alive = True
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
write_api(server, soft, users, alive, nodeinfo, soft_version)
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
print('*********************************************************************')
|
|
|
|
print(f"Server {server} ({soft}) is alive!")
|
|
|
|
|
|
|
|
elif soft == 'zap' and nodeinfo == "/api/v1/instance?":
|
|
|
|
|
|
|
|
soft = 'zap'
|
|
|
|
users = nodeinfo_json['stats']['user_count']
|
|
|
|
soft_version = nodeinfo_json['version']
|
|
|
|
alive = True
|
|
|
|
|
|
|
|
print(server, soft, users, alive, api)
|
|
|
|
|
|
|
|
print('*********************************************************************')
|
|
|
|
print(f"Server {server} ({soft}) is alive!")
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
print(f'Server {server} is dead')
|
|
|
|
print('*********************************************************************')
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-02 22:26:05 +01:00
|
|
|
def get_world_servers():
|
|
|
|
|
|
|
|
world_servers = []
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
conn = None
|
|
|
|
|
|
|
|
conn = psycopg2.connect(database=fediverse_db, user=fediverse_db_user, password="", host="/var/run/postgresql", port="5432")
|
|
|
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
|
|
# get world servers list
|
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
cur.execute("select server from world")# where checked='f'")
|
2022-03-02 22:26:05 +01:00
|
|
|
|
|
|
|
rows = cur.fetchall()
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
|
|
|
|
world_servers.append(row[0])
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
print("Remaining servers: " + str(len(world_servers)))
|
|
|
|
|
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
|
|
|
|
|
|
|
print(error)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
if conn is not None:
|
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
return world_servers
|
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
def save_time(program, start, finish):
|
|
|
|
|
|
|
|
insert_sql = "INSERT INTO execution_time(program, start, finish) VALUES(%s,%s,%s) ON CONFLICT DO NOTHING"
|
|
|
|
|
|
|
|
conn = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
conn = psycopg2.connect(database = fediverse_db, user = fediverse_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
|
|
|
|
|
|
|
cur = conn.cursor()
|
|
|
|
|
|
|
|
cur.execute(insert_sql, (program, start, finish,))
|
|
|
|
|
|
|
|
cur.execute("UPDATE execution_time SET start=(%s), finish=(%s) where program=(%s)", (start, finish, program))
|
|
|
|
|
|
|
|
conn.commit()
|
|
|
|
|
|
|
|
cur.close()
|
|
|
|
|
|
|
|
except (Exception, psycopg2.DatabaseError) as error:
|
|
|
|
|
|
|
|
print(error)
|
|
|
|
|
|
|
|
finally:
|
|
|
|
|
|
|
|
if conn is not None:
|
|
|
|
|
|
|
|
conn.close()
|
|
|
|
|
2021-05-14 13:44:19 +02:00
|
|
|
def get_parameter(parameter, file_path):
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2021-05-14 10:39:26 +02:00
|
|
|
# Check if secrets file exists
|
|
|
|
if not os.path.isfile(file_path):
|
2021-05-14 13:44:19 +02:00
|
|
|
print("File %s not found, exiting." % file_path)
|
2021-05-14 10:39:26 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
# Find parameter in file
|
2021-05-14 13:44:19 +02:00
|
|
|
with open(file_path) as f:
|
2021-05-14 10:39:26 +02:00
|
|
|
for line in f:
|
2021-05-14 13:44:19 +02:00
|
|
|
if line.startswith(parameter):
|
2021-05-14 10:39:26 +02:00
|
|
|
return line.replace(parameter + ":", "").strip()
|
|
|
|
|
|
|
|
# Cannot find parameter, exit
|
2021-05-14 13:44:19 +02:00
|
|
|
print(file_path + " Missing parameter %s " % parameter)
|
2021-05-14 10:39:26 +02:00
|
|
|
sys.exit(0)
|
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
def get_config():
|
|
|
|
|
|
|
|
# Load configuration from config file
|
|
|
|
config_filepath = "config/config.txt"
|
|
|
|
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath)
|
|
|
|
return mastodon_hostname
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2021-10-26 13:38:12 +02:00
|
|
|
def get_db_config():
|
|
|
|
|
|
|
|
# Load database config from db_config file
|
|
|
|
db_config_filepath = "config/db_config.txt"
|
|
|
|
fediverse_db = get_parameter("fediverse_db", db_config_filepath)
|
|
|
|
fediverse_db_user = get_parameter("fediverse_db_user", db_config_filepath)
|
|
|
|
return (fediverse_db, fediverse_db_user)
|
2021-05-14 10:39:26 +02:00
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# main
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-05-14 13:44:19 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
## name: fetchservers.py
|
2021-05-14 13:44:19 +02:00
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
fediverse_db, fediverse_db_user = get_db_config()
|
2022-03-02 22:26:05 +01:00
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
start = datetime.now()
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
program = 'fetchservers'
|
|
|
|
|
|
|
|
finish = start
|
|
|
|
|
|
|
|
save_time(program, start, finish)
|
|
|
|
|
|
|
|
now = start
|
|
|
|
|
|
|
|
mastodon_hostname = get_config()
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
world_servers = get_world_servers()
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
ray_start = time.time()
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-13 18:06:07 +01:00
|
|
|
results = ray.get([getsoft.remote(server) for server in world_servers])
|
2021-05-14 10:39:26 +02:00
|
|
|
|
2022-03-14 12:48:10 +01:00
|
|
|
print(f"duration = {time.time() - ray_start}.\nprocessed servers: {len(results)}")
|
|
|
|
|
|
|
|
finish = datetime.now()
|
|
|
|
|
|
|
|
save_time(program, start, finish)
|