Guest User

get_geoip_hu

a guest
Dec 12th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.53 KB | None | 0 0
  1. #!/bin/sh /etc/rc.common
  2.  
  3. START=16
  4.  
  5. IV_DIR='/usr/share/xt_geoip'
  6.  
  7. start() {
  8.     IPV4_SRC='https://www.ipdeny.com/ipblocks/data/aggregated/hu-aggregated.zone'
  9.     IPV6_SRC='https://www.ipdeny.com/ipv6/ipaddresses/aggregated/hu-aggregated.zone'
  10.  
  11.     IPV4_FILE="${IV_DIR}/HU.iv4"
  12.     IPV6_FILE="${IV_DIR}/HU.iv6"
  13.  
  14.     TEMPFILE='/tmp/geoip'
  15.  
  16.     ipv4='
  17.     function ipv4addr(s,
  18.                         t, i) {
  19.         if (split(s, t, /\./) != 4) return -1;
  20.         for (i = 0; i < 4; i++) {
  21.             a[i] = t[4 - i];
  22.         }
  23.         return 0;
  24.    }
  25.  
  26.    function hexdump(x) {
  27.         if (cnum == 0) printf("%08x:", addr);
  28.         printf(" %02x", and(x, 0xff));
  29.         addr++;
  30.         if (++cnum == cols) {
  31.             cnum = 0;
  32.             printf("\n");
  33.         }
  34.    }
  35.  
  36.    function rrot(x,
  37.                     ctemp) {
  38.         ctemp = and(x, 1);
  39.         x = or(rshift(x, 1), lshift(cy, 7));
  40.         cy = ctemp;
  41.         return x;
  42.    }
  43.  
  44.    function mask(mlen,
  45.                         i) {
  46.         for (i = 0; i < 4; i++) m[i] = 0;
  47.         while (mlen--) {
  48.             cy = 1;
  49.             for (i = 3; i >= 0; i--) m[i] = rrot(m[i]);
  50.         }
  51.    }
  52.  
  53.    BEGIN {
  54.         FS = "/";
  55.         cols = 16;
  56.         addr = 0;
  57.         cnum = 0;
  58.    }
  59.  
  60.    /^([0-9]{1,3}\.){3}[0-9]{1,3}\/[0-9]{1,2}$/ {
  61.         if (ipv4addr($1) < 0) next;
  62.         mask($2);
  63.         for (i = 3; i >= 0; i--) {
  64.             hexdump(and(a[i], m[i]));
  65.         }
  66.         for (i = 3; i >= 0; i--) {
  67.             hexdump(or(a[i], xor(m[i], 0xff)));
  68.         }
  69.    }
  70.  
  71.    END {
  72.         printf("\n");
  73.    }
  74.    '
  75.  
  76.     ipv6='
  77.    function ipv6addr(s,
  78.                         t, b, i, j, m, n) {
  79.         for (i = 0; i < 8; i++) a[i] = 0;
  80.         n = split(s, t, /::/);
  81.         if (n > 2) return -1;
  82.         for (i = 1; i <= n; i++) {
  83.             if (length(t[i]) == 0) continue;
  84.             m = split(t[i], b, /:/);
  85.             if (m > 8) return -1;
  86.             if (n == 1) {
  87.                 for (j = 1; j <= m; j++) {
  88.                     a[8 - j] = sprintf("%d", "0x" b[j]);
  89.                 }
  90.             } else {
  91.                 for (j = 1; j <= m; j++) {
  92.                     a[m - j] = sprintf("%d", "0x" b[j]);
  93.                 }
  94.             }
  95.         }
  96.         return 0;
  97.    }
  98.  
  99.    function hexdump(x) {
  100.         if (cnum == 0) printf("%08x:", addr);
  101.         printf(" %02x", and(x, 0xff));
  102.         addr++;
  103.         if (++cnum == cols) {
  104.             cnum = 0;
  105.             printf("\n");
  106.         }
  107.    }
  108.  
  109.    function hex2dump(x) {
  110.         hexdump(rshift(x, 8));
  111.         hexdump(x);
  112.    }
  113.  
  114.    function rrot(x,
  115.                     ctemp) {
  116.         ctemp = and(x, 1);
  117.         x = or(rshift(x, 1), lshift(cy, 15));
  118.         cy = ctemp;
  119.         return x;
  120.    }
  121.  
  122.    function mask(mlen,
  123.                         i) {
  124.         for (i = 0; i < 8; i++) m[i] = 0;
  125.         while (mlen--) {
  126.             cy = 1;
  127.             for (i = 7; i >= 0; i--) m[i] = rrot(m[i]);
  128.         }
  129.    }
  130.  
  131.    BEGIN {
  132.         FS = "/";
  133.         cols = 16;
  134.         addr = 0;
  135.         cnum = 0;
  136.    }
  137.  
  138.    /^[0-9A-Fa-f:\/]+$/ {
  139.         if (ipv6addr($1) < 0) next;
  140.         mask($2);
  141.         for (i = 7; i >= 0; i--) {
  142.             hex2dump(and(a[i], m[i]));
  143.         }
  144.         for (i = 7; i >= 0; i--) {
  145.             hex2dump(or(a[i], xor(m[i], 0xffff)));
  146.         }
  147.    }
  148.  
  149.    END {
  150.         printf("\n");
  151.    }
  152.    '
  153.  
  154.     [ -f "$TEMPFILE" ] && rm -f "$TEMPFILE"
  155.  
  156.     if wget -O "$TEMPFILE" "$IPV4_SRC"; then
  157.         echo 'Generating IPv4 rules...'
  158.         awk "$ipv4" "$TEMPFILE" | xxd -r >"$IPV4_FILE"
  159.     fi
  160.  
  161.     if wget -O "$TEMPFILE" "$IPV6_SRC"; then
  162.         echo 'Generating IPv6 rules...'
  163.         awk "$ipv6" "$TEMPFILE" | xxd -r >"$IPV6_FILE"
  164.     fi
  165.  
  166.     [ -f "$TEMPFILE" ] && rm -f "$TEMPFILE"
  167.  
  168.     FIREWALL='/etc/init.d/firewall'
  169.     if "$FIREWALL" status &>/dev/null; then
  170.         echo 'Restarting firewall'
  171.         "$FIREWALL" restart
  172.         echo 'Firewall restarted'
  173.     fi
  174. }
  175.  
  176. stop() {
  177.     :
  178. }
  179.  
  180. EXTRA_COMMANDS="status"
  181. EXTRA_HELP="    status          List GeoIP files with 'ls -l'"
  182.  
  183. status() {
  184.     if cd "$IV_DIR" &>/dev/null; then
  185.     ls -l
  186.     cd - &>/dev/null
  187.     fi
  188. }
  189.  
Advertisement
Add Comment
Please, Sign In to add comment