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.

399 lines
12 KiB

2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
7 months ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
12 months ago
11 months ago
12 months ago
11 months ago
12 months ago
1 year ago
2 years ago
1 year ago
12 months ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
12 months ago
1 year ago
12 months ago
11 months ago
2 years ago
12 months ago
11 months ago
2 years ago
12 months ago
12 months ago
12 months ago
12 months ago
1 year ago
3 weeks ago
1 year ago
7 months ago
1 year ago
7 months ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year ago
2 years ago
1 year 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_module(filename: str):
  127. '''
  128. @name
  129. @param filename<string>
  130. @return object
  131. '''
  132. if not filename: return None
  133. if filename[0:2] == './':
  134. return public.returnMsg(False,'不能是相对路径')
  135. if not public.path_safe_check(filename):
  136. return public.returnMsg(False,'模块路径不合法')
  137. if not os.path.exists(filename):
  138. return public.returnMsg(False,'模块文件不存在' % filename)
  139. def_object = public.get_script_object(filename)
  140. if not def_object: return public.returnMsg(False,'模块[%s]不存在' % filename)
  141. return def_object.main()
  142. def get_plugin_list(upgrade_force = False):
  143. '''
  144. @name
  145. @param upgrade_force<bool>
  146. @return dict
  147. '''
  148. api_root_url = 'https://api.bt.cn'
  149. api_url = api_root_url+ '/panel/get_plugin_list'
  150. panel_path = public.get_panel_path()
  151. data_path = os.path.join(panel_path,'data')
  152. if not os.path.exists(data_path):
  153. os.makedirs(data_path,384)
  154. plugin_list = {}
  155. plugin_list_file = os.path.join(data_path,'plugin_list.json')
  156. if os.path.exists(plugin_list_file) and not upgrade_force:
  157. plugin_list_body = public.readFile(plugin_list_file)
  158. try:
  159. plugin_list = json.loads(plugin_list_body)
  160. except:
  161. plugin_list = {}
  162. if not os.path.exists(plugin_list_file) or upgrade_force or not plugin_list:
  163. try:
  164. res = public.HttpGet(api_url)
  165. except Exception as ex:
  166. raise public.error_conn_cloud(str(ex))
  167. if not res: raise Exception(False,'云端插件列表获取失败')
  168. plugin_list = json.loads(res)
  169. if type(plugin_list)!=dict or 'list' not in plugin_list:
  170. if type(plugin_list)==str:
  171. raise Exception(plugin_list)
  172. else:
  173. raise Exception('云端插件列表获取失败')
  174. content = json.dumps(plugin_list)
  175. public.writeFile(plugin_list_file,content)
  176. plugin_bin_file = os.path.join(data_path,'plugin_bin.pl')
  177. encode_content = __encode_plugin_list(content)
  178. if encode_content:
  179. public.writeFile(plugin_bin_file,encode_content)
  180. return plugin_list
  181. def __encode_plugin_list(content):
  182. try:
  183. userInfo = public.get_user_info()
  184. if not userInfo or 'serverid' not in userInfo: return None
  185. block_size = 51200
  186. uid = str(userInfo['uid'])
  187. server_id = userInfo['serverid']
  188. key = server_id[10:26] + uid + server_id
  189. key = hashlib.md5(key.encode()).hexdigest()
  190. iv = key + server_id
  191. iv = hashlib.md5(iv.encode()).hexdigest()
  192. key = key[8:24]
  193. iv = iv[8:24]
  194. blocks = [content[i:i + block_size] for i in range(0, len(content), block_size)]
  195. encrypted_content = ''
  196. for block in blocks:
  197. encrypted_content += __aes_encrypt(block, key, iv) + '\n'
  198. return encrypted_content
  199. except:
  200. pass
  201. return None
  202. def start_total():
  203. '''
  204. @name
  205. @return dict
  206. '''
  207. pass
  208. def get_soft_list(args):
  209. '''
  210. @name
  211. @param args<dict_obj>
  212. @return dict
  213. '''
  214. pass
  215. def db_encrypt(data):
  216. '''
  217. @name
  218. @param args<dict_obj>
  219. @return dict
  220. '''
  221. try:
  222. key = __get_db_sgin()
  223. iv = __get_db_iv()
  224. str_arr = data.split('\n')
  225. res_str = ''
  226. for data in str_arr:
  227. if not data: continue
  228. res_str += __aes_encrypt(data, key, iv)
  229. except:
  230. res_str = data
  231. result = {
  232. 'status' : True,
  233. 'msg' : res_str
  234. }
  235. return result
  236. def db_decrypt(data):
  237. '''
  238. @name
  239. @param args<dict_obj>
  240. @return dict
  241. '''
  242. try:
  243. key = __get_db_sgin()
  244. iv = __get_db_iv()
  245. str_arr = data.split('\n')
  246. res_str = ''
  247. for data in str_arr:
  248. if not data: continue
  249. res_str += __aes_decrypt(data, key, iv)
  250. except:
  251. res_str = data
  252. result = {
  253. 'status' : True,
  254. 'msg' : res_str
  255. }
  256. return result
  257. def __get_db_sgin():
  258. keystr = '3gP7+k_7lSNg3$+Fj!PKW+6$KYgHtw#R'
  259. key = ''
  260. for i in range(31):
  261. if i & 1 == 0:
  262. key += keystr[i]
  263. return key
  264. def __get_db_iv():
  265. div_file = "{}/data/div.pl".format(public.get_panel_path())
  266. if not os.path.exists(div_file):
  267. str = public.GetRandomString(16)
  268. str = __aes_encrypt_module(str)
  269. div = public.get_div(str)
  270. public.WriteFile(div_file, div)
  271. if os.path.exists(div_file):
  272. div = public.ReadFile(div_file)
  273. div = __aes_decrypt_module(div)
  274. else:
  275. keystr = '4jHCpBOFzL4*piTn^-4IHBhj-OL!fGlB'
  276. div = ''
  277. for i in range(31):
  278. if i & 1 == 0:
  279. div += keystr[i]
  280. return div
  281. def __aes_encrypt_module(data):
  282. key = 'Z2B87NEAS2BkxTrh'
  283. iv = 'WwadH66EGWpeeTT6'
  284. return __aes_encrypt(data, key, iv)
  285. def __aes_decrypt_module(data):
  286. key = 'Z2B87NEAS2BkxTrh'
  287. iv = 'WwadH66EGWpeeTT6'
  288. return __aes_decrypt(data, key, iv)
  289. def __aes_decrypt(data, key, iv):
  290. from Crypto.Cipher import AES
  291. import base64
  292. encodebytes = base64.decodebytes(data.encode('utf-8'))
  293. aes = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
  294. de_text = aes.decrypt(encodebytes)
  295. unpad = lambda s: s[0:-s[-1]]
  296. de_text = unpad(de_text)
  297. return de_text.decode('utf-8')
  298. def __aes_encrypt(data, key, iv):
  299. from Crypto.Cipher import AES
  300. import base64
  301. data = (lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode('utf-8'))(data.encode('utf-8'))
  302. aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
  303. encryptedbytes = aes.encrypt(data)
  304. en_text = base64.b64encode(encryptedbytes)
  305. return en_text.decode('utf-8')
  306. def plugin_end():
  307. '''
  308. @name
  309. @return dict
  310. '''
  311. pass
  312. def daemon_task():
  313. '''
  314. @name
  315. @return dict
  316. '''
  317. pass
  318. def daemon_panel():
  319. '''
  320. @name
  321. @return dict
  322. '''
  323. pass
  324. def flush_auth_key():
  325. '''
  326. @name
  327. @return dict
  328. '''
  329. pass
  330. def get_auth_state():
  331. '''
  332. @name
  333. @return 0. 1. 2. -1.
  334. '''
  335. try:
  336. softList = get_plugin_list()
  337. if softList['ltd'] > -1:
  338. return 2
  339. elif softList['pro'] > -1:
  340. return 1
  341. else:
  342. return 0
  343. except:
  344. return -1