Fix: Include both IPv4 and IPv6 URIs in normalsub output
This commit is contained in:
@ -135,7 +135,6 @@ async def get_template_context(username, user_agent):
|
|||||||
ipv4_uri, ipv6_uri = get_uris(username)
|
ipv4_uri, ipv6_uri = get_uris(username)
|
||||||
sub_link = f"https://{DOMAIN}:{PORT}/sub/normal/{username}"
|
sub_link = f"https://{DOMAIN}:{PORT}/sub/normal/{username}"
|
||||||
|
|
||||||
# Generate QR codes only if URIs are available
|
|
||||||
ipv4_qrcode = generate_qrcode_base64(ipv4_uri) if ipv4_uri else None
|
ipv4_qrcode = generate_qrcode_base64(ipv4_uri) if ipv4_uri else None
|
||||||
ipv6_qrcode = generate_qrcode_base64(ipv6_uri) if ipv6_uri else None
|
ipv6_qrcode = generate_qrcode_base64(ipv6_uri) if ipv6_uri else None
|
||||||
sublink_qrcode = generate_qrcode_base64(sub_link)
|
sublink_qrcode = generate_qrcode_base64(sub_link)
|
||||||
@ -187,7 +186,7 @@ def get_user_info(username):
|
|||||||
|
|
||||||
def get_user_uri(username, user_agent):
|
def get_user_uri(username, user_agent):
|
||||||
"""
|
"""
|
||||||
Returns the URI for the user, adapting the output based on the User-Agent.
|
Returns the URI for the user, adapting the output based on the User-Agent. Returns BOTH IPv4 and IPv6.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
user_info = get_user_info(username)
|
user_info = get_user_info(username)
|
||||||
@ -205,33 +204,41 @@ def get_user_uri(username, user_agent):
|
|||||||
else:
|
else:
|
||||||
expiration_timestamp = 0
|
expiration_timestamp = 0
|
||||||
|
|
||||||
# Get URI
|
|
||||||
ipv4_uri, ipv6_uri = get_uris(username)
|
ipv4_uri, ipv6_uri = get_uris(username)
|
||||||
|
|
||||||
# Choose the appropriate URI based on availability. Prioritize IPv6 if available.
|
output_lines = []
|
||||||
output_uri = ipv6_uri if ipv6_uri else (ipv4_uri if ipv4_uri else "No URI available")
|
|
||||||
|
|
||||||
|
if ipv4_uri:
|
||||||
|
output_lines.append(ipv4_uri)
|
||||||
|
if ipv6_uri:
|
||||||
|
output_lines.append(ipv6_uri)
|
||||||
|
|
||||||
if "v2ray" in user_agent and "ng" in user_agent:
|
if not output_lines:
|
||||||
match = re.search(r'pinSHA256=sha256/([^&]+)', output_uri)
|
return "No URI available"
|
||||||
if match:
|
|
||||||
base64_pin = match.group(1)
|
processed_uris = []
|
||||||
try:
|
for uri in output_lines:
|
||||||
decoded_pin = base64.b64decode(base64_pin)
|
if "v2ray" in user_agent and "ng" in user_agent:
|
||||||
hex_pin = ':'.join(['{:02X}'.format(byte) for byte in decoded_pin])
|
match = re.search(r'pinSHA256=sha256/([^&]+)', uri)
|
||||||
|
if match:
|
||||||
|
base64_pin = match.group(1)
|
||||||
|
try:
|
||||||
|
decoded_pin = base64.b64decode(base64_pin)
|
||||||
|
hex_pin = ':'.join(['{:02X}'.format(byte) for byte in decoded_pin])
|
||||||
|
uri = uri.replace(f'pinSHA256=sha256/{base64_pin}', f'pinSHA256={hex_pin}')
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error processing pinSHA256: {e}")
|
||||||
|
processed_uris.append(uri)
|
||||||
|
|
||||||
output_uri = output_uri.replace(f'pinSHA256=sha256/{base64_pin}', f'pinSHA256={hex_pin}')
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error processing pinSHA256: {e}")
|
|
||||||
|
|
||||||
subscription_info = (
|
subscription_info = (
|
||||||
f"//subscription-userinfo: upload={upload}; download={download}; total={total}; expire={expiration_timestamp}\n"
|
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"
|
profile_lines = f"//profile-title: {username}-Hysteria2 🚀\n//profile-update-interval: 1\n"
|
||||||
output = profile_lines + subscription_info + output_uri
|
output = profile_lines + subscription_info + "\n".join(processed_uris)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
except subprocess.CalledProcessError:
|
except subprocess.CalledProcessError:
|
||||||
raise RuntimeError("Failed to get URI or user info.")
|
raise RuntimeError("Failed to get URI or user info.")
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
@ -254,7 +261,6 @@ def get_uris(username):
|
|||||||
safe_command = [shlex.quote(arg) for arg in command]
|
safe_command = [shlex.quote(arg) for arg in command]
|
||||||
output = subprocess.check_output(safe_command).decode().strip()
|
output = subprocess.check_output(safe_command).decode().strip()
|
||||||
|
|
||||||
# Use regex to find IPv4 and IPv6 URIs, handling cases where they might not exist.
|
|
||||||
ipv4_match = re.search(r'IPv4:\s*(.*)', output)
|
ipv4_match = re.search(r'IPv4:\s*(.*)', output)
|
||||||
ipv6_match = re.search(r'IPv6:\s*(.*)', output)
|
ipv6_match = re.search(r'IPv6:\s*(.*)', output)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user