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.

360 lines
11 KiB

1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
3 weeks ago
1 month ago
1 year ago
1 month ago
3 weeks ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
3 weeks ago
1 year ago
3 weeks ago
1 year ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
1 year ago
1 month ago
  1. #coding: utf-8
  2. # +-------------------------------------------------------------------
  3. # | 宝塔Linux面板
  4. # +-------------------------------------------------------------------
  5. # | Copyright (c) 2015-2099 宝塔软件(http://bt.cn) All rights reserved.
  6. # +-------------------------------------------------------------------
  7. # | Author: hwliang <[email protected]>
  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 '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 '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:
  93. # 新模块目录
  94. if model_index in ['mod']:
  95. _name = "{}Mod".format(module_name.split('/')[1])
  96. module_file = os.path.join(panel_path,'mod','project',module_name + 'Mod.py')
  97. elif model_index:
  98. # 旧模块目录
  99. _name = "{}Model".format(module_name)
  100. module_file = os.path.join(class_path,model_index+"Model",module_name + 'Model.py')
  101. else:
  102. _name = "{}Model".format(module_name)
  103. module_file = os.path.join(class_path,"projectModel",module_name + 'Model.py')
  104. else:
  105. # 如果没指定模块名称,则遍历所有模块目录
  106. module_list = get_module_list()
  107. for name in module_list:
  108. module_file = os.path.join(class_path,name,module_name + 'Model.py')
  109. if os.path.exists(module_file):
  110. _name = "{}Model".format(module_name)
  111. break
  112. # 判断模块入口文件是否存在
  113. if not os.path.exists(module_file):
  114. return public.returnMsg(False,'模块[%s]不存在' % module_name)
  115. # 判断模块路径是否合法
  116. if not public.path_safe_check(module_file):
  117. return public.returnMsg(False,'模块路径不合法')
  118. public.sys_path_append(os.path.dirname(module_file))
  119. # 引用模块入口文件
  120. module_main = __import__(_name)
  121. # 检查模块是否符合规范
  122. if not hasattr(module_main,'main'):
  123. return public.returnMsg(False,'指定模块入口文件不符合规范')
  124. # 实例化模块类
  125. module_obj = getattr(module_main,'main')()
  126. # 检查方法是否存在
  127. if not hasattr(module_obj,def_name):
  128. return public.returnMsg(False,'在[%s]模块中找不到[%s]方法' % (module_name,def_name))
  129. if 'module_get_object' in args and args.module_get_object == 1:
  130. return getattr(module_obj,def_name)
  131. # 执行方法
  132. return getattr(module_obj,def_name)(args)
  133. def get_plugin_list(upgrade_force = False):
  134. '''
  135. @name
  136. @param upgrade_force<bool>
  137. @return dict
  138. '''
  139. api_root_url = 'https://api.bt.cn'
  140. api_url = api_root_url+ '/panel/get_plugin_list'
  141. panel_path = public.get_panel_path()
  142. data_path = os.path.join(panel_path,'data')
  143. if not os.path.exists(data_path):
  144. os.makedirs(data_path,384)
  145. plugin_list = {}
  146. plugin_list_file = os.path.join(data_path,'plugin_list.json')
  147. if os.path.exists(plugin_list_file) and not upgrade_force:
  148. plugin_list_body = public.readFile(plugin_list_file)
  149. try:
  150. plugin_list = json.loads(plugin_list_body)
  151. except:
  152. plugin_list = {}
  153. if not os.path.exists(plugin_list_file) or upgrade_force or not plugin_list:
  154. try:
  155. res = public.HttpGet(api_url)
  156. except Exception as ex:
  157. raise public.error_conn_cloud(str(ex))
  158. if not res: raise Exception(False,'云端插件列表获取失败')
  159. plugin_list = json.loads(res)
  160. if type(plugin_list)!=dict or 'list' not in plugin_list:
  161. if type(plugin_list)==str:
  162. raise Exception(plugin_list)
  163. else:
  164. raise Exception('云端插件列表获取失败')
  165. public.writeFile(plugin_list_file,json.dumps(plugin_list))
  166. return plugin_list
  167. def start_total():
  168. '''
  169. @name
  170. @return dict
  171. '''
  172. pass
  173. def get_soft_list(args):
  174. '''
  175. @name
  176. @param args<dict_obj>
  177. @return dict
  178. '''
  179. pass
  180. def db_encrypt(data):
  181. '''
  182. @name
  183. @param args<dict_obj>
  184. @return dict
  185. '''
  186. try:
  187. key = __get_db_sgin()
  188. iv = __get_db_iv()
  189. str_arr = data.split('\n')
  190. res_str = ''
  191. for data in str_arr:
  192. if not data: continue
  193. res_str += __aes_encrypt(data, key, iv)
  194. except:
  195. res_str = data
  196. result = {
  197. 'status' : True,
  198. 'msg' : res_str
  199. }
  200. return result
  201. def db_decrypt(data):
  202. '''
  203. @name
  204. @param args<dict_obj>
  205. @return dict
  206. '''
  207. try:
  208. key = __get_db_sgin()
  209. iv = __get_db_iv()
  210. str_arr = data.split('\n')
  211. res_str = ''
  212. for data in str_arr:
  213. if not data: continue
  214. res_str += __aes_decrypt(data, key, iv)
  215. except:
  216. res_str = data
  217. result = {
  218. 'status' : True,
  219. 'msg' : res_str
  220. }
  221. return result
  222. def __get_db_sgin():
  223. keystr = '3gP7+k_7lSNg3$+Fj!PKW+6$KYgHtw#R'
  224. key = ''
  225. for i in range(31):
  226. if i & 1 == 0:
  227. key += keystr[i]
  228. return key
  229. def __get_db_iv():
  230. div_file = "{}/data/div.pl".format(public.get_panel_path())
  231. if not os.path.exists(div_file):
  232. str = public.GetRandomString(16)
  233. str = __aes_encrypt_module(str)
  234. div = public.get_div(str)
  235. public.WriteFile(div_file, div)
  236. if os.path.exists(div_file):
  237. div = public.ReadFile(div_file)
  238. div = __aes_decrypt_module(div)
  239. else:
  240. keystr = '4jHCpBOFzL4*piTn^-4IHBhj-OL!fGlB'
  241. div = ''
  242. for i in range(31):
  243. if i & 1 == 0:
  244. div += keystr[i]
  245. return div
  246. def __aes_encrypt_module(data):
  247. key = 'Z2B87NEAS2BkxTrh'
  248. iv = 'WwadH66EGWpeeTT6'
  249. return __aes_encrypt(data, key, iv)
  250. def __aes_decrypt_module(data):
  251. key = 'Z2B87NEAS2BkxTrh'
  252. iv = 'WwadH66EGWpeeTT6'
  253. return __aes_decrypt(data, key, iv)
  254. def __aes_decrypt(data, key, iv):
  255. from Crypto.Cipher import AES
  256. import base64
  257. encodebytes = base64.decodebytes(data.encode('utf-8'))
  258. aes = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
  259. de_text = aes.decrypt(encodebytes)
  260. unpad = lambda s: s[0:-s[-1]]
  261. de_text = unpad(de_text)
  262. return de_text.decode('utf-8')
  263. def __aes_encrypt(data, key, iv):
  264. from Crypto.Cipher import AES
  265. import base64
  266. data = (lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode('utf-8'))(data.encode('utf-8'))
  267. aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
  268. encryptedbytes = aes.encrypt(data)
  269. en_text = base64.b64encode(encryptedbytes)
  270. return en_text.decode('utf-8')
  271. def plugin_end():
  272. '''
  273. @name
  274. @return dict
  275. '''
  276. pass
  277. def daemon_task():
  278. '''
  279. @name
  280. @return dict
  281. '''
  282. pass
  283. def daemon_panel():
  284. '''
  285. @name
  286. @return dict
  287. '''
  288. pass
  289. def flush_auth_key():
  290. '''
  291. @name
  292. @return dict
  293. '''
  294. pass
  295. def get_auth_state():
  296. '''
  297. @name
  298. @return 0. 1. 2. -1.
  299. '''
  300. try:
  301. softList = get_plugin_list()
  302. if softList['ltd'] > -1:
  303. return 2
  304. elif softList['pro'] > -1:
  305. return 1
  306. else:
  307. return 0
  308. except:
  309. return -1