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.

217 lines
7.4 KiB

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