Skip to content

Commit 72e73f3

Browse files
committed
improve LocalNode.Create to be able to accept the list of addresses allowed to access LocalNode
1 parent ed1c5e2 commit 72e73f3

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// SPDX-FileCopyrightText: 2024 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Net;
6+
using System.Net.Sockets;
7+
8+
namespace Smdn.Net.MuninNode;
9+
10+
internal sealed class AddressListAccessRule : IAccessRule {
11+
private readonly IReadOnlyList<IPAddress> addressListAllowFrom;
12+
13+
public AddressListAccessRule(IReadOnlyList<IPAddress> addressListAllowFrom)
14+
{
15+
this.addressListAllowFrom = addressListAllowFrom ?? throw new ArgumentNullException(nameof(addressListAllowFrom));
16+
}
17+
18+
public bool IsAcceptable(IPEndPoint remoteEndPoint)
19+
{
20+
if (remoteEndPoint is null)
21+
throw new ArgumentNullException(nameof(remoteEndPoint));
22+
23+
var remoteAddress = remoteEndPoint.Address;
24+
25+
foreach (var addressAllowFrom in addressListAllowFrom) {
26+
if (addressAllowFrom.AddressFamily == AddressFamily.InterNetwork) {
27+
// test for client acceptability by IPv4 address
28+
if (remoteAddress.IsIPv4MappedToIPv6)
29+
remoteAddress = remoteAddress.MapToIPv4();
30+
}
31+
32+
if (addressAllowFrom.Equals(remoteAddress))
33+
return true;
34+
}
35+
36+
return false;
37+
}
38+
}

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/IAccessRuleServiceCollectionExtensions.cs

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,13 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Net;
6-
using System.Net.Sockets;
76

87
using Microsoft.Extensions.DependencyInjection;
98
using Microsoft.Extensions.DependencyInjection.Extensions;
109

1110
namespace Smdn.Net.MuninNode;
1211

