This commit is contained in:
flaskfarm
2022-10-17 13:28:55 +09:00
parent fefdbada91
commit 259daa8ad8
13 changed files with 243 additions and 139 deletions

View File

@@ -1,10 +1,16 @@
import os, base64, traceback
from Crypto.Cipher import AES
import base64
import os
import traceback
from Crypto import Random
from Crypto.Cipher import AES
from . import logger
BS = 16
pad = lambda s: s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
unpad = lambda s : s[0:-s[-1]]
pad = lambda s: s + (BS - len(s.encode('utf-8')) % BS) * chr(BS - len(s.encode('utf-8')) % BS)
#unpad = lambda s : s[0:-s[-1]]
unpad = lambda s : s[:-ord(s[len(s)-1:])]
key = b'140b41b22a29beb4061bda66b6747e14'
class SupportAES(object):
@@ -39,5 +45,56 @@ class SupportAES(object):
iv = enc[:16]
if len(iv) != 16:
iv = os.urandom(16)
if mykey is not None and type(mykey) == type(''):
mykey = mykey.encode()
cipher = AES.new(key if mykey is None else mykey, AES.MODE_CBC, iv )
return unpad(cipher.decrypt( enc[16:] )).decode()
@classmethod
def md5(cls, text):
import hashlib
enc = hashlib.md5()
enc.update(text.encode())
return enc.hexdigest()
@classmethod
def encrypt_(cls, raw, mykey=None, iv=None):
try:
Random.atfork()
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
raw = pad(raw)
if type(raw) == type(''):
raw = raw.encode()
if mykey is not None and type(mykey) == type(''):
mykey = mykey.encode()
if iv == None:
iv = Random.new().read( AES.block_size )
elif iv is not None and type(iv) == type(''):
iv = iv.encode()
cipher = AES.new(key if mykey is None else mykey, AES.MODE_CBC, iv )
try:
tmp = cipher.encrypt( raw )
except Exception as exception:
logger.error('Exception:%s', exception)
logger.error(traceback.format_exc())
tmp = cipher.encrypt( raw.encode() )
ret = base64.b64encode( tmp )
ret = ret.decode()
return ret
@classmethod
def decrypt_(cls, enc, mykey=None, iv=None):
enc = base64.b64decode(enc)
if iv == None:
iv = os.urandom(16)
elif iv is not None and type(iv) == type(''):
iv = iv.encode()
if mykey is not None and type(mykey) == type(''):
mykey = mykey.encode()
cipher = AES.new(key if mykey is None else mykey, AES.MODE_CBC, iv )
return unpad(cipher.decrypt( enc )).decode()

View File

@@ -94,23 +94,6 @@ class SupportFile(object):
@classmethod
def write(cls, data, filepath, mode='w'):
try:
import codecs
ofp = codecs.open(filepath, mode, encoding='utf8')
if isinstance(data, bytes) and mode == 'w':
data = data.decode('utf-8')
ofp.write(data)
ofp.close()
return True
except Exception as exception:
logger.debug('Exception:%s', exception)
logger.debug(traceback.format_exc())
return False
@classmethod

79
lib/support/base/sc.py Normal file
View File

@@ -0,0 +1,79 @@
import argparse
import os
import platform
import re
import sys
import traceback
from . import logger
class SupportSC:
LIBRARY_LOADING = False
try:
if platform.system() == 'Linux':
if (platform.platform().find('86') == -1 and platform.platform().find('64') == -1) or platform.platform().find('arch') != -1 or platform.platform().find('arm') != -1:
sys.path.insert(2, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib', 'sc', 'LinuxArm'))
else:
sys.path.insert(2, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib', 'sc', 'Linux'))
if platform.system() == 'Windows':
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'sc', 'Windows'))
import sc
LIBRARY_LOADING = True
except:
pass
@classmethod
def encode(cls, text, mode=0):
try:
import sc
return sc.encode(text, mode)
except Exception as e:
logger.error(f'Exception:{str(e)}')
logger.error(traceback.format_exc())
return None
@classmethod
def decode(cls, text):
try:
import sc
return sc.decode(text)
except:
return None
@classmethod
def load_module(cls, module_name, module_code):
try:
import sc
mod = sc.load_module(module_name, module_code)
sys.modules[mod] = mod
logger.warning(f"C-LOADING : {module_name}")
return mod
except Exception as e:
logger.error(f'Exception:{str(e)}')
logger.error(traceback.format_exc())
@classmethod
def load_module_P(cls, plugin_ins, filename):
return cls.load_module_f(plugin_ins.setting['filepath'], filename)
@classmethod
def load_module_f(cls, package_filepath, filename):
dirname = os.path.dirname(package_filepath)
filepath = os.path.join(dirname, filename + '.pyf')
if os.path.exists(filepath) and os.path.exists(filepath):
from support import SupportFile
code = SupportFile.read_file(filepath)
return cls.load_module(f"{os.path.basename(dirname)}.{filename}", code)
@classmethod
def td(self, mediacode, ts, url):
try:
import sc
ret = sc.td1(mediacode, str(ts), url).strip()
ret = re.sub('[^ -~]+', '', ret).strip()
return ret
except:
return None