Skip to content

Commit 1f81ec2

Browse files
authored
Merge pull request #402 from petrsnd/portable-dns-linux-support
Portable dns linux support
2 parents 49ab904 + 0a45b9b commit 1f81ec2

18 files changed

+212
-60
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ end_of_line = crlf
5858

5959
[*.cs]
6060

61+
[*.generated.cs]
62+
generated_code = true
63+
6164
# IDE0063: Use simple 'using' statement
6265
csharp_prefer_simple_using_statement = true:silent
6366

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ bld/
2626

2727
# Visual Studio 2015 cache/options directory
2828
.vs/
29+
# Visual Studio Code directory
30+
.vscode/
2931
# Uncomment if you have tasks that create the project's static files in wwwroot
3032
#wwwroot/
3133

@@ -285,4 +287,4 @@ __pycache__/
285287
*.btp.cs
286288
*.btm.cs
287289
*.odx.cs
288-
*.xsd.cs
290+
*.xsd.cs

Bruce/Bruce.csproj

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup Condition="'$(OS)' == 'Windows_NT'">
4+
<TargetFramework>net8.0-windows</TargetFramework>
5+
</PropertyGroup>
6+
7+
<PropertyGroup Condition="'$(OS)' != 'Windows_NT'">
8+
<TargetFramework>net8.0</TargetFramework>
9+
</PropertyGroup>
210

311
<PropertyGroup>
412
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.1</TargetFramework>
613
<RootNamespace>Kerberos.NET.CommandLine</RootNamespace>
714

815
<RollForward>Major</RollForward>
@@ -33,13 +40,21 @@
3340
</ItemGroup>
3441

3542
<ItemGroup>
36-
<PackageReference Include="DnsClient" Version="1.7.0" />
3743
<PackageReference Include="Humanizer.Core" Version="2.14.1" />
3844
</ItemGroup>
3945

4046
<ItemGroup>
47+
<ProjectReference Include="..\Kerberos.NET.PortableDns\Kerberos.NET.PortableDns.csproj" />
4148
<ProjectReference Include="..\Kerberos.NET\Kerberos.NET.csproj" />
42-
<ProjectReference Include="..\Samples\KerbDumpCore\KerbDumpCore.csproj" />
49+
<ProjectReference Include="..\Samples\KerbDumpCore\KerbDumpCore.csproj"
50+
Condition="'$(OS)' == 'Windows_NT'" />
4351
</ItemGroup>
4452

53+
<Target Name="NETSDK1146Workaround" BeforeTargets="_PackToolValidation">
54+
<PropertyGroup>
55+
<TargetPlatformIdentifier></TargetPlatformIdentifier>
56+
<TargetPlatformMoniker></TargetPlatformMoniker>
57+
</PropertyGroup>
58+
</Target>
59+
4560
</Project>

Bruce/CommandLine/KerberosDumpCommand.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
// -----------------------------------------------------------------------
55

66
using System.Threading.Tasks;
7+
#if WINDOWS
78
using System.Windows.Forms;
89
using KerbDump;
10+
#endif
911

1012
namespace Kerberos.NET.CommandLine
1113
{
@@ -14,8 +16,10 @@ public class KerberosDumpCommand : BaseCommand
1416
{
1517
static KerberosDumpCommand()
1618
{
19+
#if WINDOWS
1720
Application.EnableVisualStyles();
1821
Application.SetCompatibleTextRenderingDefault(false);
22+
#endif
1923
}
2024

2125
public KerberosDumpCommand(CommandLineParameters parameters)
@@ -29,11 +33,7 @@ public KerberosDumpCommand(CommandLineParameters parameters)
2933

3034
public override Task<bool> Execute()
3135
{
32-
if (!OSPlatform.IsWindows)
33-
{
34-
return Task.FromResult(false);
35-
}
36-
36+
#if WINDOWS
3737
using (var form = new DecoderForm()
3838
{
3939
Ticket = this.Ticket,
@@ -44,6 +44,10 @@ public override Task<bool> Execute()
4444
}
4545

4646
return Task.FromResult(true);
47+
#else
48+
return Task.FromResult(false);
49+
#endif
4750
}
4851
}
4952
}
53+

Bruce/CommandLine/KerberosHelpCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ private void WriteCommandLabel(Type type, int max = 0)
123123

124124
if (string.Equals(descName, desc, StringComparison.OrdinalIgnoreCase))
125125
{
126-
this.WriteLine(string.Format(format, label), attr.Description, commands.Skip(1));
126+
this.WriteLine(string.Format(format, label), attr.Description, string.Join(", ", commands.Skip(1)));
127127
}
128128
else
129129
{
130-
this.WriteLine(string.Format(format, label), desc, commands.Skip(1));
130+
this.WriteLine(string.Format(format, label), desc, string.Join(", ", commands.Skip(1)));
131131
}
132132
}
133133
}

Bruce/CommandLine/KerberosPasswordCommand.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,12 @@
33
// The .NET Foundation licenses this file to you under the MIT license.
44
// -----------------------------------------------------------------------
55

6-
using Kerberos.NET.Configuration;
76
using Kerberos.NET.Credentials;
8-
using Kerberos.NET.Crypto;
97
using Kerberos.NET.Entities;
108
using Microsoft.Extensions.Logging;
119
using Microsoft.Extensions.Logging.Abstractions;
1210
using System;
13-
using System.Linq;
14-
using System.Security.Cryptography.X509Certificates;
1511
using System.Threading.Tasks;
16-
using System.Windows.Forms;
1712

