From c7a7c5b65ebfaf132b8dd05daf268a41f54e5aec Mon Sep 17 00:00:00 2001 From: dnomd343 Date: Wed, 7 Sep 2022 15:06:20 +0800 Subject: [PATCH] update: common functions --- Basis/Functions.py => Utils/Common/Check.py | 48 +++++---------------- Utils/Common/Coding.py | 4 +- Utils/Common/{Url.py => Format.py} | 18 ++++++++ Utils/Common/Generate.py | 29 +++++++++++++ Utils/Common/Host.py | 20 --------- Utils/Common/__init__.py | 6 ++- 6 files changed, 63 insertions(+), 62 deletions(-) rename Basis/Functions.py => Utils/Common/Check.py (57%) rename Utils/Common/{Url.py => Format.py} (67%) create mode 100644 Utils/Common/Generate.py delete mode 100644 Utils/Common/Host.py diff --git a/Basis/Functions.py b/Utils/Common/Check.py similarity index 57% rename from Basis/Functions.py rename to Utils/Common/Check.py index 1c9ba6a..dc29da9 100644 --- a/Basis/Functions.py +++ b/Utils/Common/Check.py @@ -2,11 +2,17 @@ # -*- coding: utf-8 -*- import re -import uuid -import random -import hashlib +from IPy import IP -from Utils.Logger import logger + +def isHost(host: str) -> bool: + return isIpAddr(host) or isDomain(host) # IPv4 / IPv6 / Domain + + +def isPort(port: int) -> bool: + if type(port) != int: + return False + return port in range(1, 65536) # 1 ~ 65535 def isIpAddr(ipAddr: str) -> bool: @@ -27,37 +33,3 @@ def isDomain(domain: str) -> bool: return re.search(domainRegex, domain) is not None # regex matching except: # unexpected error return False - - -def isHost(host: str) -> bool: - return isIpAddr(host) or isDomain(host) # IPv4 / IPv6 / Domain - - -def isPort(port: int) -> bool: - if type(port) != int: - return False - return port in range(1, 65536) # 1 ~ 65535 - - -def md5Sum(data: str, encode: str = 'utf-8') -> str: - return hashlib.md5(data.encode(encoding = encode)).hexdigest() # MD5 hash - - -def genFlag(length: int = 12) -> str: # generate random task flag - flag = '' - for i in range(0, length): - tmp = random.randint(0, 15) - if tmp >= 10: - flag += chr(tmp + 87) # a ~ f - else: - flag += str(tmp) # 0 ~ 9 - logger.debug('Generate new flag -> ' + flag) - return flag - - -def genUUID() -> str: # generate uuid v5 - return str(uuid.uuid5( - uuid.NAMESPACE_DNS, genFlag(length = 16) - )) - - diff --git a/Utils/Common/Coding.py b/Utils/Common/Coding.py index 5bd6d15..e8bdb27 100644 --- a/Utils/Common/Coding.py +++ b/Utils/Common/Coding.py @@ -13,7 +13,7 @@ def urlDecode(content: str) -> str: # url decode (RFC3986) return urllib.parse.unquote(content, encoding = 'utf-8') -def base64Encode(content: str, urlSafe: bool = True, padding: bool = False) -> str: # base64 encode +def b64Encode(content: str, urlSafe: bool = True, padding: bool = False) -> str: # base64 encode content = base64.b64encode(content.encode(encoding = 'utf-8')).decode(encoding = 'utf-8') if urlSafe: content = content.replace('+', '-') # `+` => `-` @@ -23,7 +23,7 @@ def base64Encode(content: str, urlSafe: bool = True, padding: bool = False) -> s return content.replace('=', '') # remove `=` padding -def base64Decode(content: str) -> str: # base64 decode +def b64Decode(content: str) -> str: # base64 decode try: content = content.replace('-', '+').replace('_', '/') # compatible urlSafe if len(content) % 4 in range(2, 4): # remainder -> 2 or 3 diff --git a/Utils/Common/Url.py b/Utils/Common/Format.py similarity index 67% rename from Utils/Common/Url.py rename to Utils/Common/Format.py index 9e7f370..9c888a3 100644 --- a/Utils/Common/Url.py +++ b/Utils/Common/Format.py @@ -1,9 +1,27 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from IPy import IP from Utils.Logger import logger from Utils.Common.Coding import * + +def v6AddBracket(host: str) -> str: # add bracket for ipv6 + return hostFormat(host, v6Bracket = True) + + +def hostFormat(host: str, v6Bracket: bool = False) -> str: + try: + if host[:1] == '[' and host[-1:] == ']': # [IPv6] format + host = host[1:-1] # remove extra bracket + ip = IP(host) + if v6Bracket and ip.version() == 6: + return '[%s]' % str(ip) # [IPv6] + return str(ip) # IPv4 / IPV6 + except: # not ip address + return host + + def checkScheme(url: str, scheme: str, name: str) -> str: # check url scheme and remove it if not url.startswith('%s://' % scheme): logger.warning('%s url should start with `%s://`' % (name, scheme)) diff --git a/Utils/Common/Generate.py b/Utils/Common/Generate.py new file mode 100644 index 0000000..d4dcdbf --- /dev/null +++ b/Utils/Common/Generate.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import uuid +import random +import hashlib +from Utils.Logger import logger + + +def md5Sum(string: str) -> str: + return hashlib.md5(string.encode(encoding = 'utf-8')).hexdigest() # md5 hash + + +def genUUID() -> str: # generate uuid v5 + return str(uuid.uuid5( + uuid.NAMESPACE_DNS, genFlag(length = 16) + )) + + +def genFlag(length: int = 12) -> str: # generate random task flag + flag = '' + for i in range(0, length): + tmp = random.randint(0, 15) + if tmp >= 10: + flag += chr(tmp + 87) # a ~ f + else: + flag += str(tmp) # 0 ~ 9 + logger.debug('Generate new flag -> %s' % flag) + return flag diff --git a/Utils/Common/Host.py b/Utils/Common/Host.py deleted file mode 100644 index a3284f5..0000000 --- a/Utils/Common/Host.py +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env python3 -# -*- coding: utf-8 -*- - -from IPy import IP - - -def v6AddBracket(host: str) -> str: # add bracket for ipv6 - return hostFormat(host, v6Bracket = True) - - -def hostFormat(host: str, v6Bracket: bool = False) -> str: - try: - if host[:1] == '[' and host[-1:] == ']': # [IPv6] - host = host[1:-1] # remove extra bracket - ip = IP(host) - if v6Bracket and ip.version() == 6: - return '[%s]' % str(ip) # [IPv6] - return str(ip) # IPv4 / IPV6 - except: # not ip address - return host diff --git a/Utils/Common/__init__.py b/Utils/Common/__init__.py index 23f51d7..1c47bdc 100644 --- a/Utils/Common/__init__.py +++ b/Utils/Common/__init__.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from Utils.Common.Url import * -from Utils.Common.Host import * +from Utils.Common.Type import * +from Utils.Common.Check import * +from Utils.Common.Format import * from Utils.Common.Network import * +from Utils.Common.Generate import *