cn.starboot.socket.plugins.BlackListPlugin Maven / Gradle / Ivy
/*
* Copyright 2019 The aio-socket Project
*
* The aio-socket Project Licenses this file to you 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:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.starboot.socket.plugins;
import cn.starboot.socket.core.AioConfig;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* 黑名单插件,smart-socket会拒绝与黑名单中的IP建立连接
*
* @author MDong
* @version 2.10.1.v20211002-RELEASE
*/
public final class BlackListPlugin extends AbstractPlugin {
private final ConcurrentLinkedQueue ipBlackList = new ConcurrentLinkedQueue<>();
public BlackListPlugin() {
System.out.println("aio-socket version: " + AioConfig.VERSION + "; server kernel's black list plugin added successfully");
}
@Override
public AsynchronousSocketChannel shouldAccept(AsynchronousSocketChannel channel) {
InetSocketAddress inetSocketAddress = null;
try {
inetSocketAddress = (InetSocketAddress) channel.getRemoteAddress();
} catch (IOException e) {
System.out.println("get remote address error " + e);
}
if (inetSocketAddress == null) {
return channel;
}
for (BlackListRule rule : ipBlackList) {
if (!rule.access(inetSocketAddress)) {
return null;
}
}
return channel;
}
/**
* 添加黑名单失败规则
*
* @param rule .
*/
public void addRule(BlackListRule rule) {
ipBlackList.add(rule);
}
/**
* 移除黑名单规则
*
* @param rule .
*/
public void removeRule(BlackListRule rule) {
ipBlackList.remove(rule);
}
/**
* 黑名单规则定义
*/
public interface BlackListRule {
/**
* 是否允许建立连接
*
* @param address .
* @return .
*/
boolean access(InetSocketAddress address);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy