Skip to content

Commit

Permalink
Fixing bug in iotedge check (#3935)
Browse files Browse the repository at this point in the history
* Fixing bug in iotedge check

* adding comment
  • Loading branch information
huguesBouvier authored Nov 9, 2020
1 parent 82e6373 commit dd8b529
Showing 1 changed file with 66 additions and 14 deletions.
80 changes: 66 additions & 14 deletions edge-modules/iotedge-diagnostics-dotnet/src/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ namespace Diagnostics
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Threading.Tasks;
using Microsoft.Azure.Devices.Edge.Util;
using Microsoft.Extensions.Configuration;
using ProxyLib.Proxy;

class Program
{
Expand Down Expand Up @@ -73,32 +75,82 @@ static async Task EdgeAgent(string managementUri)

static async Task Upstream(string hostname, string port, string proxy)
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
if (port == "443")
{
var httpClientHandler = new HttpClientHandler();
httpClientHandler.ServerCertificateCustomValidationCallback = (message, cert, chain, sslPolicyErrors) =>
{
return true; // Is valid
};
};

if (proxy != null)
{
Environment.SetEnvironmentVariable("https_proxy", proxy);
}
if (proxy != null)
{
Environment.SetEnvironmentVariable("https_proxy", proxy);
}

var httpClient = new HttpClient(httpClientHandler);
var logsUrl = string.Format("https://{0}/devices/0000/modules", hostname);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, logsUrl);
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);
var httpClient = new HttpClient(httpClientHandler);
var logsUrl = string.Format("https://{0}/devices/0000/modules", hostname);
var httpRequest = new HttpRequestMessage(HttpMethod.Get, logsUrl);
HttpResponseMessage httpResponseMessage = await httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead);

var keys = httpResponseMessage.Headers.GetValues("iothub-errorcode");
if (!keys.Contains("InvalidProtocolVersion"))
var keys = httpResponseMessage.Headers.GetValues("iothub-errorcode");
if (!keys.Contains("InvalidProtocolVersion"))
{
throw new Exception($"Wrong value for iothub-errorcode header");
}
}
else
{
throw new Exception($"Wrong value for iothub-errorcode header");
// The current rust code never put proxy parameter when port is != than 443.
// So the code below is never exercised. It was put there to avoid silently ignoring the proxy
// if the rust code is changed.
if (proxy != null)
{
Uri proxyUri = new Uri(proxy);
IProxyClient proxyClient = MakeProxy(proxyUri);

// Setup timeouts
proxyClient.ReceiveTimeout = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;
proxyClient.SendTimeout = (int)TimeSpan.FromSeconds(60).TotalMilliseconds;

// Get TcpClient to futher work
var client = proxyClient.CreateConnection(hostname, int.Parse(port));
client.GetStream();
}
else
{
TcpClient client = new TcpClient();
await client.ConnectAsync(hostname, int.Parse(port));
client.GetStream();
}
}
}

static void ParentHostname(string parent_hostname)
{
_ = Dns.GetHostEntry(parent_hostname);
}

static IProxyClient MakeProxy(Uri proxyUri)
{
// Uses https://github.com/grinay/ProxyLib
ProxyClientFactory factory = new ProxyClientFactory();
if (proxyUri.UserInfo == string.Empty)
{
return factory.CreateProxyClient(ProxyType.Http, proxyUri.Host, proxyUri.Port);
}
else
{
if (proxyUri.UserInfo.Contains(':'))
{
var userPass = proxyUri.UserInfo.Split(':');
return factory.CreateProxyClient(ProxyType.Http, proxyUri.Host, proxyUri.Port, userPass[0], userPass[1]);
}
else
{
throw new Exception($"Invalid user info: {proxyUri.UserInfo}");
}
}
}
}
}

0 comments on commit dd8b529

Please sign in to comment.