Is it possible to use a Cloudflare Service Worker as a proxy web server?

this is just a beginner level question looking for some advice, which the following may misuse some key terms as well because of lacking knowledge, but hopefully can deliver the key message to all of you:

Background:
my company is hosting a website in AWS, everything works fine except it cannot be loaded in China because of the well known Chinese Great Firewall that will block all unregistered/unlicensed websites outside of China; the best solution is perhaps to host a server in China and get an ICP license to be approved by the Chinese government, but that will take time and many other considerations. So we are now looking for some alternatives to let our customers from China able to read content from our site.

Main idea:
use a Cloudflare service worker to fetch HTTP resources from a given webpage first and then send the HTTP content to users by the Service Worker (since Cloudflare is accessible in China)

Example:
Let Cloudflare’s registered Service Worker URL be: sample.workers.dev?abc=123 (which is the url that my user will be hitting)
Target website content to serve: http://google.com/?abc=123
When user tries to access the service worker url (sample.workers.dev), the service worker should try to handle this request by forwarding the request to google.com and stream the HTTP response back to the user

This will work for my company’s clients because we will usually generate a url and send over to the user by email or some other means, so we can send them a third party url that is accessible in China while actually loading content from our original website.

I tried all examples given by Cloudflare already: https://developers.cloudflare.com/workers/templates/ But so far no luck to archive what I want exactly.

Any thoughts?

It is almosy your case https://developers.cloudflare.com/workers/templates/pages/rewrite_links_html/
Maybe you forgot to deploy worker on your domain and set routes?

You can’t use Cloudflare’s China network unless you’re a enterprise user and have a valid ICP license, as explained on https://www.cloudflare.com/network/china/

2 Likes

Hey there, I actually tried out this one before but I am not sure how to set up to use it properly, any details tutorials that I can go check it out? I was just using the cloudflare worker default domain like xxx.workers.dev only.

I tried by simply copying the template script but obviously I missed some extra configs like the routes and so on as you mentioned?

I think https://developers.cloudflare.com/workers/reference/apis/html-rewriter/ can be my answer, but I have no idea how to use it… anyone can provide with a template that I can learn from it?

My requirement is as illustrated above:

Example:
Let Cloudflare’s registered Service Worker URL be: sample.workers.dev?abc=123 (which is the url that my user will be hitting)
Target website content to serve: http://google.com/?abc=123
When user tries to access the service worker url (sample.workers.dev), the service worker should try to handle this request by forwarding the request to google.com and stream the HTTP response back to the user

So first, what you are describing is potentially domain fronting, which isn’t allowed by Cloudflare’s Terms and Conditions. Second, the great firewall doesn’t block all websites hosted outside of China. If you’ve managed to get blocked/run afoul of Chinese censors Cloudflare is not a circumvention tool for copyright or government restrictions.

There are legitimate reasons to rewrite resources that might otherwise be served from 3rd party URLs such as Google Fonts which might not be allowed from China by making them first party resources.

But attempting to make website A which is blocked in China front through another domain simply to evade content restrictions is potentially a great way to get the domain and account banned from Cloudflare as well.

2 Likes