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.

378 lines
12 KiB

2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
1 month ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
6 months ago
2 years ago
6 months ago
6 months ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
6 months ago
6 months ago
6 months ago
5 months ago
2 years ago
6 months ago
5 months ago
2 years ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
1 month ago
6 months ago
1 month ago
6 months ago
2 years ago
6 months ago
2 years ago
6 months ago
2 years ago
6 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,hashlib
  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. content = json.dumps(plugin_list)
  159. public.writeFile(plugin_list_file,content)
  160. plugin_bin_file = os.path.join(data_path,'plugin_bin.pl')
  161. encode_content = __encode_plugin_list(content)
  162. if encode_content:
  163. public.writeFile(plugin_bin_file,encode_content)
  164. return plugin_list
  165. def __encode_plugin_list(content):
  166. try:
  167. userInfo = public.get_user_info()
  168. if not userInfo or 'serverid' not in userInfo: return None
  169. block_size = 51200
  170. uid = str(userInfo['uid'])
  171. server_id = userInfo['serverid']
  172. key = server_id[10:26] + uid + server_id
  173. key = hashlib.md5(key.encode()).hexdigest()
  174. iv = key + server_id
  175. iv = hashlib.md5(iv.encode()).hexdigest()
  176. key = key[8:24]
  177. iv = iv[8:24]
  178. blocks = [content[i:i + block_size] for i in range(0, len(content), block_size)]
  179. encrypted_content = ''
  180. for block in blocks:
  181. encrypted_content += __aes_encrypt(block, key, iv) + '\n'
  182. return encrypted_content
  183. except:
  184. pass
  185. return None
  186. def start_total():
  187. '''
  188. @name
  189. @return dict
  190. '''
  191. pass
  192. def get_soft_list(args):
  193. '''
  194. @name
  195. @param args<dict_obj>
  196. @return dict
  197. '''
  198. pass
  199. def db_encrypt(data):
  200. '''
  201. @name
  202. @param args<dict_obj>
  203. @return dict
  204. '''
  205. try:
  206. key = __get_db_sgin()
  207. iv = __get_db_iv()
  208. str_arr = data.split('\n')
  209. res_str = ''
  210. for data in str_arr:
  211. if not data: continue
  212. res_str += __aes_encrypt(data, key, iv)
  213. except:
  214. res_str = data
  215. result = {
  216. 'status' : True,
  217. 'msg' : res_str
  218. }
  219. return result
  220. def db_decrypt(data):
  221. '''
  222. @name
  223. @param args<dict_obj>
  224. @return dict
  225. '''
  226. try:
  227. key = __get_db_sgin()
  228. iv = __get_db_iv()
  229. str_arr = data.split('\n')
  230. res_str = ''
  231. for data in str_arr:
  232. if not data: continue
  233. res_str += __aes_decrypt(data, key, iv)
  234. except:
  235. res_str = data
  236. result = {
  237. 'status' : True,
  238. 'msg' : res_str
  239. }
  240. return result
  241. def __get_db_sgin():
  242. keystr = '3gP7+k_7lSNg3$+Fj!PKW+6$KYgHtw#R'
  243. key = ''
  244. for i in range(31):
  245. if i & 1 == 0:
  246. key += keystr[i]
  247. return key
  248. def __get_db_iv():
  249. div_file = "{}/data/div.pl".format(public.get_panel_path())
  250. if not os.path.exists(div_file):
  251. str = public.GetRandomString(16)
  252. str = __aes_encrypt_module(str)
  253. div = public.get_div(str)
  254. public.WriteFile(div_file, div)
  255. if os.path.exists(div_file):
  256. div = public.ReadFile(div_file)
  257. div = __aes_decrypt_module(div)
  258. else:
  259. keystr = '4jHCpBOFzL4*piTn^-4IHBhj-OL!fGlB'
  260. div = ''
  261. for i in range(31):
  262. if i & 1 == 0:
  263. div += keystr[i]
  264. return div
  265. def __aes_encrypt_module(data):
  266. key = 'Z2B87NEAS2BkxTrh'
  267. iv = 'WwadH66EGWpeeTT6'
  268. return __aes_encrypt(data, key, iv)
  269. def __aes_decrypt_module(data):
  270. key = 'Z2B87NEAS2BkxTrh'
  271. iv = 'WwadH66EGWpeeTT6'
  272. return __aes_decrypt(data, key, iv)
  273. def __aes_decrypt(data, key, iv):
  274. from Crypto.Cipher import AES
  275. import base64
  276. encodebytes = base64.decodebytes(data.encode('utf-8'))
  277. aes = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
  278. de_text = aes.decrypt(encodebytes)
  279. unpad = lambda s: s[0:-s[-1]]
  280. de_text = unpad(de_text)
  281. return de_text.decode('utf-8')
  282. def __aes_encrypt(data, key, iv):
  283. from Crypto.Cipher import AES
  284. import base64
  285. data = (lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode('utf-8'))(data.encode('utf-8'))
  286. aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
  287. encryptedbytes = aes.encrypt(data)
  288. en_text = base64.b64encode(encryptedbytes)
  289. return en_text.decode('utf-8')
  290. def plugin_end():
  291. '''
  292. @name
  293. @return dict
  294. '''
  295. pass
  296. def daemon_task():
  297. '''
  298. @name
  299. @return dict
  300. '''
  301. pass
  302. def daemon_panel():
  303. '''
  304. @name
  305. @return dict
  306. '''
  307. pass
  308. def flush_auth_key():
  309. '''
  310. @name
  311. @return dict
  312. '''
  313. pass
  314. def get_auth_state():
  315. '''
  316. @name
  317. @return 0. 1. 2. -1.
  318. '''
  319. try:
  320. softList = get_plugin_list()
  321. if softList['ltd'] > -1:
  322. return 2
  323. elif softList['pro'] > -1:
  324. return 1
  325. else:
  326. return 0
  327. except:
  328. return -1