Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.codeusa.omega.net;
- import java.io.DataInputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- /**
- * Omega
- * Blacklist.java
- * @version 1.0.0
- * @author SiniSoul (SiniSoul@live.com)
- */
- public class Blacklist {
- /**
- * The blacklisted IPs addresses.
- */
- private static BitNode[] listed = new BitNode[32];
- /**
- * Loads the blacklist file.
- * -Note: File encoding must be in UTF encoding.
- * @param f The file to load the blacklist with.
- * @throws An IOException was thrown while loading the blacklist file.
- */
- public static void load(String f) throws IOException {
- /* Construct the data input stream from the file */
- DataInputStream stream = new DataInputStream(new FileInputStream(new File(f)));
- /* Record the amount of available bytes */
- final int avail = stream.available();
- /* The current read offset */
- int offset = 0;
- /* Read the data */
- String data = "";
- while(offset++ < avail) {
- /* Read the character */
- int i = stream.readUnsignedByte();
- /* Add the character for this ip */
- data += (char) i;
- }
- /* Split the data into the raw ips */
- String[] ips = data.split("\n");
- /* Parse each of the ips */
- for(String s : ips) {
- try {
- /* Calculate the integer value of the ip */
- int value = toInteger(s);
- /* Add the IP to the listed ips */
- addIP(value);
- } catch(IOException ex) {
- System.err.println("Error while blacklisting ip | "+s);
- }
- }
- }
- /**
- * Adds an IP address to the blacklist.
- * @param ip The integer representation of the IP number.
- */
- public static void addIP(int ip) {
- /* Loop through each bit */
- for(int i = 0; i < 32; i++) {
- /* Checks to see if the bit is active */
- boolean active = (ip & (1 << i)) != 0;
- /* Construct the bit node if it needs to be */
- if(listed[i] == null)
- listed[i] = new BitNode();
- /* Bit is active */
- if(active)
- listed[i].incrementOne();
- /* Bit is not active */
- else
- listed[i].incrementZero();
- }
- }
- /**
- * Checks to see if an IP is listed.
- * @param ip The integer representation of the IP number.
- */
- public static boolean isListed(int ip) {
- /* Loop through each bit */
- for(int i = 0; i < 32; i++) {
- /* Checks to see if the bit is active */
- boolean active = (ip & (1 << i)) != 0;
- if(!listed[i].hasBit(active))
- return false;
- }
- return true;
- }
- /**
- * Converts a IP4 address to an integer.
- * @param ip The IP address to convert.
- * @return The converted IP address.
- */
- public static int toInteger(String ip) throws IOException {
- /* Split the IP */
- String[] data = ip.split("\\.");
- /* Check to see if the IP has enough portions */
- if(data.length != 4)
- throw new IOException("Invalid IP String : Data"+data.length);
- /* Parse the data */
- int integer = 0;
- for(int i = 0; i < 4; i++) {
- int value = Integer.valueOf(data[i]);
- /* Check to see if data is within bounds */
- if(value > 0xFF || value < 0x00)
- throw new IOException("Invalid IP String : Bounds - "+value);
- /* Add the value to the integer */
- integer |= (value << (i * 8));
- }
- return integer;
- }
- /**
- * A class representation of IP bits.
- */
- private static class BitNode {
- /**
- * The zero bit is active for this node.
- */
- boolean zero = false;
- /**
- * Increment the one or active bit.
- */
- public void incrementZero() {
- /* Incrememnt the amount of indexed ones */
- indexedzeros++;
- /* Automatically set the one to true */
- zero = true;
- }
- /**
- * The amount of entries for the zero bit.
- */
- private int indexedzeros = 0;
- /**
- * The one bit is active for this node.
- */
- boolean one = false;
- /**
- * Increment the one or active bit.
- */
- public void incrementOne() {
- /* Incrememnt the amount of indexed ones */
- indexedones++;
- /* Automatically set the one to true */
- one = true;
- }
- /**
- * The amount of entries for the one bit.
- */
- private int indexedones = 0;
- /**
- *
- * @param bool
- * @return
- */
- public boolean hasBit(boolean bool) {
- /* Bit is active (one) */
- if(bool)
- return one;
- /* Bit is not active (zero) */
- else
- return zero;
- }
- }
- /**
- *
- */
- private Blacklist() {}
- }
Add Comment
Please, Sign In to add comment