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.

351 lines
11 KiB

1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
3 months ago
3 months ago
1 year ago
3 months ago
3 months ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
1 year ago
3 months ago
2 months ago
1 year ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
1 year ago
3 months ago
  1. #coding: utf-8
  2. # +-------------------------------------------------------------------
  3. # | 宝塔Linux面板
  4. # +-------------------------------------------------------------------
  5. # | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
  6. # +-------------------------------------------------------------------
  7. # | Author: hwliang <hwl@bt.cn>
  8. # +-------------------------------------------------------------------
  9. #+--------------------------------------------------------------------
  10. #| 插件和模块加载器
  11. #+--------------------------------------------------------------------
  12. import public,os,sys,json
  13. def plugin_run(plugin_name,def_name,args):
  14. '''
  15. @name
  16. @param plugin_name<string>
  17. @param def_name<string>
  18. @param args<dict_obj>
  19. @return mixed
  20. '''
  21. if not plugin_name or not def_name: return public.returnMsg(False,'插件名称和插件方法名称不能为空!')
  22. # 获取插件目录
  23. plugin_path = public.get_plugin_path(plugin_name)
  24. is_php = os.path.exists(os.path.join(plugin_path,'index.php'))
  25. # 检查插件目录是否合法
  26. if is_php:
  27. plugin_file = os.path.join(plugin_path,'index.php')
  28. else:
  29. plugin_file = os.path.join(plugin_path, plugin_name + '_main.py')
  30. if not public.path_safe_check(plugin_file): return public.returnMsg(False,'插件路径不合法')
  31. # 检查插件入口文件是否存在
  32. if not os.path.exists(plugin_file): return public.returnMsg(False,'指定插件入口文件不存在')
  33. # 添加插件目录到系统路径
  34. public.sys_path_append(plugin_path)
  35. if not is_php:
  36. # 引用插件入口文件
  37. _name = "{}_main".format(plugin_name)
  38. plugin_main = __import__(_name)
  39. # 检查类名是否符合规范
  40. if not hasattr(plugin_main,_name):
  41. return public.returnMsg(False,'指定插件入口文件不符合规范')
  42. try:
  43. if sys.version_info[0] == 2:
  44. reload(plugin_main)
  45. else:
  46. from imp import reload
  47. reload(plugin_main)
  48. except:
  49. pass
  50. # 实例化插件类
  51. plugin_obj = getattr(plugin_main,_name)()
  52. # 检查方法是否存在
  53. if not hasattr(plugin_obj,def_name):
  54. return public.returnMsg(False,'在[%s]插件中找不到[%s]方法' % (plugin_name,def_name))
  55. if args is not None and 'plugin_get_object' in args and args.plugin_get_object == 1:
  56. return getattr(plugin_obj, def_name)
  57. # 执行方法
  58. return getattr(plugin_obj,def_name)(args)
  59. else:
  60. if args is not None and 'plugin_get_object' in args and args.plugin_get_object == 1:
  61. return None
  62. import panelPHP
  63. args.s = def_name
  64. args.name = plugin_name
  65. return panelPHP.panelPHP(plugin_name).exec_php_script(args)
  66. def get_module_list():
  67. '''
  68. @name
  69. @return list
  70. '''
  71. module_list = []
  72. class_path = public.get_class_path()
  73. for name in os.listdir(class_path):
  74. path = os.path.join(class_path,name)
  75. # 过滤无效文件
  76. if not name or name.endswith('.py') or name[0] == '.' or not name.endswith('Model') or os.path.isfile(path):continue
  77. module_list.append(name)
  78. return module_list
  79. def module_run(module_name,def_name,args):
  80. '''
  81. @name
  82. @param module_name<string>
  83. @param def_name<string>
  84. @param args<dict_obj>
  85. @return mixed
  86. '''
  87. if not module_name or not def_name: return public.returnMsg(False,'模块名称和模块方法名称不能为空!')
  88. model_index = args.get('model_index',None)
  89. class_path = public.get_class_path()
  90. panel_path = public.get_panel_path()
  91. module_file = None
  92. if 'model_index' in args:
  93. # 新模块目录
  94. if model_index in ['mod']:
  95. module_file = os.path.join(panel_path,'mod','project',module_name + 'Mod.py')
  96. elif model_index:
  97. # 旧模块目录
  98. module_file = os.path.join(class_path,model_index+"Model",module_name + 'Model.py')
  99. else:
  100. module_file = os.path.join(class_path,"projectModel",module_name + 'Model.py')
  101. else:
  102. # 如果没指定模块名称,则遍历所有模块目录
  103. module_list = get_module_list()
  104. for name in module_list:
  105. module_file = os.path.join(class_path,name,module_name + 'Model.py')
  106. if os.path.exists(module_file): break
  107. # 判断模块入口文件是否存在
  108. if not os.path.exists(module_file):
  109. return public.returnMsg(False,'模块[%s]不存在' % module_name)
  110. # 判断模块路径是否合法
  111. if not public.path_safe_check(module_file):
  112. return public.returnMsg(False,'模块路径不合法')
  113. def_object = public.get_script_object(module_file)
  114. if not def_object: return public.returnMsg(False,'模块[%s]不存在' % module_name)
  115. # 模块实例化并返回方法对象
  116. try:
  117. run_object = getattr(def_object.main(),def_name,None)
  118. except:
  119. return public.returnMsg(False,'模块[%s]入口实例化失败' % module_name)
  120. if not run_object: return public.returnMsg(False,'在[%s]模块中找不到[%s]方法' % (module_name,def_name))
  121. if 'module_get_object' in args and args.module_get_object == 1:
  122. return run_object
  123. # 执行方法
  124. result = run_object(args)
  125. return result
  126. def get_plugin_list(upgrade_force = False):
  127. '''
  128. @name
  129. @param upgrade_force<bool>
  130. @return dict
  131. '''
  132. api_root_url = 'https://api.bt.cn'
  133. api_url = api_root_url+ '/panel/get_plugin_list'
  134. panel_path = public.get_panel_path()
  135. data_path = os.path.join(panel_path,'data')
  136. if not os.path.exists(data_path):
  137. os.makedirs(data_path,384)
  138. plugin_list = {}
  139. plugin_list_file = os.path.join(data_path,'plugin_list.json')
  140. if os.path.exists(plugin_list_file) and not upgrade_force:
  141. plugin_list_body = public.readFile(plugin_list_file)
  142. try:
  143. plugin_list = json.loads(plugin_list_body)
  144. except:
  145. plugin_list = {}
  146. if not os.path.exists(plugin_list_file) or upgrade_force or not plugin_list:
  147. try:
  148. res = public.HttpGet(api_url)
  149. except Exception as ex:
  150. raise public.error_conn_cloud(str(ex))
  151. if not res: raise Exception(False,'云端插件列表获取失败')
  152. plugin_list = json.loads(res)
  153. if type(plugin_list)!=dict or 'list' not in plugin_list:
  154. if type(plugin_list)==str:
  155. raise Exception(plugin_list)
  156. else:
  157. raise Exception('云端插件列表获取失败')
  158. public.writeFile(plugin_list_file,json.dumps(plugin_list))
  159. return plugin_list
  160. def start_total():
  161. '''
  162. @name
  163. @return dict
  164. '''
  165. pass
  166. def get_soft_list(args):
  167. '''
  168. @name
  169. @param args<dict_obj>
  170. @return dict
  171. '''
  172. pass
  173. def db_encrypt(data):
  174. '''
  175. @name
  176. @param args<dict_obj>
  177. @return dict
  178. '''
  179. try:
  180. key = __get_db_sgin()
  181. iv = __get_db_iv()
  182. str_arr = data.split('\n')
  183. res_str = ''
  184. for data in str_arr:
  185. if not data: continue
  186. res_str += __aes_encrypt(data, key, iv)
  187. except:
  188. res_str = data
  189. result = {
  190. 'status' : True,
  191. 'msg' : res_str
  192. }
  193. return result
  194. def db_decrypt(data):
  195. '''
  196. @name
  197. @param args<dict_obj>
  198. @return dict
  199. '''
  200. try:
  201. key = __get_db_sgin()
  202. iv = __get_db_iv()
  203. str_arr = data.split('\n')
  204. res_str = ''
  205. for data in str_arr:
  206. if not data: continue
  207. res_str += __aes_decrypt(data, key, iv)
  208. except:
  209. res_str = data
  210. result = {
  211. 'status' : True,
  212. 'msg' : res_str
  213. }
  214. return result
  215. def __get_db_sgin():
  216. keystr = '3gP7+k_7lSNg3$+Fj!PKW+6$KYgHtw#R'
  217. key = ''
  218. for i in range(31):
  219. if i & 1 == 0:
  220. key += keystr[i]
  221. return key
  222. def __get_db_iv():
  223. div_file = "{}/data/div.pl".format(public.get_panel_path())
  224. if not os.path.exists(div_file):
  225. str = public.GetRandomString(16)
  226. str = __aes_encrypt_module(str)
  227. div = public.get_div(str)
  228. public.WriteFile(div_file, div)
  229. if os.path.exists(div_file):
  230. div = public.ReadFile(div_file)
  231. div = __aes_decrypt_module(div)
  232. else:
  233. keystr = '4jHCpBOFzL4*piTn^-4IHBhj-OL!fGlB'
  234. div = ''
  235. for i in range(31):
  236. if i & 1 == 0:
  237. div += keystr[i]
  238. return div
  239. def __aes_encrypt_module(data):
  240. key = 'Z2B87NEAS2BkxTrh'
  241. iv = 'WwadH66EGWpeeTT6'
  242. return __aes_encrypt(data, key, iv)
  243. def __aes_decrypt_module(data):
  244. key = 'Z2B87NEAS2BkxTrh'
  245. iv = 'WwadH66EGWpeeTT6'
  246. return __aes_decrypt(data, key, iv)
  247. def __aes_decrypt(data, key, iv):
  248. from Crypto.Cipher import AES
  249. import base64
  250. encodebytes = base64.decodebytes(data.encode('utf-8'))
  251. aes = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
  252. de_text = aes.decrypt(encodebytes)
  253. unpad = lambda s: s[0:-s[-1]]
  254. de_text = unpad(de_text)
  255. return de_text.decode('utf-8')
  256. def __aes_encrypt(data, key, iv):
  257. from Crypto.Cipher import AES
  258. import base64
  259. data = (lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode('utf-8'))(data.encode('utf-8'))
  260. aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
  261. encryptedbytes = aes.encrypt(data)
  262. en_text = base64.b64encode(encryptedbytes)
  263. return en_text.decode('utf-8')
  264. def plugin_end():
  265. '''
  266. @name
  267. @return dict
  268. '''
  269. pass
  270. def daemon_task():
  271. '''
  272. @name
  273. @return dict
  274. '''
  275. pass
  276. def daemon_panel():
  277. '''
  278. @name
  279. @return dict
  280. '''
  281. pass
  282. def flush_auth_key():
  283. '''
  284. @name
  285. @return dict
  286. '''
  287. pass
  288. def get_auth_state():
  289. '''
  290. @name
  291. @return 0. 1. 2. -1.
  292. '''
  293. try:
  294. softList = get_plugin_list()
  295. if softList['ltd'] > -1:
  296. return 2
  297. elif softList['pro'] > -1:
  298. return 1
  299. else:
  300. return 0
  301. except:
  302. return -1