Skip to content

Commit

Permalink
chg: add common_bp
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Jan 2, 2024
1 parent fb8f740 commit 5890309
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 55 deletions.
2 changes: 1 addition & 1 deletion hiddifypanel/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def create_app(cli=False, **config):
init_db()

hiddifypanel.panel.auth.init_app(app)
hiddifypanel.panel.login.init_app(app)
hiddifypanel.panel.common_bp.init_app(app)
hiddifypanel.panel.common.init_app(app)
hiddifypanel.panel.admin.init_app(app)
hiddifypanel.panel.user.init_app(app)
Expand Down
2 changes: 1 addition & 1 deletion hiddifypanel/panel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
from . import common
from . import commercial
from . import auth
from . import login
from . import common_bp
21 changes: 11 additions & 10 deletions hiddifypanel/panel/auth.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from flask_login import LoginManager, current_user, user_accessed, user_logged_in, COOKIE_NAME, AUTH_HEADER_NAME
from flask_login import LoginManager, current_user, user_accessed, user_logged_in, COOKIE_NAME, AUTH_HEADER_NAME, logout_user
from flask import g, redirect, request, session, url_for
from flask_login.utils import _get_user
from flask import current_app
Expand Down Expand Up @@ -50,14 +50,14 @@ def _load_user(self):
has_cookie = (
cookie_name in request.cookies and session.get("_remember") != "clear"
)
if has_cookie:
cookie = request.cookies[cookie_name]
user = self._load_user_from_remember_cookie(cookie)
elif self._request_callback:
user = self._load_user_from_request(request)
elif header_name in request.headers:
if header_name in request.headers:
header = request.headers[header_name]
user = self._load_user_from_header(header)
elif self._request_callback:
user = self._load_user_from_request(request)
elif has_cookie:
cookie = request.cookies[cookie_name]
user = self._load_user_from_remember_cookie(cookie)

return self._update_request_context_with_user(user)

Expand Down Expand Up @@ -160,8 +160,9 @@ def header_auth(request) -> User | AdminUser | None:
g.account = account
# g.account_uuid = account.uuid
g.is_admin = hiddify.is_admin_role(account.role) # type: ignore
if not is_api_call:
login_user(account)
login_user(account)
else:
logout_user()

return account

Expand All @@ -170,7 +171,7 @@ def unauthorized():
# TODO: show the login page
# return request.base_url
if g.user_agent.browser:
return redirect(url_for('hlogin.LoginView:basic', force=1, next={request.path}))
return redirect(url_for('common_bp.LoginView:basic_0', force=1, next={request.path}))

else:
abort(401, "Unauthorized")
Expand Down
6 changes: 3 additions & 3 deletions hiddifypanel/panel/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def all_configs():

configs['hconfigs']['first_setup'] = def_user != None and len(sslip_domains) > 0

path = f'/{hconfig(ConfigEnum.proxy_path_admin)}/'
path = f'/{hconfig(ConfigEnum.proxy_path_admin)}/l'

server_ip = hutils.ip.get_ip(4)
configs['admin_path'] = path
Expand Down Expand Up @@ -101,11 +101,11 @@ def admin_links():
domains = get_panel_domains()
admin_links += f"Secure:\n"
if not any([d for d in domains if 'sslip.io' not in d.domain]):
admin_links += f" (not signed) {hutils.utils.add_basic_auth_to_url(f'https://{server_ip}/{proxy_path}/', owner.username, owner.password)}\n"
admin_links += f" (not signed) {hutils.utils.add_basic_auth_to_url(f'https://{server_ip}/{proxy_path}/l', owner.username, owner.password)}\n"

# domains=[*domains,f'{server_ip}.sslip.io']
for d in domains:
admin_links += f" {hutils.utils.add_basic_auth_to_url(f'https://{d.domain}/{proxy_path}/', owner.username, owner.password)}\n"
admin_links += f" {hutils.utils.add_basic_auth_to_url(f'https://{d.domain}/{proxy_path}/l', owner.username, owner.password)}\n"

print(admin_links)
return admin_links
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .login import LoginView
from apiflask import APIBlueprint
bp = APIBlueprint("hlogin", __name__, url_prefix="/<proxy_path>/", template_folder="templates", enable_openapi=False)
bp = APIBlueprint("common_bp", __name__, url_prefix="/<proxy_path>/", template_folder="templates", enable_openapi=False)


def init_app(app):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class LoginView(FlaskView):
def index(self, force=None, next=None):
force_arg = request.args.get('force')
redirect_arg = request.args.get('redirect')
username_arg = request.args.get('user')
if not current_user.is_authenticated or (force_arg and not request.headers.get('Authorization')):
username_arg = request.args.get('user') or ''
if not current_user.is_authenticated:
return render_template('login.html', username=username_arg)

# abort(401, "Unauthorized1")
Expand All @@ -31,17 +31,18 @@ def index(self, force=None, next=None):

