daily pastebin goal
51%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.codeusa.omega.net;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7.  
  8. /**
  9.  * Omega
  10.  * Blacklist.java
  11.  * @version 1.0.0
  12.  * @author SiniSoul (SiniSoul@live.com)
  13.  */
  14. public class Blacklist {
  15.    
  16.     /**
  17.      * The blacklisted IPs addresses.
  18.      */
  19.     private static BitNode[] listed = new BitNode[32];
  20.    
  21.     /**
  22.      * Loads the blacklist file.
  23.      *  -Note: File encoding must be in UTF encoding.
  24.      * @param f The file to load the blacklist with.
  25.      * @throws An IOException was thrown while loading the blacklist file.
  26.      */
  27.     public static void load(String f) throws IOException {
  28.         /* Construct the data input stream from the file */
  29.         DataInputStream stream = new DataInputStream(new FileInputStream(new File(f)));
  30.         /* Record the amount of available bytes */
  31.         final int avail = stream.available();
  32.         /* The current read offset */
  33.         int offset = 0;
  34.         /* Read the data */
  35.         String data = "";
  36.         while(offset++ < avail) {
  37.             /* Read the character */
  38.             int i = stream.readUnsignedByte();
  39.             /* Add the character for this ip */
  40.             data += (char) i;
  41.         }
  42.         /* Split the data into the raw ips */
  43.         String[] ips = data.split("\n");
  44.         /* Parse each of the ips */
  45.         for(String s : ips) {
  46.             try {
  47.                 /* Calculate the integer value of the ip */
  48.                 int value = toInteger(s);  
  49.                 /* Add the IP to the listed ips */
  50.                 addIP(value);
  51.             } catch(IOException ex) {
  52.                 System.err.println("Error while blacklisting ip | "+s);
  53.             }
  54.         }
  55.     }
  56.    
  57.     /**
  58.      * Adds an IP address to the blacklist.
  59.      * @param ip The integer representation of the IP number.
  60.      */
  61.     public static void addIP(int ip) {
  62.         /* Loop through each bit */
  63.         for(int i = 0; i < 32; i++) {
  64.             /* Checks to see if the bit is active */
  65.             boolean active = (ip & (1 << i)) != 0;
  66.             /* Construct the bit node if it needs to be */
  67.             if(listed[i] == null)
  68.                 listed[i] = new BitNode();
  69.             /* Bit is active */
  70.             if(active)
  71.                 listed[i].incrementOne();
  72.             /* Bit is not active */
  73.             else
  74.                 listed[i].incrementZero();
  75.         }
  76.     }
  77.    
  78.     /**
  79.      * Checks to see if an IP is listed.
  80.      * @param ip The integer representation of the IP number.
  81.      */
  82.     public static boolean isListed(int ip) {
  83.         /* Loop through each bit */
  84.         for(int i = 0; i < 32; i++) {
  85.             /* Checks to see if the bit is active */
  86.             boolean active = (ip & (1 << i)) != 0;
  87.             if(!listed[i].hasBit(active))
  88.                 return false;
  89.         }
  90.         return true;
  91.     }
  92.    
  93.     /**
  94.      * Converts a IP4 address to an integer.
  95.      * @param ip The IP address to convert.
  96.      * @return The converted IP address.
  97.      */
  98.     public static int toInteger(String ip) throws IOException {
  99.         /* Split the IP */
  100.         String[] data = ip.split("\\.");
  101.         /* Check to see if the IP has enough portions */
  102.         if(data.length != 4)
  103.             throw new IOException("Invalid IP String : Data"+data.length);
  104.         /* Parse the data */
  105.         int integer = 0;
  106.         for(int i = 0; i < 4; i++) {
  107.             int value = Integer.valueOf(data[i]);
  108.             /* Check to see if data is within bounds */
  109.             if(value > 0xFF || value < 0x00)
  110.                 throw new IOException("Invalid IP String : Bounds - "+value);
  111.             /* Add the value to the integer */
  112.             integer |= (value << (i * 8));
  113.         }      
  114.         return integer;
  115.     }
  116.    
  117.     /**
  118.      * A class representation of IP bits.
  119.      */
  120.     private static class BitNode {
  121.        
  122.         /**
  123.          * The zero bit is active for this node.
  124.          */
  125.         boolean zero = false;
  126.        
  127.         /**
  128.          * Increment the one or active bit.
  129.          */
  130.         public void incrementZero() {
  131.             /* Incrememnt the amount of indexed ones */
  132.             indexedzeros++;
  133.             /* Automatically set the one to true */
  134.             zero = true;
  135.         }
  136.                
  137.         /**
  138.          * The amount of entries for the zero bit.
  139.          */
  140.         private int indexedzeros = 0;
  141.        
  142.         /**
  143.          * The one bit is active for this node.
  144.          */
  145.         boolean one = false;
  146.        
  147.         /**
  148.          * Increment the one or active bit.
  149.          */
  150.         public void incrementOne() {
  151.             /* Incrememnt the amount of indexed ones */
  152.             indexedones++;
  153.             /* Automatically set the one to true */
  154.             one = true;
  155.         }
  156.        
  157.         /**
  158.          * The amount of entries for the one bit.
  159.          */
  160.         private int indexedones = 0;
  161.        
  162.         /**
  163.          *
  164.          * @param bool
  165.          * @return
  166.          */
  167.         public boolean hasBit(boolean bool) {
  168.             /* Bit is active (one) */
  169.             if(bool)
  170.                 return one;
  171.             /* Bit is not active (zero) */
  172.             else
  173.                 return zero;
  174.         }
  175.     }
  176.    
  177.     /**
  178.      *
  179.      */
  180.     private Blacklist() {}
  181.    
  182. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top