-
Notifications
You must be signed in to change notification settings - Fork 393
/
IPResolver.cs
120 lines (107 loc) · 4.05 KB
/
IPResolver.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2018 Demerzel Solutions Limited
// This file is part of the Nethermind library.
//
// The Nethermind library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The Nethermind library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the Nethermind. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Net;
using Nethermind.Core;
using Nethermind.Logging;
using Nethermind.Network.Config;
using Nethermind.Network.IP;
namespace Nethermind.Network
{
public class IPResolver : IIPResolver
{
private readonly ILogger _logger;
private readonly INetworkConfig _networkConfig;
private readonly ILogManager _logManager;
public IPResolver(INetworkConfig networkConfig, ILogManager logManager)
{
_logger = logManager?.GetClassLogger() ?? throw new ArgumentNullException(nameof(logManager));
_networkConfig = networkConfig ?? throw new ArgumentNullException(nameof(networkConfig));
_logManager = logManager;
try
{
LocalIp = InitializeLocalIp();
}
catch (Exception)
{
LocalIp = IPAddress.Loopback;
}
try
{
ExternalIp = InitializeExternalIp();
}
catch (Exception)
{
ExternalIp = IPAddress.None;
}
}
public IPAddress LocalIp { get; }
public IPAddress ExternalIp { get; }
private IPAddress InitializeExternalIp()
{
IEnumerable<IIPSource> GetIPSources()
{
yield return new EnvironmentVariableIPSource();
yield return new NetworkConfigExternalIPSource(_networkConfig, _logManager);
yield return new WebIPSource("http://ipv4.icanhazip.com", _logManager);
yield return new WebIPSource("http://ipv4bot.whatismyipaddress.com", _logManager);
yield return new WebIPSource("http://checkip.amazonaws.com", _logManager);
yield return new WebIPSource("http://ipinfo.io/ip", _logManager);
yield return new WebIPSource("http://api.ipify.org", _logManager);
}
try
{
foreach (var s in GetIPSources())
{
if (s.TryGetIP(out var ip) && !ip.IsInternal())
{
ThisNodeInfo.AddInfo("External IP :", $"{ip}");
return ip;
}
}
}
catch (Exception e)
{
if(_logger.IsError) _logger.Error("Error while getting external ip", e);
}
return IPAddress.Loopback;
}
private IPAddress InitializeLocalIp()
{
IEnumerable<IIPSource> GetIPSources()
{
yield return new NetworkConfigLocalIPSource(_networkConfig, _logManager);
yield return new SocketIPSource( _logManager);
}
try
{
foreach (var s in GetIPSources())
{
if (s.TryGetIP(out var ip))
{
return ip;
}
}
}
catch (Exception e)
{
if(_logger.IsError) _logger.Error("Error while getting local ip", e);
}
return IPAddress.Loopback;
}
}
}