package org.kne.cloud.network.minecraft;

import java.net.IDN;
import java.util.Hashtable;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import org.kne.cloud.network.MultiProtocolSocketAddress;

public class MinecraftServerAddress extends MultiProtocolSocketAddress
{
	
    private String originAddr;
    private int originPort;
	private String originStr;
	public int getOriginPort() {
		return originPort;
	}
	public String getOriginAddr() {
		return originAddr;
	}
	public String toOriginString() {
		return originStr;
	}
	private MinecraftServerAddress(String ip, int port,String originaddr, int originPort, String addr)
    {
       super(IDN.toASCII(ip),port);
       this.originAddr=originaddr;
       this.originPort=originPort;
       this.originStr=addr;
    }
	public static MinecraftServerAddress findAddress(String addr)
    {
        if (addr == null)
        {
            return null;
        }
        else
        {
            String[] astring = addr.split(":");

            if (addr.startsWith("["))
            {
                int i = addr.indexOf("]");

                if (i > 0)
                {
                    String s = addr.substring(1, i);
                    String s1 = addr.substring(i + 1).trim();

                    if (s1.startsWith(":") && s1.length() > 0)
                    {
                        s1 = s1.substring(1);
                        astring = new String[] {s, s1};
                    }
                    else
                    {
                        astring = new String[] {s};
                    }
                }
            }
            
            if (astring.length > 2)
            {
                astring = new String[] {addr};
            }

            String s2 = astring[0];
            String oaddr=s2;
            int j = astring.length > 1 ? parseIntWithDefault(astring[1], 25565) : 25565;
            int oport=j;
            if (j == 25565)
            {
                String[] astring1 = getServerAddress(s2);
                s2 = astring1[0];
                j = parseIntWithDefault(astring1[1], 25565);
            }

            return new MinecraftServerAddress(s2, j,oaddr,oport,addr);
        }
    }

    /**
     * Returns a server's address and port for the specified hostname, looking up the SRV record if possible
     */
    private static String[] getServerAddress(String p_78863_0_)
    {
        try
        {
            String s = "com.sun.jndi.dns.DnsContextFactory";
            Class.forName("com.sun.jndi.dns.DnsContextFactory");
            Hashtable<String, String> hashtable = new Hashtable();
            hashtable.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
            hashtable.put("java.naming.provider.url", "dns:");
            hashtable.put("com.sun.jndi.dns.timeout.retries", "1");
            DirContext dircontext = new InitialDirContext(hashtable);
            Attributes attributes = dircontext.getAttributes("_minecraft._tcp." + p_78863_0_, new String[] {"SRV"});
            String[] astring = attributes.get("srv").get().toString().split(" ", 4);
            return new String[] {astring[3], astring[2]};
        }
        catch (Throwable var6)
        {
            return new String[] {p_78863_0_, Integer.toString(25565)};
        }
    }

    private static int parseIntWithDefault(String str, int def)
    {
        try
        {
            return Integer.parseInt(str.trim());
        }
        catch (Exception e)
        {
            return def;
        }
    }
}
