Skip to content

Commit

Permalink
Merge pull request #333 from nacos-group/dev
Browse files Browse the repository at this point in the history
release v1.3.10
  • Loading branch information
catcherwong authored Nov 3, 2024
2 parents 85b9a43 + c02eca0 commit 7e08305
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 213 deletions.
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<NugetVersion>1.3.9</NugetVersion>
<NugetVersion>1.3.10</NugetVersion>
</PropertyGroup>
</Project>
308 changes: 157 additions & 151 deletions src/Nacos.AspNetCore/UriTool.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,64 @@
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Nacos.AspNetCore.Tests")]

namespace Nacos.AspNetCore
{
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

internal static class UriTool
{
public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, int port, string preferredNetworks)
{
var splitChars = new char[] { ',', ';' };
var appPort = port <= 0 ? 80 : port;

// 1. config
if (!string.IsNullOrWhiteSpace(ip))
{
// it seems that nacos don't return the scheme
// so here use http only.
return new List<Uri> { new Uri($"http://{ip}:{appPort}") };
}

// 1.1. Ip is null && Port has value
if (string.IsNullOrWhiteSpace(ip) && appPort != 80)
{
return new List<Uri> { new Uri($"http://{GetCurrentIp(preferredNetworks)}:{appPort}") };
}

var address = string.Empty;

// 2. IServerAddressesFeature
if (features != null)
{
var addresses = features.Get<IServerAddressesFeature>();
var addressCollection = addresses?.Addresses;

if (addressCollection != null && addressCollection.Any())
{
var uris = new List<Uri>();
foreach (var item in addressCollection)
{
var url = ReplaceAddress(item, preferredNetworks);
uris.Add(new Uri(url));
}

return uris;
}
}

// 3. ASPNETCORE_URLS
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
if (!string.IsNullOrWhiteSpace(address))
{
using System.Text.RegularExpressions;

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Nacos.AspNetCore.Tests")]

namespace Nacos.AspNetCore
{
using Microsoft.AspNetCore.Hosting.Server.Features;
using Microsoft.AspNetCore.Http.Features;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

internal static class UriTool
{
public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, int port, string preferredNetworks)
{
var splitChars = new char[] { ',', ';' };
var appPort = port <= 0 ? 80 : port;

// 1. config
if (!string.IsNullOrWhiteSpace(ip))
{
// it seems that nacos don't return the scheme
// so here use http only.
return new List<Uri> { new Uri($"http://{ip}:{appPort}") };
}

// 1.1. Ip is null && Port has value
if (string.IsNullOrWhiteSpace(ip) && appPort != 80)
{
return new List<Uri> { new Uri($"http://{GetCurrentIp(preferredNetworks)}:{appPort}") };
}

var address = string.Empty;

// 2. IServerAddressesFeature
if (features != null)
{
var addresses = features.Get<IServerAddressesFeature>();
var addressCollection = addresses?.Addresses;

if (addressCollection != null && addressCollection.Any())
{
var uris = new List<Uri>();
foreach (var item in addressCollection)
{
var url = ReplaceAddress(item, preferredNetworks);
uris.Add(new Uri(url));
}

return uris;
}
}

// 3. ASPNETCORE_URLS
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
if (!string.IsNullOrWhiteSpace(address))
{
var url = ReplaceAddress(address, preferredNetworks);

var uris = url.Split(splitChars).Select(x => new Uri(x));
Expand All @@ -67,101 +69,105 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from ASPNETCORE_URLS");
}
}

return uris;
}

// 4. --urls
var cmdArgs = Environment.GetCommandLineArgs();
if (cmdArgs != null && cmdArgs.Any())
{
var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));

if (!string.IsNullOrWhiteSpace(cmd))
{
address = cmd.Split('=')[1];

var url = ReplaceAddress(address, preferredNetworks);

var uris = url.Split(splitChars).Select(x => new Uri(x));

}

return uris;
}

// 4. --urls
var cmdArgs = Environment.GetCommandLineArgs();
if (cmdArgs != null && cmdArgs.Any())
{
var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));

if (!string.IsNullOrWhiteSpace(cmd))
{
address = cmd.Split('=')[1];

var url = ReplaceAddress(address, preferredNetworks);

var uris = url.Split(splitChars).Select(x => new Uri(x));

foreach (var item in uris)
{
if (!IPAddress.TryParse(item.Host, out _))
{
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from --urls");
}
}

return uris;
}
}

