Guest User

Untitled

a guest
Oct 14th, 2020
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. let config = {
  2. basic: {
  3. upstream: 'http://45.88.179.211:8887/',
  4. mobileRedirect: 'http://45.88.179.211:8887/'
  5. },
  6.  
  7. firewall: {
  8. blockedRegion: [],
  9. blockedIPAddress: [],
  10. scrapeShield: true
  11. },
  12.  
  13. routes: {
  14. },
  15.  
  16. optimization: {
  17. cacheEverything: false,
  18. cacheTtl: 5,
  19. mirage: true,
  20. polish: 'off',
  21. minify: {
  22. javascript: true,
  23. css: true,
  24. html: true
  25. }
  26. }
  27. }
  28.  
  29. addEventListener('fetch', event => {
  30. event.respondWith(fetchAndApply(event.request));
  31. })
  32.  
  33. async function fetchAndApply(request) {
  34. const region = request.headers.get('cf-ipcountry');
  35. const ipAddress = request.headers.get('cf-connecting-ip') || '';
  36. const userAgent = request.headers.get('user-agent') || '';
  37.  
  38. if (region !== '' && config.firewall.blockedRegion.includes(region.toUpperCase())) {
  39. return new Response(
  40. 'Access denied: booster.js is not available in your region.',
  41. {
  42. status: 403
  43. }
  44. );
  45. } else if (ipAddress !== '' && config.firewall.blockedRegion.includes(ipAddress)) {
  46. return new Response(
  47. 'Access denied: Your IP address is blocked by booster.js.',
  48. {
  49. status: 403
  50. }
  51. );
  52. }
  53.  
  54. let requestURL = new URL(request.url);
  55. let upstreamURL = null;
  56.  
  57. if (userAgent && isMobile(userAgent) === true) {
  58. upstreamURL = new URL(config.basic.mobileRedirect);
  59. } else if (region && config.routes.hasOwnProperty(region.toUpperCase())) {
  60. upstreamURL = new URL(config.routes[region.toUpperCase()]);
  61. } else {
  62. upstreamURL = new URL(config.basic.upstream);
  63. }
  64.  
  65. requestURL.protocol = upstreamURL.protocol;
  66. requestURL.host = upstreamURL.host;
  67. requestURL.pathname = upstreamURL.pathname + requestURL.pathname;
  68.  
  69. let fetchedResponse = await fetch(
  70. new Request(requestURL, {
  71. cf: {
  72. cacheEverything: config.optimization.cacheEverything,
  73. cacheTtl: config.optimization.cacheTtl,
  74. mirage: config.optimization.mirage,
  75. polish: config.optimization.polish,
  76. minify: config.optimization.minify,
  77. scrapeShield: config.firewall.scrapeShield
  78. },
  79. method: request.method,
  80. headers: request.headers,
  81. body: request.body
  82. })
  83. );
  84.  
  85. let modifiedResponseHeaders = new Headers(fetchedResponse.headers);
  86. if (modifiedResponseHeaders.has("x-pjax-url")) {
  87. let pjaxURL = new URL(modifiedResponseHeaders.get("x-pjax-url"));
  88. pjaxURL.protocol = requestURL.protocol;
  89. pjaxURL.host = requestURL.host;
  90. pjaxURL.pathname = pjaxURL.path.replace(requestURL.pathname, '/')
  91.  
  92. modifiedResponseHeaders.set(
  93. "x-pjax-url",
  94. pjaxURL.href
  95. );
  96. }
  97.  
  98. return new Response(
  99. fetchedResponse.body,
  100. {
  101. headers: modifiedResponseHeaders,
  102. status: fetchedResponse.status,
  103. statusText: fetchedResponse.statusText
  104. }
  105. );
  106. }
  107.  
  108. async function isMobile(userAgent) {
  109. console.log(userAgent)
  110. let agents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
  111. for (let agent of agents) {
  112. if (userAgent.indexOf(agent) > 0) {
  113. console.log(agent)
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
Add Comment
Please, Sign In to add comment