Skip to content

Commit

Permalink
add build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Davis committed Mar 29, 2020
1 parent 4838fc0 commit 3e1df12
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 23 additions & 0 deletions build-from-apnic.sh
Original file line number Diff line number Diff line change
@@ -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}
23 changes: 23 additions & 0 deletions build-from-maxmind.sh
Original file line number Diff line number Diff line change
@@ -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}
13 changes: 9 additions & 4 deletions build_geoip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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]} <csv file>')
if len(sys.argv) != 3:
print(f'usage: python {sys.argv[0]} maxmind|apnic <file>')
exit(1)
#csv2bin(sys.argv[1])
apnic2bin(sys.argv[1])
if sys.argv[1] == 'maxmind':
csv2bin(sys.argv[2])
else:
apnic2bin(sys.argv[2])

0 comments on commit 3e1df12

Please sign in to comment.