Skip to content

Commit

Permalink
config_validator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
4n0nymou3 authored Jan 13, 2025
1 parent f0051dd commit aaf79eb
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions src/config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ def is_vmess_config(config: str) -> bool:
base64_part = config[8:]
decoded = ConfigValidator.decode_base64_url(base64_part)
if decoded:
json.loads(decoded)
return True
if isinstance(decoded, bytes):
decoded = decoded.decode('utf-8')
json_data = json.loads(decoded)
return all(key in json_data for key in ['add', 'port', 'id'])
return False
except:
return False
Expand All @@ -51,7 +53,7 @@ def is_tuic_config(config: str) -> bool:
try:
if config.startswith('tuic://'):
parsed = urlparse(config)
return bool(parsed.netloc and ':' in parsed.netloc)
return bool(parsed.netloc and ':' in parsed.netloc and parsed.query)
return False
except:
return False
Expand Down Expand Up @@ -131,27 +133,56 @@ def is_valid_config(config: str) -> bool:
return False

protocols = ['vmess://', 'vless://', 'ss://', 'trojan://', 'hysteria2://', 'wireguard://', 'tuic://', 'ssconf://']
return any(config.startswith(p) for p in protocols)
valid = any(config.startswith(p) for p in protocols)

if valid:
clean_config = ConfigValidator.clean_config(config)
if clean_config != config:
return False

if config.startswith('vmess://'):
return ConfigValidator.is_vmess_config(config)
elif config.startswith('tuic://'):
return ConfigValidator.is_tuic_config(config)

return valid

@classmethod
def validate_protocol_config(cls, config: str, protocol: str) -> bool:
@staticmethod
def validate_protocol_config(config: str, protocol: str) -> bool:
try:
if protocol in ['vmess://', 'vless://', 'ss://', 'tuic://']:
if protocol == 'vmess://':
return cls.is_vmess_config(config)
return ConfigValidator.is_vmess_config(config)
if protocol == 'tuic://':
return cls.is_tuic_config(config)
return ConfigValidator.is_tuic_config(config)

base64_part = config[len(protocol):]
decoded_url = unquote(base64_part)
if cls.is_base64(decoded_url) or cls.is_base64(base64_part):
return True
if cls.decode_base64_url(base64_part) or cls.decode_base64_url(decoded_url):

if ConfigValidator.is_base64(decoded_url) or ConfigValidator.is_base64(base64_part):
return True

decoded = ConfigValidator.decode_base64_url(base64_part)
if decoded:
try:
if isinstance(decoded, bytes):
decoded = decoded.decode('utf-8')
if '"' in decoded or '{' in decoded:
json.loads(decoded)
return True
except:
pass

return False

elif protocol in ['trojan://', 'hysteria2://', 'wireguard://']:
parsed = urlparse(config)
return bool(parsed.netloc and '@' in parsed.netloc)
return bool(parsed.netloc and ('@' in parsed.netloc or ':' in parsed.netloc))

elif protocol == 'ssconf://':
return True

return False

except:
return False

0 comments on commit aaf79eb

Please sign in to comment.