Esempio n. 1
0
  @FilterWith(XSRFFilter.class)
  public Result generate(
      Context context,
      @Param("groups") Integer groups,
      @Param("searchPerGroup") Integer searchPerGroup,
      @Param("targetPerGroup") Integer targetPerGroup) {
    FlashScope flash = context.getFlashScope();

    if (groups == null
        || groups < 1
        || searchPerGroup == null
        || searchPerGroup < 1
        || targetPerGroup == null
        || targetPerGroup < 1) {
      flash.put("error", "error.invalidParameters");
      return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
    }

    for (int i = 0; i < groups; i++) {
      Group group = new Group(Group.Module.GOOGLE, "group#" + i);
      baseDB.group.insert(group);

      List<GoogleSearch> searches = new ArrayList<>();
      for (int j = 0; j < searchPerGroup; j++) {
        GoogleSearch search = new GoogleSearch("search#" + j + "#" + group.getName());
        search.setTld("com");
        searches.add(search);
      }
      googleDB.search.insert(searches, group.getId());

      List<GoogleTarget> targets = new ArrayList<>();
      for (int j = 0; j < targetPerGroup; j++) {
        int targetFakeId = j + 1;
        GoogleTarget target =
            new GoogleTarget(
                group.getId(),
                "target#" + targetFakeId + "#" + group.getName(),
                PatternType.REGEX,
                "^https?://www.site" + targetFakeId + ".com.+");
        targets.add(target);
      }
      googleDB.target.insert(targets);
    }

    flash.put("warning", "admin.debug.generated");
    return Results.redirect(router.getReverseRoute(DebugController.class, "debug"));
  }
  @FilterWith({XSRFFilter.class, AdminFilter.class})
  public Result addSearch(
      Context context,
      @Params("keyword[]") String[] keywords,
      @Params("tld[]") String tlds[],
      @Params("datacenter[]") String[] datacenters,
      @Params("device[]") Integer[] devices,
      @Params("local[]") String[] locals,
      @Params("custom[]") String[] customs) {
    FlashScope flash = context.getFlashScope();
    Group group = context.getAttribute("group", Group.class);

    if (keywords == null
        || tlds == null
        || datacenters == null
        || devices == null
        || locals == null
        || customs == null
        || keywords.length != tlds.length
        || keywords.length != datacenters.length
        || keywords.length != devices.length
        || keywords.length != locals.length
        || keywords.length != customs.length) {
      flash.error("error.invalidParameters");
      return Results.redirect(
          router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
    }

    Set<GoogleSearch> searches = new HashSet<>();

    for (int i = 0; i < keywords.length; i++) {
      GoogleSearch search = new GoogleSearch();

      if (keywords[i].isEmpty()) {
        flash.error("admin.google.keywordEmpty");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }
      search.setKeyword(keywords[i]);

      if (!Validator.isGoogleTLD(tlds[i])) {
        flash.error("admin.google.invalidTLD");
        return Results.redirect(
            router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId()));
      }
      search.setTld(tlds[i]);

      if (!datacenters[i].isEmpty()) {
        if (!Validator.isIPv4(datacenters[i])) {
          flash.error("error.invalidIP");
          return Results.redirect(
              router.getReverseRoute(
                  GoogleGroupController.class, "view", "groupId", group.getId()));
        }
        search.setDatacenter(datacenters[i]);
      }

      if (devices[i] != null && devices[i] >= 0 && devices[i] < GoogleDevice.values().length) {
        search.setDevice(GoogleDevice.values()[devices[i]]);
      } else {
        search.setDevice(GoogleDevice.DESKTOP);
      }

      if (!Validator.isEmpty(locals[i])) {
        search.setLocal(locals[i]);
      }

      if (!Validator.isEmpty(customs[i])) {
        search.setCustomParameters(customs[i]);
      }

      searches.add(search);
    }

    List<GoogleSearch> knownSearches = new ArrayList<>();
    synchronized (searchLock) {
      for (GoogleSearch search : searches) {
        int id = googleDB.search.getId(search);
        if (id > 0) {
          search.setId(id);
          knownSearches.add(search);
        }
      }
      googleDB.search.insert(searches, group.getId());
    }

    googleDB.serpRescan.rescan(null, getTargets(context), knownSearches, false);

    flash.success("google.group.searchInserted");
    return Results.redirect(
        router.getReverseRoute(GoogleGroupController.class, "view", "groupId", group.getId())
            + "#tab-searches");
  }