Why do you need a security chain, because you’re afraid your bike will be stolen? Now it’s all shared, so why worry?!

Anti-theft, is to prevent you from stealing my link. You have quoted my resources (pictures and audio) on your website. It is ok for you to run, but it will waste my traffic. If resources are quoted more than once, my server may not be able to support it.

Under normal circumstances to picture anti-theft chain, we also take a look at the picture anti-theft chain is how to do

Picture anti-theft chain

Let’s take a look at the picture first. After I started a local service, I loaded the picture links of baidu and 360 search websites respectively, corresponding to the appearance under anti-theft chain (what about the beautiful girl as promised).

This is the so-called picture anti-theft chain, after all, see such a picture, we have no interest, and before the picture gap is too big, there is no need to retain

So the key part of the picture is how to do anti-theft chain? And see below

Host
Referer

Now let’s directly practice, do a picture to prevent theft chain, serving

// js part const fs = require('fs');
const path = require('path');
const http = require('http');
const url = require('url');
const getHostName = function (str) {
    let { hostname } = url.parse(str);
    return hostname;
};

http.createServer((req, res) => {
    let refer = req.headers['referer'] || req.headers['referrer']; // the header of the request is lower case. // The header of the request is lower caselet { pathname } = url.parse(req.url);
    let src = path.join(__dirname, 'public'.'. '+ pathname); // SRC = fs.stat(SRC, err => {// check whether the file existsif(! err) {if(refer) {// Not all images have a sourcelet referHost = getHostName(refer);
                let host = req.headers['host'].split(':') [0];if(referHost ! // fs.createreadStream (path.join(__dirname,'public'.'./1.jpg')).pipe(res);
                } elseFs.createreadstream (SRC).pipe(res); }}elseFs.createreadstream (SRC).pipe(res); }}else {
            res.end('end'); }}); }).listen(8888);Copy the code

Through the above less than 40 lines of code to complete the image anti-theft chain, it is not hot trouble, the use of the request head to do things or quite a lot, first look at the effect of anti-theft chain

<-- HTML --> <body> <img SRC ="http://www.chenhd.me:8888/2.png" />
</body>
Copy the code

Here we modify the hosts file to specify 127.0.0.1 as two different domain names for access

  1. 127.0.0.1 www.chenhd.me
  2. 127.0.0.1 www.chd.me

Friendly tips:

  • For Windows, change the hosts file address to C:\Windos\System32\drivers\etc, copy the hosts file, modify it, and replace it
  • On a MAC operating system, you can directly modify the hosts file using the vi /etc/hosts command on the terminal

As the HTML part of our image reference address is the www.chenhd.me domain name of the image, so this is normal access, directly display 2.png image, so cool

Well, that’s all for today’s sharing. Thank you for watching