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

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