support fingering the instance
This commit is contained in:
parent
6398840ee1
commit
fc13d46022
35
fingerd.py
35
fingerd.py
|
@ -3,6 +3,7 @@
|
||||||
import os
|
import os
|
||||||
import asyncio
|
import asyncio
|
||||||
import asyncpg
|
import asyncpg
|
||||||
|
import aiohttp
|
||||||
import qtoml as toml
|
import qtoml as toml
|
||||||
from contextvars import ContextVar
|
from contextvars import ContextVar
|
||||||
|
|
||||||
|
@ -22,6 +23,12 @@ async def handle_finger(reader, writer):
|
||||||
+ len(config['instance_name'])
|
+ len(config['instance_name'])
|
||||||
)
|
)
|
||||||
handle = data.decode().rstrip()
|
handle = data.decode().rstrip()
|
||||||
|
|
||||||
|
if not handle:
|
||||||
|
# `finger @instance.example`
|
||||||
|
await handle_finger_instance(reader, writer)
|
||||||
|
return
|
||||||
|
|
||||||
username = handle.removeprefix('@').removesuffix('@' + config['instance_name'])
|
username = handle.removeprefix('@').removesuffix('@' + config['instance_name'])
|
||||||
if '@' in username:
|
if '@' in username:
|
||||||
writer.write(b'Error: user must be local to this instance\n')
|
writer.write(b'Error: user must be local to this instance\n')
|
||||||
|
@ -42,14 +49,32 @@ async def handle_finger(reader, writer):
|
||||||
writer.close()
|
writer.close()
|
||||||
await writer.wait_closed()
|
await writer.wait_closed()
|
||||||
|
|
||||||
|
async def handle_finger_instance(reader, writer):
|
||||||
|
async with http.get(f'https://{config["instance_name"]}/nodeinfo/2.0') as resp:
|
||||||
|
data = (await resp.json())['usage']
|
||||||
|
user_data = data['users']
|
||||||
|
resp = (
|
||||||
|
f'Total users: {user_data["total"]:,}\n'
|
||||||
|
f'Active users (past month): {user_data["activeMonth"]:,}\n'
|
||||||
|
f'Active users (past six months): {user_data["activeHalfyear"]:,}\n'
|
||||||
|
f'Total posts: {data["localPosts"]:,}\n'
|
||||||
|
).encode()
|
||||||
|
writer.write(resp)
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
|
global http
|
||||||
|
|
||||||
db.set(await asyncpg.create_pool(**config['db']))
|
db.set(await asyncpg.create_pool(**config['db']))
|
||||||
|
|
||||||
async with await asyncio.start_server(
|
async with (
|
||||||
handle_finger,
|
# globals used here only because ContextVar wasn't working
|
||||||
'0.0.0.0',
|
aiohttp.ClientSession() as http,
|
||||||
os.getenv('PORT', FINGER_PORT),
|
await asyncio.start_server(
|
||||||
) as server:
|
handle_finger,
|
||||||
|
'0.0.0.0',
|
||||||
|
os.getenv('PORT', FINGER_PORT),
|
||||||
|
) as server,
|
||||||
|
):
|
||||||
addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets)
|
addrs = ', '.join(str(sock.getsockname()) for sock in server.sockets)
|
||||||
print(f'Serving on {addrs}')
|
print(f'Serving on {addrs}')
|
||||||
|
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
asyncpg ~= 0.28.0
|
asyncpg ~= 0.28.0
|
||||||
qtoml ~= 0.3.1
|
qtoml ~= 0.3.1
|
||||||
|
aiohttp ~= 3.8
|
||||||
|
|
Reference in New Issue