{"id":527367,"date":"2021-08-20T16:13:57","date_gmt":"2021-08-20T08:13:57","guid":{"rendered":"http:\/\/4563.org\/?p=527367"},"modified":"2021-08-20T16:13:57","modified_gmt":"2021-08-20T08:13:57","slug":"%e6%88%91%e7%94%a8cf%e5%8f%8d%e4%bb%a3%e4%ba%86loc%e5%8f%af%e6%98%af%e5%8f%aa%e6%9c%89%e9%a6%96%e9%a1%b5%e6%80%8e%e4%b9%88%e5%8a%9e%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=527367","title":{"rendered":"\u6211\u7528cf\u53cd\u4ee3\u4e86loc,\u53ef\u662f\u53ea\u6709\u9996\u9875\u600e\u4e48\u529e\uff1f"},"content":{"rendered":"\n<p>  \t\t\t\t\t<strong>\u4e09\u5b63\u7a3b<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3>\u6211\u7528cf\u53cd\u4ee3\u4e86loc,\u53ef\u662f\u53ea\u6709\u9996\u9875\u600e\u4e48\u529e\uff1f<\/h3>\n<p>  \t\thttps:\/\/billowing-block-5b0e.zzllxa.workers.dev<\/p>\n<p>  \/\/ \u66ff\u6362\u6210\u4f60\u60f3\u955c\u50cf\u7684\u7ad9\u70b9<br \/>  const upstream = &#8216;hostloc.com&#8217;<\/p>\n<p>  \/\/ \u5982\u679c\u90a3\u4e2a\u7ad9\u70b9\u6709\u4e13\u95e8\u7684\u79fb\u52a8\u9002\u914d\u7ad9\u70b9\uff0c\u5426\u5219\u4fdd\u6301\u548c\u4e0a\u9762\u4e00\u81f4<br \/>  const upstream_mobile = &#8216;hostloc.com&#8217;<\/p>\n<p>  \/\/ \u4f60\u5e0c\u671b\u7981\u6b62\u54ea\u4e9b\u56fd\u5bb6\u8bbf\u95ee<br \/>  const blocked_region = [&#8216;CN&#8217;]<\/p>\n<p>  \/\/ \u7981\u6b62\u81ea\u8bbf\u95ee<br \/>  const blocked_ip_address = [&#8216;0.0.0.0&#8217;, &#8216;127.0.0.1&#8217;]<\/p>\n<p>  \/\/ \u66ff\u6362\u6210\u4f60\u60f3\u955c\u50cf\u7684\u7ad9\u70b9<br \/>  const replace_dict = {<br \/>  &nbsp; &nbsp; &#8216;$upstream&#8217;: &#8216;$custom_domain&#8217;,<br \/>  &nbsp; &nbsp; &#8216;\/\/hostloc.com&#8217;: &#8221;<br \/>  }<\/p>\n<p>  \/\/\u4ee5\u4e0b\u5185\u5bb9\u90fd\u4e0d\u7528\u52a8<br \/>  addEventListener(&#8216;fetch&#8217;, event =&gt; {<br \/>  &nbsp; &nbsp; event.respondWith(fetchAndApply(event.request));<br \/>  })<\/p>\n<p>  async function fetchAndApply(request) {<\/p>\n<p>  &nbsp; &nbsp; const region = request.headers.get(&#8216;cf-ipcountry&#8217;).toUpperCase();<br \/>  &nbsp; &nbsp; const ip_address = request.headers.get(&#8216;cf-connecting-ip&#8217;);<br \/>  &nbsp; &nbsp; const user_agent = request.headers.get(&#8216;user-agent&#8217;);<\/p>\n<p>  &nbsp; &nbsp; let response = null;<br \/>  &nbsp; &nbsp; let url = new URL(request.url);<br \/>  &nbsp; &nbsp; let url_host = url.host;<\/p>\n<p>  &nbsp; &nbsp; if (url.protocol == &#8216;http:&#8217;) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;url.protocol = &#8216;https:&#8217;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;response = Response.redirect(url.href);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;return response;<br \/>  &nbsp; &nbsp; }<\/p>\n<p>  &nbsp; &nbsp; if (await device_status(user_agent)) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;upstream_domain = upstream<br \/>  &nbsp; &nbsp; } else {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;upstream_domain = upstream_mobile<br \/>  &nbsp; &nbsp; }<\/p>\n<p>  &nbsp; &nbsp; url.host = upstream_domain;<\/p>\n<p>  &nbsp; &nbsp; if (blocked_region.includes(region)) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;response = new Response(&#8216;Access denied: WorkersProxy is not available in your region yet.&#8217;, {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;status: 403<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;});<br \/>  &nbsp; &nbsp; } else if(blocked_ip_address.includes(ip_address)){<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;response = new Response(&#8216;Access denied: Your IP address is blocked by WorkersProxy.&#8217;, {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;status: 403<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;});<br \/>  &nbsp; &nbsp; } else{<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let method = request.method;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let request_headers = request.headers;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let new_request_headers = new Headers(request_headers);<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;new_request_headers.set(&#8216;Host&#8217;, upstream_domain);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;new_request_headers.set(&#8216;Referer&#8217;, url.href);<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;let original_response = await fetch(url.href, {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;method: method,<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;headers: new_request_headers<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;})<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;let original_response_clone = original_response.clone();<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let original_text = null;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let response_headers = original_response.headers;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let new_response_headers = new Headers(response_headers);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;let status = original_response.status;<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;new_response_headers.set(&#8216;access-control-allow-origin&#8217;, &#8216;*&#8217;);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;new_response_headers.set(&#8216;access-control-allow-credentials&#8217;, true);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;new_response_headers.delete(&#8216;content-security-policy&#8217;);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;new_response_headers.delete(&#8216;content-security-policy-report-only&#8217;);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;new_response_headers.delete(&#8216;clear-site-data&#8217;);<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;const content_type = new_response_headers.get(&#8216;content-type&#8217;);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if (content_type.includes(&#8216;text\/html&#8217;) &amp;&amp; content_type.includes(&#8216;UTF-8&#8217;)) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;original_text = await replace_response_text(original_response_clone, upstream_domain, url_host);<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;} else {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;original_text = original_response_clone.body<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;}<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;response = new Response(original_text, {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;status,<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;headers: new_response_headers<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;})<br \/>  &nbsp; &nbsp; }<br \/>  &nbsp; &nbsp; return response;<br \/>  }<\/p>\n<p>  async function replace_response_text(response, upstream_domain, host_name) {<br \/>  &nbsp; &nbsp; let text = await response.text()<\/p>\n<p>  &nbsp; &nbsp; var i, j;<br \/>  &nbsp; &nbsp; for (i in replace_dict) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;j = replace_dict<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;if (i == &#8216;$upstream&#8217;) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;i = upstream_domain<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;} else if (i == &#8216;$custom_domain&#8217;) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;i = host_name<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;}<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;if (j == &#8216;$upstream&#8217;) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;j = upstream_domain<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;} else if (j == &#8216;$custom_domain&#8217;) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;j = host_name<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;}<\/p>\n<p>  &nbsp; &nbsp;&nbsp; &nbsp;let re = new RegExp(i, &#8216;g&#8217;)<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;text = text.replace(re, j);<br \/>  &nbsp; &nbsp; }<br \/>  &nbsp; &nbsp; return text;<br \/>  }<\/p>\n<p>  async function device_status (user_agent_info) {<br \/>  &nbsp; &nbsp; var agents = [&quot;Android&quot;, &quot;iPhone&quot;, &quot;SymbianOS&quot;, &quot;Windows Phone&quot;, &quot;iPad&quot;, &quot;iPod&quot;];<br \/>  &nbsp; &nbsp; var flag = true;<br \/>  &nbsp; &nbsp; for (var v = 0; v &lt; agents.length; v++) { if (user_agent_info.indexOf(agents) &gt; 0) {<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;flag = false;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;break;<br \/>  &nbsp; &nbsp;&nbsp; &nbsp;}<br \/>  &nbsp; &nbsp; }<br \/>  &nbsp; &nbsp; return flag;<br \/>  }\t\t\t\t<\/p>\n<p>  \t\t\t\t\t<strong>optimism<\/strong>  \t\t\t\t\u5927\u4f6c\u6709\u8bdd\u8bf4 : \t<\/p>\n<h3><\/h3>\n<p>  \t\t\u518d\u7814\u7a76\u4e00\u4e0b \t\t\t  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e09\u5b63\u7a3b \u5927\u4f6c\u6709\u8bdd\u8bf4 : \u6211\u7528cf\u53cd&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/527367"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=527367"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/527367\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=527367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=527367"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=527367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}