pbing.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. async function goUrl(request, url) {
  69. let fp = {
  70. method: request.method
  71. };
  72. fp.headers = new Headers(request.headers);
  73. for(var i = 2; i < arguments.length-1; i=i+2){
  74. fp.headers[arguments[i]] = arguments[i+1];
  75. }
  76. return await fetch(url, fp);
  77. }
  78. export default {
  79. /**
  80. * fetch
  81. * @param {Request} request
  82. * @param {*} env
  83. * @param {*} ctx
  84. * @returns
  85. */
  86. async fetch(request, env, ctx) {
  87. const url = request.url;
  88. let iSlash = url.indexOf('/',11);
  89. let nUrl = url.substring(iSlash+1);
  90. let iColon = nUrl.indexOf(':',3)+3;
  91. if(!nUrl.startsWith('syndey.bing.com',iColon) &&
  92. !nUrl.startsWith('www.bing.com/turing',iColon))
  93. return await goUrl(request, nUrl);
  94. const targetUrl = new URL(nUrl);
  95. const newHeaders = new Headers();
  96. request.headers.forEach((value, key) => {
  97. // console.log(`old : ${key} : ${value}`);
  98. if (KEEP_REQ_HEADERS.includes(key)) {
  99. newHeaders.set(key, value);
  100. }
  101. });
  102. newHeaders.set('host', targetUrl.host);
  103. newHeaders.set('origin', targetUrl.origin);
  104. newHeaders.set('referer', 'https://www.bing.com/search?q=Bing+AI');
  105. const randIP = getRandomIP();
  106. // console.log('randIP : ', randIP);
  107. newHeaders.set('X-Forwarded-For', randIP);
  108. const oldUA = request.headers.get('user-agent');
  109. const isMobile = oldUA.includes('Mobile') || oldUA.includes('Android');
  110. if (isMobile) {
  111. newHeaders.set(
  112. 'user-agent',
  113. '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'
  114. );
  115. } else {
  116. 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');
  117. }
  118. // newHeaders.forEach((value, key) => console.log(`${key} : ${value}`));
  119. const newReq = new Request(targetUrl, {
  120. method: request.method,
  121. headers: newHeaders,
  122. body: request.body,
  123. });
  124. // console.log('request url : ', newReq.url);
  125. const res = await fetch(newReq);
  126. return res;
  127. },
  128. };