file.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import path from "path";
  2. import {readFileSync, existsSync} from 'fs';
  3. import {fileURLToPath} from "url";
  4. import '../libs_drpy/jinja.js'
  5. export function getParsesDict(host) {
  6. const __filename = fileURLToPath(import.meta.url);
  7. const __dirname = path.dirname(__filename);
  8. const jx_conf = path.join(__dirname, '../config/parses.conf');
  9. let jx_list = [];
  10. if (existsSync(jx_conf)) {
  11. const jx_conf_text = readFileSync(jx_conf, 'utf-8');
  12. let jx_conf_content = jx_conf_text.trim();
  13. let var_dict = {host, hostName: host.split(':').length > 1 ? host.slice(0, host.lastIndexOf(":")) : host};
  14. // console.log(var_dict);
  15. jx_conf_content = jinja.render(jx_conf_content, var_dict);
  16. const jxs = jx_conf_content.split('\n').filter(it => it.trim() && !it.trim().startsWith('#')).map(it => it.trim());
  17. // console.log(jxs);
  18. jxs.forEach((jx) => {
  19. let jx_arr = jx.split(',');
  20. let jx_name = jx_arr[0];
  21. let jx_url = jx_arr[1];
  22. let jx_type = jx_arr.length > 2 ? Number(jx_arr[2]) || 0 : 0;
  23. let jx_ua = jx_arr.length > 3 ? jx_arr[3] : 'Mozilla/5.0';
  24. let jx_flag = jx_arr.length > 4 ? jx_arr[4] : '';
  25. let jx_obj = {
  26. 'name': jx_name,
  27. 'url': jx_url,
  28. 'type': jx_type,
  29. "header": {
  30. "User-Agent": jx_ua
  31. },
  32. }
  33. if (jx_flag) {
  34. jx_obj.ext = {
  35. "flag": jx_flag.split('|')
  36. }
  37. }
  38. jx_list.push(jx_obj);
  39. });
  40. }
  41. // console.log('getParsesDict:', jx_conf);
  42. // console.log(jx_list);
  43. return jx_list
  44. }