Skip to content

Commit c26496e

Browse files
authored
preferredNetworks supports multiple prefixes and regular expressions (#331)
1 parent d2918c3 commit c26496e

File tree

2 files changed

+226
-210
lines changed

2 files changed

+226
-210
lines changed

src/Nacos.AspNetCore/UriTool.cs

Lines changed: 157 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,64 @@
1-
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Nacos.AspNetCore.Tests")]
2-
3-
namespace Nacos.AspNetCore
4-
{
5-
using Microsoft.AspNetCore.Hosting.Server.Features;
6-
using Microsoft.AspNetCore.Http.Features;
7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
10-
using System.Net;
11-
using System.Net.NetworkInformation;
12-
using System.Net.Sockets;
13-
14-
internal static class UriTool
15-
{
16-
public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, int port, string preferredNetworks)
17-
{
18-
var splitChars = new char[] { ',', ';' };
19-
var appPort = port <= 0 ? 80 : port;
20-
21-
// 1. config
22-
if (!string.IsNullOrWhiteSpace(ip))
23-
{
24-
// it seems that nacos don't return the scheme
25-
// so here use http only.
26-
return new List<Uri> { new Uri($"http://{ip}:{appPort}") };
27-
}
28-
29-
// 1.1. Ip is null && Port has value
30-
if (string.IsNullOrWhiteSpace(ip) && appPort != 80)
31-
{
32-
return new List<Uri> { new Uri($"http://{GetCurrentIp(preferredNetworks)}:{appPort}") };
33-
}
34-
35-
var address = string.Empty;
36-
37-
// 2. IServerAddressesFeature
38-
if (features != null)
39-
{
40-
var addresses = features.Get<IServerAddressesFeature>();
41-
var addressCollection = addresses?.Addresses;
42-
43-
if (addressCollection != null && addressCollection.Any())
44-
{
45-
var uris = new List<Uri>();
46-
foreach (var item in addressCollection)
47-
{
48-
var url = ReplaceAddress(item, preferredNetworks);
49-
uris.Add(new Uri(url));
50-
}
51-
52-
return uris;
53-
}
54-
}
55-
56-
// 3. ASPNETCORE_URLS
57-
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
58-
if (!string.IsNullOrWhiteSpace(address))
59-
{
1+
using System.Text.RegularExpressions;
2+
3+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Nacos.AspNetCore.Tests")]
4+
5+
namespace Nacos.AspNetCore
6+
{
7+
using Microsoft.AspNetCore.Hosting.Server.Features;
8+
using Microsoft.AspNetCore.Http.Features;
9+
using System;
10+
using System.Collections.Generic;
11+
using System.Linq;
12+
using System.Net;
13+
using System.Net.NetworkInformation;
14+
using System.Net.Sockets;
15+
16+
internal static class UriTool
17+
{
18+
public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, int port, string preferredNetworks)
19+
{
20+
var splitChars = new char[] { ',', ';' };
21+
var appPort = port <= 0 ? 80 : port;
22+
23+
// 1. config
24+
if (!string.IsNullOrWhiteSpace(ip))
25+
{
26+
// it seems that nacos don't return the scheme
27+
// so here use http only.
28+
return new List<Uri> { new Uri($"http://{ip}:{appPort}") };
29+
}
30+
31+
// 1.1. Ip is null && Port has value
32+
if (string.IsNullOrWhiteSpace(ip) && appPort != 80)
33+
{
34+
return new List<Uri> { new Uri($"http://{GetCurrentIp(preferredNetworks)}:{appPort}") };
35+
}
36+
37+
var address = string.Empty;
38+
39+
// 2. IServerAddressesFeature
40+
if (features != null)
41+
{
42+
var addresses = features.Get<IServerAddressesFeature>();
43+
var addressCollection = addresses?.Addresses;
44+
45+
if (addressCollection != null && addressCollection.Any())
46+
{
47+
var uris = new List<Uri>();
48+
foreach (var item in addressCollection)
49+
{
50+
var url = ReplaceAddress(item, preferredNetworks);
51+
uris.Add(new Uri(url));
52+
}
53+
54+
return uris;
55+
}
56+
}
57+
58+
// 3. ASPNETCORE_URLS
59+
address = Environment.GetEnvironmentVariable("ASPNETCORE_URLS");
60+
if (!string.IsNullOrWhiteSpace(address))
61+
{
6062
var url = ReplaceAddress(address, preferredNetworks);
6163

6264
var uris = url.Split(splitChars).Select(x => new Uri(x));
@@ -67,101 +69,105 @@ public static IEnumerable<Uri> GetUri(IFeatureCollection features, string ip, in
6769
{
6870
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from ASPNETCORE_URLS");
6971
}
70-
}
71-
72-
return uris;
73-
}
74-
75-
// 4. --urls
76-
var cmdArgs = Environment.GetCommandLineArgs();
77-
if (cmdArgs != null && cmdArgs.Any())
78-
{
79-
var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));
80-
81-
if (!string.IsNullOrWhiteSpace(cmd))
82-
{
83-
address = cmd.Split('=')[1];
84-
85-
var url = ReplaceAddress(address, preferredNetworks);
86-
87-
var uris = url.Split(splitChars).Select(x => new Uri(x));
88-
72+
}
73+
74+
return uris;
75+
}
76+
77+
// 4. --urls
78+
var cmdArgs = Environment.GetCommandLineArgs();
79+
if (cmdArgs != null && cmdArgs.Any())
80+
{
81+
var cmd = cmdArgs.FirstOrDefault(x => x.StartsWith("--urls", StringComparison.OrdinalIgnoreCase));
82+
83+
if (!string.IsNullOrWhiteSpace(cmd))
84+
{
85+
address = cmd.Split('=')[1];
86+
87+
var url = ReplaceAddress(address, preferredNetworks);
88+
89+
var uris = url.Split(splitChars).Select(x => new Uri(x));
90+
8991
foreach (var item in uris)
9092
{
9193
if (!IPAddress.TryParse(item.Host, out _))
9294
{
9395
throw new Nacos.V2.Exceptions.NacosException("Invalid ip address from --urls");
9496
}
95-
}
96-
97-
return uris;
98-
}
99-
}
100-
101-
// 5. current ip address third
102-
address = $"http://{GetCurrentIp(preferredNetworks)}:{appPort}";
103-
104-
return new List<Uri> { new Uri(address) };
105-
}
106-
107-
private static string ReplaceAddress(string address, string preferredNetworks)
108-
{
109-
var ip = GetCurrentIp(preferredNetworks);
110-
111-
if (address.Contains("*"))
112-
{
113-
address = address.Replace("*", ip);
114-
}
115-
else if (address.Contains("+"))
116-
{
117-
address = address.Replace("+", ip);
118-
}
119-
else if (address.Contains("localhost", StringComparison.OrdinalIgnoreCase))
120-
{
121-
address = address.Replace("localhost", ip, StringComparison.OrdinalIgnoreCase);
122-
}
123-
else if (address.Contains("0.0.0.0", StringComparison.OrdinalIgnoreCase))
124-
{
125-
address = address.Replace("0.0.0.0", ip, StringComparison.OrdinalIgnoreCase);
126-
}
127-
128-
return address;
129-
}
130-
131-
private static string GetCurrentIp(string preferredNetworks)
132-
{
133-
var instanceIp = "127.0.0.1";
134-
135-
try
136-
{
137-
// 获取可用网卡
138-
var nics = NetworkInterface.GetAllNetworkInterfaces()?.Where(network => network.OperationalStatus == OperationalStatus.Up);
139-
140-
// 获取所有可用网卡IP信息
141-
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);
142-
143-
foreach (var ipadd in ipCollection)
144-
{
145-
if (!IPAddress.IsLoopback(ipadd.Address) && ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
146-
{
147-
if (string.IsNullOrEmpty(preferredNetworks))
148-
{
149-
instanceIp = ipadd.Address.ToString();
150-
break;
151-
}
152-
153-
if (!ipadd.Address.ToString().StartsWith(preferredNetworks)) continue;
154-
instanceIp = ipadd.Address.ToString();
155-
break;
156-
}
157-
}
158-
}
159-
catch
160-
{
161-
// ignored
162-
}
163-
164-
return instanceIp;
165-
}
166-
}
167-
}
97+
}
98+
99+
return uris;
100+
}
101+
}
102+
103+
// 5. current ip address third
104+
address = $"http://{GetCurrentIp(preferredNetworks)}:{appPort}";
105+
106+
return new List<Uri> { new Uri(address) };
107+
}
108+
109+
private static string ReplaceAddress(string address, string preferredNetworks)
110+
{
111+
var ip = GetCurrentIp(preferredNetworks);
112+
113+
if (address.Contains("*"))
114+
{
115+
address = address.Replace("*", ip);
116+
}
117+
else if (address.Contains("+"))
118+
{
119+
address = address.Replace("+", ip);
120+
}
121+
else if (address.Contains("localhost", StringComparison.OrdinalIgnoreCase))
122+
{
123+
address = address.Replace("localhost", ip, StringComparison.OrdinalIgnoreCase);
124+
}
125+
else if (address.Contains("0.0.0.0", StringComparison.OrdinalIgnoreCase))
126+
{
127+
address = address.Replace("0.0.0.0", ip, StringComparison.OrdinalIgnoreCase);
128+
}
129+
130+
return address;
131+
}
132+
133+
private static string GetCurrentIp(string preferredNetworks)
134+
{
135+
var instanceIp = "127.0.0.1";
136+
137+
try
138+
{
139+
// 获取可用网卡
140+
var nics = NetworkInterface.GetAllNetworkInterfaces()?.Where(network => network.OperationalStatus == OperationalStatus.Up);
141+
142+
// 获取所有可用网卡IP信息
143+
var ipCollection = nics?.Select(x => x.GetIPProperties())?.SelectMany(x => x.UnicastAddresses);
144+
145+
var preferredNetworksArr = preferredNetworks.Split(",");
146+
foreach (var ipadd in ipCollection)
147+
{
148+
if (!IPAddress.IsLoopback(ipadd.Address) &&
149+
ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
150+
{
151+
if (string.IsNullOrEmpty(preferredNetworks))
152+
{
153+
instanceIp = ipadd.Address.ToString();
154+
break;
155+
}
156+
157+
if (!preferredNetworksArr.Any(preferredNetwork =>
158+
ipadd.Address.ToString().StartsWith(preferredNetwork)
159+
|| Regex.IsMatch(ipadd.Address.ToString(), preferredNetwork))) continue;
160+
instanceIp = ipadd.Address.ToString();
161+
break;
162+
}
163+
}
164+
}
165+
catch
166+
{
167+
// ignored
168+
}
169+
170+
return instanceIp;
171+
}
172+
}
173+
}

0 commit comments

Comments
 (0)