/**
   * Get all predefined all floating IPs defined in cartridge definition.
   *
   * @param array of {@link NetworkInterface}
   * @return list of predefined floating IPs
   */
  public List<String> getAllPredefinedFloatingIPs(NetworkInterface[] networkInterfaces) {
    String nwInterfacesNullMsg = "Input NetworkInterface array cannot be null";
    assertNotNull(networkInterfaces, nwInterfacesNullMsg);

    List<String> allPredefinedFloatingIPs = new ArrayList<String>();
    for (NetworkInterface networkInterface : networkInterfaces) {
      // if no floating networks defined, skip it
      if (null == networkInterface.getFloatingNetworks()) {
        continue;
      }
      FloatingNetwork[] floatingNetworks =
          networkInterface.getFloatingNetworks().getFloatingNetworks();
      if (floatingNetworks == null || floatingNetworks.length == 0) {
        continue;
      }

      for (FloatingNetwork floatingNetwork : floatingNetworks) {
        String floatingIP = floatingNetwork.getFloatingIP();
        // we are giving more priority to network uuid over fixed floating IPs
        // so if both network uuid and floating IPs defined, we are not going to assign those
        // floating IPs
        // so these can be assigned to some other interfaces
        // hence excluding from predefined floating IPs list
        String networkUuid = floatingNetwork.getNetworkUuid();
        if (networkUuid == null || networkUuid.isEmpty()) {
          if (floatingIP != null && InetAddresses.isInetAddress(floatingIP)) {
            allPredefinedFloatingIPs.add(floatingIP);
          }
        }
      }
    }
    return allPredefinedFloatingIPs;
  }
 public void testForStringIPv6Input() throws UnknownHostException {
   String ipStr = "3ffe::1";
   InetAddress ipv6Addr = null;
   // Shouldn't hit DNS, because it's an IP string literal.
   ipv6Addr = InetAddress.getByName(ipStr);
   assertEquals(ipv6Addr, InetAddresses.forString(ipStr));
   assertTrue(InetAddresses.isInetAddress(ipStr));
 }
 public void test3ff31() {
   try {
     InetAddresses.forString("3ffe:::1");
     fail("IllegalArgumentException expected");
   } catch (IllegalArgumentException expected) {
     // expected behavior
   }
   assertFalse(InetAddresses.isInetAddress("016.016.016.016"));
 }
  public void testConvertDottedQuadToHex() throws UnknownHostException {
    String[] ipStrings = {
      "7::0.128.0.127", "7::0.128.0.128", "7::128.128.0.127", "7::0.128.128.127"
    };

    for (String ipString : ipStrings) {
      // Shouldn't hit DNS, because it's an IP string literal.
      InetAddress ipv6Addr = InetAddress.getByName(ipString);
      assertEquals(ipv6Addr, InetAddresses.forString(ipString));
      assertTrue(InetAddresses.isInetAddress(ipString));
    }
  }
  public void testForStringIPv6EightColons() throws UnknownHostException {
    String[] eightColons = {
      "::7:6:5:4:3:2:1", "::7:6:5:4:3:2:0", "7:6:5:4:3:2:1::", "0:6:5:4:3:2:1::",
    };

    for (int i = 0; i < eightColons.length; i++) {
      InetAddress ipv6Addr = null;
      // Shouldn't hit DNS, because it's an IP string literal.
      ipv6Addr = InetAddress.getByName(eightColons[i]);
      assertEquals(ipv6Addr, InetAddresses.forString(eightColons[i]));
      assertTrue(InetAddresses.isInetAddress(eightColons[i]));
    }
  }
  /**
   * Tests the SMTP settings from the initial setup form, rendering the result as JSON.
   *
   * @param setup the initial setup form.
   */
  @Restrictions({
    @Restrict({"SYSTEM_ADMIN", "SECURITY_ADMIN"}),
    @Restrict({"RESTRICTED_SYSTEM_ADMIN", "RESTRICTED_SECURITY_ADMIN"})
  })
  public static void testSmtpSettings(SetupForm setup) {
    setup.validateSmtp();
    Validation.required("setup.smtpTo", setup.smtpTo);
    Validation.email("setup.smtpTo", setup.smtpTo);
    if (Validation.hasErrors()) {
      renderJSON(ValidationResponse.collectErrors());
    }

    MailSettingsValidator.Settings settings = new MailSettingsValidator.Settings();

    if (StringUtils.isNotEmpty(setup.nameservers)
        && !InetAddresses.isInetAddress(setup.smtpServer)) {
      Set<String> ips = Sets.newHashSet(setup.nameservers.split(","));

      try {
        settings.server = DnsUtils.getHostIpAddress(ips, setup.smtpServer).getHostAddress();
      } catch (ViPRException e) {
        renderJSON(ValidationResponse.invalid(e.getMessage()));
      }
    } else {
      settings.server = setup.smtpServer;
    }
    settings.port = ConfigProperties.getPort(setup.smtpPort, setup.smtpEnableTls);
    settings.username = setup.smtpUsername;
    settings.password = PasswordUtil.decryptedValue(setup.smtpPassword);
    settings.channel = StringUtils.equals("yes", setup.smtpEnableTls) ? "starttls" : "clear";
    settings.authType = setup.smtpAuthType;
    settings.fromAddress = setup.smtpFrom;

    try {
      MailSettingsValidator.validate(settings, setup.smtpTo);
    } catch (RuntimeException e) {
      Logger.error(e, "Failed to send email");
      Validation.addError(null, "setup.testEmail.failure", e.getMessage());
      if (StringUtils.isEmpty(setup.nameservers)) {
        Validation.addError(null, "setup.smtpServer.invalidEmptyNameserver");
      }
    }

    if (Validation.hasErrors()) {
      renderJSON(ValidationResponse.collectErrors());
    } else {
      renderJSON(ValidationResponse.valid(MessagesUtils.get("setup.testEmail.success")));
    }
  }
 public static long ipToLong(String ip) throws ElasticsearchIllegalArgumentException {
   try {
     if (!InetAddresses.isInetAddress(ip)) {
       throw new ElasticsearchIllegalArgumentException(
           "failed to parse ip [" + ip + "], not a valid ip address");
     }
     String[] octets = pattern.split(ip);
     if (octets.length != 4) {
       throw new ElasticsearchIllegalArgumentException(
           "failed to parse ip [" + ip + "], not a valid ipv4 address (4 dots)");
     }
     return (Long.parseLong(octets[0]) << 24)
         + (Integer.parseInt(octets[1]) << 16)
         + (Integer.parseInt(octets[2]) << 8)
         + Integer.parseInt(octets[3]);
   } catch (Exception e) {
     if (e instanceof ElasticsearchIllegalArgumentException) {
       throw (ElasticsearchIllegalArgumentException) e;
     }
     throw new ElasticsearchIllegalArgumentException("failed to parse ip [" + ip + "]", e);
   }
 }
