|
|
@@ -40,8 +40,8 @@ public class BlacklistService {
|
|
|
private R2dbcEntityTemplate template;
|
|
|
|
|
|
//private static final List<String> firstUrl = List.of("out/planNew/hfoInfo","out/planNew/hfoInfo","out/listening/landingPageList");
|
|
|
- private static final CopyOnWriteArrayList<String> ip_balck_list = new CopyOnWriteArrayList<>();
|
|
|
- private static final CopyOnWriteArrayList<String> ip_white_list = new CopyOnWriteArrayList<>();
|
|
|
+// private static final CopyOnWriteArrayList<String> ip_balck_list = new CopyOnWriteArrayList<>();
|
|
|
+// private static final CopyOnWriteArrayList<String> ip_white_list = new CopyOnWriteArrayList<>();
|
|
|
private static final AtomicInteger ipRateLimitRange= new AtomicInteger();//访问次数
|
|
|
private static final String key="ip_rate_limit:";//为用户ip访问记录次数ip_rate_limit:1.180.116.135
|
|
|
public static final AtomicReference<String> wxUrl=new AtomicReference<>();
|
|
|
@@ -51,7 +51,7 @@ public class BlacklistService {
|
|
|
* @param exchange
|
|
|
* @return
|
|
|
*/
|
|
|
- public Mono<Boolean> getRateReturn(ServerWebExchange exchange){
|
|
|
+ public Mono<Boolean> getRateReturn(ServerWebExchange exchange,String unique){
|
|
|
ServerHttpRequest httpRequest = exchange.getRequest();
|
|
|
InetSocketAddress remoteAddress = httpRequest.getRemoteAddress();
|
|
|
String ipAddress;
|
|
|
@@ -62,7 +62,13 @@ public class BlacklistService {
|
|
|
}
|
|
|
if (Objects.nonNull(ipAddress)) {
|
|
|
try {
|
|
|
- return getIpRateNum(ipAddress);
|
|
|
+ return getFrist( unique).flatMap(f->{
|
|
|
+ if (f) {
|
|
|
+ return getIpRateNum(ipAddress);
|
|
|
+ }else {
|
|
|
+ return Mono.just(true);
|
|
|
+ }
|
|
|
+ }) ;
|
|
|
}catch (Exception e){
|
|
|
log.error("ip黑名单验证:" +ipAddress+"::Error:::"+e.getMessage());
|
|
|
return Mono.just(false);
|
|
|
@@ -70,16 +76,25 @@ public class BlacklistService {
|
|
|
}
|
|
|
return Mono.just(false);
|
|
|
}
|
|
|
+ private Mono<Boolean> getFrist(String unique){
|
|
|
+ if(Objects.nonNull(unique)){
|
|
|
+ return reactiveRedisTemplate.opsForValue().get(unique)
|
|
|
+ .switchIfEmpty(reactiveRedisTemplate.opsForValue().set(unique, "1", Duration.ofHours(5L)).flatMap(f-> Mono.just("9999")))
|
|
|
+ .flatMap(f->{
|
|
|
+ if ("9999".equals(f)) {
|
|
|
+ return Mono.just(true);
|
|
|
+ }else {
|
|
|
+ return Mono.just(false);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }else {
|
|
|
+ return Mono.just(true);
|
|
|
+ }
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取ip访问次数
|
|
|
*/
|
|
|
public Mono<Boolean> getIpRateNum(String ip){
|
|
|
- if (ip_white_list.contains(ip)) {
|
|
|
- return Mono.just(true);
|
|
|
- }
|
|
|
- if (ip_balck_list.contains(ip)) {
|
|
|
- return Mono.just(false);
|
|
|
- }
|
|
|
String keys=key+ip;
|
|
|
long secondsUntilNextMidnight = getSecondsUntilNextMidnight();
|
|
|
return reactiveRedisTemplate.opsForValue().get(keys)
|
|
|
@@ -141,35 +156,35 @@ public class BlacklistService {
|
|
|
/**
|
|
|
* 数据源同步本地 黑白名单
|
|
|
*/
|
|
|
- @Scheduled(fixedDelay = 160000)
|
|
|
- public void getIpBalckList(){
|
|
|
- template.select(Query.query(Criteria.where("status").is(1)), IpWhite.class).collectList().flatMap(ipInfo -> {
|
|
|
- Map<Integer, List<IpWhite>> collect = ipInfo.stream().filter(f -> Objects.nonNull(f.type())).collect(Collectors.groupingBy(IpWhite::type));
|
|
|
- List<String> ips = getCollect(collect,1);
|
|
|
- if(!ips.isEmpty()){
|
|
|
- List<String> addIps = getAddIps(ips, ip_white_list);
|
|
|
- if (!addIps.isEmpty()) {
|
|
|
- ip_white_list.addAll(addIps);
|
|
|
- }
|
|
|
- List<String> removers = getAddIps(ip_white_list, ips);
|
|
|
- if(!removers.isEmpty()){
|
|
|
- ip_white_list.removeAll(removers);
|
|
|
- }
|
|
|
- }
|
|
|
- List<String> ips2 = getCollect(collect,2);
|
|
|
- if(!ips.isEmpty()){
|
|
|
- List<String> addIps2 = getAddIps(ips2, ip_balck_list);
|
|
|
- if (!addIps2.isEmpty()) {
|
|
|
- ip_balck_list.addAll(addIps2);
|
|
|
- }
|
|
|
- List<String> removers2 = getAddIps(ip_balck_list, ips2);
|
|
|
- if(!removers2.isEmpty()){
|
|
|
- ip_balck_list.removeAll(removers2);
|
|
|
- }
|
|
|
- }
|
|
|
- return Mono.just(ipInfo);
|
|
|
- }).subscribe();
|
|
|
- }
|
|
|
+// @Scheduled(fixedDelay = 160000)
|
|
|
+// public void getIpBalckList(){
|
|
|
+// template.select(Query.query(Criteria.where("status").is(1)), IpWhite.class).collectList().flatMap(ipInfo -> {
|
|
|
+// Map<Integer, List<IpWhite>> collect = ipInfo.stream().filter(f -> Objects.nonNull(f.type())).collect(Collectors.groupingBy(IpWhite::type));
|
|
|
+// List<String> ips = getCollect(collect,1);
|
|
|
+// if(!ips.isEmpty()){
|
|
|
+// List<String> addIps = getAddIps(ips, ip_white_list);
|
|
|
+// if (!addIps.isEmpty()) {
|
|
|
+// ip_white_list.addAll(addIps);
|
|
|
+// }
|
|
|
+// List<String> removers = getAddIps(ip_white_list, ips);
|
|
|
+// if(!removers.isEmpty()){
|
|
|
+// ip_white_list.removeAll(removers);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// List<String> ips2 = getCollect(collect,2);
|
|
|
+// if(!ips.isEmpty()){
|
|
|
+// List<String> addIps2 = getAddIps(ips2, ip_balck_list);
|
|
|
+// if (!addIps2.isEmpty()) {
|
|
|
+// ip_balck_list.addAll(addIps2);
|
|
|
+// }
|
|
|
+// List<String> removers2 = getAddIps(ip_balck_list, ips2);
|
|
|
+// if(!removers2.isEmpty()){
|
|
|
+// ip_balck_list.removeAll(removers2);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return Mono.just(ipInfo);
|
|
|
+// }).subscribe();
|
|
|
+// }
|
|
|
|
|
|
|
|
|
@NotNull
|