forked from MichaCo/DnsClient.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDigCommand.cs
More file actions
174 lines (151 loc) · 5.87 KB
/
DigCommand.cs
File metadata and controls
174 lines (151 loc) · 5.87 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
using System;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using DnsClient;
using McMaster.Extensions.CommandLineUtils;
namespace DigApp
{
public class DigCommand : DnsCommand
{
public static string OS
{
get
{
return RuntimeInformation.OSDescription.Trim();
}
}
public static string Version
{
get
{
return typeof(DigCommand).GetTypeInfo().Assembly.GetName().Version.ToString();
}
}
public CommandArgument DomainArg { get; }
public CommandArgument QClassArg { get; }
public CommandArgument QTypeArg { get; }
public CommandOption ReversArg { get; }
public DigCommand(CommandLineApplication app, string[] originalArgs)
: base(app, originalArgs)
{
DomainArg = app.Argument("domain", "domain name", false);
QTypeArg = app.Argument("q-type", "QType", false);
QClassArg = app.Argument("q-class", "QClass", false);
ReversArg = app.Option("-x", "Reverse lookup shortcut", CommandOptionType.NoValue);
}
protected override async Task<int> Execute()
{
Console.WriteLine();
Console.WriteLine($"; <<>> MiniDiG {Version} {OS} <<>> {string.Join(" ", OriginalArgs)}");
string useDomain = string.IsNullOrWhiteSpace(DomainArg.Value) ? "." : DomainArg.Value;
QueryType useQType = 0;
QueryClass useQClass = 0;
if (!string.IsNullOrWhiteSpace(QClassArg.Value))
{
// q class != null => 3 params (domain already set above).
Enum.TryParse(QClassArg.Value, true, out useQClass);
Enum.TryParse(QTypeArg.Value, true, out useQType);
}
else
{
// q class is null => 2 params only
// test if no domain is specified and first param is either qtype or qclass
if (Enum.TryParse(DomainArg.Value, true, out useQType))
{
useDomain = ".";
Enum.TryParse(QTypeArg.Value, true, out useQClass);
}
else if (Enum.TryParse(DomainArg.Value, true, out useQClass))
{
useDomain = ".";
}
else if (!Enum.TryParse(QTypeArg.Value, true, out useQType))
{
// could be q class as second and no QType
Enum.TryParse(QTypeArg.Value, true, out useQClass);
}
}
if (ReversArg.HasValue())
{
useQType = QueryType.PTR;
useQClass = QueryClass.IN;
if (!IPAddress.TryParse(useDomain, out IPAddress ip))
{
Console.WriteLine($";; Error: Reverse lookup for invalid ip {useDomain}.");
return 1;
}
useDomain = ip.GetArpaName();
}
if (useQType == 0)
{
if (string.IsNullOrWhiteSpace(useDomain) || useDomain == ".")
{
useQType = QueryType.NS;
}
else
{
useQType = QueryType.A;
}
}
try
{
// finally running the command
var options = GetLookupSettings();
options.EnableAuditTrail = true;
var lookup = GetDnsLookup(options);
Console.WriteLine($"; Servers: {string.Join(", ", lookup.NameServers)}");
var parsedDnsString = DnsString.Parse(useDomain);
if (parsedDnsString.NumberOfLabels == 1 && !parsedDnsString.Original.EndsWith("."))
{
foreach (var server in lookup.NameServers)
{
if (server.DnsSuffix != null)
{
var newQuery = parsedDnsString + server.DnsSuffix;
var serverResult = useQClass == 0 ?
await lookup.QueryServerAsync(new[] { server }, newQuery, useQType).ConfigureAwait(false) :
await lookup.QueryServerAsync(new[] { server }, newQuery, useQType, useQClass).ConfigureAwait(false);
if (!serverResult.HasError)
{
Console.WriteLine(serverResult.AuditTrail);
return 0;
}
}
}
}
var result = useQClass == 0 ?
await lookup.QueryAsync(useDomain, useQType).ConfigureAwait(false) :
await lookup.QueryAsync(useDomain, useQType, useQClass).ConfigureAwait(false);
Console.WriteLine(result.AuditTrail);
}
catch (Exception ex)
{
var agg = ex as AggregateException;
var dns = ex as DnsResponseException;
agg?.Handle(e =>
{
dns = e as DnsResponseException;
if (dns != null)
{
return true;
}
return false;
});
if (dns != null)
{
Console.WriteLine(dns.AuditTrail);
Console.WriteLine($";; Error: {ex.Message}");
return 24;
}
else
{
Console.WriteLine($";; Error: {ex.Message}");
return 40;
}
}
return 0;
}
}
}