2020年8月

由MaxMind提供,有ASN,国家与市三种类型,支持IPv4与IPv6,使用mmdb或CSV格式分发。
注意:自2019年12月30起,需要注册账号下载。
https://dev.maxmind.com/geoip/geoip2/geolite2/

Python的使用

安装官方提供的模块:

pip3 install geoip2

示例:

import geoip2.database
gi = geoip2.database.Reader('GeoLite2-Country.mmdb')
gn = gi.country('1.1.1.1')
#city,country,asnisp
#print(gn)
print(gn.country.names['zh-CN'])
print(gn.country.iso_code)
#输出:
澳大利亚
AU

Nginx的使用

Nginx需要编译Geoip2模块(有Geoip模块,但只适用旧版的dat格式)。
https://github.com/leev/ngx_http_geoip2_module

(http)
geoip2 /etc/nginx/geoip2/GeoLite2-Country.mmdb {
    #自动重载
    auto_reload 5m;
    $geoip2_metadata_country_build metadata build_epoch;
    #国家代码
    $geoip2_country_code default=US country iso_code;
    #国家名称
    #$geoip2_country_name country names zh-CN;
}
if($geoip2_country_code != CN) {
    deny all;
}

注意替换example.com与证书路径!
配置如下:

server {
    listen [::]:443;
    listen 443;
    server_name duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    sub_filter_types text/html text/css application/x-javascript text/xml;
    access_log off;

    location / {
        proxy_pass https://duckduckgo.com;
        proxy_set_header Host duckduckgo.com;
        proxy_set_header Accept-Encoding "";
        proxy_set_header Referer "";
        sub_filter "duckduckgo.com" "duckgo.example.com";
        sub_filter_once off;
    }
}

server {
    listen [::]:443;
    listen 443;
    server_name images.duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    access_log off;

    location / {
        proxy_pass https://images.duckduckgo.com;
        proxy_set_header Host images.duckduckgo.com;
    }
}

server {
    listen [::]:443;
    listen 443;
    server_name icons.duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    access_log off;

    location / {
        proxy_pass https://icons.duckduckgo.com;
        proxy_set_header Host icons.duckduckgo.com;
    }
}

server {
    listen [::]:443;
    listen 443;
    server_name ac.duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    access_log off;

    location / {
        proxy_pass https://ac.duckduckgo.com;
        proxy_set_header Host ac.duckduckgo.com;
    }
}

server {
    listen [::]:443;
    listen 443;
    server_name external-content.duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    access_log off;

    location / {
        proxy_pass https://external-content.duckduckgo.com;
        proxy_set_header Host external-content.duckduckgo.com;
    }
}
server {
    listen [::]:443;
    listen 443;
    server_name improving.duckgo.example.com;
    ssl on;
    ssl_certificate /root/ssl_cert/duck.crt;
    ssl_certificate_key /root/ssl_cert/duck.key;
    access_log off;

    location / {
        proxy_pass https://improving.duckduckgo.com;
        proxy_set_header Host improving.duckduckgo.com;
    }
}

引入

<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/lrsjng.jquery-qrcode/0.18.0/jquery-qrcode.min.js"></script>

快速使用

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" name="viewport" content="width=device-width,initial-scale=1" />
    <title>QRCode</title>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/lrsjng.jquery-qrcode/0.18.0/jquery-qrcode.min.js"></script>
</head>
<body>
    <div id="qrcode_demo"></div>
    <script>
        $(function(){
            $('#qrcode_demo').qrcode({text:"https://blog.yeziruo.cn/"});
        });
    </script>
</body>
</html>

参数

render: "table" 或 "canvas"(默认)
text: 文本内容
width: 256 宽
height: 256 高
background: "#ffffff" 背景颜色
foreground: "#66ccff" 前景颜色
typeNumber: -1 计算模式(暂不清楚作用)
correctLevel: QRErrorCorrectLevel.H 纠错等级(不建议指定,否则将不会渲染)
    QRErrorCorrectLevel.L,  (7%)
    QRErrorCorrectLevel.M, (15%)
    QRErrorCorrectLevel.Q, (25%)
    QRErrorCorrectLevel.H, (30%)

中文支持

使用该插件生成二维码,如果内容包含中文,则需要将Unicode(UTF-16)转为UTF-8。

function utf16to8(str) {
    var out, i, len, c;
    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
        }
    }
    return out;
}