Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh /etc/rc.common
- START=16
- IV_DIR='/usr/share/xt_geoip'
- start() {
- IPV4_SRC='https://www.ipdeny.com/ipblocks/data/aggregated/hu-aggregated.zone'
- IPV6_SRC='https://www.ipdeny.com/ipv6/ipaddresses/aggregated/hu-aggregated.zone'
- IPV4_FILE="${IV_DIR}/HU.iv4"
- IPV6_FILE="${IV_DIR}/HU.iv6"
- TEMPFILE='/tmp/geoip'
- ipv4='
- function ipv4addr(s,
- t, i) {
- if (split(s, t, /\./) != 4) return -1;
- for (i = 0; i < 4; i++) {
- a[i] = t[4 - i];
- }
- return 0;
- }
- function hexdump(x) {
- if (cnum == 0) printf("%08x:", addr);
- printf(" %02x", and(x, 0xff));
- addr++;
- if (++cnum == cols) {
- cnum = 0;
- printf("\n");
- }
- }
- function rrot(x,
- ctemp) {
- ctemp = and(x, 1);
- x = or(rshift(x, 1), lshift(cy, 7));
- cy = ctemp;
- return x;
- }
- function mask(mlen,
- i) {
- for (i = 0; i < 4; i++) m[i] = 0;
- while (mlen--) {
- cy = 1;
- for (i = 3; i >= 0; i--) m[i] = rrot(m[i]);
- }
- }
- BEGIN {
- FS = "/";
- cols = 16;
- addr = 0;
- cnum = 0;
- }
- /^([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$/ {
- if (ipv4addr($1) < 0) next;
- mask($2);
- for (i = 3; i >= 0; i--) {
- hexdump(and(a[i], m[i]));
- }
- for (i = 3; i >= 0; i--) {
- hexdump(or(a[i], xor(m[i], 0xff)));
- }
- }
- END {
- printf("\n");
- }
- '
- ipv6='
- function ipv6addr(s,
- t, b, i, j, m, n) {
- for (i = 0; i < 8; i++) a[i] = 0;
- n = split(s, t, /::/);
- if (n > 2) return -1;
- for (i = 1; i <= n; i++) {
- if (length(t[i]) == 0) continue;
- m = split(t[i], b, /:/);
- if (m > 8) return -1;
- if (n == 1) {
- for (j = 1; j <= m; j++) {
- a[8 - j] = sprintf("%d", "0x" b[j]);
- }
- } else {
- for (j = 1; j <= m; j++) {
- a[m - j] = sprintf("%d", "0x" b[j]);
- }
- }
- }
- return 0;
- }
- function hexdump(x) {
- if (cnum == 0) printf("%08x:", addr);
- printf(" %02x", and(x, 0xff));
- addr++;
- if (++cnum == cols) {
- cnum = 0;
- printf("\n");
- }
- }
- function hex2dump(x) {
- hexdump(rshift(x, 8));
- hexdump(x);
- }
- function rrot(x,
- ctemp) {
- ctemp = and(x, 1);
- x = or(rshift(x, 1), lshift(cy, 15));
- cy = ctemp;
- return x;
- }
- function mask(mlen,
- i) {
- for (i = 0; i < 8; i++) m[i] = 0;
- while (mlen--) {
- cy = 1;
- for (i = 7; i >= 0; i--) m[i] = rrot(m[i]);
- }
- }
- BEGIN {
- FS = "/";
- cols = 16;
- addr = 0;
- cnum = 0;
- }
- /^[0-9A-Fa-f:\/]+$/ {
- if (ipv6addr($1) < 0) next;
- mask($2);
- for (i = 7; i >= 0; i--) {
- hex2dump(and(a[i], m[i]));
- }
- for (i = 7; i >= 0; i--) {
- hex2dump(or(a[i], xor(m[i], 0xffff)));
- }
- }
- END {
- printf("\n");
- }
- '
- [ -f "$TEMPFILE" ] && rm -f "$TEMPFILE"
- if wget -O "$TEMPFILE" "$IPV4_SRC"; then
- echo 'Generating IPv4 rules...'
- awk "$ipv4" "$TEMPFILE" | xxd -r >"$IPV4_FILE"
- fi
- if wget -O "$TEMPFILE" "$IPV6_SRC"; then
- echo 'Generating IPv6 rules...'
- awk "$ipv6" "$TEMPFILE" | xxd -r >"$IPV6_FILE"
- fi
- [ -f "$TEMPFILE" ] && rm -f "$TEMPFILE"
- FIREWALL='/etc/init.d/firewall'
- if "$FIREWALL" status &>/dev/null; then
- echo 'Restarting firewall'
- "$FIREWALL" restart
- echo 'Firewall restarted'
- fi
- }
- stop() {
- :
- }
- EXTRA_COMMANDS="status"
- EXTRA_HELP=" status List GeoIP files with 'ls -l'"
- status() {
- if cd "$IV_DIR" &>/dev/null; then
- ls -l
- cd - &>/dev/null
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment