    001    package net.minecraft.client.multiplayer;
    002
    003    import cpw.mods.fml.relauncher.Side;
    004    import cpw.mods.fml.relauncher.SideOnly;
    005    import java.util.Hashtable;
    006    import javax.naming.directory.Attributes;
    007    import javax.naming.directory.InitialDirContext;
    008
    009    @SideOnly(Side.CLIENT)
    010    public class ServerAddress
    011    {
    012        private final String ipAddress;
    013        private final int serverPort;
    014
    015        private ServerAddress(String par1Str, int par2)
    016        {
    017            this.ipAddress = par1Str;
    018            this.serverPort = par2;
    019        }
    020
    021        public String getIP()
    022        {
    023            return this.ipAddress;
    024        }
    025
    026        public int getPort()
    027        {
    028            return this.serverPort;
    029        }
    030
    031        public static ServerAddress func_78860_a(String par0Str)
    032        {
    033            if (par0Str == null)
    034            {
    035                return null;
    036            }
    037            else
    038            {
    039                String[] var1 = par0Str.split(":");
    040
    041                if (par0Str.startsWith("["))
    042                {
    043                    int var2 = par0Str.indexOf("]");
    044
    045                    if (var2 > 0)
    046                    {
    047                        String var3 = par0Str.substring(1, var2);
    048                        String var4 = par0Str.substring(var2 + 1).trim();
    049
    050                        if (var4.startsWith(":") && var4.length() > 0)
    051                        {
    052                            var4 = var4.substring(1);
    053                            var1 = new String[] {var3, var4};
    054                        }
    055                        else
    056                        {
    057                            var1 = new String[] {var3};
    058                        }
    059                    }
    060                }
    061
    062                if (var1.length > 2)
    063                {
    064                    var1 = new String[] {par0Str};
    065                }
    066
    067                String var5 = var1[0];
    068                int var6 = var1.length > 1 ? parseIntWithDefault(var1[1], 25565) : 25565;
    069
    070                if (var6 == 25565)
    071                {
    072                    String[] var7 = getServerAddress(var5);
    073                    var5 = var7[0];
    074                    var6 = parseIntWithDefault(var7[1], 25565);
    075                }
    076
    077                return new ServerAddress(var5, var6);
    078            }
    079        }
    080
    081        /**
    082         * Returns a server's address and port for the specified hostname, looking up the SRV record if possible
    083         */
    084        private static String[] getServerAddress(String par0Str)
    085        {
    086            try
    087            {
    088                Hashtable var1 = new Hashtable();
    089                var1.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
    090                var1.put("java.naming.provider.url", "dns:");
    091                InitialDirContext var2 = new InitialDirContext(var1);
    092                Attributes var3 = var2.getAttributes("_minecraft._tcp." + par0Str, new String[] {"SRV"});
    093                String[] var4 = var3.get("srv").get().toString().split(" ", 4);
    094                return new String[] {var4[3], var4[2]};
    095            }
    096            catch (Throwable var5)
    097            {
    098                return new String[] {par0Str, Integer.toString(25565)};
    099            }
    100        }
    101
    102        private static int parseIntWithDefault(String par0Str, int par1)
    103        {
    104            try
    105            {
    106                return Integer.parseInt(par0Str.trim());
    107            }
    108            catch (Exception var3)
    109            {
    110                return par1;
    111            }
    112        }
    113    }