com.gdxsoft.easyweb.utils.UDns Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of emp-script-utils Show documentation
Show all versions of emp-script-utils Show documentation
Easy Web Appliaction Utils: UArgon2, UMail, UDigest, URsa,
UAes, UDes, UFile, UConvert, UFormat, UNet, SendMail, SmtpCfg
The newest version!
package com.gdxsoft.easyweb.utils;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UDns {
private static Logger LOGGER = LoggerFactory.getLogger(UDns.class);
/**
* Query the public key from DNS record
*
* @param domain the email sign domain
* @param selector the selector
* @return public key
* @throws Exception
*/
public static String queryDkimPublickey(String domain, String selector) {
String recordname = selector + "._domainkey." + domain;
String value = null;
List records = UDns.nslookup(recordname, "txt");
if (records == null) {
LOGGER.error("NO " + recordname);
return null;
}
value = records.get(0);
// v=DKIM1; k=rsa; p=MIGfMA0G...
String[] tags = value.split(";");
for (String tag : tags) {
tag = tag.trim();
if (tag.startsWith("p=")) {
String base64Key = tag.substring(2);
// DER format
return base64Key;
}
}
return null;
}
public static Hashtable createDefaultEnv() {
Hashtable env = new Hashtable();
// java.naming.factory.initial
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
return env;
}
/**
* Query a domain
*
* @param domain the domain name
* @param queryType the query type, E.g. txt, a, aaaa, mx, ns ...
* @param dnsServer the DNS server
* @return the multiple results
*/
public static List nslookup(String domain, String queryType, String dnsServer) {
Hashtable env = createDefaultEnv();
// 设置域名服务器
// java.naming.provider.url
env.put(Context.PROVIDER_URL, "dns://" + dnsServer);
return nslookup(env, domain, queryType);
}
/**
* Query a domain
*
* @param domain the domain name
* @param queryType the query type, E.g. txt, a, aaaa, mx, ns ...
* @return the multiple results
*/
public static List nslookup(String domain, String queryType) {
Hashtable env = createDefaultEnv();
return nslookup(env, domain, queryType);
}
/**
* Query a domain
*
* @param env the java.naming.factory environment
* @param domain the domain name
* @param queryType the query type, E.g. txt, a, aaaa, mx, ns ...
* @return The all results
*/
public static List nslookup(Hashtable env, String domain, String queryType) {
String qt = queryType.toLowerCase().trim();
List values = new ArrayList();
try {
DirContext dnsContext = new InitialDirContext(env);
Attributes attribs = dnsContext.getAttributes(domain, new String[] { qt });
Attribute records = attribs.get(qt);
if (records == null) {
LOGGER.error("There is no " + queryType + " record available for " + domain);
return null;
}
NamingEnumeration> vals = records.getAll();
while (vals.hasMore()) {
Object obj = vals.next();
values.add(obj.toString());
}
} catch (NamingException ne) {
LOGGER.error(ne.getExplanation());
}
return values;
}
}