@route("/l/")
@route("/l")
def basic(self, force=None, next=None):
force_arg = force or request.args.get('force')
redirect_arg = redirect or request.args.get('redirect')
def basic(self):
force_arg = request.args.get('force')
redirect_arg = request.args.get('next')
if not current_user.is_authenticated or (force_arg and not request.headers.get('Authorization')):
username = request.authorization.username if request.authorization else ''
nexturl = url_for('hlogin.LoginView:index', force=force, next=next, user=username)
if request.headers.get('Authorization'):

loginurl = url_for('common_bp.LoginView:index', force=force, next=next, user=username)
if request.headers.get('Authorization') or (auth.current_user and auth.current_user != username):
flash(_('Incorrect Password'), 'error')
# flash(request.authorization.username, 'error')
return redirect(nexturl)
return render_template("redirect.html", url=nexturl), 401
return redirect(loginurl)
return render_template("redirect.html", url=loginurl), 401
# abort(401, "Unauthorized1")

if redirect_arg:
Expand All @@ -53,3 +54,28 @@ def basic(self, force=None, next=None):
return redirect(url_for('client.UserView:index'))
from hiddifypanel.panel.user import UserView
return UserView().auto_sub()

@route('/manifest.webmanifest')
@login_required()
def create_pwa_manifest(self):
domain = urlparse(request.base_url).hostname
name = (domain if g.is_admin else g.user.name)
return jsonify({
"name": f"Hiddify {name}",
"short_name": f"{name}"[:12],
"theme_color": "#f2f4fb",
"background_color": "#1a1b21",
"display": "standalone",
"scope": f"/",
"start_url": hiddify.hutils.utils.add_basic_auth_to_url(f"https://{domain}/{g.proxy_path}/?pwa=true", g.account.username, g.account.password),
"description": "Hiddify, for a free Internet",
"orientation": "any",
"icons": [
{
"src": hiddify.static_url_for(filename='images/hiddify-dark.png'),
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
}
]
})
8 changes: 4 additions & 4 deletions hiddifypanel/panel/hiddify.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,12 @@ def proxy_path_validator(proxy_path):
return

if proxy_path not in [admin_proxy_path, deprecated_path, client_proxy_path]:
return apiflask_abort(400, Markup(f"Invalid Proxy Path <a href=/{admin_proxy_path}/admin>Admin Panel</a>")) if dbg_mode else abort(400, 'invalid request')
abort(400, 'invalid request')

if is_admin_panel_call() and proxy_path not in admin_proxy_path:
return apiflask_abort(400, Markup(f"Invalid Proxy Path <a href=/{admin_proxy_path}/admin>Admin Panel</a>")) if dbg_mode else abort(400, 'invalid request')
if is_admin_panel_call() and proxy_path != admin_proxy_path:
abort(400, 'invalid request')
if is_user_panel_call() and proxy_path != client_proxy_path:
return apiflask_abort(400, Markup(f"Invalid Proxy Path <a href=/{client_proxy_path}/admin>User Panel</a>")) if dbg_mode else abort(400, 'invalid request')
abort(400, 'invalid request')

if is_api_call(request.path):
if is_admin_api_call() and proxy_path != admin_proxy_path:
Expand Down
26 changes: 1 addition & 25 deletions hiddifypanel/panel/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,31 +381,7 @@ def all_configs(self, base64=False):
resp = do_base_64(resp)
return add_headers(resp, c)

@ route('/manifest.webmanifest')
@login_required()
def create_pwa_manifest(self):

domain = urlparse(request.base_url).hostname
name = (domain if g.is_admin else g.user.name)
return jsonify({
"name": f"Hiddify {name}",
"short_name": f"{name}"[:12],
"theme_color": "#f2f4fb",
"background_color": "#1a1b21",
"display": "standalone",
"scope": f"/",
"start_url": f"https://{domain}"+url_for("admin.Dashboard:index" if g.is_admin else "client.UserView:new_1")+"?pwa=true",
"description": "Hiddify, for a free Internet",
"orientation": "any",
"icons": [
{
"src": hiddify.static_url_for(filename='images/hiddify-dark.png'),
"sizes": "512x512",
"type": "image/png",
"purpose": "maskable any"
}
]
})


@login_required(roles={Role.user})
@ route("/offline.html")
Expand Down
2 changes: 1 addition & 1 deletion hiddifypanel/templates/master.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<link href="{{static_url_for(filename='images/splash/ipadpro3_splash.png')}}" media="(device-width: 834px) and (device-height: 1194px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait)" rel="apple-touch-startup-image" />
<link href="{{static_url_for(filename='images/splash/ipadpro2_splash.png')}}" media="(device-width: 1024px) and (device-height: 1366px) and (-webkit-device-pixel-ratio: 2) and (orientation: portrait)" rel="apple-touch-startup-image" />
<link rel="icon" type="image/x-icon" href="{{ static_url_for( filename='images/favicon.ico')}}">
<link rel="manifest" href="{{ url_for('client.UserView:create_pwa_manifest')}}">
<link rel="manifest" href="{{ url_for('common_bp.LoginView:create_pwa_manifest')}}">

<!-- Tell the browser to be responsive to screen width -->
<meta name="viewport" content="width=device-width, initial-scale=1">
Expand Down

0 comments on commit 5890309

Please sign in to comment.