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。