Beispiel #8
0
 public static void checkIpAddress(String ip) {
   InetAddresses.isInetAddress(ip);
 }
 /**
  * Check whether the given IP is valid.
  *
  * @param ip IP to be validated
  * @return true if valid, false otherwise
  */
 private boolean isValidIP(String ip) {
   return (ip != null && InetAddresses.isInetAddress(ip));
 }
  @Override
  public boolean onCommand(
      final CommandSender sender, Command command, String commandName, String[] args) {
    if (args.length < 1) {
      return false;
    }

    if (CommandUtils.isValidNameDelimiter(args[0])) {
      CommandUtils.handleMultipleNames(sender, commandName, args);
      return true;
    }

    final String ipStr = args[0];
    final boolean isName = !InetAddresses.isInetAddress(ipStr);

    if (isName && ipStr.length() > 16) {
      Message message = Message.get("sender.error.invalidIp");
      message.set("ip", ipStr);

      sender.sendMessage(message.toString());
      return true;
    }

    final String reason = args.length > 1 ? CommandUtils.getReason(1, args).getMessage() : "";

    plugin
        .getServer()
        .getScheduler()
        .runTaskAsynchronously(
            plugin,
            new Runnable() {

              @Override
              public void run() {
                final Long ip = CommandUtils.getIp(ipStr);

                if (ip == null) {
                  sender.sendMessage(
                      Message.get("sender.error.notFound").set("player", ipStr).toString());
                  return;
                }

                if (!plugin.getIpMuteStorage().isMuted(ip)) {
                  Message message = Message.get("unmuteip.error.noExists");
                  message.set("ip", ipStr);

                  sender.sendMessage(message.toString());
                  return;
                }

                IpMuteData mute = plugin.getIpMuteStorage().getMute(ip);

                final PlayerData actor = CommandUtils.getActor(sender);

                if (actor == null) return;

                boolean unmuted;

                try {
                  unmuted = plugin.getIpMuteStorage().unmute(mute, actor, reason);
                } catch (SQLException e) {
                  sender.sendMessage(Message.get("sender.error.exception").toString());
                  e.printStackTrace();
                  return;
                }

                if (!unmuted) {
                  return;
                }

                Message message = Message.get("unmuteip.notify");
                message.set("ip", ipStr).set("actor", actor.getName()).set("reason", reason);

                if (!sender.hasPermission("bm.notify.unmuteip")) {
                  message.sendTo(sender);
                }

                CommandUtils.broadcast(message.toString(), "bm.notify.unmuteip");
              }
            });

    return true;
  }
