Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Add support for using hostname in rule "Address" field.
Browse files Browse the repository at this point in the history
  • Loading branch information
stackia committed Oct 20, 2015
1 parent 672346f commit cdf80c3
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 33 deletions.
34 changes: 27 additions & 7 deletions ARSoft.Tools.Net/Dns/DnsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,35 @@ static DnsClient()
public DnsClient(IPAddress dnsServer, int queryTimeout)
: this(new List<IPAddress> { dnsServer }, queryTimeout) {}

/// <summary>
/// Provides a new instance with custom dns servers and query timeout
/// <summary>
/// Provides a new instance with custom dns server and query timeout
/// </summary>
/// <param name="dnsServers"> The IPAddresses of the dns servers to use </param>
/// <param name="dnsServer"> The IPAddress of the dns server to use </param>
/// <param name="queryTimeout"> Query timeout in milliseconds </param>
public DnsClient(List<IPAddress> dnsServers, int queryTimeout)
: base(dnsServers, queryTimeout, 53) {}

protected override int MaximumQueryMessageSize
/// /// <param name="port"> The dns server port </param>
public DnsClient(IPAddress dnsServer, int queryTimeout, int port)
: this(new List<IPAddress> { dnsServer }, queryTimeout, port)
{ }

/// <summary>
/// Provides a new instance with custom dns servers and query timeout
/// </summary>
/// <param name="dnsServers"> The IPAddresses of the dns servers to use </param>
/// <param name="queryTimeout"> Query timeout in milliseconds </param>
public DnsClient(List<IPAddress> dnsServers, int queryTimeout)
: this(dnsServers, queryTimeout, 53) {}

/// <summary>
/// Provides a new instance with custom dns servers and query timeout
/// </summary>
/// <param name="dnsServers"> The IPAddresses of the dns servers to use </param>
/// <param name="queryTimeout"> Query timeout in milliseconds </param>
/// <param name="port"> The dns server port </param>
public DnsClient(List<IPAddress> dnsServers, int queryTimeout, int port)
: base(dnsServers, queryTimeout, port)
{ }

protected override int MaximumQueryMessageSize
{
get { return 512; }
}
Expand Down
6 changes: 3 additions & 3 deletions ARSoft.Tools.Net/Dns/DnsMessageEntryBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ public abstract class DnsMessageEntryBase
/// <summary>
/// Domain name
/// </summary>
public string Name { get; internal set; }
public string Name { get; set; }

/// <summary>
/// Type of the record
/// </summary>
public RecordType RecordType { get; internal set; }
public RecordType RecordType { get; set; }

/// <summary>
/// Class of the record
/// </summary>
public RecordClass RecordClass { get; internal set; }
public RecordClass RecordClass { get; set; }

internal abstract int MaximumLength { get; }

Expand Down
65 changes: 43 additions & 22 deletions DNSAgent/DnsAgent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,35 +262,56 @@ await Task.Run(async () =>
if (!Regex.IsMatch(question.Name, Rules[i].Pattern)) continue;
// Domain name matched
if (Rules[i].NameServer != null) // Name server override
targetNameServer = Rules[i].NameServer;
if (Rules[i].QueryTimeout != null) // Query timeout override
queryTimeout = Rules[i].QueryTimeout.Value;
if (Rules[i].CompressionMutation != null) // Compression pointer mutation override
useCompressionMutation = Rules[i].CompressionMutation.Value;
if (Rules[i].Address != null)
{
IPAddress ip;
IPAddress.TryParse(Rules[i].Address, out ip);
if (ip == null) continue; // Invalid rule
if (question.RecordType == RecordType.A &&
if (ip == null) // Invalid IP, may be a domain name
{
var serverEndpoint = Utils.CreateIpEndPoint(targetNameServer, 53);
var dnsClient = new DnsClient(serverEndpoint.Address, queryTimeout, serverEndpoint.Port);
var response = await Task<DnsMessage>.Factory.FromAsync(dnsClient.BeginResolve, dnsClient.EndResolve,
Rules[i].Address, question.RecordType, question.RecordClass, null);
if (response == null)
{
Logger.Warning($"Remote resolve failed for {Rules[i].Address}.");
return;
}
message.ReturnCode = response.ReturnCode;
foreach (var answerRecord in response.AnswerRecords)
{
answerRecord.Name = question.Name;
message.AnswerRecords.Add(answerRecord);
}
message.IsQuery = false;
}
else
{
if (question.RecordType == RecordType.A &&
ip.AddressFamily == AddressFamily.InterNetwork)
message.AnswerRecords.Add(new ARecord(question.Name, 600, ip));
else if (question.RecordType == RecordType.Aaaa &&
ip.AddressFamily == AddressFamily.InterNetworkV6)
message.AnswerRecords.Add(new AaaaRecord(question.Name, 600, ip));
else // Type mismatch
continue;
message.ReturnCode = ReturnCode.NoError;
message.IsQuery = false;
message.AnswerRecords.Add(new ARecord(question.Name, 600, ip));
else if (question.RecordType == RecordType.Aaaa &&
ip.AddressFamily == AddressFamily.InterNetworkV6)
message.AnswerRecords.Add(new AaaaRecord(question.Name, 600, ip));
else // Type mismatch
continue;
message.ReturnCode = ReturnCode.NoError;
message.IsQuery = false;
}
}
else
{
if (Rules[i].NameServer != null) // Name server override
targetNameServer = Rules[i].NameServer;
if (Rules[i].QueryTimeout != null) // Query timeout override
queryTimeout = Rules[i].QueryTimeout.Value;
if (Rules[i].CompressionMutation != null) // Compression pointer mutation override
useCompressionMutation = Rules[i].CompressionMutation.Value;
}
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion DNSAgent/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.3.*")]
[assembly: AssemblyVersion("1.4.*")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit cdf80c3

Please sign in to comment.