dnsutils.java
来自「spam source codejasen-0.9jASEN - java An」· Java 代码 · 共 457 行 · 第 1/2 页
JAVA
457 行
/*
* @(#)DNSUtils.java 7/09/2004
*
* Copyright (c) 2004, 2005 jASEN.org
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* 4. Any modification or additions to the software must be contributed back
* to the project.
*
* 5. Any investigation or reverse engineering of source code or binary to
* enable emails to bypass the filters, and hence inflict spam and or viruses
* onto users who use or do not use jASEN could subject the perpetrator to
* criminal and or civil liability.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JASEN.ORG,
* OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package org.jasen.util;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import org.jasen.core.parsers.URLParser;
import org.jasen.error.DNSException;
import org.jasen.error.ErrorHandlerBroker;
import org.jasen.interfaces.DNSResolver;
import org.jasen.net.MXRecord;
/**
* <P>
* General DNS utilities.
* </P>
* @author Jason Polites
*/
public final class DNSUtils
{
/**
*
*/
public DNSUtils() {
super ();
}
/**
* The current list of generic top level domains<br/>
* This will be overwritten by a propertied file if one is found
*/
public static String[] TLD = { "aero", "arpa", "biz", "com", "coop", "info", "museum", "name", "net", "org", "pro", "gov", "edu", "mil", "int", "travel", "post" };
static
{
Properties props = new Properties();
try
{
InputStream in = DNSUtils.class.getClassLoader().getResourceAsStream("dns.properties");
if(in == null) {
// try the local file
in = DNSUtils.class.getClassLoader().getResourceAsStream("org/jasen/util/dns.properties");
}
if(in != null) {
props.load(in);
String tlds = props.getProperty("tlds");
if(tlds != null) {
TLD = tlds.split(",");
}
}
}
catch (IOException ignore)
{
// Use the hard coded list above
ErrorHandlerBroker.getInstance().getErrorHandler().handleException(ignore);
}
finally {
Arrays.sort (TLD);
}
}
/**
* Lists the MX records for the given canonical domain
*
* @param canonical The canonical domain. Eg., microsoft.com, NOT www.microsoft.com
* @return The MX (mail exchanger) records for the given domain
* @throws DNSException
*/
public static MXRecord[] getMXRecords(DNSResolver resolver, String canonical) throws UnknownHostException, DNSException {
MXRecord[] records = null;
Attributes atrs = null;
try
{
atrs = resolver.lookup(canonical, "MX");
if (atrs != null && atrs.size () > 0)
{
Attribute at = atrs.get ("MX");
if (at != null && at.size () > 0)
{
// The MX records returned will be in the format:
// <preference> domain.
// eg.
// 10 maila.microsoft.com.
String record = null;
MXRecord mxRecord = null;
String[] parsed = null;
records = new MXRecord[at.size ()];
for (int i = 0; i < at.size (); i++)
{
record = at.get (i).toString ();
parsed = record.split (" ");
if (parsed.length > 1)
{
mxRecord = new MXRecord ();
mxRecord.setPreference (Integer.parseInt (parsed[0]));
if (parsed[1].endsWith ("."))
{
parsed[1] = parsed[1].substring (0, parsed[1].length () - 1);
}
mxRecord.setAddress (InetAddress.getByName (parsed[1]));
records[i] = mxRecord;
}
else
{
throw new DNSException ("Error parsing MX record");
}
}
}
}
}
catch (NamingException e)
{
throw new DNSException (e);
}
return records;
}
/**
* Returns true if the given address is a valid IPv4 or IPv6 address
* @param address
* @return True if the String passed is a valid IP address, false otherwise
*/
public static boolean isIPAddress(String address) {
return (isIPv4Address(address) || isIPv6Address(address));
}
/**
* Returns true if the given string is a correctly formed IPv4 address.
* <br/>
* The long regex stores each of the 4 numbers of the IP address into a
* capturing group. <br/>Taken from: <a
* href="http://www.regular-expressions.info/examples.html">http://www.regular-expressions.info/examples.html
* </a>
*
* @param address
* @return True if the String passed is a valid IP address, false otherwise
*/
public static boolean isIPv4Address(String address) {
String pattern = "\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b";
return address.matches (pattern);
}
/**
* Returns true if the given string is a correctly formed IPv6 address.
* <br>
* <pre>
There are three conventional forms for representing IPv6 addresses as
text strings:
1. The preferred form is x:x:x:x:x:x:x:x, where the 'x's are the
hexadecimal values of the eight 16-bit pieces of the address.
Examples:
FEDC:BA98:7654:3210:FEDC:BA98:7654:3210
1080:0:0:0:8:800:200C:417A
Note that it is not necessary to write the leading zeros in an
individual field, but there must be at least one numeral in every
field (except for the case described in 2.).
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?