From f9c42d6cb5d1667ffb3c6468633021b5056eb07e Mon Sep 17 00:00:00 2001 From: Brown Jiang <317787106@qq.com> Date: Wed, 30 Aug 2023 18:17:08 +0800 Subject: [PATCH] feat(net): get external IPv4 from libp2p (#5407) * get external ip from libp2p * add test case testIpFromLibP2p --- .../common/parameter/CommonParameter.java | 3 ++ .../src/main/java/org/tron/core/Constant.java | 1 - .../java/org/tron/core/config/args/Args.java | 37 +++------------ .../org/tron/core/net/TronNetService.java | 2 +- .../org/tron/core/config/args/ArgsTest.java | 45 ++++++++++++++++++- 5 files changed, 52 insertions(+), 36 deletions(-) diff --git a/common/src/main/java/org/tron/common/parameter/CommonParameter.java b/common/src/main/java/org/tron/common/parameter/CommonParameter.java index 3ba55f9b47f..aa9f4e40f14 100644 --- a/common/src/main/java/org/tron/common/parameter/CommonParameter.java +++ b/common/src/main/java/org/tron/common/parameter/CommonParameter.java @@ -18,6 +18,7 @@ import org.tron.core.config.args.Overlay; import org.tron.core.config.args.SeedNode; import org.tron.core.config.args.Storage; +import org.tron.p2p.P2pConfig; import org.tron.p2p.dns.update.PublishConfig; public class CommonParameter { @@ -169,6 +170,8 @@ public class CommonParameter { @Setter public int minParticipationRate; @Getter + public P2pConfig p2pConfig; + @Getter @Setter public int nodeListenPort; @Getter diff --git a/common/src/main/java/org/tron/core/Constant.java b/common/src/main/java/org/tron/core/Constant.java index 57db2d59639..3e859f6fd96 100644 --- a/common/src/main/java/org/tron/core/Constant.java +++ b/common/src/main/java/org/tron/core/Constant.java @@ -280,7 +280,6 @@ public class Constant { public static final String NODE_DISCOVERY_BIND_IP = "node.discovery.bind.ip"; public static final String NODE_DISCOVERY_EXTERNAL_IP = "node.discovery.external.ip"; - public static final String AMAZONAWS_URL = "http://checkip.amazonaws.com"; public static final String NODE_BACKUP_PRIORITY = "node.backup.priority"; public static final String NODE_BACKUP_PORT = "node.backup.port"; diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 69dc8d52423..74a755aca7f 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -15,15 +15,12 @@ import com.typesafe.config.ConfigObject; import io.grpc.internal.GrpcUtil; import io.grpc.netty.NettyServerBuilder; -import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.Socket; -import java.net.URL; import java.text.ParseException; import java.util.ArrayList; import java.util.Arrays; @@ -73,6 +70,7 @@ import org.tron.core.store.AccountStore; import org.tron.keystore.Credentials; import org.tron.keystore.WalletUtils; +import org.tron.p2p.P2pConfig; import org.tron.p2p.dns.update.DnsType; import org.tron.p2p.dns.update.PublishConfig; import org.tron.p2p.utils.NetUtil; @@ -628,6 +626,7 @@ public static void setParam(final String[] args, final String confFileName) { ? config.getInt(Constant.NODE_MIN_PARTICIPATION_RATE) : 0; + PARAMETER.p2pConfig = new P2pConfig(); PARAMETER.nodeListenPort = config.hasPath(Constant.NODE_LISTEN_PORT) ? config.getInt(Constant.NODE_LISTEN_PORT) : 0; @@ -1533,36 +1532,10 @@ private static void externalIp(final com.typesafe.config.Config config) { if (!config.hasPath(Constant.NODE_DISCOVERY_EXTERNAL_IP) || config .getString(Constant.NODE_DISCOVERY_EXTERNAL_IP).trim().isEmpty()) { if (PARAMETER.nodeExternalIp == null) { - logger.info("External IP wasn't set, using checkip.amazonaws.com to identify it..."); - BufferedReader in = null; - try { - in = new BufferedReader(new InputStreamReader( - new URL(Constant.AMAZONAWS_URL).openStream())); - PARAMETER.nodeExternalIp = in.readLine(); - if (PARAMETER.nodeExternalIp == null || PARAMETER.nodeExternalIp.trim().isEmpty()) { - throw new IOException("Invalid address: '" + PARAMETER.nodeExternalIp + "'"); - } - try { - InetAddress.getByName(PARAMETER.nodeExternalIp); - } catch (Exception e) { - throw new IOException("Invalid address: '" + PARAMETER.nodeExternalIp + "'"); - } - logger.info("External address identified: {}", PARAMETER.nodeExternalIp); - } catch (IOException e) { + logger.info("External IP wasn't set, using ipv4 from libp2p"); + PARAMETER.nodeExternalIp = PARAMETER.p2pConfig.getIp(); + if (StringUtils.isEmpty(PARAMETER.nodeExternalIp)) { PARAMETER.nodeExternalIp = PARAMETER.nodeDiscoveryBindIp; - logger.warn( - "Can't get external IP. Fall back to peer.bind.ip: " - + PARAMETER.nodeExternalIp + " :" - + e); - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - //ignore - } - } - } } } else { diff --git a/framework/src/main/java/org/tron/core/net/TronNetService.java b/framework/src/main/java/org/tron/core/net/TronNetService.java index 87d878be8c9..03becf5d4e9 100644 --- a/framework/src/main/java/org/tron/core/net/TronNetService.java +++ b/framework/src/main/java/org/tron/core/net/TronNetService.java @@ -148,7 +148,7 @@ public static boolean hasIpv4Stack(Set ipSet) { } private P2pConfig getConfig() { - P2pConfig config = new P2pConfig(); + P2pConfig config = parameter.getP2pConfig(); return updateConfig(config); } diff --git a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java index 143e10706aa..13e272d1e13 100644 --- a/framework/src/test/java/org/tron/core/config/args/ArgsTest.java +++ b/framework/src/test/java/org/tron/core/config/args/ArgsTest.java @@ -16,8 +16,16 @@ package org.tron.core.config.args; import com.google.common.collect.Lists; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigMergeable; +import com.typesafe.config.ConfigOrigin; +import com.typesafe.config.ConfigRenderOptions; +import com.typesafe.config.ConfigValue; +import com.typesafe.config.ConfigValueType; import io.grpc.internal.GrpcUtil; import io.grpc.netty.NettyServerBuilder; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Arrays; import lombok.extern.slf4j.Slf4j; import org.junit.After; @@ -28,7 +36,10 @@ import org.tron.common.utils.ByteArray; import org.tron.common.utils.LocalWitnesses; import org.tron.common.utils.PublicMethod; +import org.tron.common.utils.ReflectUtils; import org.tron.core.Constant; +import org.tron.core.config.Configuration; +import org.tron.core.net.peer.PeerManager; @Slf4j public class ArgsTest { @@ -44,7 +55,7 @@ public void destroy() { @Test public void get() { - Args.setParam(new String[]{"-w"}, Constant.TEST_CONF); + Args.setParam(new String[] {"-w"}, Constant.TEST_CONF); CommonParameter parameter = Args.getInstance(); @@ -55,7 +66,7 @@ public void get() { localWitnesses.initWitnessAccountAddress(true); Args.setLocalWitnesses(localWitnesses); address = ByteArray.toHexString(Args.getLocalWitnesses() - .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())); + .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine())); Assert.assertEquals(0, parameter.getBackupPriority()); @@ -112,5 +123,35 @@ public void get() { ByteArray.toHexString(Args.getLocalWitnesses() .getWitnessAccountAddress(CommonParameter.getInstance().isECKeyCryptoEngine()))); } + + @Test + public void testIpFromLibP2p() + throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + Args.setParam(new String[] {"-w"}, Constant.TEST_CONF); + CommonParameter parameter = Args.getInstance(); + + String configuredBindIp = parameter.getNodeDiscoveryBindIp(); + String configuredExternalIp = parameter.getNodeExternalIp(); + Assert.assertEquals("127.0.0.1", configuredBindIp); + Assert.assertEquals("46.168.1.1", configuredExternalIp); + + Config config = Configuration.getByFileName(null, Constant.TEST_CONF); + Config config2 = config.withoutPath(Constant.NODE_DISCOVERY_BIND_IP); + Config config3 = config2.withoutPath(Constant.NODE_DISCOVERY_EXTERNAL_IP); + + CommonParameter.getInstance().setNodeDiscoveryBindIp(null); + CommonParameter.getInstance().setNodeExternalIp(null); + + Method method1 = Args.class.getDeclaredMethod("bindIp", Config.class); + method1.setAccessible(true); + method1.invoke(Args.class, config3); + + Method method2 = Args.class.getDeclaredMethod("externalIp", Config.class); + method2.setAccessible(true); + method2.invoke(Args.class, config3); + + Assert.assertNotEquals(configuredBindIp, parameter.getNodeDiscoveryBindIp()); + Assert.assertNotEquals(configuredExternalIp, parameter.getNodeExternalIp()); + } }