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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 io
  19. import json
  20. import os
  21. import pickle
  22. import socket
  23. import time
  24. import uuid
  25. import requests
  26. import logging
  27. from enum import Enum, IntEnum
  28. import importlib
  29. from Cryptodome.PublicKey import RSA
  30. from Cryptodome.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
  31. from filelock import FileLock
  32. from api.constants import SERVICE_CONF
  33. from . import file_utils
  34. def conf_realpath(conf_name):
  35. conf_path = f"conf/{conf_name}"
  36. return os.path.join(file_utils.get_project_base_directory(), conf_path)
  37. def read_config(conf_name=SERVICE_CONF):
  38. local_config = {}
  39. local_path = conf_realpath(f'local.{conf_name}')
  40. # load local config file
  41. if os.path.exists(local_path):
  42. local_config = file_utils.load_yaml_conf(local_path)
  43. if not isinstance(local_config, dict):
  44. raise ValueError(f'Invalid config file: "{local_path}".')
  45. global_config_path = conf_realpath(conf_name)
  46. global_config = file_utils.load_yaml_conf(global_config_path)
  47. if not isinstance(global_config, dict):
  48. raise ValueError(f'Invalid config file: "{global_config_path}".')
  49. global_config.update(local_config)
  50. return global_config
  51. CONFIGS = read_config()
  52. def show_configs():
  53. msg = f"Current configs, from {conf_realpath(SERVICE_CONF)}:"
  54. for k, v in CONFIGS.items():
  55. msg += f"\n\t{k}: {v}"
  56. logging.info(msg)
  57. def get_base_config(key, default=None):
  58. if key is None:
  59. return None
  60. if default is None:
  61. default = os.environ.get(key.upper())
  62. return CONFIGS.get(key, default)
  63. use_deserialize_safe_module = get_base_config(
  64. 'use_deserialize_safe_module', False)
  65. class BaseType:
  66. def to_dict(self):
  67. return dict([(k.lstrip("_"), v) for k, v in self.__dict__.items()])
  68. def to_dict_with_type(self):
  69. def _dict(obj):
  70. module = None
  71. if issubclass(obj.__class__, BaseType):
  72. data = {}
  73. for attr, v in obj.__dict__.items():
  74. k = attr.lstrip("_")
  75. data[k] = _dict(v)
  76. module = obj.__module__
  77. elif isinstance(obj, (list, tuple)):
  78. data = []
  79. for i, vv in enumerate(obj):
  80. data.append(_dict(vv))
  81. elif isinstance(obj, dict):
  82. data = {}
  83. for _k, vv in obj.items():
  84. data[_k] = _dict(vv)
  85. else:
  86. data = obj
  87. return {"type": obj.__class__.__name__,
  88. "data": data, "module": module}
  89. return _dict(self)
  90. class CustomJSONEncoder(json.JSONEncoder):
  91. def __init__(self, **kwargs):
  92. self._with_type = kwargs.pop("with_type", False)
  93. super().__init__(**kwargs)
  94. def default(self, obj):
  95. if isinstance(obj, datetime.datetime):
  96. return obj.strftime('%Y-%m-%d %H:%M:%S')
  97. elif isinstance(obj, datetime.date):
  98. return obj.strftime('%Y-%m-%d')
  99. elif isinstance(obj, datetime.timedelta):
  100. return str(obj)
  101. elif issubclass(type(obj), Enum) or issubclass(type(obj), IntEnum):
  102. return obj.value
  103. elif isinstance(obj, set):
  104. return list(obj)
  105. elif issubclass(type(obj), BaseType):
  106. if not self._with_type:
  107. return obj.to_dict()
  108. else:
  109. return obj.to_dict_with_type()
  110. elif isinstance(obj, type):
  111. return obj.__name__
  112. else:
  113. return json.JSONEncoder.default(self, obj)
  114. def rag_uuid():
  115. return uuid.uuid1().hex
  116. def string_to_bytes(string):
  117. return string if isinstance(
  118. string, bytes) else string.encode(encoding="utf-8")
  119. def bytes_to_string(byte):
  120. return byte.decode(encoding="utf-8")
  121. def json_dumps(src, byte=False, indent=None, with_type=False):
  122. dest = json.dumps(
  123. src,
  124. indent=indent,
  125. cls=CustomJSONEncoder,
  126. with_type=with_type)
  127. if byte:
  128. dest = string_to_bytes(dest)
  129. return dest
  130. def json_loads(src, object_hook=None, object_pairs_hook=None):
  131. if isinstance(src, bytes):
  132. src = bytes_to_string(src)
  133. return json.loads(src, object_hook=object_hook,
  134. object_pairs_hook=object_pairs_hook)
  135. def current_timestamp():
  136. return int(time.time() * 1000)
  137. def timestamp_to_date(timestamp, format_string="%Y-%m-%d %H:%M:%S"):
  138. if not timestamp:
  139. timestamp = time.time()
  140. timestamp = int(timestamp) / 1000
  141. time_array = time.localtime(timestamp)
  142. str_date = time.strftime(format_string, time_array)
  143. return str_date
  144. def date_string_to_timestamp(time_str, format_string="%Y-%m-%d %H:%M:%S"):
  145. time_array = time.strptime(time_str, format_string)
  146. time_stamp = int(time.mktime(time_array) * 1000)
  147. return time_stamp
  148. def serialize_b64(src, to_str=False):
  149. dest = base64.b64encode(pickle.dumps(src))
  150. if not to_str:
  151. return dest
  152. else:
  153. return bytes_to_string(dest)
  154. def deserialize_b64(src):
  155. src = base64.b64decode(
  156. string_to_bytes(src) if isinstance(
  157. src, str) else src)
  158. if use_deserialize_safe_module:
  159. return restricted_loads(src)
  160. return pickle.loads(src)
  161. safe_module = {
  162. 'numpy',
  163. 'rag_flow'
  164. }
  165. class RestrictedUnpickler(pickle.Unpickler):
  166. def find_class(self, module, name):
  167. import importlib
  168. if module.split('.')[0] in safe_module:
  169. _module = importlib.import_module(module)
  170. return getattr(_module, name)
  171. # Forbid everything else.
  172. raise pickle.UnpicklingError("global '%s.%s' is forbidden" %
  173. (module, name))
  174. def restricted_loads(src):
  175. """Helper function analogous to pickle.loads()."""
  176. return RestrictedUnpickler(io.BytesIO(src)).load()
  177. def get_lan_ip():
  178. if os.name != "nt":
  179. import fcntl
  180. import struct
  181. def get_interface_ip(ifname):
  182. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  183. return socket.inet_ntoa(
  184. fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', string_to_bytes(ifname[:15])))[20:24])
  185. ip = socket.gethostbyname(socket.getfqdn())
  186. if ip.startswith("127.") and os.name != "nt":
  187. interfaces = [
  188. "bond1",
  189. "eth0",
  190. "eth1",
  191. "eth2",
  192. "wlan0",
  193. "wlan1",
  194. "wifi0",
  195. "ath0",
  196. "ath1",
  197. "ppp0",
  198. ]
  199. for ifname in interfaces:
  200. try:
  201. ip = get_interface_ip(ifname)
  202. break
  203. except IOError:
  204. pass
  205. return ip or ''
  206. def from_dict_hook(in_dict: dict):
  207. if "type" in in_dict and "data" in in_dict:
  208. if in_dict["module"] is None:
  209. return in_dict["data"]
  210. else:
  211. return getattr(importlib.import_module(
  212. in_dict["module"]), in_dict["type"])(**in_dict["data"])
  213. else:
  214. return in_dict
  215. def decrypt_database_password(password):
  216. encrypt_password = get_base_config("encrypt_password", False)
  217. encrypt_module = get_base_config("encrypt_module", False)
  218. private_key = get_base_config("private_key", None)
  219. if not password or not encrypt_password:
  220. return password
  221. if not private_key:
  222. raise ValueError("No private key")
  223. module_fun = encrypt_module.split("#")
  224. pwdecrypt_fun = getattr(
  225. importlib.import_module(
  226. module_fun[0]),
  227. module_fun[1])
  228. return pwdecrypt_fun(private_key, password)
  229. def decrypt_database_config(
  230. database=None, passwd_key="password", name="database"):
  231. if not database:
  232. database = get_base_config(name, {})
  233. database[passwd_key] = decrypt_database_password(database[passwd_key])
  234. return database
  235. def update_config(key, value, conf_name=SERVICE_CONF):
  236. conf_path = conf_realpath(conf_name=conf_name)
  237. if not os.path.isabs(conf_path):
  238. conf_path = os.path.join(
  239. file_utils.get_project_base_directory(), conf_path)
  240. with FileLock(os.path.join(os.path.dirname(conf_path), ".lock")):
  241. config = file_utils.load_yaml_conf(conf_path=conf_path) or {}
  242. config[key] = value
  243. file_utils.rewrite_yaml_conf(conf_path=conf_path, config=config)
  244. def get_uuid():
  245. return uuid.uuid1().hex
  246. def datetime_format(date_time: datetime.datetime) -> datetime.datetime:
  247. return datetime.datetime(date_time.year, date_time.month, date_time.day,
  248. date_time.hour, date_time.minute, date_time.second)
  249. def get_format_time() -> datetime.datetime:
  250. return datetime_format(datetime.datetime.now())
  251. def str2date(date_time: str):
  252. return datetime.datetime.strptime(date_time, '%Y-%m-%d')
  253. def elapsed2time(elapsed):
  254. seconds = elapsed / 1000
  255. minuter, second = divmod(seconds, 60)
  256. hour, minuter = divmod(minuter, 60)
  257. return '%02d:%02d:%02d' % (hour, minuter, second)
  258. def decrypt(line):
  259. file_path = os.path.join(
  260. file_utils.get_project_base_directory(),
  261. "conf",
  262. "private.pem")
  263. rsa_key = RSA.importKey(open(file_path).read(), "Welcome")
  264. cipher = Cipher_pkcs1_v1_5.new(rsa_key)
  265. return cipher.decrypt(base64.b64decode(
  266. line), "Fail to decrypt password!").decode('utf-8')
  267. def download_img(url):
  268. if not url:
  269. return ""
  270. response = requests.get(url)
  271. return "data:" + \
  272. response.headers.get('Content-Type', 'image/jpg') + ";" + \
  273. "base64," + base64.b64encode(response.content).decode("utf-8")
  274. def delta_seconds(date_string: str):
  275. dt = datetime.datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
  276. return (datetime.datetime.now() - dt).total_seconds()