Home  | My Account  |  Membership
WorldDeveloper.org
Forum Details
Topics
Posts
Categories
 
577 member(s)
718,905 hit(s)
14 categories
51 topics
26 messages
0 online
Forums: Unfiled
Java Utility for perfrorming DNS Blacklist lookups. For more information on DNSBL see http://en.wikipedia.org/wiki/DNSBL
hostj2me
43 topic(s)
23 post(s)

January 16, 2009 5:43 PM


package com.astrientlabs.net.dns;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.naming.Context;
import javax.naming.NameNotFoundException;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import com.astrientlabs.prefs.Preferences;

public class DNSBL
{
private static String[] RECORD_TYPES = { "A", "TXT" };

private DirContext ictx;
private List lookupServices = new ArrayList();


public DNSBL() throws NamingException
{
StringBuilder dnsServers = new StringBuilder("");
List nameservers = sun.net.dns.ResolverConfiguration.open().nameservers();
for( Object dns : nameservers )
{
dnsServers.append("dns://").append(dns).append(" ");
}

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
env.put("com.sun.jndi.dns.timeout.initial", Preferences.system.get("dns.timeout","4000"));
env.put("com.sun.jndi.dns.timeout.retries", Preferences.system.get("dns.retry","1"));
env.put(Context.PROVIDER_URL,Preferences.system.get("dns.url",dnsServers.toString()));

ictx = new InitialDirContext(env);
}

public void addLookupService(String service)
{
lookupServices.add(service);
}

public void check(String ip) throws DNSBLException
{
String[] parts = ip.split("\\.");
StringBuilder buffer = new StringBuilder();

for (int i = 0; i parts.length; i++)
{
buffer.insert(0, '.');
buffer.insert(0, parts[i]);
}

ip = buffer.toString();



Attribute attribute;
Attributes attributes;


String lookupHost;
for ( String service : lookupServices )
{
lookupHost = ip + service;
try
{
attributes = ictx.getAttributes(lookupHost, RECORD_TYPES);
attribute = attributes.get("TXT");

if ( attribute != null )
{
throw new DNSBLException(lookupHost + ": " + attribute.get());
}
}
catch (NameNotFoundException e)
{
//this is good
//LogWriter.system.log(getClass(),e);
}
catch (NamingException e)
{
//LogWriter.system.log(getClass(),e);
}
}
}


public static void main (String[] args)
{


try
{
DNSBL dnsBL = new DNSBL();

dnsBL.addLookupService("blackholes.easynet.nl");
dnsBL.addLookupService("cbl.abuseat.org");
dnsBL.addLookupService("proxies.blackholes.wirehub.net");
dnsBL.addLookupService("bl.spamcop.net");
dnsBL.addLookupService("sbl.spamhaus.org");
dnsBL.addLookupService("dnsbl.njabl.org");
dnsBL.addLookupService("list.dsbl.org");
dnsBL.addLookupService("multihop.dsbl.org");
dnsBL.addLookupService("cbl.abuseat.org");

try
{
dnsBL.check("201.223.47.44");
}
catch (DNSBLException se)
{
System.out.println(se.getMessage());
}

try
{
dnsBL.check("38.100.193.183");
}
catch (DNSBLException se)
{
System.out.println(se.getMessage());
}

try
{
dnsBL.check("189.94.149.137");
}
catch (DNSBLException se)
{
System.out.println(se.getMessage());
}

try
{
dnsBL.check("220.227.113.25");
}
catch (DNSBLException se)
{
System.out.println(se.getMessage());
}



}
catch (Exception e)
{
e.printStackTrace();
}

System.exit(0);
}
}
class DNSBLException extends Exception
{

public DNSBLException()
{
super();
}

public DNSBLException(String message, Throwable cause)
{
super(message, cause);
}

public DNSBLException(String message)
{
super(message);
}

public DNSBLException(Throwable cause)
{
super(cause);
}
}


©2008 WorldDeveloper.org - PO Box 310575 Miami, FL 33231

WorldDeveloper.org is a managed social network for application developers around the world.