Skip to content

Commit

Permalink
Merge pull request #192 from Iam54r1n4/devp10-patch-3
Browse files Browse the repository at this point in the history
Handle v2rayng sub & bug fix
  • Loading branch information
hiddify-com authored Apr 12, 2024
2 parents 22b76cc + 0cd35d2 commit 52df6ae
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 17 deletions.
8 changes: 5 additions & 3 deletions hiddifypanel/hutils/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


def flash(message: str, category: str = "message"):
# if isinstance(message, LazyString)
if not isinstance(message,str):
message = str(message)
return flask_flash(message, category)


Expand Down Expand Up @@ -48,7 +49,7 @@ def hurl_for(endpoint, **values):
def get_user_agent() -> dict:
ua = __parse_user_agent(request.user_agent.string)

if ua.get('v', 1) < 5:
if ua.get('v', 1) < 6:
__parse_user_agent.invalidate_all() # type:ignore
ua = __parse_user_agent(request.user_agent.string)
return ua
Expand All @@ -68,7 +69,7 @@ def __parse_user_agent(ua: str) -> dict:
match = re.search(ua_version_pattern, request.user_agent.string)
generic_version = list(map(int, match.group(1).split('.'))) if match else [0, 0, 0]
res = {}
res['v'] = 5
res['v'] = 6
res["is_bot"] = uaa.is_bot
res["is_browser"] = re.match('^Mozilla', ua, re.IGNORECASE) and True
res['os'] = uaa.os.family
Expand All @@ -79,6 +80,7 @@ def __parse_user_agent(ua: str) -> dict:
res['is_hiddify'] = re.match('^(HiddifyNext)', ua, re.IGNORECASE) and True
res['is_streisand'] = re.match('^(Streisand)', ua, re.IGNORECASE) and True
res['is_shadowrocket'] = re.match('^(Shadowrocket)', ua, re.IGNORECASE) and True
res['is_v2rayng'] = re.match('^(v2rayNG)', ua, re.IGNORECASE) and True

if res['is_singbox']:
res['singbox_version'] = generic_version
Expand Down
13 changes: 9 additions & 4 deletions hiddifypanel/hutils/proxy/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,15 @@ def is_tls(l3) -> bool:
def get_proxies(child_id: int = 0, only_enabled=False) -> list['Proxy']:
proxies = Proxy.query.filter(Proxy.child_id == child_id).all()
proxies = [c for c in proxies if 'restls' not in c.transport]
# if not hconfig(ConfigEnum.tuic_enable, child_id):
# proxies = [c for c in proxies if c.proto != ProxyProto.tuic]
# if not hconfig(ConfigEnum.hysteria_enable, child_id):
# proxies = [c for c in proxies if c.proto != ProxyProto.hysteria2]

if not hconfig(ConfigEnum.tuic_enable, child_id):
proxies = [c for c in proxies if c.proto != ProxyProto.tuic]
if not hconfig(ConfigEnum.wireguard_enable, child_id):
proxies = [c for c in proxies if c.proto != ProxyProto.wireguard]
if not hconfig(ConfigEnum.ssh_server_enable, child_id):
proxies = [c for c in proxies if c.proto != ProxyProto.ssh]
if not hconfig(ConfigEnum.hysteria_enable, child_id):
proxies = [c for c in proxies if c.proto != ProxyProto.hysteria2]
if not hconfig(ConfigEnum.shadowsocks2022_enable, child_id):
proxies = [c for c in proxies if 'shadowsocks' != c.transport]

Expand Down
9 changes: 9 additions & 0 deletions hiddifypanel/hutils/proxy/xrayjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@
def configs_as_json(domains: list[Domain], remarks: str) -> str:
'''Returns xray configs as json'''
outbounds = []