1312
public static class IAccessRuleServiceCollectionExtensions {
14-
internal class AddressListAccessRule : IAccessRule {
15-
private readonly IReadOnlyList<IPAddress> addressListAllowFrom;
16-
17-
public AddressListAccessRule(IReadOnlyList<IPAddress> addressListAllowFrom)
18-
{
19-
this.addressListAllowFrom = addressListAllowFrom ?? throw new ArgumentNullException(nameof(addressListAllowFrom));
20-
}
21-
22-
public bool IsAcceptable(IPEndPoint remoteEndPoint)
23-
{
24-
if (remoteEndPoint is null)
25-
throw new ArgumentNullException(nameof(remoteEndPoint));
26-
27-
var remoteAddress = remoteEndPoint.Address;
28-
29-
foreach (var addressAllowFrom in addressListAllowFrom) {
30-
if (addressAllowFrom.AddressFamily == AddressFamily.InterNetwork) {
31-
// test for client acceptability by IPv4 address
32-
if (remoteAddress.IsIPv4MappedToIPv6)
33-
remoteAddress = remoteAddress.MapToIPv4();
34-
}
35-
36-
if (addressAllowFrom.Equals(remoteAddress))
37-
return true;
38-
}
39-
40-
return false;
41-
}
42-
}
43-
4413
/// <param name="services">The <see cref="IServiceCollection"/> to add services to.</param>
4514
/// <param name="addressListAllowFrom">The <see cref="IReadOnlyList{IPAddress}"/> indicates the read-only list of addresses allowed to access <see cref="NodeBase"/>.</param>
4615
public static IServiceCollection AddMuninNodeAccessRule(

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/LocalNode.Create.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ public ConcreteLocalNode(
3434
IPluginProvider pluginProvider,
3535
string hostName,
3636
int port,
37+
IAccessRule? accessRule,
3738
IServiceProvider? serviceProvider = null
3839
)
3940
: base(
40-
accessRule: serviceProvider?.GetService<IAccessRule>(),
41+
accessRule: accessRule ?? serviceProvider?.GetService<IAccessRule>(),
4142
logger: serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<LocalNode>()
4243
)
4344
{
@@ -66,9 +67,13 @@ protected override EndPoint GetLocalEndPointToBind()
6667
/// The hostname advertised by this node. This value is used as the display name for HTML generated by Munin.
6768
/// If <see langword="null"/> or empty, the default hostname is used.
6869
/// </param>
70+
/// <param name="addressListAllowFrom">
71+
/// The <see cref="IReadOnlyList{IPAddress}"/> indicates the read-only list of addresses allowed to access <see cref="LocalNode"/>.
72+
/// </param>
6973
/// <param name="serviceProvider">
7074
/// The <see cref="IServiceProvider"/>.
71-
/// This constructor overload attempts to get a service of <see cref="ILoggerFactory"/>, to create an <see cref="ILogger"/>.
75+
/// This overload attempts to get a service of <see cref="ILoggerFactory"/>, to create an <see cref="ILogger"/>.
76+
/// Also attempts to get a service of <see cref="IAccessRule"/> if <paramref name="addressListAllowFrom"/> is <see langword="null"/>.
7277
/// </param>
7378
/// <remarks>
7479
/// Most Munin-Node uses port 4949 by default, but it is recommended to use other port numbers to avoid conflicts with other nodes.
@@ -77,12 +82,14 @@ public static LocalNode Create(
7782
IReadOnlyCollection<IPlugin> plugins,
7883
int port,
7984
string? hostName = null,
85+
IReadOnlyList<IPAddress>? addressListAllowFrom = null,
8086
IServiceProvider? serviceProvider = null
8187
)
8288
=> Create(
8389
pluginProvider: new ReadOnlyCollectionPluginProvider(plugins ?? throw new ArgumentNullException(nameof(plugins))),
8490
hostName: string.IsNullOrEmpty(hostName) ? DefaultHostName : hostName,
8591
port: port,
92+
addressListAllowFrom: addressListAllowFrom,
8693
serviceProvider: serviceProvider
8794
);
8895

@@ -99,9 +106,13 @@ public static LocalNode Create(
99106
/// The hostname advertised by this node. This value is used as the display name for HTML generated by Munin.
100107
/// If <see langword="null"/> or empty, the default hostname is used.
101108
/// </param>
109+
/// <param name="addressListAllowFrom">
110+
/// The <see cref="IReadOnlyList{IPAddress}"/> indicates the read-only list of addresses allowed to access <see cref="LocalNode"/>.
111+
/// </param>
102112
/// <param name="serviceProvider">
103113
/// The <see cref="IServiceProvider"/>.
104-
/// This constructor overload attempts to get a service of <see cref="ILoggerFactory"/>, to create an <see cref="ILogger"/>.
114+
/// This overload attempts to get a service of <see cref="ILoggerFactory"/>, to create an <see cref="ILogger"/>.
115+
/// Also attempts to get a service of <see cref="IAccessRule"/> if <paramref name="addressListAllowFrom"/> is <see langword="null"/>.
105116
/// </param>
106117
/// <remarks>
107118
/// Most Munin-Node uses port 4949 by default, but it is recommended to use other port numbers to avoid conflicts with other nodes.
@@ -110,12 +121,14 @@ public static LocalNode Create(
110121
IPluginProvider pluginProvider,
111122
int port,
112123
string? hostName = null,
124+
IReadOnlyList<IPAddress>? addressListAllowFrom = null,
113125
IServiceProvider? serviceProvider = null
114126
)
115127
=> new ConcreteLocalNode(
116128
pluginProvider: pluginProvider ?? throw new ArgumentNullException(nameof(pluginProvider)),
117129
hostName: string.IsNullOrEmpty(hostName) ? DefaultHostName : hostName,
118130
port: port,
131+
accessRule: addressListAllowFrom is null ? null : new AddressListAccessRule(addressListAllowFrom),
119132
serviceProvider: serviceProvider
120133
);
121134
}

0 commit comments

Comments
 (0)