Commit 43dfbd5a authored by Liang Ding's avatar Liang Ding

🔒 加入社区黑名单 IP

parent 660f049c
...@@ -19,19 +19,29 @@ package org.b3log.solo.processor; ...@@ -19,19 +19,29 @@ package org.b3log.solo.processor;
import org.b3log.latke.http.RequestContext; import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.handler.Handler; import org.b3log.latke.http.handler.Handler;
import org.b3log.latke.util.Requests;
import org.b3log.latke.util.Stopwatchs; import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.util.Solos;
/** /**
* Before request handler. * Before request handler.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.0, Nov 3, 2019 * @version 1.0.0.1, Jan 11, 2020
* @since 3.6.7 * @since 3.6.7
*/ */
public class BeforeRequestHandler implements Handler { public class BeforeRequestHandler implements Handler {
@Override @Override
public void handle(final RequestContext context) { public void handle(final RequestContext context) {
final String remoteAddr = Requests.getRemoteAddr(context.getRequest());
if (Solos.BLACKLIST_IPS.contains(remoteAddr)) {
context.sendStatus(200);
context.abort();
return;
}
Stopwatchs.start("Request Initialized [requestURI=" + context.requestURI() + "]"); Stopwatchs.start("Request Initialized [requestURI=" + context.requestURI() + "]");
} }
} }
...@@ -22,6 +22,7 @@ import org.b3log.latke.logging.Level; ...@@ -22,6 +22,7 @@ import org.b3log.latke.logging.Level;
import org.b3log.latke.logging.Logger; import org.b3log.latke.logging.Logger;
import org.b3log.latke.service.annotation.Service; import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.Stopwatchs; import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.util.Solos;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
...@@ -96,27 +97,27 @@ public class CronMgmtService { ...@@ -96,27 +97,27 @@ public class CronMgmtService {
SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> { SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try { try {
articleMgmtService.refreshGitHub(); Solos.reloadBlacklistIPs();
userMgmtService.refreshUSite();
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Executes cron failed", e); LOGGER.log(Level.ERROR, "Executes cron failed", e);
} finally { } finally {
Stopwatchs.release(); Stopwatchs.release();
} }
}, delay, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS); }, delay, 30, TimeUnit.MINUTES);
delay += 2000; delay += 2000;
SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> { SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try { try {
articleMgmtService.refreshGitHub();
userMgmtService.refreshUSite();
exportService.exportHacPai(); exportService.exportHacPai();
} catch (final Exception e) { } catch (final Exception e) {
LOGGER.log(Level.ERROR, "Executes cron failed", e); LOGGER.log(Level.ERROR, "Executes cron failed", e);
} finally { } finally {
Stopwatchs.release(); Stopwatchs.release();
} }
}, delay + 1000 * 60 * 10, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS); }, delay, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
delay += 2000; delay += 2000;
} }
/** /**
......
...@@ -38,17 +38,20 @@ import org.b3log.solo.model.Article; ...@@ -38,17 +38,20 @@ import org.b3log.solo.model.Article;
import org.b3log.solo.model.Common; import org.b3log.solo.model.Common;
import org.b3log.solo.model.UserExt; import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository; import org.b3log.solo.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.List;
import java.util.MissingResourceException; import java.util.MissingResourceException;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import java.util.Set; import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
/** /**
* Solo utilities. * Solo utilities.
* *
* @author <a href="http://88250.b3log.org">Liang Ding</a> * @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.9.0.6, Jan 8, 2020 * @version 1.10.0.0, Jan 11, 2020
* @since 2.8.0 * @since 2.8.0
*/ */
public final class Solos { public final class Solos {
...@@ -113,6 +116,31 @@ public final class Solos { ...@@ -113,6 +116,31 @@ public final class Solos {
COOKIE_SECRET = cookieSecret; COOKIE_SECRET = cookieSecret;
} }
/**
* Blacklist IPs.
*/
public static final List<String> BLACKLIST_IPS = new CopyOnWriteArrayList<>();
/**
* Reloads blacklist IPs.
*/
public static void reloadBlacklistIPs() {
final HttpResponse res = HttpRequest.post("https://hacpai.com/apis/blacklist/ip").trustAllCerts(true).
connectionTimeout(3000).timeout(7000).header("User-Agent", Solos.USER_AGENT).send();
if (200 != res.statusCode()) {
return;
}
res.charset("UTF-8");
final JSONObject result = new JSONObject(res.bodyText());
if (0 != result.optInt(Keys.CODE)) {
return;
}
final JSONArray ips = result.optJSONArray(Common.DATA);
BLACKLIST_IPS.clear();
BLACKLIST_IPS.addAll(CollectionUtils.jsonArrayToList(ips));
}
/** /**
* Constructs a successful result. * Constructs a successful result.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment