Files
SMANAGE/backup/_flask_backup/tools.py
T

266 lines
6.0 KiB
Python

#前置保证
from gevent import monkey
monkey.patch_all()
print("Patch...")
import sys, os
from io import BytesIO
import traceback
import ssl
from pathlib import Path
import configparser
import zipfile
import json
from datetime import date, timedelta
import xml.etree.ElementTree as ET
from Crypto.Cipher import AES
import binascii
import hashlib
import bcrypt
import base64
import string
import struct
import uuid
import re
import io
from threading import Thread
import subprocess
import datetime,time
from PIL import Image,ImageDraw
import warnings
import flask
warnings.filterwarnings(
'ignore',
category=UserWarning,
message="Current server name '.*' doesn't match configured server name '.*'",
module = flask.app.__name__
)
from flask_wtf import FlaskForm
from flask_wtf.csrf import validate_csrf
from wtforms import SubmitField, HiddenField
from wtforms.validators import DataRequired
import flask_socketio
import flask_wtf.csrf
import flask_cors
import flask_sqlalchemy
import flask_bcrypt
import flask_wtf
import wtforms
import wtforms.validators
import gevent.pywsgi as pywsgi
import flask_limiter
import flask_limiter.util
from mcstatus import JavaServer, BedrockServer
from openai import OpenAI
import sqlalchemy
import socket
import random
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import secrets
import colorama
import logging
import logging.handlers
colorama.init()
import smtplib
import email.mime.text
import email.mime.multipart
import email.mime.base
import email.encoders
import email.header
print("Import...")
import MilRcon
import typing
import pyotp
import qrcode
import inspect
import threading
#基础变量
temp=os.environ["temp"]
import py_compile
py_compile.compile(__file__)
#UUID
# 读/写/添
def openw(file, write, mode="1", encoding="UTF-8"):
if mode == "1" or mode == 1:
with open(file, "w", encoding=encoding) as f:
f.write(write)
elif mode == "2" or mode == 2:
with open(file, "wb") as f:
f.write(write)
def opena(file, write, mode="1", encoding="UTF-8"):
if mode == "1" or mode == 1:
with open(file, "a", encoding=encoding) as f:
f.write(write)
elif mode == "2" or mode == 2:
with open(file, "ab") as f:
f.write(write)
def openr(file, mode="1", encoding="UTF-8"):
if mode == "1" or mode == 1:
with open(file, "r", encoding=encoding) as f:
read = f.read()
elif mode == "2" or mode == 2:
with open(file, "rb") as f:
read = f.read()
return read
def IsRegex(s):
try:
re.compile(s)
return True
except re.error:
return False
def TimeFormatInt(i=None,s="%Y-%m-%d %H:%M:%S",end=""):
if not i: i=time.time()
return time.strftime(s+end, time.localtime(i))
temp=os.environ["temp"]
def chunk_string(s, n):
return [s[i:i+n] for i in range(0, len(s), n)]
def four_split(s):
a=[0,0,0,0]
a[1]=s[0]
a[3]=s[1]
a[0]=s[2]
a[2]=s[3]
s=""
for i in a:
s+=i
return s
def R_four_split(s):
a=[0,0,0,0]
a[0]=s[1]
a[1]=s[3]
a[2]=s[0]
a[3]=s[2]
s=""
for i in a:
s+=i
return s
def CBDF(file,dllfile):
f=open(file,"rb")
get=f.read()
f.close()
s_hex=get.hex()
s_hex=s_hex[::-1]
s_hex=chunk_string(s_hex,4)
ret=""
for i in s_hex:
if len(i) == 4:
ret+=four_split(i)
else:
ret+=i
hexgo=binascii.a2b_hex(ret)
f=open(dllfile,"wb")
f.write(hexgo)
f.close()
def CBDS(s,encoding="UTF-8"):
s=bytes(s,encoding=encoding)
s_hex=s.hex()
s_hex=s_hex[::-1]
s_hex=chunk_string(s_hex,4)
ret=""
for i in s_hex:
if len(i) == 4:
ret+=four_split(i)
else:
ret+=i
hexgo=binascii.a2b_hex(ret)
return hexgo
def CBDMS(s,encoding="UTF-8"):
s=bytes(s,encoding=encoding)
s_hex=s.hex()
s_hex=s_hex[::-1]
s_hex=chunk_string(s_hex,4)
ret=""
for i in s_hex:
if len(i) == 4:
ret+=four_split(i)
else:
ret+=i
return ret
def GBDMS(s):
ret=""
s_hex=chunk_string(s,4)
for i in s_hex:
if len(i) == 4:
ret+=R_four_split(i)
else:
ret+=i
ret=ret[::-1]
hexgo = binascii.a2b_hex(ret).decode("utf-8")
return hexgo
def GBDS(s,encoding="utf-8"):
s_hex = s.hex()
ret=""
s_hex=chunk_string(s_hex,4)
for i in s_hex:
if len(i) == 4:
ret+=R_four_split(i)
else:
ret+=i
ret=ret[::-1]
hexgo = binascii.a2b_hex(ret).decode(encoding)
return hexgo
def GBDF(dllfile):
f = open(dllfile, "rb")
get = f.read()
f.close()
s_hex = get.hex()
ret=""
s_hex=chunk_string(s_hex,4)
for i in s_hex:
if len(i) == 4:
ret+=R_four_split(i)
else:
ret+=i
ret=ret[::-1]
hexgo = binascii.a2b_hex(ret)
return hexgo
def NewUUID():
return str(uuid.uuid3(uuid.NAMESPACE_DNS, str(random.randint(0, 10000000000000000000))))
def NewBUUID():
return uuid.uuid4().bytes
def asyncs(f):
def wrapper(*args, **kwargs):
thr = Thread(target = f, args = args, kwargs = kwargs)
thr.start()
return wrapper
def GetFileSha256(FilePath: str, ChunkSize: int = 4096) -> str:
Sha256Hash: hashlib._Hash = hashlib.sha256()
with open(FilePath, 'rb') as f:
while True:
Chunk: bytes = f.read(ChunkSize)
if not Chunk: break
Sha256Hash.update(Chunk)
return Sha256Hash.hexdigest()