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.

213 lines
7.2 KiB

1 year ago
  1. #coding: utf-8
  2. import public,os,sys,json
  3. #获取插件列表(0/1)
  4. def get_plugin_list(force = 0):
  5. api_root_url = 'https://api.bt.cn'
  6. api_url = api_root_url+ '/panel/get_plugin_list'
  7. cache_file = 'data/plugin_list.json'
  8. if force==0 and os.path.exists(cache_file):
  9. jsonData = public.readFile(cache_file)
  10. softList = json.loads(jsonData)
  11. else:
  12. try:
  13. jsonData = public.HttpGet(api_url)
  14. except Exception as ex:
  15. raise public.error_conn_cloud(str(ex))
  16. softList = json.loads(jsonData)
  17. if type(softList)!=dict or 'list' not in softList: raise Exception('云端插件列表获取失败')
  18. public.writeFile(cache_file, jsonData)
  19. return softList
  20. #获取授权状态() 返回:0.免费版 1.专业版 2.企业版 -1.获取失败
  21. def get_auth_state():
  22. try:
  23. softList = get_plugin_list()
  24. if softList['ltd'] > -1:
  25. return 2
  26. elif softList['pro'] > -1:
  27. return 1
  28. else:
  29. return 0
  30. except:
  31. return -1
  32. #执行插件方法(插件名,方法名,参数)
  33. def plugin_run(plugin_name, def_name, args):
  34. if not plugin_name or not def_name: return public.returnMsg(False,'插件名称和插件方法名称不能为空!')
  35. if not path_check(plugin_name) or not path_check(def_name): return public.returnMsg(False,'插件名或方法名不能包含特殊符号!')
  36. p_path = public.get_plugin_path(plugin_name)
  37. if not os.path.exists(p_path + '/index.php') and not os.path.exists(p_path + '/%s_main.py' % plugin_name): return public.returnMsg(False,'插件不存在!')
  38. is_php = os.path.exists(p_path + '/index.php')
  39. if not is_php:
  40. public.package_path_append(p_path)
  41. plugin_main = __import__(plugin_name + '_main')
  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. plu = eval('plugin_main.' + plugin_name + '_main()')
  51. if not hasattr(plu, def_name):
  52. return public.returnMsg(False,'在[%s]插件中找不到[%s]方法' % (plugin_name,def_name))
  53. if 'plugin_get_object' in args and args.plugin_get_object == 1:
  54. if not is_php:
  55. return getattr(plu, def_name)
  56. else:
  57. return None
  58. else:
  59. if not is_php:
  60. data = eval('plu.' + def_name + '(args)')
  61. else:
  62. import panelPHP
  63. args.s = def_name
  64. args.name = plugin_name
  65. data = panelPHP.panelPHP(plugin_name).exec_php_script(args)
  66. return data
  67. #执行模块方法(模块名,方法名,参数)
  68. def module_run(mod_name, def_name, args):
  69. if not mod_name or not def_name: return public.returnMsg(False,'模块名称和模块方法名称不能为空!')
  70. if not path_check(mod_name) or not path_check(def_name): return public.returnMsg(False,'模块名或方法名不能包含特殊符号!')
  71. if 'model_index' in args:
  72. if args.model_index:
  73. mod_file = "{}/{}Model/{}Model.py".format(public.get_class_path(),args.model_index,mod_name)
  74. else:
  75. mod_file = "{}/projectModel/{}Model.py".format(public.get_class_path(),mod_name)
  76. else:
  77. module_list = get_module_list()
  78. for module_dir in module_list:
  79. mod_file = "{}/{}/{}Model.py".format(public.get_class_path(),module_dir,mod_name)
  80. if os.path.exists(mod_file): break
  81. if not os.path.exists(mod_file):
  82. return public.returnMsg(False,'模块[%s]不存在' % mod_name)
  83. def_object = public.get_script_object(mod_file)
  84. if not def_object: return public.returnMsg(False,'模块[%s]不存在!' % mod_name)
  85. try:
  86. run_object = getattr(def_object.main(),def_name,None)
  87. except:
  88. return public.returnMsg(False,'模块入口实例化失败' % mod_name)
  89. if not run_object: return public.returnMsg(False,'在[%s]模块中找不到[%s]方法' % (mod_name,def_name))
  90. if 'module_get_object' in args and args.module_get_object == 1:
  91. return run_object
  92. result = run_object(args)
  93. return result
  94. #获取模块文件夹列表
  95. def get_module_list():
  96. list = []
  97. class_path = public.get_class_path()
  98. f_list = os.listdir(class_path)
  99. for fname in f_list:
  100. f_path = class_path+'/'+fname
  101. if os.path.isdir(f_path) and len(fname) > 6 and fname.find('.') == -1 and fname.find('Model') != -1:
  102. list.append(fname)
  103. return list
  104. #检查路径是否合法
  105. def path_check(path):
  106. list = ["./","..",",",";",":","?","'","\"","<",">","|","\\","\n","\r","\t","\b","\a","\f","\v","*","%","&","$","#","@","!","~","`","^","(",")","+","=","{","}","[","]"]
  107. for i in path:
  108. if i in list:
  109. return False
  110. return True
  111. #数据加密
  112. def db_encrypt(data):
  113. try:
  114. key = __get_db_sgin()
  115. iv = __get_db_iv()
  116. str_arr = data.split('\n')
  117. res_str = ''
  118. for data in str_arr:
  119. if not data: continue
  120. res_str += __aes_encrypt(data, key, iv)
  121. except:
  122. res_str = data
  123. result = {
  124. 'status' : True,
  125. 'msg' : res_str
  126. }
  127. return result
  128. #数据解密
  129. def db_decrypt(data):
  130. try:
  131. key = __get_db_sgin()
  132. iv = __get_db_iv()
  133. str_arr = data.split('\n')
  134. res_str = ''
  135. for data in str_arr:
  136. if not data: continue
  137. res_str += __aes_decrypt(data, key, iv)
  138. except:
  139. res_str = data
  140. result = {
  141. 'status' : True,
  142. 'msg' : res_str
  143. }
  144. return result
  145. def __get_db_sgin():
  146. keystr = '3gP7+k_7lSNg3$+Fj!PKW+6$KYgHtw#R'
  147. key = ''
  148. for i in range(31):
  149. if i & 1 == 0:
  150. key += keystr[i]
  151. return key
  152. def __get_db_iv():
  153. div_file = "{}/data/div.pl".format(public.get_panel_path())
  154. if not os.path.exists(div_file):
  155. str = public.GetRandomString(16)
  156. str = __aes_encrypt_module(str)
  157. div = public.get_div(str)
  158. public.WriteFile(div_file, div)
  159. if os.path.exists(div_file):
  160. div = public.ReadFile(div_file)
  161. div = __aes_decrypt_module(div)
  162. else:
  163. keystr = '4jHCpBOFzL4*piTn^-4IHBhj-OL!fGlB'
  164. div = ''
  165. for i in range(31):
  166. if i & 1 == 0:
  167. div += keystr[i]
  168. return div
  169. def __aes_encrypt_module(data):
  170. key = 'Z2B87NEAS2BkxTrh'
  171. iv = 'WwadH66EGWpeeTT6'
  172. return __aes_encrypt(data, key, iv)
  173. def __aes_decrypt_module(data):
  174. key = 'Z2B87NEAS2BkxTrh'
  175. iv = 'WwadH66EGWpeeTT6'
  176. return __aes_decrypt(data, key, iv)
  177. def __aes_decrypt(data, key, iv):
  178. from Crypto.Cipher import AES
  179. import base64
  180. encodebytes = base64.decodebytes(data.encode('utf-8'))
  181. aes = AES.new(key.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
  182. de_text = aes.decrypt(encodebytes)
  183. unpad = lambda s: s[0:-s[-1]]
  184. de_text = unpad(de_text)
  185. return de_text.decode('utf-8')
  186. def __aes_encrypt(data, key, iv):
  187. from Crypto.Cipher import AES
  188. import base64
  189. data = (lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16).encode('utf-8'))(data.encode('utf-8'))
  190. aes = AES.new(key.encode('utf8'), AES.MODE_CBC, iv.encode('utf8'))
  191. encryptedbytes = aes.encrypt(data)
  192. en_text = base64.b64encode(encryptedbytes)
  193. return en_text.decode('utf-8')