Skip to content

Commit

Permalink
Explicit references instead of using StandardLib
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaCo committed Dec 29, 2016
1 parent d109b3c commit 150df0d
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 52 deletions.
15 changes: 3 additions & 12 deletions samples/MiniDig/DnsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public abstract class DnsCommand

public string[] OriginalArgs { get; }

public CommandOption PortArg { get; set; }

public CommandOption ServerArg { get; set; }

public CommandOption TriesArg { get; set; }
Expand Down Expand Up @@ -141,9 +139,7 @@ public TimeSpan GetMinimumTTL()

return TimeSpan.Zero;
}

public int GetPortValue() => PortArg.HasValue() ? int.Parse(PortArg.Value()) : 53;


public int GetTimeoutValue() => ConnectTimeoutArg.HasValue() ? int.Parse(ConnectTimeoutArg.Value()) : 5000;

public int GetTriesValue() => TriesArg.HasValue() ? int.Parse(TriesArg.Value()) : 10;
Expand All @@ -165,14 +161,9 @@ protected virtual void Configure()
{
ServerArg = App.Option(
"-s | --server",
"The DNS server.",
"The DNS server <name|ip>#<port> (multiple)",
CommandOptionType.MultipleValue);

PortArg = App.Option(
"-p | --port",
$"The port to use to connect to the DNS server [{NameServer.DefaultPort}].",
CommandOptionType.SingleValue);


NoRecurseArg = App.Option(
"-nr | --norecurse",
"Non recurive mode.",
Expand Down
4 changes: 2 additions & 2 deletions samples/MiniDig/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static void Main(string[] args)
//}
//return;

var app = new CommandLineApplication(throwOnUnexpectedArg: false);
var app = new CommandLineApplication(throwOnUnexpectedArg: true);

var perfApplication = app.Command("perf", (perfApp) => new PerfCommand(perfApp, args), false);
var perfApplication = app.Command("perf", (perfApp) => new PerfCommand(perfApp, args), throwOnUnexpectedArg: true);

var defaultCommand = new DigCommand(app, args);

Expand Down
2 changes: 0 additions & 2 deletions src/DnsClient/LookupClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,6 @@ private void DisableEndpoint(NameServer server)
_endpoints.ToList().ForEach(p => p.Enabled = true);
}
}

Debug.WriteLine($"Disabling name server {server.Endpoint}.");
}

private class Audit
Expand Down
16 changes: 11 additions & 5 deletions src/DnsClient/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,21 @@
"frameworks": {
"netstandard1.3": {
"dependencies": {
"NETStandard.Library": "1.6.0",
"System.Net.NameResolution": "4.0.0",
"System.Net.NetworkInformation": "4.3.0"
"System.Collections": "4.3.0",
"System.Collections.Concurrent": "4.3.0",
"System.Linq": "4.3.0",
"System.Net.NameResolution": "4.3.0",
"System.Net.NetworkInformation": "4.3.0",
"System.Net.Sockets": "4.3.0",
"System.Runtime.Extensions": "4.3.0",
"System.Text.Encoding": "4.3.0",
"System.Threading": "4.3.0"
},
"buildOptions": { "define": [ "XPLAT" ] }
},
"net451": {
"net45": {
"frameworkAssemblies": {
}
}
}
}
}
53 changes: 22 additions & 31 deletions test/FullFrameworkOwinApp/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using DnsClient;
using Owin;

Expand All @@ -13,46 +11,39 @@ public class Startup
{
public void Configuration(IAppBuilder app)
{
//var x = GetService("TenantConfigurationService").Result;

// forced sync context within async owin, bad bad things will happen
app.Run(ctx =>
{
//var task = Task.Factory.StartNew(()=> GetService("consul"));
//var uri = task.Unwrap().GetAwaiter().GetResult();
var uri = GetService("consul").Result;
ctx.Response.Write(uri.OriginalString);
var query = ctx.Request.Query.Get("q") ?? Dns.GetHostName();
// explicitly use Result here although middleware could be async
// just to test the bad blocking behavior of the owin stuff and see
// if QueryAsync produces a deadlock.
try
{
var ip = GetService(query).Result;
ctx.Response.Write($"{{ \"answer\": \"{ip.ToString()}\"}}");
}
catch(Exception ex)
{
ctx.Response.StatusCode = (int)HttpStatusCode.NotFound;
ctx.Response.Write(ex.InnerException?.Message ?? ex.Message);
}
return Task.FromResult(0);
});

////app.Run(async ctx =>
////{
////    await ctx.Response.WriteAsync((await GetService("TenantConfigurationService")).OriginalString);
////    //return Task.FromResult(0);
////});
}

private static async Task<Uri> GetService(string serviceName)
private static async Task<IPAddress> GetService(string query)
{
            // TODO: What about the datacenter/tag features?
            var dnsQuery = string.Format("{0}.service.consul", serviceName);
var dnsClient = new LookupClient(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8600));
var dnsResult = await dnsClient.QueryAsync(dnsQuery, QueryType.SRV).ConfigureAwait(false);

var srv = dnsResult.Answers.SrvRecords().FirstOrDefault();
if (srv == null)
{
throw new InvalidOperationException($"SRV record not found for {dnsQuery}");
}
var dnsClient = new LookupClient();
var dnsResult = await dnsClient.QueryAsync(query, QueryType.ANY).ConfigureAwait(false);

var ip = dnsResult.Additionals.ARecords().FirstOrDefault(p => p.QueryName == srv.Target)?.Address;
if (ip == null)
var aRecord = dnsResult.Answers.ARecords().FirstOrDefault();
if (aRecord == null)
{
throw new InvalidOperationException($"Invalid DNS response. A record missing for {srv.Target}");
throw new InvalidOperationException($"Record not found for {query}");
}

// TODO: HTTP scheme is hard-coded!!!
return new Uri($"http://{ip}:{srv.Port}", UriKind.Absolute);
return aRecord.Address;
}
}
}

0 comments on commit 150df0d

Please sign in to comment.