使用CloudFlare搭建反向代理
CloudFlare,互联网大善人!
FREE套餐CloudFlare每日提供 10w 请求的免费额度,个人用完全足够,但并不能保证很多人同时使用,强烈建议仅自用
@@ -258,9 +258,12 @@
拓展
IP白名单 将下面的IP白名单 换成你自己的服务器IP即可,极大的保证了不被滥用以及稳定不被墙
1 2 3 4 5 6 7 const allowedIPs = [ '123.123.123.123' , '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ];
-
通用反代 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 addEventListener ('fetch' , event => { event.respondWith (handleRequest (event.request )); }); const specialCases = { "*" : { "Origin" : "DELETE" , "Referer" : "DELETE" } }; const allowedIPs = [ '123.123.123.123' , '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ]; function handleSpecialCases (request ) { const rules = specialCases["*" ]; for (const [key, value] of Object .entries (rules)) { switch (value) { case "KEEP" : break ; case "DELETE" : request.headers .delete (key); break ; default : request.headers .set (key, value); break ; } } } async function handleRequest (request ) { const url = new URL (request.url ); const userIP = request.headers .get ('CF-Connecting-IP' ) || request.headers .get ('X-Forwarded-For' ) || request.connection .remoteAddress ; if (!allowedIPs.includes (userIP)) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Caused by playing Genshin Impact</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } if (url.pathname === "/" ) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Why don't you play Genshin Impact?</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } if (url.pathname .startsWith ("/" )) { let targetUrl = url.pathname .slice (1 ); if (!/^https?:\/\// .test (targetUrl)) { targetUrl = "https://" + targetUrl; } const modifiedRequest = new Request (targetUrl, { headers : request.headers , method : request.method , body : request.body , redirect : 'follow' }); handleSpecialCases (modifiedRequest); const response = await fetch (modifiedRequest); const modifiedResponse = new Response (response.body , response); modifiedResponse.headers .set ('Access-Control-Allow-Origin' , '*' ); return modifiedResponse; } return new Response ("Path not found" , { status : 404 }); }
+
通用反代 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 addEventListener ('fetch' , event => { event.respondWith (handleRequest (event.request )); }); const specialCases = { "*" : { "Origin" : "DELETE" , "Referer" : "DELETE" } }; const allowedIPs = [ '123.123.123.123' , '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ]; function handleSpecialCases (request ) { const rules = specialCases["*" ]; for (const [key, value] of Object .entries (rules)) { switch (value) { case "KEEP" : break ; case "DELETE" : request.headers .delete (key); break ; default : request.headers .set (key, value); break ; } } } async function handleRequest (request ) { const url = new URL (request.url ); const userIP = request.headers .get ('CF-Connecting-IP' ) || request.headers .get ('X-Forwarded-For' ) || request.connection .remoteAddress ; if (!allowedIPs.includes (userIP)) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Caused by playing Genshin Impact</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } if (url.pathname === "/" ) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Why don't you play Genshin Impact?</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } if (url.pathname .startsWith ("/" )) { let targetUrl = url.pathname .slice (1 ); if (!/^https?:\/\// .test (targetUrl)) { targetUrl = "https://" + targetUrl; } const modifiedRequest = new Request (targetUrl, { headers : request.headers , method : request.method , body : request.body , redirect : 'follow' }); handleSpecialCases (modifiedRequest); const response = await fetch (modifiedRequest); const modifiedResponse = new Response (response.body , response); modifiedResponse.headers .set ('Access-Control-Allow-Origin' , '*' ); return modifiedResponse; } return new Response ("Path not found" , { status : 404 }); }
-
指定反代 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 addEventListener ('fetch' , event => { event.respondWith (handleRequest (event.request )); }); const domainMappings = { "/steam-store" : "https://store.steampowered.com" , "/steam-api" : "https://api.steampowered.com" }; const specialCases = { "*" : { "Origin" : "DELETE" , "Referer" : "DELETE" } }; const allowedIPs = [ '123.123.123.123' , '2001:0db8:85a3:0000:0000:8a2e:0370:7334' ]; function handleSpecialCases (request ) { const rules = specialCases["*" ]; for (const [key, value] of Object .entries (rules)) { switch (value) { case "KEEP" : break ; case "DELETE" : request.headers .delete (key); break ; default : request.headers .set (key, value); break ; } } } function getUserIP (request ) { return request.headers .get ('CF-Connecting-IP' ) || request.headers .get ('X-Forwarded-For' ) || request.connection .remoteAddress ; } async function handleRequest (request ) { const url = new URL (request.url ); const userIP = getUserIP (request); if (!allowedIPs.includes (userIP)) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Caused by playing Genshin Impact</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } if (url.pathname === "/" ) { return new Response (` <html> <head> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; margin: 0; background-color: #f9f9f9; } h1 { font-family: Arial, sans-serif; color: #333; text-align: center; } </style> </head> <body> <h1>Why don't you play Genshin Impact?</h1> </body> </html> ` , { headers : { "Content-Type" : "text/html" } }); } const basePath = Object .keys (domainMappings).find (path => url.pathname .startsWith (path)); if (!basePath) { return new Response ("Path not found in domain mappings" , { status : 404 }); } const targetBase = domainMappings[basePath]; const targetPath = url.pathname .slice (basePath.length ) + url.search + url.hash ; const targetUrl = new URL (targetPath, targetBase); const modifiedRequest = new Request (targetUrl, { headers : request.headers , method : request.method , body : request.body , redirect : 'follow' }); handleSpecialCases (modifiedRequest); const response = await fetch (modifiedRequest); const modifiedResponse = new Response (response.body , response); modifiedResponse.headers .set ('Access-Control-Allow-Origin' , '*' ); return modifiedResponse; }
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 MapleLeaf ! 赞助
wechat
alipay