Skip to content

How to set up and use Cloudflare workers

lymanjre edited this page May 20, 2023 · 10 revisions

Lang_Farsi  فارسی          

How to set up and use Cloudflare workers

Workers is one of Cloudflare's famous CDN services, which is actually a server-less service, through which programming codes can be executed without the need for server or infrastructure configuration. In fact, this service is a type of cloud computing based on SaaS.

In other words, the working method of Workers is that instead of trying to open your website (here Hidify panel) directly; You send requests to workers and then workers re-send requests to your domain and server.

The purpose here is to hide the domain behind the workers.

How to use a Workers?

To use a Worker you need to have an active domain on the Cloudflare.

Domain and subdomain registration on Cloudflare

Here you need a purchased domain and you need to register it in the Cloudflare. If you have any ambiguity about the domain and how it is registered; Read this.

Sign in to your account in your account according to the description of how to register your domain.

Enter the registered domain and register a new subdomain in the DNS section.

There is no need for the proxy to be ON. The Worker works with both proxy modes (ON and OFF).

After registering a sub domain, you must change certificate settings.

Setting up domain certificate

Set the certificate setting of your domain to Full.

Creating Worker service

Go to the Worker section on your dashboard page on Cloudflare.

Then select the Create a Service option.

Here you can choose the name of your Worker service. Cloudflare also offers you a name. You can change it, but note that this name should be unique.

Picture

The Starter option should be also on HTTP Handler. Finally, by selecting the Create service, your Worker service will be created.

Then you need to click the Quick Edit button to put your favorite code in the Worker.

Imgage

On the editing page of the Worker, wipe the default codes on the left side.

Image

Then put the following code in its place.

addEventListener(
   "fetch", event => {
       
       const ip = event.request.headers.get('cf-connecting-ip') || event.request.headers.get('x-forwarded-for') || (event.request.socket && event.request.socket.remoteAddress);
       let url = new URL(event.request.url);
       const worker_domain=url.hostname;
       url.hostname = "sub.domain.com";                        
       url.protocol = event.request.headers.get('x-forwarded-proto') || "https";
       let request = new Request(url, event.request);
       if (ip)
        request.headers.set('cf-connecting-ip', ip);
        request.headers.set('Host', worker_domain);
       event.respondWith(
           fetch(request)
       )
   }
)

Note:

  • In the sixth line, you must put the registered domain for the url.hostname value. That is, for example, you have registered the subdomain sub.domain.com in Cloudflare according to the description of the first step; Here you need to put that domain for the url.hostname value.

Image

Click the Save and deploy button.

A very important note: Do not worry if an error is displayed in the section that displays the result of the code execution (shown in the upper right of the image) at the stage of saving the code. Save the code and continue.

On the workers page, copy your workers address without https. For example, like this:

Image

This step was completed successfully. Now you have to register the address of your Workers service on Hiddify panel.

Workers setting up on Hiddify

Go to the Domains menu and click Create.

Image

Make the settings according to the picture above and save it.

Note: You can use CDN and AutoCDN modes

Work is finished. A CDN/AutoCDN domain with the details of your workers has been added to your previous domains and you can use its connections.

Important point: Workers in the free plan only processes 100,000 requests per day, so this service is useful for those who do not have high traffic on their server.

Adding the configurations of Workers to the domain related to subscription links

As you probably know, in Hiddify you can put subscription links on a separate domain and add other domains that are set for connections to this domain. The advantage of this is that the domain of subscription links will always be available and will not be filtered because this way separates connections from subscription links.

Well, suppose the domain of our subscription links that you have already registered is t1.hiddify.com. Go to the settings related to this domain and in the Show Configs of Domains field, check the new domain that you added in the previous step for Workers as shown below.

Finally, if you go to the users' page and click on the user link as shown below,

You will see that the new configs related to Workers have been added to the previous configs.

This wiki page is moved here. Please find latest updated articles there.

این ویکی به اینجا منتقل شده است. لطفا آخرین آپدیت‌ها و مقاله‌ها را آنجا دنبال کنید.

Clone this wiki locally