http://music.163.com/#/song?id=2645757
今天才知道博客里怎么放歌
因为MaxMind不再更新v1版的GeoIP数据库,所以自己从v2的CSV文件转格式。
使用的工具是https://github.com/fffonion/geolite2legacy。
城市和ASN数据库可以从这里下载,每日更新。也可以直接使用cidr.me来查询,使用方法可以参阅这篇文章。
ASN数据来自HE BGP toolkit,可以同时查询上一级的ASN。使用的工具是https://github.com/fffonion/GeoIPASNum-Generator。
另外这个老哥也有(每月?)更新的数据库,但是IPv6+IPv4的数据库有问题,应该用的是上游的转换脚本(骗了个PR)。
注意从2019年12月30日开始,需要使用License Key下载数据库。
附更新脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#!/bin/bash D=$(dirname $(readlink -f $0)) T=$(mktemp -d) cd $T F=GeoLite2-City L=GeoLiteCityv6 # create license key from https://www.maxmind.com/en/accounts/current/license-key LICENSE=xxxxx wget "https://download.maxmind.com/app/geoip_download?edition_id=${F}-CSV&license_key=${LICENSE}&suffix=zip" -O ${F}-CSV.zip unzip ${F}-CSV.zip rm ${F}-CSV.zip cd ${F}-CSV_*/ tail -n+2 ${F}-Blocks-IPv4.csv | awk -F, '{ split($1,a,"/"); split(a[1],a1,"."); m = 96+a[2]; printf("::ffff:%02x%02x:%02x%02x/%d,%s,%s,%s,%s,%s,%s,%s,%s\n"),a1[1],a1[2],a1[3],a1[4],m,$2,$3,$4,$5,$6,$7,$8,$9}' >> ${F}-Blocks-IPv6.csv ls -lht cd $T zip ${F}-CSV.zip ${F}-CSV_*/* python $D/geolite2legacy/geolite2legacy.py -i $T/${F}-CSV.zip -o $T/GeoLiteCity.dat -f /home/wow/geolite2legacy/geoname2fips.csv python $D/geolite2legacy/geolite2legacy.py -i $T/${F}-CSV.zip -o $T/GeoLiteCityv6.dat -f /home/wow/geolite2legacy/geoname2fips.csv -6 for L in GeoLiteCityv6 GeoLiteCity; do if [[ -s $T/${L}.dat ]]; then cp $T/${L}.dat $D fi done rm -rf $T |
支持OpenSSL 1.1.x, 1.0.x和1.0.2系列
使用opm:
1 |
opm install fffonion/lua-resty-acme |
opm中没有luaossl库,所以这种安装使用的是基于FFI的Openssl后端;需要OpenResty链接了大于等于1.1版本的OpenSSL。
也可以使用luarocks安装:
1 |
luarocks install lua-resty-acme |
以/etc/openresty目录为例,如果目录不存在,请自行修改。
生成一个账户密钥
1 |
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out /etc/openresty/account.key |
生成一个默认证书
1 |
openssl req -newkey rsa:2048 -nodes -keyout /etc/openresty/default.pem -x509 -days 365 -out /etc/openresty/default.key |
在Nginx配置的http
节插入以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
resolver 8.8.8.8; lua_shared_dict acme 16m; init_by_lua_block { require("resty.acme.autossl").init({ -- setting the following to true -- implies that you read and accepted https://letsencrypt.org/repository/ tos_accepted = true, -- uncomment following for first time setup -- staging = true, -- uncomment folloing to enable RSA + ECC double cert -- domain_key_types = { 'rsa', 'ecc' }, account_key_path = "/etc/openresty/account.key", account_email = "此处填写邮箱", domain_whitelist = { "你的域名1", "你的域名2" }, }) } init_worker_by_lua_block { require("resty.acme.autossl").init_worker() } |
首次配置时,建议将init_by_lua_block中的staing = true取消注释,以防错误过多触发限流;测试通过后再加回注释使用生产API。
在需要使用证书的server
节插入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
server { server_name example.com; # required to verify Let's Encrypt API lua_ssl_trusted_certificate /etc/ssl/certs/ca-certificates.crt; lua_ssl_verify_depth 2; # fallback certs, make sure to create them before hand ssl_certificate /etc/openresty/default.pem; ssl_certificate_key /etc/openresty/default.key; ssl_certificate_by_lua_block { require("resty.acme.autossl").ssl_certificate() } location /.well-known { content_by_lua_block { require("resty.acme.autossl").serve_http_challenge() } } } |
CentOS/Fedora等系统的根证书在/etc/ssl/certs/ca-bundle.crt
,请根据实际情况修改lua_ssl_trusted_certificate。
保存后,reload nginx。
在一般情况下,domain_whitelist
必须配置,以防止恶意请求通过伪造SNI头进行拒绝服务攻击。
如果要匹配一系列域名,可以使用__index
来实现。比如下面的例子仅匹配example.com的子域名:
1 2 3 |
domain_whitelist = setmetatable({}, { __index = function(_, k) return ngx.re.match(k, [[\.example\.com$]], "jo") end}), |
将init_by_lua_block中的domain_key_types = { 'rsa', 'ecc' }
取消注释后,即可同时申请两套证书。
为了让申请到证书前的握手不出错断开,给Nginx配置默认的ECC证书
1 2 3 |
openssl ecparam -name prime256v1 -genkey -out /etc/openresty/default-ecc.key openssl req -new -sha256 -key /etc/openresty/default-ecc.key -subj "/" -out temp.csr openssl x509 -req -sha256 -days 365 -in temp.csr -signkey /etc/openresty/default-ecc.key -out /etc/openresty/default-ecc.pem |
然后在server节中原有的ssl_certificate下增加两行
1 2 |
ssl_certificate /etc/openresty/default-ecc.pem; ssl_certificate_key /etc/openresty/default-ecc.key; |
关于加密套件等的选择可借助搜索引擎。
0.5.0开始支持tls-alpn-01,可以支持在只开放443端口的环境里完成验证。方法是通过蜜汁FFI偏移找到当前请求的SSL结构,然后设置了新的ALPN。需要多个stream server多次proxy,拓扑结构如下:
1 2 3 4 5 |
[stream unix:/tmp/nginx-tls-alpn.sock ssl] Y / [stream 443] --- ALPN是acme-tls ? N \ [http unix:/tmp/nginx-default.sock ssl] |
第一个stream server打开了443端口,根据请求的ALPN分发到不同的后段;如果是acme-tls则转发到我们的库,否则转发到正常的https。
示例配置见Github。