From 3e1df126c26008c3ac562cebecceed50b6690521 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Sun, 29 Mar 2020 22:44:07 +0800 Subject: [PATCH] add build scripts --- README.md | 2 ++ build-from-apnic.sh | 23 +++++++++++++++++++++++ build-from-maxmind.sh | 23 +++++++++++++++++++++++ build_geoip.py | 13 +++++++++---- 4 files changed, 57 insertions(+), 4 deletions(-) create mode 100755 build-from-apnic.sh create mode 100755 build-from-maxmind.sh diff --git a/README.md b/README.md index 3301ead..1ecded6 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ 用于生成asus-merlin固件可用的geoip文件(geoipdb.bin和geoipdb.idx) 支持两种格式的IP地址文件: + 1、Maxmind Legacy (https://legacy-geoip-csv.ufficyo.com/) + 2、APNIC (http://ftp.apnic.net/stats/apnic/delegated-apnic-latest) diff --git a/build-from-apnic.sh b/build-from-apnic.sh new file mode 100755 index 0000000..9d8d946 --- /dev/null +++ b/build-from-apnic.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +ip_data_uri="http://ftp.apnic.net/stats/apnic/delegated-apnic-latest" + +tempDir=`mktemp -d` +if [ $? -ne 0 ]; then + echo "mktemp failed" + exit 1 +fi + +wget ${ip_data_uri} -P ${tempDir} +if [ $? -ne 0 ]; then + echo "file ${ip_data_uri} download failed" + rm -rf ${tempDir} + exit 1 +fi + +python3 build_geoip.py apnic ${tempDir}/delegated-apnic-latest +if [ $? -ne 0 ]; then + echo "build geoip failed" +fi + +rm -rf ${tempDir} diff --git a/build-from-maxmind.sh b/build-from-maxmind.sh new file mode 100755 index 0000000..4e5cb23 --- /dev/null +++ b/build-from-maxmind.sh @@ -0,0 +1,23 @@ +#!/bin/bash + +ip_data_uri="https://legacy-geoip-csv.ufficyo.com/Legacy-MaxMind-GeoIP-database.tar.gz" + +tempDir=`mktemp -d` +if [ $? -ne 0 ]; then + echo "mktemp failed" + exit 1 +fi + +wget ${ip_data_uri} -O - | tar -xvzf - -C ${tempDir} +if [ $? -ne 0 ]; then + echo "file ${ip_data_uri} download failed" + rm -rf ${tempDir} + exit 1 +fi + +python3 build_geoip.py maxmind ${tempDir}/GeoIP-legacy.csv +if [ $? -ne 0 ]; then + echo "build geoip failed" +fi + +rm -rf ${tempDir} diff --git a/build_geoip.py b/build_geoip.py index c1222a1..89078d8 100755 --- a/build_geoip.py +++ b/build_geoip.py @@ -45,6 +45,9 @@ def csv2bin(filename): for line in open(filename): fields = line.strip().split(',') cc = fields[4][1:-1] + # ipv6 not supported + if ":" in fields[0]: + continue records[cc].append([int(fields[2][1:-1]), int(fields[3][1:-1])]) for r in records: @@ -66,8 +69,10 @@ def csv2bin(filename): offset += 2 + 2 + 8 * len(records[r]) if __name__ == '__main__': - if len(sys.argv) != 2: - print(f'usage: python {sys.argv[0]} ') + if len(sys.argv) != 3: + print(f'usage: python {sys.argv[0]} maxmind|apnic ') exit(1) - #csv2bin(sys.argv[1]) - apnic2bin(sys.argv[1]) + if sys.argv[1] == 'maxmind': + csv2bin(sys.argv[2]) + else: + apnic2bin(sys.argv[2])