You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #
  2. # Copyright 2024 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import base64
  17. import datetime
  18. import hashlib
  19. import io
  20. import json
  21. import os
  22. import pickle
  23. import socket
  24. import time
  25. import uuid
  26. import requests
  27. import logging
  28. import copy
  29. from enum import Enum, IntEnum
  30. import importlib
  31. from Cryptodome.PublicKey import RSA
  32. from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
  33. from filelock import FileLock
  34. from api.constants import SERVICE_CONF
  35. from . import file_utils
  36. def conf_realpath(conf_name):
  37. conf_path = f"conf/{conf_name}"
  38. return os.path.join(file_utils.get_project_base_directory(), conf_path)
  39. def read_config(conf_name=SERVICE_CONF):
  40. local_config = {}
  41. local_path = conf_realpath(f'local.{conf_name}')
  42. # load local config file
  43. if os.path.exists(local_path):
  44. local_config = file_utils.load_yaml_conf(local_path)
  45. if not isinstance(local_config, dict):
  46. raise ValueError(f'Invalid config file: "{local_path}".')
  47. global_config_path = conf_realpath(conf_name)
  48. global_config = file_utils.load_yaml_conf(global_config_path)
  49. if not isinstance(global_config, dict):
  50. raise ValueError(f'Invalid config file: "{global_config_path}".')
  51. global_config.update(local_config)
  52. return global_config
  53. CONFIGS = read_config()
  54. def show_configs():
  55. msg = f"Current configs, from {conf_realpath(SERVICE_CONF)}:"
  56. for k, v in CONFIGS.items():
  57. if isinstance(v, dict):
  58. if "password" in v:
  59. v = copy.deepcopy(v)
  60. v["password"] = "*" * 8
  61. if "access_key" in v:
  62. v = copy.deepcopy(v)
  63. v["access_key"] = "*" * 8
  64. if "secret_key" in v:
  65. v = copy.deepcopy(v)
  66. v["secret_key"] = "*" * 8
  67. if "secret" in v:
  68. v = copy.deepcopy(v)
  69. v["secret"] = "*" * 8
  70. if "sas_token" in v:
  71. v = copy.deepcopy(v)
  72. v["sas_token"] = "*" * 8
  73. if "oauth" in k:
  74. v = copy.deepcopy(v)
  75. for key, val in v.items():
  76. if "client_secret" in val:
  77. val["client_secret"] = "*" * 8
  78. if "authentication" in k:
  79. v = copy.deepcopy(v)
  80. for key, val in v.items():
  81. if "http_secret_key" in val:
  82. val["http_secret_key"] = "*" * 8
  83. msg += f"\n\t{k}: {v}"
  84. logging.info(msg)
  85. def get_base_config(key, default=None):
  86. if key is None:
  87. return None
  88. if default is None:
  89. default = os.environ.get(key.upper())
  90. return CONFIGS.get(key, default)
  91. use_deserialize_safe_module = get_base_config(
  92. 'use_deserialize_safe_module', False)
  93. class BaseType:
  94. def to_dict(self):
  95. return dict([(k.lstrip("_"), v) for k, v in self.__dict__.items()])
  96. def to_dict_with_type(self):
  97. def _dict(obj):
  98. module = None
  99. if issubclass(obj.__class__, BaseType):
  100. data = {}
  101. for attr, v in obj.__dict__.items():
  102. k = attr.lstrip("_")
  103. data[k] = _dict(v)
  104. module = obj.__module__
  105. elif isinstance(obj, (list, tuple)):
  106. data = []
  107. for i, vv in enumerate(obj):
  108. data.append(_dict(vv))
  109. elif isinstance(obj, dict):
  110. data = {}
  111. for _k, vv in obj.items():
  112. data[_k] = _dict(vv)
  113. else:
  114. data = obj
  115. return {"type": obj.__class__.__name__,
  116. "data": data, "module": module}
  117. return _dict(self)
  118. class CustomJSONEncoder(json.JSONEncoder):
  119. def __init__(self, **kwargs):
  120. self._with_type = kwargs.pop("with_type", False)
  121. super().__init__(**kwargs)
  122. def default(self, obj):
  123. if isinstance(obj, datetime.datetime):
  124. return obj.strftime('%Y-%m-%d %H:%M:%S')
  125. elif isinstance(obj, datetime.date):
  126. return obj.strftime('%Y-%m-%d')
  127. elif isinstance(obj, datetime.timedelta):
  128. return str(obj)
  129. elif issubclass(type(obj), Enum) or issubclass(type(obj), IntEnum):
  130. return obj.value
  131. elif isinstance(obj, set):
  132. return list(obj)
  133. elif issubclass(type(obj), BaseType):
  134. if not self._with_type:
  135. return obj.to_dict()
  136. else:
  137. return obj.to_dict_with_type()
  138. elif isinstance(obj, type):
  139. return obj.__name__
  140. else:
  141. return json.JSONEncoder.default(self, obj)
  142. def rag_uuid():
  143. return uuid.uuid1().hex
  144. def string_to_bytes(string):
  145. return string if isinstance(
  146. string, bytes) else string.encode(encoding="utf-8")
  147. def bytes_to_string(byte):
  148. return byte.decode(encoding="utf-8")
  149. def json_dumps(src, byte=False, indent=None, with_type=False):
  150. dest = json.dumps(
  151. src,
  152. indent=indent,
  153. cls=CustomJSONEncoder,
  154. with_type=with_type)
  155. if byte:
  156. dest = string_to_bytes(dest)
  157. return dest
  158. def json_loads(src, object_hook=None, object_pairs_hook=None):
  159. if isinstance(src, bytes):
  160. src = bytes_to_string(src)
  161. return json.loads(src, object_hook=object_hook,
  162. object_pairs_hook=object_pairs_hook)
  163. def current_timestamp():
  164. return int(time.time() * 1000)
  165. def timestamp_to_date(timestamp, format_string="%Y-%m-%d %H:%M:%S"):
  166. if not timestamp:
  167. timestamp = time.time()
  168. timestamp = int(timestamp) / 1000
  169. time_array = time.localtime(timestamp)
  170. str_date = time.strftime(format_string, time_array)
  171. return str_date
  172. def date_string_to_timestamp(time_str, format_string="%Y-%m-%d %H:%M:%S"):
  173. time_array = time.strptime(time_str, format_string)
  174. time_stamp = int(time.mktime(time_array) * 1000)
  175. return time_stamp
  176. def serialize_b64(src, to_str=False):
  177. dest = base64.b64encode(pickle.dumps(src))
  178. if not to_str:
  179. return dest
  180. else:
  181. return bytes_to_string(dest)
  182. def deserialize_b64(src):
  183. src = base64.b64decode(
  184. string_to_bytes(src) if isinstance(
  185. src, str) else src)
  186. if use_deserialize_safe_module:
  187. return restricted_loads(src)
  188. return pickle.loads(src)
  189. safe_module = {
  190. 'numpy',
  191. 'rag_flow'
  192. }
  193. class RestrictedUnpickler(pickle.Unpickler):
  194. def find_class(self, module, name):
  195. import importlib
  196. if module.split('.')[0] in safe_module:
  197. _module = importlib.import_module(module)
  198. return getattr(_module, name)
  199. # Forbid everything else.
  200. raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
  201. (module, name))
  202. def restricted_loads(src):
  203. """Helper function analogous to pickle.loads()."""
  204. return RestrictedUnpickler(io.BytesIO(src)).load()
  205. def get_lan_ip():
  206. if os.name != "nt":
  207. import fcntl
  208. import struct
  209. def get_interface_ip(ifname):
  210. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  211. return socket.inet_ntoa(
  212. fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', string_to_bytes(ifname[:15])))[20:24])
  213. ip = socket.gethostbyname(socket.getfqdn())
  214. if ip.startswith("127.") and os.name != "nt":
  215. interfaces = [
  216. "bond1",
  217. "eth0",
  218. "eth1",
  219. "eth2",
  220. "wlan0",
  221. "wlan1",
  222. "wifi0",
  223. "ath0",
  224. "ath1",
  225. "ppp0",
  226. ]
  227. for ifname in interfaces:
  228. try:
  229. ip = get_interface_ip(ifname)
  230. break
  231. except IOError:
  232. pass
  233. return ip or ''
  234. def from_dict_hook(in_dict: dict):
  235. if "type" in in_dict and "data" in in_dict:
  236. if in_dict["module"] is None:
  237. return in_dict["data"]
  238. else:
  239. return getattr(importlib.import_module(
  240. in_dict["module"]), in_dict["type"])(**in_dict["data"])
  241. else:
  242. return in_dict
  243. def decrypt_database_password(password):
  244. encrypt_password = get_base_config("encrypt_password", False)
  245. encrypt_module = get_base_config("encrypt_module", False)
  246. private_key = get_base_config("private_key", None)
  247. if not password or not encrypt_password:
  248. return password
  249. if not private_key:
  250. raise ValueError("No private key")
  251. module_fun = encrypt_module.split("#")
  252. pwdecrypt_fun = getattr(
  253. importlib.import_module(
  254. module_fun[0]),
  255. module_fun[1])
  256. return pwdecrypt_fun(private_key, password)
  257. def decrypt_database_config(
  258. database=None, passwd_key="password", name="database"):
  259. if not database:
  260. database = get_base_config(name, {})
  261. database[passwd_key] = decrypt_database_password(database[passwd_key])
  262. return database
  263. def update_config(key, value, conf_name=SERVICE_CONF):
  264. conf_path = conf_realpath(conf_name=conf_name)
  265. if not os.path.isabs(conf_path):
  266. conf_path = os.path.join(
  267. file_utils.get_project_base_directory(), conf_path)
  268. with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
  269. config = file_utils.load_yaml_conf(conf_path=conf_path) or {}
  270. config[key] = value
  271. file_utils.rewrite_yaml_conf(conf_path=conf_path, config=config)
  272. def get_uuid():
  273. return uuid.uuid1().hex
  274. def datetime_format(date_time: datetime.datetime) -> datetime.datetime:
  275. return datetime.datetime(date_time.year, date_time.month, date_time.day,
  276. date_time.hour, date_time.minute, date_time.second)
  277. def get_format_time() -> datetime.datetime:
  278. return datetime_format(datetime.datetime.now())
  279. def str2date(date_time: str):
  280. return datetime.datetime.strptime(date_time, '%Y-%m-%d')
  281. def elapsed2time(elapsed):
  282. seconds = elapsed / 1000
  283. minuter, second = divmod(seconds, 60)
  284. hour, minuter = divmod(minuter, 60)
  285. return '%02d:%02d:%02d' % (hour, minuter, second)
  286. def decrypt(line):
  287. file_path = os.path.join(
  288. file_utils.get_project_base_directory(),
  289. "conf",
  290. "private.pem")
  291. rsa_key = RSA.importKey(open(file_path).read(), "Welcome")
  292. cipher = Cipher_pkcs1_v1_5.new(rsa_key)
  293. return cipher.decrypt(base64.b64decode(
  294. line), "Fail to decrypt password!").decode('utf-8')
  295. def decrypt2(crypt_text):
  296. from base64 import b64decode, b16decode
  297. from Crypto.Cipher import PKCS1_v1_5 as Cipher_PKCS1_v1_5
  298. from Crypto.PublicKey import RSA
  299. decode_data = b64decode(crypt_text)
  300. if len(decode_data) == 127:
  301. hex_fixed = '00' + decode_data.hex()
  302. decode_data = b16decode(hex_fixed.upper())
  303. file_path = os.path.join(
  304. file_utils.get_project_base_directory(),
  305. "conf",
  306. "private.pem")
  307. pem = open(file_path).read()
  308. rsa_key = RSA.importKey(pem, "Welcome")
  309. cipher = Cipher_PKCS1_v1_5.new(rsa_key)
  310. decrypt_text = cipher.decrypt(decode_data, None)
  311. return (b64decode(decrypt_text)).decode()
  312. def download_img(url):
  313. if not url:
  314. return ""
  315. response = requests.get(url)
  316. return "data:" + \
  317. response.headers.get('Content-Type', 'image/jpg') + ";" + \
  318. "base64," + base64.b64encode(response.content).decode("utf-8")
  319. def delta_seconds(date_string: str):
  320. dt = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
  321. return (datetime.datetime.now() - dt).total_seconds()
  322. def hash_str2int(line:str, mod: int=10 ** 8) -> int:
  323. return int(hashlib.sha1(line.encode("utf-8")).hexdigest(), 16) % mod