{"id":1034,"date":"2019-11-12T10:54:37","date_gmt":"2019-11-12T19:54:37","guid":{"rendered":"\/blog\/?p=1034"},"modified":"2023-09-21T09:26:47","modified_gmt":"2023-09-21T00:26:47","slug":"iptables-geoip-%ec%a0%81%ec%9a%a9%ec%9d%84-%ec%9c%84%ed%95%9c-%ea%b5%ad%ea%b0%80%eb%b3%84-db%ec%84%a4%ec%b9%98-%ec%8a%a4%ed%81%ac%eb%a6%bd%ed%8a%b8","status":"publish","type":"post","link":"https:\/\/hasu0707.duckdns.org\/blog\/?p=1034","title":{"rendered":"iptables GeoIP \uc801\uc6a9\uc744 \uc704\ud55c \uad6d\uac00\ubcc4 DB\uc124\uce58 \uc2a4\ud06c\ub9bd\ud2b8"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#!\/bin\/bash\n######################################################################\n#\n# iptables\uc758 GeoIP \uc801\uc6a9\uc744 \uc704\ud55c \uad6d\uac00\ubcc4 DB \uc900\ube44 \uc791\uc5c5\n#\n######################################################################\nSCRIPT_NAME=$(basename $0)\nCURRENT_DIR=$(pwd)\nXTABLES_ADDONS_VER=2.14\n\n######################################################################\n#\n# Error Handling\n#\n######################################################################\nfunction error_exit\n{\n  clear\n  echo \"${SCRIPT_NAME}: ${1:-\\\"Unknown Error\\\"}\" 1>&amp;2\n  exit 1\n}\n\n######################################################################\n#\n# \uc2e4\ud589\uc5d0 \ud544\uc694\ud55c \ud328\ud0a4\uc9c0 \uc124\uce58\n#\n######################################################################\nfunction yum_prepare() {\n  yum -y install perl-NetAddr-IP perl-Net-CIDR-Lite perl-Text-CSV_XS iptables-devel kernel-devel\n}\n\n######################################################################\n#\n# xtables-addons \ub2e4\uc6b4\ub85c\ub4dc\n#\n######################################################################\nfunction download_xtables_addons() {\n  if [ -d xtables-addons-* ]\n  then\n    rm -rf xtables-addons-*\n  fi\n  if [ -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz ]\n  then\n    rm -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz\n  fi\n  wget https:\/\/sourceforge.net\/projects\/xtables-addons\/files\/Xtables-addons\/xtables-addons-${XTABLES_ADDONS_VER}.tar.xz\/download -O xtables-addons-${XTABLES_ADDONS_VER}.tar.xz\n  if [ ! -f xtables-addons-${XTABLES_ADDONS_VER}.tar.xz ]\n  then\n    error_exit\n  fi\n  tar -xJf xtables-addons-*.tar.xz\n  rm -f xtables-addons-*.tar.xz\n}\n\n######################################################################\n#\n# \uc2e0DB > \uad6cDB\ub85c \ubcc0\ud658\ud558\uae30 \uc704\ud55c GeoLite2xtables \ub2e4\uc6b4\ub85c\ub4dc\n#\n######################################################################\nfunction download_geolite2xtables() {\n  if [ -d GeoLite2xtables ]\n  then\n    rm -rf GeoLite2xtables\n  fi\n  git clone https:\/\/github.com\/mschmitt\/GeoLite2xtables.git\n  if [ ! -d GeoLite2xtables ]\n  then\n    error_exit\n  fi\n}\n\n######################################################################\n#\n# \uc2e0DB\ub97c \uad6cDB\ub85c \ubcc0\ud658\ud558\ub294 \uc791\uc5c5\n#\n######################################################################\nfunction conv_geolite2xtables() {\n  if [ -d \/usr\/share\/xt_geoip ]\n  then\n    rm -rf \/usr\/share\/xt_geoip\n  fi\n  rm -f \/tmp\/GeoLite2-Country-Blocks-*\n  rm -f \/tmp\/CountryInfo.txt\n\n  mkdir -p \/usr\/share\/xt_geoip\/{BE,LE}\n  cd ${CURRENT_DIR}\/GeoLite2xtables\n  echo \"***********************00_download_geolite2\"\n  .\/00_download_geolite2\n  echo \"***********************10_download_countryinfo\"\n  .\/10_download_countryinfo\n  echo \"***********************20_convert_geolite2\"\n  cat \/tmp\/GeoLite2-Country-Blocks-IPv{4,6}.csv | .\/20_convert_geolite2 \/tmp\/CountryInfo.txt > \/usr\/share\/xt_geoip\/GeoIP-legacy.csv\n  echo \"***********************xt_geoip_build\"\n  cd ${CURRENT_DIR}\/xtables-addons-${XTABLES_ADDONS_VER}\/geoip\n  .\/xt_geoip_build -D \/usr\/share\/xt_geoip \/usr\/share\/xt_geoip\/GeoIP-legacy.csv\n  echo \"***********************tar -cvzf xt_geoip.tar.gz \/usr\/share\/xt_geoip\"\n  if [ -f \/usr\/share\/xt_geoip\/GeoIP-legacy.csv ]\n  then\n    rm -f \/usr\/share\/xt_geoip\/GeoIP-legacy.csv\n  fi\n  cd ${CURRENT_DIR}\n  tar -cvzf xt_geoip.tar.gz \/usr\/share\/xt_geoip\n}\n\n######################################################################\n#\n# \ub2e4\uc6b4\ubc1b\uc740 \ud30c\uc77c\ub4e4 \uc0ad\uc81c\n#\n######################################################################\nfunction clean_data() {\n  cd ${CURRENT_DIR}\n  if [ -d xtables-addons-* ]\n  then\n    rm -rf xtables-addons-*\n  fi\n  if [ -d GeoLite2xtables ]\n  then\n    rm -rf GeoLite2xtables\n  fi\n  rm -f \/tmp\/GeoLite2-Country-Blocks-*\n  rm -f \/tmp\/CountryInfo.txt\n}\n\nyum_prepare\ndownload_xtables_addons\ndownload_geolite2xtables\nconv_geolite2xtables\nclean_data<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[39],"tags":[],"class_list":["post-1034","post","type-post","status-publish","format-standard","hentry","category-os_linux_unix_macos"],"_links":{"self":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1034"}],"version-history":[{"count":0,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1034\/revisions"}],"wp:attachment":[{"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hasu0707.duckdns.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}