Caddy Simple Image Anti-leech

When I was searching for a keyword a few days ago, I found that a post on another website was highly overlapped with an article on my website, at least 85% identical. What’s even more egregious is that the image address is also from my website, and the source was not marked, so I completely ignored it. Copyright Statement.

Frankly speaking, this kind of situation is very common in mainland China, and it may not be realistic to expect the other party to correct their evil ways. A relatively simple solution is to add a watermark or add anti-hotlink protection to pictures. Adding watermarks is ugly and is not recommended if you have no choice but to do so. Therefore, anti-hotlinking of pictures is the lowest-cost method.

1. caddyfile configuration

 1@invalid_referer {
 2file
 3path *.webp *.png
 4not header Referer https://mephisto.cc*
 5}
 6
 7rewrite @invalid_referer /images/403.png
 8
 9#respond @invalid_referer "Traffic blocked" 403 {
10     # close
11#}

As shown above, webp and png images are matched, and the Referer in the request header does not start with my site, and is redirected to a 403.png image. Reference article: https://github.com/wolfogre/blog-utterances/issues/30

In addition, there is another processing method https://github.com/Stevie-Ray/referrer-spam-blocker/blob/master/referral-spam.caddy2, which is not simple and clear enough.

Much the same, the second method directly returns the text "Traffic blocked" and status code 403, which is equivalent to saving bandwidth costs. The first method is to switch to another guide image, which helps guide the user to the correct address to access, at the expense of the image's traffic bandwidth cost.

Example of my persuasive image:

invalid referer image

The pictures were drawn using inkscape, and the instructions are very friendly, allowing little lost book children to come to my original site to communicate and learn 😀 for readers’ reference. After changing the Caddyfile, remember to reload the caddy service.

2. Confirm the result

Clear the browser's local cache, turn on incognito mode, and go to the anti-leeching website to view the results.

invalid referer example

It can be seen that this processing method is still effective for simple copy and paste behavior. If we discover later that there are many ways to bypass this simple restriction, we will consider adding a watermark.

Lastmod: Wednesday, August 30, 2023

Translations: