Skip to content

Commit

Permalink
пофиксил постоянную блокировку IP
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Aug 17, 2024
1 parent 81cf165 commit 7425299
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
7 changes: 2 additions & 5 deletions src/main/java/ru/org/linux/auth/BanIPController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;

@Controller
Expand All @@ -42,7 +40,7 @@ public BanIPController(IPBlockDao ipBlockDao) {
this.ipBlockDao = ipBlockDao;
}

@RequestMapping(value="/banip.jsp", method= RequestMethod.POST)
@RequestMapping(value="/banip.jsp", method=RequestMethod.POST)
public ModelAndView banIP(
HttpServletRequest request,
@RequestParam("ip") String ip,
Expand Down Expand Up @@ -79,8 +77,7 @@ public ModelAndView banIP(

User moderator = AuthUtil.getCurrentUser();

ipBlockDao.blockIP(ip, moderator.getId(), reason, banTo.map(v -> new Timestamp(v.toInstant().toEpochMilli())).orElse(null),
allowPosting, captchaRequired);
ipBlockDao.blockIP(ip, moderator.getId(), reason, banTo.orElse(null), allowPosting, captchaRequired);

return new ModelAndView(new RedirectView("sameip.jsp?ip=" + URLEncoder.encode(ip, StandardCharsets.UTF_8)));
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ru/org/linux/auth/IPBlockDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import javax.annotation.Nullable;
import javax.sql.DataSource;
import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.util.List;

@Repository
Expand Down Expand Up @@ -57,7 +57,7 @@ public static void checkBlockIP(IPBlockInfo block, Errors errors, @Nullable User
}
}

public void blockIP(String ip, int moderatorId, String reason, @Nullable Timestamp banUntil,
public void blockIP(String ip, int moderatorId, String reason, @Nullable OffsetDateTime banUntil,
boolean allowPosting, boolean captchaRequired) {
IPBlockInfo blockInfo = getBlockInfo(ip);

Expand Down
26 changes: 13 additions & 13 deletions src/main/java/ru/org/linux/spring/DelIPController.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@ public ModelAndView delIp(@RequestParam("reason") String reason,
default -> throw new UserErrorException("Invalid count");
};

Optional<OffsetDateTime> banTo = switch (banTime) {
case "hour" -> Optional.of(OffsetDateTime.now().plusHours(1));
case "day" -> Optional.of(OffsetDateTime.now().plusDays(1));
case "month" -> Optional.of(OffsetDateTime.now().plusMonths(1));
case "3month" -> Optional.of(OffsetDateTime.now().plusMonths(3));
case "6month" -> Optional.of(OffsetDateTime.now().plusMonths(6));
case null, default -> Optional.empty();
};

Timestamp ts = new Timestamp(delFrom.toEpochMilli());
params.put("message", "Удаляем темы и сообщения после "+ ts +" с IP "+ip+"<br>");

Expand All @@ -97,10 +88,20 @@ public ModelAndView delIp(@RequestParam("reason") String reason,
params.put("topics", deleteResult.getDeletedTopicIds().size()); // кол-во удаленных топиков
params.put("deleted", deleteResult.getDeleteInfo());

if (banTo.isPresent()) {
if (banTime != null) {
Optional<OffsetDateTime> banTo = switch (banTime) {
case "hour" -> Optional.of(OffsetDateTime.now().plusHours(1));
case "day" -> Optional.of(OffsetDateTime.now().plusDays(1));
case "month" -> Optional.of(OffsetDateTime.now().plusMonths(1));
case "3month" -> Optional.of(OffsetDateTime.now().plusMonths(3));
case "6month" -> Optional.of(OffsetDateTime.now().plusMonths(6));
case "unlim" -> Optional.empty();
default -> throw new UserErrorException("Invalid count");
};

boolean allowPosting, captchaRequired;

switch(banMode) {
switch (banMode) {
case "anonymous_and_captcha" -> {
allowPosting = true;
captchaRequired = true;
Expand All @@ -115,8 +116,7 @@ public ModelAndView delIp(@RequestParam("reason") String reason,
}
}

ipBlockDao.blockIP(ip, moderator.getId(), reason,
banTo.map(v -> new Timestamp(v.toInstant().toEpochMilli())).get(), allowPosting, captchaRequired);
ipBlockDao.blockIP(ip, moderator.getId(), reason, banTo.orElse(null), allowPosting, captchaRequired);
}

for (int topicId : deleteResult.getDeletedTopicIds()) {
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/ru/org/linux/auth/TorBlockUpdater.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2022 Linux.org.ru
* Copyright 1998-2024 Linux.org.ru
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -15,12 +15,11 @@
package ru.org.linux.auth

import com.typesafe.scalalogging.StrictLogging
import org.joda.time.DateTime
import org.springframework.scheduling.annotation.Scheduled
import org.springframework.stereotype.Component
import sttp.client3.*

import java.sql.Timestamp
import java.time.OffsetDateTime

@Component
class TorBlockUpdater(httpClient: SttpBackend[Identity, Any], dao: IPBlockDao) extends StrictLogging {
Expand All @@ -35,8 +34,7 @@ class TorBlockUpdater(httpClient: SttpBackend[Identity, Any], dao: IPBlockDao) e
logger.debug("Updating TOR exit node list")

body.linesIterator.foreach { ip =>
dao.blockIP(ip, 0, "TOR Exit Node", new Timestamp(DateTime.now().plusMonths(1).getMillis),
true, false)
dao.blockIP(ip, 0, "TOR Exit Node", OffsetDateTime.now().plusMonths(1), true, false)
}
case Left(error) =>
logger.warn(s"Can't update TOR exit node list: $error")
Expand Down

0 comments on commit 7425299

Please sign in to comment.