home.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : index.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. import json
  7. import ujson
  8. import os
  9. import re
  10. from flask import Blueprint,abort,render_template,render_template_string,url_for,redirect,make_response,send_from_directory,request
  11. from controllers.service import storage_service,rules_service,parse_service
  12. from controllers.classes import getClasses,getClassInfo
  13. from utils.files import getPics,custom_merge,getAlist,get_live_url,get_multi_rules,getCustonDict
  14. from js.rules import getRules,getPys
  15. from utils.encode import parseText,base64Encode,base64Decode
  16. from base.R import R
  17. from utils.system import getHost,is_linux
  18. from utils.cfg import cfg
  19. from utils import parser
  20. from utils.ua import time,get_interval
  21. from utils.log import logger
  22. from utils.update import getLocalVer,getHotSuggest
  23. from js.rules import getJxs
  24. import random
  25. from utils.web import getParmas,verfy_token
  26. import functools
  27. home = Blueprint("home", __name__,static_folder='/static')
  28. @home.route('/')
  29. def forbidden(): # put application's code here
  30. abort(403)
  31. @home.route('/favicon.ico') # 设置icon
  32. def favicon():
  33. # return home.send_static_file('img/favicon.svg')
  34. return redirect('/static/img/favicon.svg')
  35. # 对于当前文件所在路径,比如这里是static下的favicon.ico
  36. # return send_from_directory(os.path.join(app.root_path, 'static'), 'img/favicon.svg', mimetype='image/vnd.microsoft.icon')
  37. @home.route('/index')
  38. def index():
  39. sup_port = cfg.get('SUP_PORT', 9001)
  40. lsg = storage_service()
  41. pid_url = lsg.getItem('PID_URL')
  42. manager0 = ':'.join(getHost(0).split(':')[0:2])
  43. manager1 = ':'.join(getHost(1).split(':')[0:2])
  44. manager2 = pid_url or ':'.join(getHost(2).split(':')[0:2]).replace('https','http')
  45. if sup_port:
  46. manager0 += f':{sup_port}'
  47. manager1 += f':{sup_port}'
  48. if not pid_url:
  49. manager2 += f':{sup_port}'
  50. # print(manager2)
  51. ver = getLocalVer()
  52. return render_template('index.html',ver=ver,getHost=getHost,manager0=manager0,manager1=manager1,manager2=manager2,is_linux=is_linux())
  53. @home.route('/rules/clear')
  54. def rules_to_clear():
  55. return render_template('rules_to_clear.html',rules=getRules(),classes=getClasses())
  56. @home.route('/rules/view')
  57. def rules_to_view():
  58. return render_template('rules_to_view.html',rules=getRules(),classes=getClasses())
  59. @home.route('/pics')
  60. def random_pics():
  61. id = getParmas('id')
  62. # print(f'id:{id}')
  63. pics = getPics()
  64. # print(pics)
  65. new_conf = cfg
  66. lsg = storage_service()
  67. store_conf_dict = lsg.getStoreConfDict()
  68. new_conf.update(store_conf_dict)
  69. if not new_conf.WALL_PAPER and len(pics) > 0:
  70. if id and f'images/{id}.jpg' in pics:
  71. pic = f'images/{id}.jpg'
  72. else:
  73. pic = random.choice(pics)
  74. file = open(pic, "rb").read()
  75. response = make_response(file)
  76. response.headers['Content-Type'] = 'image/jpeg'
  77. return response
  78. else:
  79. return redirect(new_conf.WALL_PAPER)
  80. @home.route('/clear')
  81. def clear_rule():
  82. rule = getParmas('rule')
  83. if not rule:
  84. return R.failed('规则字段必填')
  85. cache_path = os.path.abspath(f'cache/{rule}.js')
  86. if not os.path.exists(cache_path):
  87. return R.failed('服务端没有此规则的缓存文件!'+cache_path)
  88. os.remove(cache_path)
  89. return R.success('成功删除文件:'+cache_path)
  90. @home.route("/plugin/<name>",methods=['GET'])
  91. def plugin(name):
  92. # name=道长影视模板.js
  93. if not name or not name.split('.')[-1] in ['js','txt','py','json']:
  94. return R.failed(f'非法猥亵,未指定文件名。必须包含js|txt|json|py')
  95. try:
  96. return parser.toJs(name)
  97. except Exception as e:
  98. return R.failed(f'非法猥亵\n{e}')
  99. @home.route('/files/<name>')
  100. def get_files(name):
  101. base_path = 'base/files'
  102. os.makedirs(base_path,exist_ok=True)
  103. file_path = os.path.join(base_path, f'{name}')
  104. if not os.path.exists(file_path):
  105. return R.failed(f'{file_path}文件不存在')
  106. with open(file_path, mode='rb') as f:
  107. file_byte = f.read()
  108. response = make_response(file_byte)
  109. filename = name
  110. response.headers['Content-Type'] = 'application/octet-stream'
  111. response.headers['Content-Disposition'] = f'attachment;filename="{filename}"'
  112. return response
  113. @home.route('/txt/<path:filename>')
  114. def custom_static_txt(filename):
  115. # 自定义静态目录 {{ url_for('custom_static',filename='help.txt')}}
  116. # print(filename)
  117. return send_from_directory('txt', filename)
  118. @home.route('/libs/<path:filename>')
  119. def custom_static_libs(filename):
  120. # 自定义静态目录 {{ url_for('custom_static',filename='help.txt')}}
  121. # print(filename)
  122. return send_from_directory('libs', filename)
  123. @home.route('/js/<path:filename>')
  124. def custom_static_js(filename):
  125. # 自定义静态目录 {{ url_for('custom_static',filename='help.txt')}}
  126. # print(filename)
  127. return send_from_directory('js', filename)
  128. # @home.route('/txt/<name>')
  129. # def get_txt_files(name):
  130. # base_path = 'txt'
  131. # os.makedirs(base_path,exist_ok=True)
  132. # file_path = os.path.join(base_path, f'{name}')
  133. # if not os.path.exists(file_path):
  134. # return R.failed(f'{file_path}文件不存在')
  135. #
  136. # with open(file_path, mode='r',encoding='utf-8') as f:
  137. # file_byte = f.read()
  138. # response = make_response(file_byte)
  139. # response.headers['Content-Type'] = 'text/plain; charset=utf-8'
  140. # return response
  141. @home.route('/lives')
  142. def get_lives():
  143. # ?path=base/live.txt
  144. path = getParmas('path')
  145. live_path = path or 'base/直播.txt'
  146. if not re.search('(txt|json|conf)$',live_path,re.M|re.S) or not re.search('^(txt|base)',live_path,re.M|re.S):
  147. abort(403)
  148. if not os.path.exists(live_path):
  149. # with open(live_path,mode='w+',encoding='utf-8') as f:
  150. # f.write('')
  151. return ''
  152. with open(live_path,encoding='utf-8') as f:
  153. live_text = f.read()
  154. if len(live_text) > 100 and live_text.find('http') < 0:
  155. try:
  156. live_text = base64Decode(live_text)
  157. logger.info(f'{path} base64解码完毕')
  158. except Exception as e:
  159. logger.info(f'{path} base64解码失败:{e}')
  160. response = make_response(live_text)
  161. response.headers['Content-Type'] = 'text/plain; charset=utf-8'
  162. return response
  163. @home.route('/liveslib')
  164. def get_liveslib():
  165. live_path = 'js/custom_spider.jar'
  166. if not os.path.exists(live_path):
  167. with open(live_path,mode='w+',encoding='utf-8') as f:
  168. f.write('')
  169. with open(live_path,mode='rb') as f:
  170. live_text = f.read()
  171. response = make_response(live_text)
  172. filename = 'custom_spider.jar'
  173. response.headers['Content-Type'] = 'application/octet-stream'
  174. response.headers['Content-Disposition'] = f'attachment;filename="{filename}"'
  175. return response
  176. @home.route('/hotsugg')
  177. def get_hot_search():
  178. s_from = getParmas('from')
  179. size = getParmas('size')
  180. data = getHotSuggest(s_from,size)
  181. return R.success('获取成功',data)
  182. def merged_hide(merged_config):
  183. t1 = time()
  184. store_rule = rules_service()
  185. hide_rules = store_rule.getHideRules()
  186. hide_rule_names = list(map(lambda x: x['name'], hide_rules))
  187. # print(hide_rule_names)
  188. all_cnt = len(merged_config['sites'])
  189. def filter_show(x):
  190. name = x['api'].split('rule=')[1].split('&')[0] if 'rule=' in x['api'] else x['key'].replace('dr_','')
  191. # print(name)
  192. if not str(x['key']).startswith('dr_') and name == 'drpy':
  193. name = x['key']
  194. return name not in hide_rule_names
  195. merged_config['sites'] = list(filter(filter_show, merged_config['sites']))
  196. logger.info(f'数据库筛选隐藏规则耗时{get_interval(t1)}毫秒,共计{all_cnt}条规则,隐藏后可渲染{len(merged_config["sites"])}条规则')
  197. @home.route('/config/<int:mode>')
  198. def config_render(mode):
  199. # print(dict(app.config))
  200. tt = time()
  201. UA = request.headers['User-Agent']
  202. ver = getParmas('ver')
  203. logger.info(f'ver:{ver},UA:{UA}')
  204. if ver not in ['1','2']:
  205. ISTVB = 'okhttp/3' in UA
  206. elif ver == '1':
  207. ISTVB = False
  208. elif ver == '2':
  209. ISTVB = True
  210. # print(ISTVB)
  211. if mode == 1:
  212. jyw_ip = getHost(mode)
  213. logger.info(jyw_ip)
  214. new_conf = cfg
  215. lsg = storage_service()
  216. store_conf_dict = lsg.getStoreConfDict()
  217. new_conf.update(store_conf_dict)
  218. # print(new_conf)
  219. # print(type(new_conf),new_conf)
  220. host = getHost(mode)
  221. # ali_token = lsg.getItem('ALI_TOKEN')
  222. ali_token = new_conf.ALI_TOKEN
  223. xr_mode = new_conf.XR_MODE
  224. js_proxy = new_conf.JS_PROXY
  225. js0_password = new_conf.JS0_PASSWORD
  226. js_mode = int(new_conf.JS_MODE or 0)
  227. print(f'{type(js_mode)} jsmode:{js_mode}')
  228. # print(ali_token)
  229. customConfig = getCustonDict(host,ali_token,js0_password)
  230. # print(customConfig)
  231. jxs = getJxs(host=host)
  232. use_py = lsg.getItem('USE_PY')
  233. pys = getPys() if use_py else []
  234. # print(pys)
  235. alists = getAlist()
  236. alists_str = json.dumps(alists, ensure_ascii=False)
  237. live_url = get_live_url(new_conf,mode)
  238. rules = getRules('js',js_mode)
  239. rules = get_multi_rules(rules)
  240. # html = render_template('config.txt',rules=getRules('js'),host=host,mode=mode,jxs=jxs,base64Encode=base64Encode,config=new_conf)
  241. html = render_template('config.txt',js0_password=js0_password,UA=UA,xr_mode=xr_mode,ISTVB=ISTVB,pys=pys,rules=rules,host=host,mode=mode,js_mode=js_mode,jxs=jxs,alists=alists,alists_str=alists_str,live_url=live_url,config=new_conf)
  242. merged_config = custom_merge(parseText(html),customConfig)
  243. # print(merged_config['sites'])
  244. merged_hide(merged_config)
  245. # response = make_response(html)
  246. # print(len(merged_config['sites']))
  247. print(merged_config['sites'])
  248. merged_config['sites'] = sort_sites_by_order(merged_config['sites'],js_mode)
  249. # print(merged_config['parses'])
  250. parses = sort_parses_by_order(merged_config['parses'],host)
  251. # print(parses)
  252. merged_config['parses'] = parses
  253. config_text = json.dumps(merged_config,ensure_ascii=False,indent=1)
  254. # 依赖代理逻辑修改,改为admin/view去动态代理
  255. # if js_proxy:
  256. # # print('js_proxy:',js_proxy)
  257. # if '=>' in js_proxy:
  258. # oldsrc = js_proxy.split('=>')[0]
  259. # newsrc = js_proxy.split('=>')[1]
  260. # print(f'js1源代理已启用,全局替换{oldsrc}为{newsrc}')
  261. # config_text = config_text.replace(oldsrc,newsrc)
  262. response = make_response(config_text)
  263. # response = make_response(str(merged_config))
  264. response.headers['Content-Type'] = 'application/json; charset=utf-8'
  265. logger.info(f'自动生成动态配置共计耗时:{get_interval(tt)}毫秒')
  266. return response
  267. def comp(x, y):
  268. if x['order'] > y['order']:
  269. return 1
  270. elif x['order'] < y['order']:
  271. return - 1
  272. else:
  273. if x['write_date'] < y['write_date']:
  274. return 1
  275. elif x['write_date'] > y['write_date']:
  276. return -1
  277. else:
  278. return 0
  279. def sort_sites_by_order(sites,js_mode=0):
  280. rules = rules_service()
  281. rule_list = rules.query_all()
  282. # print(rule_list)
  283. rule_names = list(map(lambda x: x['name'], rule_list))
  284. # print(rule_names)
  285. # print(sites)
  286. for i in range(len(sites)):
  287. # sites[i]['id'] = i+1
  288. site_name = sites[i]['api'].split('rule=')[1].split('&')[0] if 'rule=' in sites[i]['api'] else sites[i]['key']
  289. if js_mode and str(site_name).startswith('dr'):
  290. site_name = site_name.replace('dr_','')
  291. if not str(sites[i]['key']).startswith('dr_') and site_name == 'drpy':
  292. site_name = sites[i]['key']
  293. # print(sites[i])
  294. # print(site_name)
  295. if site_name in rule_names:
  296. site_rule = rule_list[rule_names.index(site_name)]
  297. sites[i]['state'] = 1 if site_rule['state'] is None else site_rule['state']
  298. sites[i]['order'] = 0 if site_rule['order'] is None else site_rule['order']
  299. sites[i]['write_date'] = 0 if site_rule['write_date'] is None else site_rule['write_date'].timestamp()
  300. else:
  301. sites[i]['state'] = 1
  302. sites[i]['order'] = 0
  303. sites[i]['write_date'] = 0
  304. # sites[i]['site_name'] = site_name
  305. # print(sites)
  306. # sites.sort(key=lambda x: x['order'], reverse=False)
  307. sites.sort(key=functools.cmp_to_key(comp), reverse=False)
  308. # print(sites)
  309. for site in sites:
  310. del site['state']
  311. del site['order']
  312. del site['write_date']
  313. return sites
  314. def sort_parses_by_order(parses,host):
  315. t1 = time()
  316. parse = parse_service()
  317. parse_list = parse.query_all()
  318. parse_url_list = list(map(lambda x: x['url'], parse_list))
  319. new_parses = []
  320. new_parses_url = []
  321. for i in range(len(parses)):
  322. # parses[i]['id'] = i + 1
  323. # 去重
  324. if parses[i]['url'] in new_parses_url:
  325. # print(f"重复的解析:{parses[i]['name']},{parses[i]['url']}")
  326. continue
  327. if str(parses[i]['url']).startswith(host):
  328. parses[i]['url'] = parses[i]['url'].replace(host,'')
  329. if parses[i]['url'] in parse_url_list:
  330. parse_rule = parse_list[parse_url_list.index(parses[i]['url'])]
  331. parses[i]['state'] = 1 if parse_rule['state'] is None else parse_rule['state']
  332. parses[i]['order'] = 0 if parse_rule['order'] is None else parse_rule['order']
  333. parses[i]['write_date'] = 0 if parse_rule['write_date'] is None else parse_rule['write_date'].timestamp()
  334. else:
  335. parses[i]['state'] = 1
  336. parses[i]['order'] = 0
  337. parses[i]['write_date'] = 0
  338. if not parses[i].get('header'):
  339. parses[i]['header'] = {'User-Agent': 'Mozilla/5.0'}
  340. if str(parses[i]['url']).startswith('/'):
  341. parses[i]['url'] = host + parses[i]['url']
  342. new_parses.append(parses[i])
  343. new_parses_url.append(parses[i]['url'])
  344. new_parses.sort(key=functools.cmp_to_key(comp), reverse=False)
  345. # print(sites)
  346. for par in new_parses:
  347. del par['state']
  348. del par['order']
  349. del par['write_date']
  350. # print(new_parses)
  351. logger.info(f'{len(new_parses)}/{len(parses)}条解析解析排序耗时:{get_interval(t1)}毫秒')
  352. return new_parses
  353. @home.route('/configs')
  354. def config_gen():
  355. if not verfy_token():
  356. return R.failed('请登录后再试')
  357. # 生成文件
  358. os.makedirs('txt',exist_ok=True)
  359. new_conf = cfg
  360. lsg = storage_service()
  361. store_conf_dict = lsg.getStoreConfDict()
  362. new_conf.update(store_conf_dict)
  363. try:
  364. use_py = lsg.getItem('USE_PY')
  365. js_mode = int(new_conf.JS_MODE or 0)
  366. js0_password = new_conf.JS0_PASSWORD
  367. pys = getPys() if use_py else False
  368. alists = getAlist()
  369. alists_str = json.dumps(alists,ensure_ascii=False)
  370. rules = getRules('js',js_mode)
  371. rules = get_multi_rules(rules)
  372. host0 = getHost(0)
  373. jxs = getJxs(host=host0)
  374. set_local = render_template('config.txt',js0_password=js0_password,pys=pys,rules=rules,alists=alists,alists_str=alists_str,live_url=get_live_url(new_conf,0),mode=0,js_mode=js_mode,host=host0,jxs=jxs)
  375. # print(set_local)
  376. host1 = getHost(1)
  377. jxs = getJxs(host=host1)
  378. set_area = render_template('config.txt',js0_password=js0_password,pys=pys,rules=rules,alists=alists,alists_str=alists_str,live_url=get_live_url(new_conf,1),mode=1,js_mode=js_mode,host=host1,jxs=jxs)
  379. host2 = getHost(2) or host1
  380. # print('远程地址:'+host2)
  381. jxs = getJxs(host=host2)
  382. set_online = render_template('config.txt',js0_password=js0_password,pys=pys,rules=rules,alists=alists,alists_str=alists_str,live_url=get_live_url(new_conf,2),mode=1,js_mode=js_mode,host=host2,jxs=jxs)
  383. ali_token = new_conf.ALI_TOKEN
  384. # parses = []
  385. with open('txt/pycms0.json','w+',encoding='utf-8') as f:
  386. customConfig = getCustonDict(host0,ali_token,js0_password)
  387. set_dict = custom_merge(parseText(set_local), customConfig)
  388. merged_hide(set_dict)
  389. set_dict['sites'] = sort_sites_by_order(set_dict['sites'], js_mode)
  390. # if not parses:
  391. # print('生成静态配置时初始化排序parses')
  392. # parses = sort_parses_by_order(set_dict['parses'])
  393. # set_dict['parses'] = parses
  394. set_dict['parses'] = sort_parses_by_order(set_dict['parses'],host0)
  395. # set_dict = json.loads(set_local)
  396. f.write(json.dumps(set_dict,ensure_ascii=False,indent=4))
  397. with open('txt/pycms1.json','w+',encoding='utf-8') as f:
  398. customConfig = getCustonDict(host1,ali_token,js0_password)
  399. set_dict = custom_merge(parseText(set_area), customConfig)
  400. merged_hide(set_dict)
  401. set_dict['sites'] = sort_sites_by_order(set_dict['sites'], js_mode)
  402. set_dict['parses'] = sort_parses_by_order(set_dict['parses'],host1)
  403. # set_dict = json.loads(set_area)
  404. f.write(json.dumps(set_dict,ensure_ascii=False,indent=4))
  405. with open('txt/pycms2.json','w+',encoding='utf-8') as f:
  406. customConfig = getCustonDict(host2,ali_token,js0_password)
  407. set_dict = custom_merge(parseText(set_online), customConfig)
  408. merged_hide(set_dict)
  409. set_dict['sites'] = sort_sites_by_order(set_dict['sites'], js_mode)
  410. set_dict['parses'] = sort_parses_by_order(set_dict['parses'],host2)
  411. # set_dict = json.loads(set_online)
  412. f.write(json.dumps(set_dict,ensure_ascii=False,indent=4))
  413. files = [os.path.abspath(rf'txt\pycms{i}.json') for i in range(3)]
  414. # print(files)
  415. return R.success('猫配置生成完毕,文件位置在:\n'+'\n'.join(files))
  416. except Exception as e:
  417. return R.failed(f'配置文件生成错误:\n{e}')
  418. @home.route("/info",methods=['get'])
  419. def info_all():
  420. data = storage_service.query_all()
  421. return R.ok(data=data)