1813
namespace Kerberos.NET.CommandLine
1914
{

Bruce/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
using System;
1+
using Kerberos.NET.PortableDns;
2+
using System;
23
using System.Diagnostics;
34
using System.IO;
45
using System.Linq;
5-
using Kerberos.NET.CommandLine.Dns;
6-
using Kerberos.NET.Dns;
76

87
namespace Kerberos.NET.CommandLine
98
{
@@ -12,7 +11,10 @@ class Program
1211
[STAThread]
1312
static void Main(string[] args)
1413
{
15-
DnsQuery.RegisterImplementation(new PlatformIndependentDnsClient());
14+
if (!OSPlatform.IsWindows)
15+
{
16+
PortableDnsClient.Configure();
17+
}
1618

1719
var assembly = Path.GetFileNameWithoutExtension(Process.GetCurrentProcess().ProcessName);
1820

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="DnsClient" Version="1.8.0" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\Kerberos.NET\Kerberos.NET.csproj" />
13+
</ItemGroup>
14+
15+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using DnsClient;
2+
using System.Linq;
3+
using System.Net;
4+
5+
namespace Kerberos.NET.PortableDns
6+
{
7+
public static class PortableDnsClient
8+
{
9+
public static void Configure()
10+
{
11+
PortableDnsImplementation.Options = new LookupClientOptions();
12+
}
13+
14+
public static void Configure(params NameServer[] nameServers)
15+
{
16+
PortableDnsImplementation.Options = new LookupClientOptions(nameServers);
17+
}
18+
19+
public static void Configure(params IPEndPoint[] nameServers)
20+
{
21+
PortableDnsImplementation.Options = new LookupClientOptions(nameServers);
22+
}
23+
24+
public static void Configure(params IPAddress[] nameServers)
25+
{
26+
PortableDnsImplementation.Options = new LookupClientOptions(nameServers);
27+
}
28+
29+
public static void Configure(params string[] nameServers)
30+
{
31+
PortableDnsImplementation.Options = new LookupClientOptions(nameServers.Select(n => {
32+
var parts = n.Split(':');
33+
IPAddress address;
34+
switch (parts.Length)
35+
{
36+
case 1:
37+
address = IPAddress.Parse(parts[0]);
38+
return new IPEndPoint(address, 53);
39+
case 2:
40+
address = IPAddress.Parse(parts[0]);
41+
var port = int.Parse(parts[1]);
42+
return new IPEndPoint(address, port);
43+
default:
44+
throw new System.FormatException($"{n} is not in the correct format 'IPaddress:Port'");
45+
}
46+
}).ToArray());
47+
}
48+
49+
public static LookupClientOptions Options => PortableDnsImplementation.Options;
50+
}
51+
}

Bruce/Dns/PlatformIndependentDnsClient.cs renamed to Kerberos.NET.PortableDns/PortableDnsImplementation.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
1-
using System;
1+
using DnsClient;
2+
using Kerberos.NET.Dns;
3+
using System;
24
using System.Collections.Generic;
35
using System.Linq;
46
using System.Threading.Tasks;
5-
using DnsClient;
6-
using Kerberos.NET.Dns;
77

8-
namespace Kerberos.NET.CommandLine.Dns
8+
namespace Kerberos.NET.PortableDns
99
{
10-
internal class PlatformIndependentDnsClient : IKerberosDnsQuery
10+
internal class PortableDnsImplementation : IKerberosDnsQuery
1111
{
12-
private static readonly WindowsDnsQuery WindowsDns = new WindowsDnsQuery();
13-
14-
public async Task<IReadOnlyCollection<DnsRecord>> Query(string query, DnsRecordType type)
12+
static PortableDnsImplementation()
1513
{
16-
if (WindowsDns.IsSupported)
17-
{
18-
return await WindowsDns.Query(query, type);
19-
}
14+
DnsQuery.RegisterImplementation(new PortableDnsImplementation());
15+
}
2016

21-
var client = new LookupClient();
17+
public static LookupClientOptions Options { get; set; } = new LookupClientOptions();
2218

23-
var response = await client.QueryAsync(query, (QueryType)type);
19+
private static LookupClient Create()
20+
{
21+
return new LookupClient(Options);
22+
}
2423

24+
public async Task<IReadOnlyCollection<DnsRecord>> Query(string query, DnsRecordType type)
25+
{
26+
var client = Create();
27+
var response = await client.QueryAsync(query, (QueryType)type);
2528
var srvRecords = response.Answers.SrvRecords().Select(a => new DnsRecord
2629
{
2730
Name = a.DomainName,
@@ -38,9 +41,7 @@ public async Task<IReadOnlyCollection<DnsRecord>> Query(string query, DnsRecordT
3841
foreach (var srv in srvRecords)
3942
{
4043
var c1 = merged.Where(m => m.Key.Equals(srv.Target, StringComparison.InvariantCultureIgnoreCase));
41-
4244
var canon = c1.SelectMany(r => r);
43-
4445
srv.Canonical = canon.ToList();
4546
}
4647

0 commit comments

Comments
 (0)