public bool AddNode(string endpointStr, string command) { Guard.NotNull(this.ConnectionManager, nameof(this.ConnectionManager)); IPEndPoint endpoint = NodeSettings.ConvertIpAddressToEndpoint(endpointStr, this.ConnectionManager.Network.DefaultPort); switch (command) { case "add": this.ConnectionManager.AddNodeAddress(endpoint); break; case "remove": this.ConnectionManager.RemoveNodeAddress(endpoint); break; case "onetry": this.ConnectionManager.ConnectAsync(endpoint).GetAwaiter().GetResult(); break; default: throw new ArgumentException("command"); } return(true); }
public void CheckConvertingIPv6AddressWithPortToEndpoint() { // Act IPEndPoint endpoint = NodeSettings.ConvertIpAddressToEndpoint("[1233:3432:2434:2343:3234:2345:6546:4534]:5443", 1234); // Assert Assert.Equal(5443, endpoint.Port); Assert.Equal("1233:3432:2434:2343:3234:2345:6546:4534", endpoint.Address.ToString()); }
public void CheckConvertingIPv4AddressToEndpoint() { // Act IPEndPoint endpoint = NodeSettings.ConvertIpAddressToEndpoint("15.61.23.23", 1234); // Assert Assert.Equal(1234, endpoint.Port); Assert.Equal("15.61.23.23", endpoint.Address.ToString()); }
/// <summary> /// Loads the rpc settings from the application configuration. /// </summary> /// <param name="nodeSettings">Application configuration.</param> private void LoadSettingsFromConfig(NodeSettings nodeSettings) { var config = nodeSettings.ConfigReader; this.Server = config.GetOrDefault <bool>("server", false); this.RPCPort = nodeSettings.Network.RPCPort; if (this.Server) { this.RpcUser = config.GetOrDefault <string>("rpcuser", null); this.RpcPassword = config.GetOrDefault <string>("rpcpassword", null); this.RPCPort = config.GetOrDefault <int>("rpcport", nodeSettings.Network.RPCPort); try { this.AllowIp = config .GetAll("rpcallowip") .Select(p => IPAddress.Parse(p)) .ToList(); } catch (FormatException) { throw new ConfigurationException("Invalid rpcallowip value"); } try { this.DefaultBindings = config .GetAll("rpcbind") .Select(p => NodeSettings.ConvertIpAddressToEndpoint(p, this.RPCPort)) .ToList(); } catch (FormatException) { throw new ConfigurationException("Invalid rpcbind value"); } } }
/// <summary> /// Loads the ConnectionManager related settings from the application configuration. /// </summary> /// <param name="nodeSettings">Application configuration.</param> public void Load(NodeSettings nodeSettings) { var config = nodeSettings.ConfigReader; try { this.Connect.AddRange(config.GetAll("connect") .Select(c => NodeSettings.ConvertIpAddressToEndpoint(c, nodeSettings.Network.DefaultPort))); } catch (FormatException) { throw new ConfigurationException("Invalid 'connect' parameter."); } try { this.AddNode.AddRange(config.GetAll("addnode") .Select(c => NodeSettings.ConvertIpAddressToEndpoint(c, nodeSettings.Network.DefaultPort))); } catch (FormatException) { throw new ConfigurationException("Invalid 'addnode' parameter."); } var port = config.GetOrDefault <int>("port", nodeSettings.Network.DefaultPort); try { this.Listen.AddRange(config.GetAll("bind") .Select(c => new NodeServerEndpoint(NodeSettings.ConvertIpAddressToEndpoint(c, port), false))); } catch (FormatException) { throw new ConfigurationException("Invalid 'bind' parameter"); } try { this.Listen.AddRange(config.GetAll("whitebind") .Select(c => new NodeServerEndpoint(NodeSettings.ConvertIpAddressToEndpoint(c, port), true))); } catch (FormatException) { throw new ConfigurationException("Invalid 'listen' parameter"); } if (this.Listen.Count == 0) { this.Listen.Add(new NodeServerEndpoint(new IPEndPoint(IPAddress.Parse("0.0.0.0"), port), false)); } var externalIp = config.GetOrDefault <string>("externalip", null); if (externalIp != null) { try { this.ExternalEndpoint = NodeSettings.ConvertIpAddressToEndpoint(externalIp, port); } catch (FormatException) { throw new ConfigurationException("Invalid 'externalip' parameter"); } } if (this.ExternalEndpoint == null) { this.ExternalEndpoint = new IPEndPoint(IPAddress.Loopback, nodeSettings.Network.DefaultPort); } this.BanTimeSeconds = config.GetOrDefault <int>("bantime", ConnectionManagerSettings.DefaultMisbehavingBantimeSeconds); }