Added userinfo
This commit is contained in:
@ -4,6 +4,7 @@ import subprocess
|
|||||||
import time
|
import time
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
|
import json # Import the json module
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
from aiohttp.web_middlewares import middleware
|
from aiohttp.web_middlewares import middleware
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@ -62,6 +63,32 @@ async def handle(request):
|
|||||||
|
|
||||||
def get_user_uri(username):
|
def get_user_uri(username):
|
||||||
try:
|
try:
|
||||||
|
user_info_command = [
|
||||||
|
'python3',
|
||||||
|
'/etc/hysteria/core/cli.py',
|
||||||
|
'get-user',
|
||||||
|
'-u', username
|
||||||
|
]
|
||||||
|
safe_user_info_command = [shlex.quote(arg) for arg in user_info_command]
|
||||||
|
user_info_output = subprocess.check_output(safe_user_info_command).decode()
|
||||||
|
user_info = json.loads(user_info_output)
|
||||||
|
|
||||||
|
|
||||||
|
upload = user_info.get('upload_bytes', 0)
|
||||||
|
download = user_info.get('download_bytes', 0)
|
||||||
|
total = user_info.get('max_download_bytes', 0)
|
||||||
|
creation_date_str = user_info.get('account_creation_date', '')
|
||||||
|
expiration_days = user_info.get('expiration_days', 0)
|
||||||
|
if creation_date_str and expiration_days > 0:
|
||||||
|
try:
|
||||||
|
creation_date = time.strptime(creation_date_str, "%Y-%m-%d")
|
||||||
|
expiration_timestamp = int(time.mktime(creation_date)) + (expiration_days * 24 * 60 * 60)
|
||||||
|
except ValueError:
|
||||||
|
expiration_timestamp = 0
|
||||||
|
else:
|
||||||
|
expiration_timestamp = 0
|
||||||
|
|
||||||
|
# Get URI
|
||||||
command = [
|
command = [
|
||||||
'python3',
|
'python3',
|
||||||
'/etc/hysteria/core/cli.py',
|
'/etc/hysteria/core/cli.py',
|
||||||
@ -74,9 +101,20 @@ def get_user_uri(username):
|
|||||||
output = re.sub(r'IPv4:\s*', '', output)
|
output = re.sub(r'IPv4:\s*', '', output)
|
||||||
output = re.sub(r'IPv6:\s*', '', output)
|
output = re.sub(r'IPv6:\s*', '', output)
|
||||||
|
|
||||||
|
subscription_info = (
|
||||||
|
f"//subscription-userinfo: upload={upload}; download={download}; total={total}; expire={expiration_timestamp}\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
profile_lines = f"//profile-title: {username}-Hysteria2🚀\n//profile-update-interval: 1\n"
|
||||||
|
output = profile_lines + subscription_info + output
|
||||||
|
|
||||||
return output
|
return output
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
raise RuntimeError("Failed to get URI.")
|
raise RuntimeError("Failed to get URI or user info.")
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
raise RuntimeError("Failed to parse user info JSON.")
|
||||||
|
except ValueError:
|
||||||
|
raise RuntimeError("expiration_timestamp OR account_creation_date in config file is invalid")
|
||||||
|
|
||||||
async def handle_404(request):
|
async def handle_404(request):
|
||||||
print(f"404 Not Found: {request.path}")
|
print(f"404 Not Found: {request.path}")
|
||||||
|
|||||||
Reference in New Issue
Block a user