Browse Source

In theory make DNS plugin work with IPv6.

pull/142/head
Greg Holmes 9 years ago
parent
commit
4448dd999d

+ 43
- 9
dns/src/com/dmdirc/addons/dns/DNSCommand.java View File

@@ -31,6 +31,12 @@ import com.dmdirc.commandparser.commands.Command;
31 31
 import com.dmdirc.commandparser.commands.context.CommandContext;
32 32
 import com.dmdirc.interfaces.CommandController;
33 33
 
34
+import com.google.common.net.InetAddresses;
35
+
36
+import java.net.InetAddress;
37
+import java.net.UnknownHostException;
38
+import java.util.ArrayList;
39
+import java.util.Collection;
34 40
 import java.util.Timer;
35 41
 import java.util.TimerTask;
36 42
 
@@ -70,17 +76,45 @@ public class DNSCommand extends Command {
70 76
 
71 77
             @Override
72 78
             public void run() {
73
-                if (args.getArguments()[0].matches("\\b(?:\\d{1,3}\\.){3}\\d{1,3}\\b")) {
74
-                    sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Resolved: "
75
-                            + args.getArguments()[0] + ": "
76
-                            + DNSPlugin.getHostname(args.getArguments()[0]));
77
-                } else {
78
-                    sendLine(origin, args.isSilent(), FORMAT_OUTPUT, "Resolved: "
79
-                            + args.getArguments()[0] + ": "
80
-                            + DNSPlugin.getIPs(args.getArguments()[0]));
81
-                }
79
+                resolve(origin, args.isSilent(), args.getArguments()[0]);
82 80
             }
83 81
         }, 0);
84 82
     }
85 83
 
84
+    private void resolve(@Nonnull final FrameContainer origin, final boolean isSilent,
85
+            final String arg) {
86
+        try {
87
+            final InetAddress address = InetAddresses.forString(arg);
88
+            sendLine(origin, isSilent, FORMAT_OUTPUT, "Resolved: "
89
+                    + arg + ": " + address.getCanonicalHostName());
90
+        } catch (IllegalArgumentException ex) {
91
+            sendLine(origin, isSilent, FORMAT_OUTPUT, "Resolved: "
92
+                    + arg + ": " + getIPs(arg));
93
+        }
94
+    }
95
+
96
+    /**
97
+     * Returns the IP(s) for a hostname.
98
+     *
99
+     * @param hostname Hostname to resolve.
100
+     *
101
+     * @return Resolved IP(s)
102
+     */
103
+    private String getIPs(final String hostname) {
104
+        final Collection<String> results = new ArrayList<>();
105
+
106
+        try {
107
+            final InetAddress[] ips = InetAddress.getAllByName(hostname);
108
+
109
+            for (InetAddress ip : ips) {
110
+                results.add(ip.getHostAddress());
111
+            }
112
+
113
+        } catch (UnknownHostException ex) {
114
+            return "[]";
115
+        }
116
+
117
+        return results.toString();
118
+    }
119
+
86 120
 }

+ 0
- 45
dns/src/com/dmdirc/addons/dns/DNSPlugin.java View File

@@ -25,11 +25,6 @@ package com.dmdirc.addons.dns;
25 25
 import com.dmdirc.plugins.PluginInfo;
26 26
 import com.dmdirc.plugins.implementations.BaseCommandPlugin;
27 27
 
28
-import java.net.InetAddress;
29
-import java.net.UnknownHostException;
30
-import java.util.ArrayList;
31
-import java.util.Collection;
32
-
33 28
 import dagger.ObjectGraph;
34 29
 
35 30
 /**
@@ -44,44 +39,4 @@ public final class DNSPlugin extends BaseCommandPlugin {
44 39
         setObjectGraph(graph.plus(new DNSModule()));
45 40
         registerCommand(DNSCommand.class, DNSCommand.INFO);
46 41
     }
47
-
48
-    /**
49
-     * Returns the IP(s) for a hostname.
50
-     *
51
-     * @param hostname Hostname to resolve.
52
-     *
53
-     * @return Resolved IP(s)
54
-     */
55
-    public static String getIPs(final String hostname) {
56
-        Collection<String> results = new ArrayList<>();
57
-
58
-        try {
59
-            final InetAddress[] ips = InetAddress.getAllByName(hostname);
60
-
61
-            for (InetAddress ip : ips) {
62
-                results.add(ip.getHostAddress());
63
-            }
64
-
65
-        } catch (UnknownHostException ex) {
66
-            results = new ArrayList<>();
67
-        }
68
-
69
-        return results.toString();
70
-    }
71
-
72
-    /**
73
-     * Returns the hostname for an ip.
74
-     *
75
-     * @param ip IP to resolve
76
-     *
77
-     * @return Resolved hostname
78
-     */
79
-    public static String getHostname(final String ip) {
80
-        try {
81
-            return InetAddress.getByName(ip).getHostName();
82
-        } catch (UnknownHostException ex) {
83
-            return "";
84
-        }
85
-    }
86
-
87 42
 }

Loading…
Cancel
Save