bing.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const SYDNEY_ORIGIN = 'https://sydney.bing.com';
  2. const KEEP_REQ_HEADERS = [
  3. 'accept',
  4. 'accept-encoding',
  5. 'accept-language',
  6. 'connection',
  7. 'cookie',
  8. 'upgrade',
  9. 'user-agent',
  10. 'sec-websocket-extensions',
  11. 'sec-websocket-key',
  12. 'sec-websocket-version',
  13. 'x-request-id',
  14. 'content-length',
  15. 'content-type',
  16. 'access-control-request-headers',
  17. 'access-control-request-method',
  18. ];
  19. const IP_RANGE = [
  20. ['3.2.50.0', '3.5.31.255'], //192,000
  21. ['3.12.0.0', '3.23.255.255'], //786,432
  22. ['3.30.0.0', '3.33.34.255'], //205,568
  23. ['3.40.0.0', '3.63.255.255'], //1,572,864
  24. ['3.80.0.0', '3.95.255.255'], //1,048,576
  25. ['3.100.0.0', '3.103.255.255'], //262,144
  26. ['3.116.0.0', '3.119.255.255'], //262,144
  27. ['3.128.0.0', '3.247.255.255'], //7,864,320
  28. ];
  29. /**
  30. * 随机整数 [min,max)
  31. * @param {number} min
  32. * @param {number} max
  33. * @returns
  34. */
  35. const getRandomInt = (min, max) => Math.floor(Math.random() * (max - min)) + min;
  36. /**
  37. * ip 转 int
  38. * @param {string} ip
  39. * @returns
  40. */
  41. const ipToInt = (ip) => {
  42. const ipArr = ip.split('.');
  43. let result = 0;
  44. result += +ipArr[0] << 24;
  45. result += +ipArr[1] << 16;
  46. result += +ipArr[2] << 8;
  47. result += +ipArr[3];
  48. return result;
  49. };
  50. /**
  51. * int 转 ip
  52. * @param {number} intIP
  53. * @returns
  54. */
  55. const intToIp = (intIP) => {
  56. return `${(intIP >> 24) & 255}.${(intIP >> 16) & 255}.${(intIP >> 8) & 255}.${intIP & 255}`;
  57. };
  58. const getRandomIP = () => {
  59. const randIndex = getRandomInt(0, IP_RANGE.length);
  60. const startIp = IP_RANGE[randIndex][0];
  61. const endIp = IP_RANGE[randIndex][1];
  62. const startIPInt = ipToInt(startIp);
  63. const endIPInt = ipToInt(endIp);
  64. const randomInt = getRandomInt(startIPInt, endIPInt);
  65. const randomIP = intToIp(randomInt);
  66. return randomIP;
  67. };
  68. export default {
  69. /**
  70. * fetch
  71. * @param {Request} request
  72. * @param {*} env
  73. * @param {*} ctx
  74. * @returns
  75. */
  76. async fetch(request, env, ctx) {
  77. const currentUrl = new URL(request.url);
  78. const targetUrl = new URL(SYDNEY_ORIGIN + currentUrl.pathname + currentUrl.search);
  79. const newHeaders = new Headers();
  80. request.headers.forEach((value, key) => {
  81. // console.log(`old : ${key} : ${value}`);
  82. if (KEEP_REQ_HEADERS.includes(key)) {
  83. newHeaders.set(key, value);
  84. }
  85. });
  86. newHeaders.set('host', targetUrl.host);
  87. newHeaders.set('origin', targetUrl.origin);
  88. newHeaders.set('referer', 'https://www.bing.com/search?q=Bing+AI');
  89. const randIP = getRandomIP();
  90. // console.log('randIP : ', randIP);
  91. newHeaders.set('X-Forwarded-For', randIP);
  92. const oldUA = request.headers.get('user-agent');
  93. const isMobile = oldUA.includes('Mobile') || oldUA.includes('Android');
  94. if (isMobile) {
  95. newHeaders.set(
  96. 'user-agent',
  97. 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.7 Mobile/15E148 Safari/605.1.15 BingSapphire/1.0.410427012'
  98. );
  99. } else {
  100. newHeaders.set('user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35');
  101. }
  102. // newHeaders.forEach((value, key) => console.log(`${key} : ${value}`));
  103. const newReq = new Request(targetUrl, {
  104. method: request.method,
  105. headers: newHeaders,
  106. body: request.body,
  107. });
  108. // console.log('request url : ', newReq.url);
  109. const res = await fetch(newReq);
  110. return res;
  111. },
  112. };