Restrict content to certain hostnames

We are looking to switch CDN from AWS to Cloudflare. We want to know if this scenario would be possible with CloudFlare:

We need to restrict access to files only to certain domain names. There are about 2K domain names that should be allowed access to the files and other than the said domain names the request should be blocked.

From a connection perspective, how is Cloudflare to know which domain is making a connection? My observation is that it’s IP address-based.

by checking the referrer value in the header?

That’s certainly an easy way to block casual attempts to access that content.

With a list of 2000 hostnames, that’s going to be pretty difficult. Firewall rules can do this, but each rule is limited to 4K characters, and you only get 5 Firewall Rules.

But if you’re trying to use Cloudflare just as a CDN, you’ll be in violation of ToS 2.8.