Skip to content

Commit

Permalink
Merge branch 'develop' into mainnet
Browse files Browse the repository at this point in the history
  • Loading branch information
niels1286 committed Dec 4, 2018
2 parents dac74fa + 503a636 commit fcfe8dc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public boolean argsValidate(String[] args) {
if (StringUtils.isBlank(args[1]) || StringUtils.isBlank(args[2])) {
return false;
}
if(!CreateP2shTransactionForm.validToData(args[3])){
if(!StringUtils.validAddressSimple(args[3])){
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import io.netty.util.ReferenceCountUtil;
import io.nuls.core.tools.log.Log;
import io.nuls.network.constant.NetworkParam;
import io.nuls.network.manager.ConnectionManager;
Expand Down Expand Up @@ -100,7 +101,7 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
SocketChannel socketChannel = (SocketChannel) ctx.channel();
String remoteIP = socketChannel.remoteAddress().getHostString();
int port = socketChannel.remoteAddress().getPort();
Log.info("-----------------client channelInactive node is null -----------------" + remoteIP + ":" + port);
// Log.info("-----------------client channelInactive node is null -----------------" + remoteIP + ":" + port);
}
}

Expand All @@ -109,25 +110,22 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
try {
Attribute<Node> nodeAttribute = ctx.channel().attr(key);
Node node = nodeAttribute.get();
ByteBuf buf = (ByteBuf) msg;

if (node != null) {
if (node.isAlive()) {
ByteBuf buf = (ByteBuf) msg;
try {
connectionManager.receiveMessage(buf, node);
} finally {
buf.release();
}
// NetworkThreadPool.doRead(buf, node);
connectionManager.receiveMessage(buf, node);
}
} else {
SocketChannel socketChannel = (SocketChannel) ctx.channel();
String remoteIP = socketChannel.remoteAddress().getHostString();
int port = socketChannel.remoteAddress().getPort();
Log.info("-----------------client channelRead node is null -----------------" + remoteIP + ":" + port);
// Log.info("-----------------client channelRead node is null -----------------" + remoteIP + ":" + port);
}
} catch (Exception e) {
throw e;
} finally {
ReferenceCountUtil.release(msg);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.ReferenceCountUtil;
import io.nuls.core.tools.log.Log;
import io.nuls.core.tools.network.IpUtil;
import io.nuls.kernel.context.NulsContext;
Expand Down Expand Up @@ -64,15 +65,18 @@ public class ServerChannelHandler extends ChannelInboundHandlerAdapter {

private ConnectionManager connectionManager = ConnectionManager.getInstance();

private static long severChannelRegister = 0;

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
super.channelRegistered(ctx);
SocketChannel channel = (SocketChannel) ctx.channel();

String remoteIP = channel.remoteAddress().getHostString();
severChannelRegister++;
// Log.info("----------------------severChannel Register count:" + severChannelRegister);
//查看是否是本机尝试连接本机地址 ,如果是直接关闭连接
if (networkParam.getLocalIps().contains(remoteIP)) {
// Log.info("----------------------本机尝试连接本机地址关闭 ------------------------- " + nodeId);
ctx.channel().close();
return;
}
Expand All @@ -83,7 +87,6 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
for (Node node : nodeMap.values()) {
if (node.getIp().equals(remoteIP)) {
if (node.getType() == Node.OUT) {
// Log.info("--------------- 相同ip外网连接 -----------------" + nodeId);
ctx.channel().close();
return;
//
Expand Down Expand Up @@ -158,17 +161,14 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
Node node = nodeManager.getNode(nodeId);
if (node != null && node.isAlive()) {
ByteBuf buf = (ByteBuf) msg;
// NetworkThreadPool.doRead(buf, node);
try {
connectionManager.receiveMessage(buf, node);
} finally {
buf.release();
}
connectionManager.receiveMessage(buf, node);
}
} catch (Exception e) {
// System.out.println(" ---------------------- server channelRead exception------------------------- " + nodeId);
e.printStackTrace();
throw e;
} finally {
ReferenceCountUtil.release(msg);
}
}

Expand All @@ -188,10 +188,10 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
// Log.info("----------------- server exceptionCaught -------------------");
if (!(cause instanceof IOException) ) {
if (!(cause instanceof IOException)) {
SocketChannel channel = (SocketChannel) ctx.channel();
String nodeId = IpUtil.getNodeId(channel.remoteAddress());
Log.error("----------------nodeId:" +nodeId);
Log.error("----------------nodeId:" + nodeId);
Log.error(cause);
// nodeManager.deleteNodeFromDB(nodeId);
// return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,17 @@ public NodeGroup getNodeGroup(String groupName) {
* 添加主动连接节点,并创建连接
*/
public boolean addNode(Node node) {
if (node.getIp().equals("222.183.234.88") || node.getIp().equals("222.183.238.45") || node.getIp().equals("85.26.34.202")) {
System.out.println(1);
}

//判断是否是本地地址
if (networkParam.getLocalIps().contains(node.getIp())) {
return false;
}
if (node.getStatus() != Node.WAIT) {
return false;
}
if(!IpUtil.isboolIp(node.getIp())) {
return false;
}
lock.lock();
try {
//同一ip地址,不再重复连接
Expand All @@ -234,7 +235,6 @@ public boolean addNode(Node node) {
if (count >= 50) {
return false;
}

node.setType(Node.OUT);
node.setTestConnect(false);
disConnectNodes.put(node.getId(), node);
Expand Down Expand Up @@ -288,16 +288,10 @@ public void removeNode(String nodeId) {
public void removeNode(Node node) {
lock.lock();
try {
if (node.getIp().equals("222.186.180.162")) {
Log.error("----------removeNode:222.186.180.162,,,step1");
}
if (node.getChannel() != null && node.getChannel().isActive()) {
node.getChannel().close();
return;
}
if (node.getIp().equals("222.186.180.162")) {
Log.error("----------removeNode:222.186.180.162,,,step2");
}
node.destroy();
removeNodeFromGroup(node);
removeNodeHandler(node);
Expand All @@ -311,7 +305,6 @@ public void removeHandshakeNode(String nodeId) {
if (node != null) {
removeNode(node);
} else {
// Log.info("------------removeHandshakeNode node is null-----------" + nodeId);
getNetworkStorageService().deleteNode(nodeId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,16 @@ public NetworkEventResult process(BaseMessage message, Node node) {
P2PNodeBody cacheBody = nodeCacheManager.getNode(nodeBody.getId());
//已经缓存则什么都不处理
if (cacheBody != null) {
// Log.info("---------------------cacheBody is not null------------------------------" + cacheBody.toString());
Node node1 = nodeManager.getNode(cacheBody.getId());
// if(node1 != null) {
// node1.setFailCount(0);
// }
return null;
}
//尝试建立连接
Node newNode = new Node(nodeBody.getNodeIp(), nodeBody.getSeverPort(), nodeBody.getSeverPort(), Node.OUT);
nodeManager.addNode(newNode);
boolean b = nodeManager.addNode(newNode);
nodeCacheManager.cacheNode(nodeBody);
//广播交易
broadcastHandler.broadcastToAllNode(message, node, true,100);
if (b) {
broadcastHandler.broadcastToAllNode(message, node, true, 100);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void run() {
while (true) {
try {
nodeManager.broadNodeSever();
Thread.sleep(10 * DateUtil.MINUTE_TIME);
Thread.sleep(60 * DateUtil.MINUTE_TIME);
} catch (Throwable e) {
Log.error(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.nuls.kernel.model.RpcClientResult;
import io.nuls.network.cache.NodeCacheManager;
import io.nuls.network.constant.NetworkConstant;
import io.nuls.network.constant.NetworkParam;
import io.nuls.network.model.Node;
import io.nuls.network.model.NodeGroup;
import io.nuls.network.rpc.model.NetworkInfoDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -145,4 +146,36 @@ public static String getNodeId(InetSocketAddress socketAddress) {
}
return socketAddress.getHostString() + ":" + socketAddress.getPort();
}

/** * 判断是否为合法IP * @return the ip */
public static boolean isboolIp(String ipAddress) {
String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pattern = Pattern.compile(ip);
Matcher matcher = pattern.matcher(ipAddress);
return matcher.matches();
}

public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("119.23.248.143");
list.add("119.23.212.239");
list.add("39.108.181.47");
list.add("185.7�\u000B��<\u0011��");
list.add("39.104.136.6");
list.add("47.107.45.135");
list.add("51.15.63.174");
list.add("39.108.167.178");
list.add("47.106.149.140");
list.add("139.159.147.21");
list.add("122.114.0.96");
list.add("127.0.0.1");
list.add("192.168.1.1");
list.add("256.2.3.4");
list.add("0.0.0.0");
list.add("0.0.0");

for(String str :list) {
System.out.println(isboolIp(str));
}
}
}

0 comments on commit fcfe8dc

Please sign in to comment.