Created
July 15, 2020 04:08
-
-
Save miladoll/8fcc9658031783d44ae2a52fcae0c362 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
: <<__WHAT_IS_THIS__ | |
Cloudflareの公開情報から以下のファイルを作成する。 | |
/etc/cron.daily/ に置いてね | |
* /etc/nginx/conf.d/00-trusted_proxies.conf | |
Cloudflareからのアクセスの場合、 | |
X-Cloudflare-Proxy-IP から | |
リモートIPアドレスをHTTPヘッダに引き継がせる | |
* /etc/nginx/conf.d/50-geo_cloudflare.conf | |
Cloudflareからのアクセスの場合 1 を返すgeo。 | |
location コンテキストで | |
if ( $cloudflare = 0 ) { return 403; } | |
などとして使う | |
http コンテキストで、 | |
ほかの設定ファイルより先に読み込ませるのを推奨 | |
__WHAT_IS_THIS__ | |
set -e | |
cloudflare_ip_ann=( \ | |
'https://www.cloudflare.com/ips-v4' | |
'https://www.cloudflare.com/ips-v6' | |
) | |
CURL='curl -s' | |
list='' | |
for url in "${cloudflare_ip_ann[@]}" ; | |
do | |
cur_list=$($CURL "$url") | |
list=$( sed '/^$/d' <<_EOF_listadd | |
$list | |
$cur_list | |
_EOF_listadd | |
) | |
done | |
YYYYMMDDHHMM=$(date +'# %Y/%m/%d+%H:%M') | |
trusted_proxies_list=$( \ | |
sed -e 's|^\(.*\)$|set_real_ip_from \1;|' \ | |
<<_EOF_ls | |
$list | |
_EOF_ls | |
echo 'real_ip_header X-Cloudflare-Proxy-IP;' | |
echo "$YYYYMMDDHHMM" | |
) | |
indented_list=$( \ | |
sed -e 's|^\(.*\)$| \1 1;|' \ | |
<<_EOF_indent | |
$list | |
_EOF_indent | |
) | |
allowed_geo_list=$( \ | |
cat <<_EOF_wrap | |
geo \$realip_remote_addr \$cloudflare { | |
default 0; | |
$indented_list | |
$YYYYMMDDHHMM | |
} | |
_EOF_wrap | |
) | |
conf_file="/etc/nginx/conf.d/00-trusted_proxies.conf" | |
temp_conf_file="$conf_file.$$" | |
cat <<_EOF_ > $temp_conf_file | |
$trusted_proxies_list | |
_EOF_ | |
mv $temp_conf_file $conf_file | |
conf_file="/etc/nginx/conf.d/50-geo_cloudflare.conf" | |
temp_conf_file="$conf_file.$$" | |
cat <<_EOF_ > $temp_conf_file | |
$allowed_geo_list | |
_EOF_ | |
mv $temp_conf_file $conf_file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment