From 1c036167fb8607e1d7084fbd0b5ea1be04d55fb9 Mon Sep 17 00:00:00 2001 From: Amirul Ashraf Date: Wed, 3 May 2023 14:08:51 +0800 Subject: [PATCH] Allow specifying local ip. And use any by default instead of loopback. (#5635) * Allow specifying local ip. And use any by default instead of loopback. * Fix does not even start * Whitespace * Minor cleanup --- .../Steps/InitializeNetwork.cs | 1 + .../Rlpx/RlpxPeerTests.cs | 2 +- .../Nethermind.Network/IP/SocketIPSource.cs | 39 ------------------- .../Nethermind.Network/IPResolver.cs | 5 +-- .../Nethermind.Network/Rlpx/RlpxHost.cs | 9 ++++- 5 files changed, 12 insertions(+), 44 deletions(-) delete mode 100644 src/Nethermind/Nethermind.Network/IP/SocketIPSource.cs diff --git a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs index a1d57903747..f890001ae8c 100644 --- a/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs +++ b/src/Nethermind/Nethermind.Init/Steps/InitializeNetwork.cs @@ -483,6 +483,7 @@ private async Task InitPeer() _api.MessageSerializationService, _api.NodeKey.PublicKey, _networkConfig.P2PPort, + _networkConfig.LocalIp, encryptionHandshakeServiceA, _api.SessionMonitor, _api.DisconnectsAnalyzer, diff --git a/src/Nethermind/Nethermind.Network.Test/Rlpx/RlpxPeerTests.cs b/src/Nethermind/Nethermind.Network.Test/Rlpx/RlpxPeerTests.cs index 3f2b0ae4886..2198e9825df 100644 --- a/src/Nethermind/Nethermind.Network.Test/Rlpx/RlpxPeerTests.cs +++ b/src/Nethermind/Nethermind.Network.Test/Rlpx/RlpxPeerTests.cs @@ -24,7 +24,7 @@ public async Task Start_stop() { RlpxHost host = new( Substitute.For(), - TestItem.PublicKeyA, GegAvailableLocalPort(), + TestItem.PublicKeyA, GegAvailableLocalPort(), null, Substitute.For(), Substitute.For(), NullDisconnectsAnalyzer.Instance, diff --git a/src/Nethermind/Nethermind.Network/IP/SocketIPSource.cs b/src/Nethermind/Nethermind.Network/IP/SocketIPSource.cs deleted file mode 100644 index 16b9ba08c49..00000000000 --- a/src/Nethermind/Nethermind.Network/IP/SocketIPSource.cs +++ /dev/null @@ -1,39 +0,0 @@ -// SPDX-FileCopyrightText: 2022 Demerzel Solutions Limited -// SPDX-License-Identifier: LGPL-3.0-only - -using System.Net; -using System.Net.Sockets; -using System.Threading.Tasks; -using Nethermind.Logging; -using Nethermind.Network.Config; - -namespace Nethermind.Network.IP -{ - public class SocketIPSource : IIPSource - { - private readonly ILogger _logger; - - public SocketIPSource(ILogManager logManager) - { - _logger = logManager.GetClassLogger(); - } - - public async Task<(bool, IPAddress)> TryGetIP() - { - try - { - using Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, 0); - await socket.ConnectAsync("www.google.com", 80); - IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; - IPAddress ipAddress = endPoint?.Address; - if (_logger.IsDebug) _logger.Debug($"Local ip: {ipAddress}"); - return (ipAddress is not null, ipAddress); - } - catch (SocketException) - { - if (_logger.IsError) _logger.Error($"Error while getting local ip from socket. You can set a manual override via config {nameof(NetworkConfig)}.{nameof(NetworkConfig.LocalIp)}"); - return (false, null); - } - } - } -} diff --git a/src/Nethermind/Nethermind.Network/IPResolver.cs b/src/Nethermind/Nethermind.Network/IPResolver.cs index 2b000d56604..7aee676aa3e 100644 --- a/src/Nethermind/Nethermind.Network/IPResolver.cs +++ b/src/Nethermind/Nethermind.Network/IPResolver.cs @@ -80,7 +80,7 @@ IEnumerable GetIPSources() if (_logger.IsError) _logger.Error("Error while getting external ip", e); } - return IPAddress.Loopback; + return IPAddress.Any; } private async Task InitializeLocalIp() @@ -88,7 +88,6 @@ private async Task InitializeLocalIp() IEnumerable GetIPSources() { yield return new NetworkConfigLocalIPSource(_networkConfig, _logManager); - yield return new SocketIPSource(_logManager); } try @@ -107,7 +106,7 @@ IEnumerable GetIPSources() if (_logger.IsError) _logger.Error("Error while getting local ip", e); } - return IPAddress.Loopback; + return IPAddress.Any; } } } diff --git a/src/Nethermind/Nethermind.Network/Rlpx/RlpxHost.cs b/src/Nethermind/Nethermind.Network/Rlpx/RlpxHost.cs index 6d6b5f069e1..23a72cec0e7 100644 --- a/src/Nethermind/Nethermind.Network/Rlpx/RlpxHost.cs +++ b/src/Nethermind/Nethermind.Network/Rlpx/RlpxHost.cs @@ -35,6 +35,7 @@ public class RlpxHost : IRlpxHost private bool _isInitialized; public PublicKey LocalNodeId { get; } public int LocalPort { get; } + public string? LocalIp { get; set; } private readonly IHandshakeService _handshakeService; private readonly IMessageSerializationService _serializationService; private readonly ILogManager _logManager; @@ -47,6 +48,7 @@ public class RlpxHost : IRlpxHost public RlpxHost(IMessageSerializationService serializationService, PublicKey localNodeId, int localPort, + string? localIp, IHandshakeService handshakeService, ISessionMonitor sessionMonitor, IDisconnectsAnalyzer disconnectsAnalyzer, @@ -78,6 +80,7 @@ TimeSpan sendLatency _handshakeService = handshakeService ?? throw new ArgumentNullException(nameof(handshakeService)); LocalNodeId = localNodeId ?? throw new ArgumentNullException(nameof(localNodeId)); LocalPort = localPort; + LocalIp = localIp; _sendLatency = sendLatency; } @@ -109,7 +112,11 @@ public async Task Init() InitializeChannel(ch, session); })); - _bootstrapChannel = await bootstrap.BindAsync(LocalPort).ContinueWith(t => + Task openTask = LocalIp is null + ? bootstrap.BindAsync(LocalPort) + : bootstrap.BindAsync(IPAddress.Parse(LocalIp), LocalPort); + + _bootstrapChannel = await openTask.ContinueWith(t => { if (t.IsFaulted) {