# TODO: check what are unsupported protocols in other apps
unsupported_protos = {}
if g.user_agent.get('is_v2rayng'):
# TODO: ensure which protocols are not supported in v2rayng
unsupported_protos = {ProxyProto.wireguard, ProxyProto.hysteria, ProxyProto.hysteria2, ProxyProto.tuic, ProxyProto.ss, ProxyProto.ssr, ProxyProto.ssh}

for proxy in hutils.proxy.get_valid_proxies(domains):
if unsupported_protos and proxy['proto'] in unsupported_protos:
continue
outbound = to_xray(proxy)
if 'msg' not in outbound:
outbounds.append(outbound)
Expand Down
6 changes: 3 additions & 3 deletions hiddifypanel/models/config_enum.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ def dbvalues(cls):
v2ray_enable = _BoolConfigDscr(ConfigCategory.hidden, ApplyMode.apply)
torrent_block = _BoolConfigDscr(ConfigCategory.general, ApplyMode.apply)

tuic_enable = _BoolConfigDscr(ConfigCategory.hidden, ApplyMode.apply)
tuic_port = _StrConfigDscr(ConfigCategory.hidden, ApplyMode.apply, hide_in_virtual_child=True)
tuic_enable = _BoolConfigDscr(ConfigCategory.tuic, ApplyMode.apply)
tuic_port = _StrConfigDscr(ConfigCategory.tuic, ApplyMode.apply, hide_in_virtual_child=True)

# the hysteria is refereing to hysteria2
hysteria_enable = _BoolConfigDscr(ConfigCategory.hidden, ApplyMode.apply)
hysteria_enable = _BoolConfigDscr(ConfigCategory.hysteria, ApplyMode.apply)
hysteria_port = _StrConfigDscr(ConfigCategory.hidden, ApplyMode.apply, hide_in_virtual_child=True)
# if be enable hysteria2 will be use salamander as obfs
hysteria_obfs_enable = _BoolConfigDscr(ConfigCategory.hysteria, ApplyMode.apply)
Expand Down
9 changes: 2 additions & 7 deletions hiddifypanel/panel/commercial/ProxyDetailsAdmin.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
from hiddifypanel.models import *
from hiddifypanel.panel import hiddify
from hiddifypanel.panel.admin.adminlte import AdminLTEModelView
from flask_babel import gettext as __
from flask_babel import lazy_gettext as _
from flask import g, redirect
from markupsafe import Markup

from hiddifypanel.hutils.flask import hurl_for, flash
from hiddifypanel.auth import login_required
from flask_admin.model.template import EndpointLinkRowAction
from flask_admin.actions import action
from flask_admin.contrib.sqla import form, filters as sqla_filters, tools
# Define a custom field type for the related domains
from flask import current_app
from hiddifypanel import hutils


Expand All @@ -31,7 +26,7 @@ def action_disable(self, ids):
count = query.update({'enable': False})

self.session.commit()
flash(_('%(count)s records were successfully disabled.', count=count), 'success')
hutils.flask.flash(_('%(count)s records were successfully disabled.', count=count), 'success')
hutils.proxy.get_proxies.invalidate_all()

@action('enable', 'Enable', 'Are you sure you want to enable selected proxies?')
Expand All @@ -40,7 +35,7 @@ def action_enable(self, ids):
count = query.update({'enable': True})

self.session.commit()
flash(_('%(count)s records were successfully enabled.', count=count), 'success')
hutils.flask.flash(_('%(count)s records were successfully enabled.', count=count), 'success')
hutils.proxy.get_proxies.invalidate_all()

# list_template = 'model/domain_list.html'
Expand Down
3 changes: 3 additions & 0 deletions hiddifypanel/panel/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ def get_proper_config(self):
if re.match('^(Clash|Stash)', ua, re.IGNORECASE):
return self.clash_config(meta_or_normal="normal")

if g.user_agent.get('is_v2rayng'):
return self.xray()

# if 'HiddifyNext' in ua or 'Dart' in ua:
# return self.clash_config(meta_or_normal="meta")

Expand Down

0 comments on commit 52df6ae

Please sign in to comment.