Beispiel #11
0
  public void testForStringBogusInput() {
    String[] bogusInputs = {
      "",
      "016.016.016.016",
      "016.016.016",
      "016.016",
      "016",
      "000.000.000.000",
      "000",
      "0x0a.0x0a.0x0a.0x0a",
      "0x0a.0x0a.0x0a",
      "0x0a.0x0a",
      "0x0a",
      "42.42.42.42.42",
      "42.42.42",
      "42.42",
      "42",
      "42..42.42",
      "42..42.42.42",
      "42.42.42.42.",
      "42.42.42.42...",
      ".42.42.42.42",
      "...42.42.42.42",
      "42.42.42.-0",
      "42.42.42.+0",
      ".",
      "...",
      "bogus",
      "bogus.com",
      "192.168.0.1.com",
      "12345.67899.-54321.-98765",
      "257.0.0.0",
      "42.42.42.-42",
      "3ffe::1.net",
      "3ffe::1::1",
      "1::2::3::4:5",
      "::7:6:5:4:3:2:", // should end with ":0"
      ":6:5:4:3:2:1::", // should begin with "0:"
      "2001::db:::1",
      "FEDC:9878",
      "+1.+2.+3.4",
      "1.2.3.4e0",
      "::7:6:5:4:3:2:1:0", // too many parts
      "7:6:5:4:3:2:1:0::", // too many parts
      "9:8:7:6:5:4:3::2:1", // too many parts
      "0:1:2:3::4:5:6:7", // :: must remove at least one 0.
      "3ffe:0:0:0:0:0:0:0:1", // too many parts (9 instead of 8)
      "3ffe::10000", // hextet exceeds 16 bits
      "3ffe::goog",
      "3ffe::-0",
      "3ffe::+0",
      "3ffe::-1",
      ":",
      ":::",
      "::1.2.3",
      "::1.2.3.4.5",
      "::1.2.3.4:",
      "1.2.3.4::",
      "2001:db8::1:",
      ":2001:db8::1",
      ":1:2:3:4:5:6:7",
      "1:2:3:4:5:6:7:",
      ":1:2:3:4:5:6:"
    };

    for (int i = 0; i < bogusInputs.length; i++) {
      try {
        InetAddresses.forString(bogusInputs[i]);
        fail("IllegalArgumentException expected for '" + bogusInputs[i] + "'");
      } catch (IllegalArgumentException expected) {
        // expected behavior
      }
      assertFalse(InetAddresses.isInetAddress(bogusInputs[i]));
    }
  }