Skip to content

Commit

Permalink
🔒 加入社区黑名单 IP
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jan 11, 2020
1 parent 660f049 commit 43dfbd5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/main/java/org/b3log/solo/processor/BeforeRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,29 @@

import org.b3log.latke.http.RequestContext;
import org.b3log.latke.http.handler.Handler;
import org.b3log.latke.util.Requests;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.util.Solos;

/**
* Before request handler.
*
* @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
*/
public class BeforeRequestHandler implements Handler {

@Override
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() + "]");
}
}
11 changes: 6 additions & 5 deletions src/main/java/org/b3log/solo/service/CronMgmtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.b3log.latke.logging.Logger;
import org.b3log.latke.service.annotation.Service;
import org.b3log.latke.util.Stopwatchs;
import org.b3log.solo.util.Solos;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand Down Expand Up @@ -96,27 +97,27 @@ public void start() {

SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try {
articleMgmtService.refreshGitHub();
userMgmtService.refreshUSite();
Solos.reloadBlacklistIPs();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Executes cron failed", e);
} finally {
Stopwatchs.release();
}
}, delay, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
}, delay, 30, TimeUnit.MINUTES);
delay += 2000;

SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try {
articleMgmtService.refreshGitHub();
userMgmtService.refreshUSite();
exportService.exportHacPai();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Executes cron failed", e);
} finally {
Stopwatchs.release();
}
}, delay + 1000 * 60 * 10, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
}, delay, 1000 * 60 * 60 * 24, TimeUnit.MILLISECONDS);
delay += 2000;

}

/**
Expand Down
30 changes: 29 additions & 1 deletion src/main/java/org/b3log/solo/util/Solos.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@
import org.b3log.solo.model.Common;
import org.b3log.solo.model.UserExt;
import org.b3log.solo.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Solo utilities.
*
* @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
*/
public final class Solos {
Expand Down Expand Up @@ -113,6 +116,31 @@ public final class Solos {
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.
*
Expand Down

0 comments on commit 43dfbd5

Please # to comment.