//Websiteyouintendedtoretrieveforusers.//constupstream='www.google.com'constupstream='yourid.free.fr'//Custompathnamefortheupstreamwebsite.constupstream_path='/'//Websiteyouintendedtoretrieveforusersusingmobiledevices.//constupstream_mobile='www.google.com'constupstream_mobile='yourid.free.fr'//Countriesandregionswhereyouwishtosuspendyourservice.constblocked_region=[]//IPaddresseswhichyouwishtoblockfromusingyourservice.constblocked_ip_address=['0.0.0.0','127.0.0.1']//WhethertouseHTTPSprotocolforupstreamaddress.consthttps=false//Whethertodisablecache.constdisable_cache=false//Replacetexts.//constreplace_dict={//'$upstream':'$custom_domain',//'//google.com':''//}constreplace_dict={'$upstream':'$custom_domain',}letdata={}addEventListener('fetch',event=>{event.respondWith(fetchAndApply(event.request));})asyncfunctionfetchAndApply(request){constregion=request.headers.get('cf-ipcountry').toUpperCase();constip_address=request.headers.get('cf-connecting-ip');constuser_agent=request.headers.get('user-agent');letresponse=null;leturl=newURL(request.url);leturl_hostname=url.hostname;if(https==true){url.protocol='https:';}else{url.protocol='http:';}if(awaitdevice_status(user_agent)){varupstream_domain=upstream;}else{varupstream_domain=upstream_mobile;}url.host=upstream_domain;if(url.pathname=='/'){url.pathname=upstream_path;}else{url.pathname=upstream_path+url.pathname;}if(blocked_region.includes(region)){response=newResponse('Accessdenied:WorkersProxyisnotavailableinyourregionyet.',{status:403});}elseif(blocked_ip_address.includes(ip_address)){response=newResponse('Accessdenied:YourIPaddressisblockedbyWorkersProxy.',{status:403});}else{letmethod=request.method;letrequest_headers=request.headers;letnew_request_headers=newHeaders(request_headers);new_request_headers.set('Host',upstream_domain);new_request_headers.set('Referer',url.protocol+'//'+upstream_domain);if(method=='POST'){letorigin_formData=awaitrequest.text()data={method:method,headers:new_request_headers,body:origin_formData}console.log(data)}elseif(method=='GET'){data={method:method,headers:new_request_headers}}letfuckKeys=Object.fromEntries(new_request_headers)console.log(method)console.log(fuckKeys)letoriginal_response=awaitfetch(url.href,data)connection_upgrade=new_request_headers.get("Upgrade");if(connection_upgrade&&connection_upgrade.toLowerCase()=="websocket"){returnoriginal_response;}letoriginal_response_clone=original_response.clone();letoriginal_text=null;letresponse_headers=original_response.headers;letnew_response_headers=newHeaders(response_headers);letstatus=original_response.status;if(disable_cache){new_response_headers.set('Cache-Control','no-store');}new_response_headers.set('access-control-allow-origin','*');new_response_headers.set('access-control-allow-credentials',true);new_response_headers.delete('content-security-policy');new_response_headers.delete('content-security-policy-report-only');new_response_headers.delete('clear-site-data');if(new_response_headers.get("x-pjax-url")){new_response_headers.set("x-pjax-url",response_headers.get("x-pjax-url").replace("//"+upstream_domain,"//"+url_hostname));}constcontent_type=new_response_headers.get('content-type');if(content_type!=null&&content_type.includes('text/html')&&content_type.includes('UTF-8')){original_text=awaitreplace_response_text(original_response_clone,upstream_domain,url_hostname);}else{original_text=original_response_clone.body}response=newResponse(original_text,{status,headers:new_response_headers})}returnresponse;}asyncfunctionreplace_form_data(formData,upstream_domain,host_name){lettext=awaitresponse.text()vari,j;for(iinreplace_dict){j=replace_dict[i]if(i=='$upstream'){i=upstream_domain}elseif(i=='$custom_domain'){i=host_name}if(j=='$upstream'){j=upstream_domain}elseif(j=='$custom_domain'){j=host_name}letre=newRegExp(i,'g')text=text.replace(re,j);}returntext;}asyncfunctionreplace_response_text(response,upstream_domain,host_name){lettext=awaitresponse.text()vari,j;for(iinreplace_dict){j=replace_dict[i]if(i=='$upstream'){i=upstream_domain}elseif(i=='$custom_domain'){i=host_name}if(j=='$upstream'){j=upstream_domain}elseif(j=='$custom_domain'){j=host_name}letre=newRegExp(i,'g')text=text.replace(re,j);}returntext;}asyncfunctiondevice_status(user_agent_info){varagents=["Android","iPhone","SymbianOS","WindowsPhone","iPad","iPod"];varflag=true;for(varv=0;v<agents.length;v++){if(user_agent_info.indexOf(agents[v])>0){flag=false;break;}}returnflag;}
教程