Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
200 lines (167 loc) · 7.73 KB
/
Copy pathProgram.cs
File metadata and controls
200 lines (167 loc) · 7.73 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using CommandLine;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using MdnsListener.Configuration;
using MdnsListener.Interfaces;
using MdnsListener.Services;
namespace MdnsListener;
/// <summary>
/// Entry point for the mDNS listener application.
/// </summary>
public class Program
{
public static async Task<int> Main(string[] args)
{
// Parse command line arguments
var parser = new Parser(with => with.HelpWriter = Console.Out);
var parserResult = parser.ParseArguments<CommandLineOptions>(args);
return await parserResult.MapResult(
async options => await RunWithOptionsAsync(options, args),
_ => Task.FromResult(1) // Return error code on parse failure
);
}
private static async Task<int> RunWithOptionsAsync(CommandLineOptions options, string[] args)
{
// Handle help request
if (options.Help)
{
return 0;
}
var builder = Host.CreateApplicationBuilder(args);
// Configure application settings with command line overrides
ConfigureAppConfiguration(builder.Configuration, options);
// Configure logging with command line log level
ConfigureLogging(builder, options);
// Configure services using dependency injection
ConfigureServices(builder.Services, builder.Configuration, options);
// Build and run the host
using var host = builder.Build();
var logger = host.Services.GetRequiredService<ILoggerFactory>()
.CreateLogger("MdnsListener.Program");
try
{
logger.LogInformation("Starting mDNS Listener application. Press Ctrl+C to exit.");
// Log active filters if any
if (options.HasFilters() && !options.IncludeAll)
{
logger.LogInformation("Active filters:");
if (options.ServiceNames.Any())
logger.LogInformation(" Service names: {Names}", string.Join(", ", options.ServiceNames));
if (options.ServiceTypes.Any())
logger.LogInformation(" Service types: {Types}", string.Join(", ", options.ServiceTypes));
if (options.DomainPatterns.Any())
logger.LogInformation(" Domain patterns: {Patterns}", string.Join(", ", options.DomainPatterns));
}
await host.RunAsync();
return 0;
}
catch (Exception ex)
{
logger.LogCritical(ex, "Application terminated unexpectedly");
return 1;
}
}
private static void ConfigureAppConfiguration(IConfigurationBuilder configuration, CommandLineOptions options)
{
// Set base path to the directory where the executable is located
var assemblyLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;
var assemblyDirectory = Path.GetDirectoryName(assemblyLocation) ?? Directory.GetCurrentDirectory();
configuration.SetBasePath(assemblyDirectory);
// Load custom config file if specified
if (!string.IsNullOrEmpty(options.ConfigFile))
{
configuration.AddJsonFile(options.ConfigFile, optional: false, reloadOnChange: true);
}
else
{
// Make appsettings.json optional with default values
configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "Production"}.json",
optional: true, reloadOnChange: true);
}
configuration.AddEnvironmentVariables();
// Add command line options as configuration
var commandLineConfig = new Dictionary<string, string?>();
// Override filter settings if specified on command line
if (options.HasFilters() || options.IncludeAll)
{
commandLineConfig[$"{MdnsListenerOptions.SectionName}:ServiceFilter:IncludeAll"] = options.IncludeAll.ToString();
if (options.ServiceNames.Any())
{
var names = options.ServiceNames.ToList();
for (int i = 0; i < names.Count; i++)
{
commandLineConfig[$"{MdnsListenerOptions.SectionName}:ServiceFilter:ServiceNames:{i}"] = names[i];
}
}
if (options.ServiceTypes.Any())
{
var types = options.ServiceTypes.ToList();
for (int i = 0; i < types.Count; i++)
{
commandLineConfig[$"{MdnsListenerOptions.SectionName}:ServiceFilter:ServiceTypes:{i}"] = types[i];
}
}
if (options.DomainPatterns.Any())
{
var patterns = options.DomainPatterns.ToList();
for (int i = 0; i < patterns.Count; i++)
{
commandLineConfig[$"{MdnsListenerOptions.SectionName}:ServiceFilter:DomainPatterns:{i}"] = patterns[i];
}
}
}
configuration.AddInMemoryCollection(commandLineConfig);
}
private static void ConfigureLogging(HostApplicationBuilder builder, CommandLineOptions options)
{
// Clear default logging configuration
builder.Logging.ClearProviders();
// Add console and debug providers
builder.Logging.AddConsole();
builder.Logging.AddDebug();
// If log level is specified on command line, override all configuration
if (Enum.TryParse<LogLevel>(options.LogLevel, true, out var logLevel))
{
builder.Logging.SetMinimumLevel(logLevel);
// Also override specific category levels
builder.Logging.AddFilter("MdnsListener", logLevel);
builder.Logging.AddFilter("Microsoft", logLevel);
builder.Logging.AddFilter("System", logLevel);
}
else
{
// Otherwise use configuration from appsettings.json
builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging"));
}
}
private static void ConfigureServices(IServiceCollection services, IConfiguration configuration, CommandLineOptions options)
{
// Configure options
services.Configure<MdnsListenerOptions>(
configuration.GetSection(MdnsListenerOptions.SectionName));
// Register command line options
services.AddSingleton(options);
// Register core services as singletons
services.AddSingleton<IDnsPacketParser, SecureDnsPacketParser>();
services.AddSingleton<IServiceFilter, ConfigurableServiceFilter>();
// Register the cache as both IServiceCache and IHostedService
services.AddSingleton<ServiceCacheWithExpiration>();
services.AddSingleton<IServiceCache>(provider =>
provider.GetRequiredService<ServiceCacheWithExpiration>());
services.AddHostedService(provider =>
provider.GetRequiredService<ServiceCacheWithExpiration>());
// Register packet handler
services.AddSingleton<IPacketHandler, PacketHandler>();
// Register the listener as both IMdnsListener and IHostedService
services.AddSingleton<Services.MdnsListener>();
services.AddSingleton<IMdnsListener>(provider =>
provider.GetRequiredService<Services.MdnsListener>());
services.AddHostedService(provider =>
provider.GetRequiredService<Services.MdnsListener>());
// Register event handlers
services.AddSingleton<IHostedService, MdnsEventLogger>();
}
}