// 5. current ip address third
address = $"http://{GetCurrentIp(preferredNetworks)}:{appPort}";

return new List<Uri> { new Uri(address) };
}

private static string ReplaceAddress(string address, string preferredNetworks)
{
var ip = GetCurrentIp(preferredNetworks);

if (address.Contains("*"))
{
address = address.Replace("*", ip);
}
else if (address.Contains("+"))
{
address = address.Replace("+", ip);
}
else if (address.Contains("localhost", StringComparison.OrdinalIgnoreCase))
{
address = address.Replace("localhost", ip, StringComparison.OrdinalIgnoreCase);
}
else if (address.Contains("0.0.0.0", StringComparison.OrdinalIgnoreCase))
{
address = address.Replace("0.0.0.0", ip, StringComparison.OrdinalIgnoreCase);
}

return address;
}

private static string GetCurrentIp(string preferredNetworks)
{
var instanceIp = "127.0.0.1";

try
{
// 获取可用网卡
var nics = NetworkInterface.GetAllNetworkInterfaces()?.Where(network => network.OperationalStatus == OperationalStatus.Up);

// 获取所有可用网卡IP信息
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);

foreach (var ipadd in ipCollection)
{
if (!IPAddress.IsLoopback(ipadd.Address) && ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (string.IsNullOrEmpty(preferredNetworks))
{
instanceIp = ipadd.Address.ToString();
break;
}

if (!ipadd.Address.ToString().StartsWith(preferredNetworks)) continue;
instanceIp = ipadd.Address.ToString();
break;
}
}
}
catch
{
// ignored
}

return instanceIp;
}
}
}
}

return uris;
}
}

// 5. current ip address third
address = $"http://{GetCurrentIp(preferredNetworks)}:{appPort}";

return new List<Uri> { new Uri(address) };
}

private static string ReplaceAddress(string address, string preferredNetworks)
{
var ip = GetCurrentIp(preferredNetworks);

if (address.Contains("*"))
{
address = address.Replace("*", ip);
}
else if (address.Contains("+"))
{
address = address.Replace("+", ip);
}
else if (address.Contains("localhost", StringComparison.OrdinalIgnoreCase))
{
address = address.Replace("localhost", ip, StringComparison.OrdinalIgnoreCase);
}
else if (address.Contains("0.0.0.0", StringComparison.OrdinalIgnoreCase))
{
address = address.Replace("0.0.0.0", ip, StringComparison.OrdinalIgnoreCase);
}

return address;
}

private static string GetCurrentIp(string preferredNetworks)
{
var instanceIp = "127.0.0.1";

try
{
// 获取可用网卡
var nics = NetworkInterface.GetAllNetworkInterfaces()?.Where(network => network.OperationalStatus == OperationalStatus.Up);

// 获取所有可用网卡IP信息
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);

var preferredNetworksArr = preferredNetworks.Split(",");
foreach (var ipadd in ipCollection)
{
if (!IPAddress.IsLoopback(ipadd.Address) &&
ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
{
if (string.IsNullOrEmpty(preferredNetworks))
{
instanceIp = ipadd.Address.ToString();
break;
}

if (!preferredNetworksArr.Any(preferredNetwork =>
ipadd.Address.ToString().StartsWith(preferredNetwork)
|| Regex.IsMatch(ipadd.Address.ToString(), preferredNetwork))) continue;
instanceIp = ipadd.Address.ToString();
break;
}
}
}
catch
{
// ignored
}

return instanceIp;
}
}
}
2 changes: 1 addition & 1 deletion src/Nacos/Nacos.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.4" />
<PackageReference Include="System.Text.Json" Version="8.0.5" />
<PackageReference Include="Google.Protobuf" Version="3.27.2" />
<PackageReference Include="Grpc.Net.Client" Version="2.65.0" />
<!--<PackageReference Include="Grpc.Net.Client" Version="2.33.1" />-->
Expand Down
2 changes: 1 addition & 1 deletion src/Nacos/V2/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class Constants
{
public static string CLIENT_VERSION = "Nacos-CSharp-Client:v1.3.9";
public static string CLIENT_VERSION = "Nacos-CSharp-Client:v1.3.10";

public const string ClientName = "NacosClient";

Expand Down
Loading

0 comments on commit 7e08305

Please sign in to comment.