|
@@ -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
|
}
|