Browse Source

update: common functions

dev
dnomd343 2 years ago
parent
commit
c7a7c5b65e
  1. 48
      Utils/Common/Check.py
  2. 4
      Utils/Common/Coding.py
  3. 18
      Utils/Common/Format.py
  4. 29
      Utils/Common/Generate.py
  5. 20
      Utils/Common/Host.py
  6. 6
      Utils/Common/__init__.py

48
Basis/Functions.py → 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)
))

4
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

18
Utils/Common/Url.py → 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))

29
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

20
Utils/Common/Host.py

@ -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

6
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 *

Loading…
Cancel
Save