Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: info api profile title & short api expire time #70

Merged
merged 2 commits into from
Dec 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hiddifypanel/panel/commercial/restapi/v2/user/info_api.py
Original file line number Diff line number Diff line change
@@ -44,7 +44,8 @@ def get(self):
c = get_common_data(g.user_uuid, 'new')

dto = ProfileSchema()
dto.profile_title = c['user'].name if c.get('user') and c.get('user').name else c['profile_title'].split(' ')[1]
# user is exist for sure
dto.profile_title = c['user'].name
dto.profile_url = f"https://{urlparse(request.base_url).hostname}/{g.proxy_path}/{g.user_uuid}/#{g.user.name}"
dto.profile_usage_current = g.user.current_usage_GB
dto.profile_usage_total = g.user.usage_limit_GB
11 changes: 7 additions & 4 deletions hiddifypanel/panel/commercial/restapi/v2/user/short_api.py
Original file line number Diff line number Diff line change
@@ -2,28 +2,31 @@
from urllib.parse import urlparse
from apiflask import Schema
from flask import g, request
from apiflask.fields import String,Integer
from apiflask.fields import String, Integer
from flask import current_app as app
from datetime import datetime
from flask.views import MethodView
from hiddifypanel.models.config import hconfig
from hiddifypanel.models.config_enum import ConfigEnum
from hiddifypanel.panel import hiddify


class ShortSchema(Schema):
short = String(required=True, description="the short url slug")
full_url = String(required=True, description="full short url")
expire_in = Integer(required=True, description="expire_in is in seconds")


class ShortAPI(MethodView):
decorators = [hiddify.user_auth]

@app.output(ShortSchema)
def get(self):
short, expire_date = hiddify.add_short_link("/"+hconfig(ConfigEnum.proxy_path)+"/"+str(g.user_uuid)+"/")
short, expire_in = hiddify.add_short_link("/"+hconfig(ConfigEnum.proxy_path)+"/"+str(g.user_uuid)+"/")
full_url = f"https://{urlparse(request.base_url).hostname}/{short}"
dto = ShortSchema()
dto.full_url = full_url
dto.short = short
# expire_in is in seconds
dto.expire_in = (expire_date - datetime.now()).seconds
return dto
dto.expire_in = expire_in
return dto
14 changes: 10 additions & 4 deletions hiddifypanel/panel/hiddify.py
Original file line number Diff line number Diff line change
@@ -28,10 +28,16 @@ def add_temporary_access():
g.temp_admin_link = temp_admin_link


@cache.cache(ttl=600)
# TODO: check this function functionality
def add_short_link(link: str, period_min: int = 5) -> Tuple[str, datetime]:
def add_short_link(link: str, period_min: int = 5) -> Tuple[str, int]:
short_code, expire_date = add_short_link_imp(link, period_min)
return short_code, (expire_date - datetime.now()).seconds


@cache.cache(ttl=300)
# TODO: Change ttl dynamically
def add_short_link_imp(link: str, period_min: int = 5) -> Tuple[str, datetime]:
# pattern = "\^/([^/]+)(/)?\?\$\ {return 302 " + re.escape(link) + ";}"

pattern = r"([^/]+)\("

with open(current_app.config['HIDDIFY_CONFIG_PATH']+"/nginx/parts/short-link.conf", 'r') as f:
@@ -736,7 +742,7 @@ def is_ssh_password_authentication_enabled():
return True


@cache.cache(ttl=600)
@cache.cache(ttl=300)
def get_direct_host_or_ip(prefer_version: int):
direct = Domain.query.filter(Domain.mode == DomainType.direct, Domain.sub_link_only == False).first()
if not (direct):