You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DNSCommand.java 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.dns;
  18. import com.dmdirc.commandparser.BaseCommandInfo;
  19. import com.dmdirc.commandparser.CommandArguments;
  20. import com.dmdirc.commandparser.CommandInfo;
  21. import com.dmdirc.commandparser.CommandType;
  22. import com.dmdirc.commandparser.commands.BaseCommand;
  23. import com.dmdirc.commandparser.commands.context.CommandContext;
  24. import com.dmdirc.interfaces.CommandController;
  25. import com.dmdirc.interfaces.WindowModel;
  26. import com.google.common.net.InetAddresses;
  27. import javax.annotation.Nonnull;
  28. import javax.inject.Inject;
  29. import java.net.InetAddress;
  30. import java.net.UnknownHostException;
  31. import java.util.ArrayList;
  32. import java.util.Collection;
  33. import java.util.Timer;
  34. import java.util.TimerTask;
  35. /**
  36. * Performs DNS lookups for nicknames, hostnames or IPs.
  37. */
  38. public class DNSCommand extends BaseCommand {
  39. /** A command info object for this command. */
  40. public static final CommandInfo INFO = new BaseCommandInfo("dns",
  41. "dns <IP|hostname> - Performs DNS lookup of the specified ip/hostname/nickname",
  42. CommandType.TYPE_GLOBAL);
  43. /**
  44. * Creates a new instance of this command.
  45. *
  46. * @param controller The controller to use for command information.
  47. */
  48. @Inject
  49. public DNSCommand(final CommandController controller) {
  50. super(controller);
  51. }
  52. @Override
  53. public void execute(@Nonnull final WindowModel origin,
  54. final CommandArguments args, final CommandContext context) {
  55. if (args.getArguments().length == 0) {
  56. showUsage(origin, args.isSilent(), "dns", "<IP|hostname>");
  57. return;
  58. }
  59. showOutput(origin, args.isSilent(), "Resolving: " + args.getArguments()[0]);
  60. new Timer("DNS Command Timer").schedule(new TimerTask() {
  61. @Override
  62. public void run() {
  63. resolve(origin, args.isSilent(), args.getArguments()[0]);
  64. }
  65. }, 0);
  66. }
  67. private void resolve(@Nonnull final WindowModel origin, final boolean isSilent,
  68. final String arg) {
  69. try {
  70. final InetAddress address = InetAddresses.forString(arg);
  71. showOutput(origin, isSilent, "Resolved: "
  72. + arg + ": " + address.getCanonicalHostName());
  73. } catch (IllegalArgumentException ex) {
  74. showOutput(origin, isSilent, "Resolved: "
  75. + arg + ": " + getIPs(arg));
  76. }
  77. }
  78. /**
  79. * Returns the IP(s) for a hostname.
  80. *
  81. * @param hostname Hostname to resolve.
  82. *
  83. * @return Resolved IP(s)
  84. */
  85. private String getIPs(final String hostname) {
  86. final Collection<String> results = new ArrayList<>();
  87. try {
  88. final InetAddress[] ips = InetAddress.getAllByName(hostname);
  89. for (InetAddress ip : ips) {
  90. results.add(ip.getHostAddress());
  91. }
  92. } catch (UnknownHostException ex) {
  93. return "[]";
  94. }
  95. return results.toString();
  96. }
  97. }