Skip to content

Commit 38ddf17

Browse files
committed
Adds logic for ignore server certificates
1 parent 13c3283 commit 38ddf17

File tree

1 file changed

+58
-13
lines changed

1 file changed

+58
-13
lines changed

src/https/Program.cs

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
using System.Linq;
66
using System.Net.Http;
77
using System.Net.Http.Headers;
8-
using System.Threading;
98
using System.Threading.Tasks;
10-
using System.Xml;
119
using System.Xml.Linq;
1210
using Utf8Json;
1311

@@ -128,7 +126,7 @@ static IEnumerable<Content> ParseContents(IEnumerable<string> args)
128126
}
129127
}
130128
}
131-
129+
132130
void Help()
133131
{
134132
var stream = _stdout();
@@ -158,6 +156,11 @@ static void AddHeaders(HttpRequestMessage request, IEnumerable<Content> contents
158156
readonly Func<Stream> _stdin;
159157
readonly Func<Stream> _stdout;
160158
readonly bool _useStdin;
159+
public Program()
160+
: this(Console.OpenStandardError, Console.OpenStandardInput, Console.OpenStandardOutput, Console.IsInputRedirected)
161+
{
162+
163+
}
161164
public Program(Func<Stream> stderr, Func<Stream> stdin, Func<Stream> stdout, bool useStdin)
162165
{
163166
_stderr = stderr;
@@ -167,8 +170,7 @@ public Program(Func<Stream> stderr, Func<Stream> stdin, Func<Stream> stdout, boo
167170
}
168171

169172
public static Task<int> Main(string[] args) =>
170-
new Program(Console.OpenStandardError, Console.OpenStandardInput, Console.OpenStandardOutput, Console.IsInputRedirected)
171-
.RunAsync(args);
173+
new Program().RunAsync(args);
172174

173175
public async Task<int> RunAsync(string[] args)
174176
{
@@ -207,8 +209,16 @@ public async Task<int> RunAsync(string[] args)
207209
var stdoutWriter = new StreamWriter(stdout) { AutoFlush = true };
208210
{
209211
var renderer = new Renderer(stdoutWriter, stderrWriter);
212+
213+
var http = options.IgnoreCertificate
214+
? new HttpClient(
215+
new HttpClientHandler
216+
{
217+
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator
218+
}
219+
)
220+
: new HttpClient();
210221

211-
var http = new HttpClient();
212222
var request = new HttpRequestMessage(
213223
command.Method ?? HttpMethod.Get,
214224
command.Uri
@@ -277,16 +287,19 @@ class Options
277287
{
278288
public ContentType RequestContentType { get; }
279289
public string XmlRootName { get; }
290+
public bool IgnoreCertificate { get; }
280291

281-
public Options(ContentType requestContentType, string xmlRootName)
292+
public Options(ContentType requestContentType, string xmlRootName, bool ignoreCertificate)
282293
{
283294
RequestContentType = requestContentType;
284295
XmlRootName = xmlRootName;
296+
IgnoreCertificate = ignoreCertificate;
285297
}
286298
public static Options Parse(IEnumerable<string> args)
287299
{
288300
var requestContentType = ContentType.Json;
289301
var xmlRootName = default(string);
302+
var ignoreCertificate = false;
290303
foreach (var arg in args)
291304
{
292305
if (arg.StartsWith("--json"))
@@ -314,8 +327,12 @@ public static Options Parse(IEnumerable<string> args)
314327
{
315328
requestContentType = ContentType.FormUrlEncoded;
316329
}
330+
else if (arg.StartsWith("--ignore-certificate"))
331+
{
332+
ignoreCertificate = true;
333+
}
317334
}
318-
return new Options(requestContentType, xmlRootName);
335+
return new Options(requestContentType, xmlRootName, ignoreCertificate);
319336
}
320337
}
321338

@@ -411,21 +428,49 @@ public void WriteHeaders(HttpResponseHeaders responseHeaders, HttpContentHeaders
411428
}
412429
}
413430

414-
public void WriteException(Exception ex) =>
415-
WriteException(ex, 0);
416-
417-
public void WriteException(Exception ex, int depth)
431+
public void WriteException(Exception ex)
432+
{
433+
var help = WriteException(ex, 0);
434+
switch (help)
435+
{
436+
case ExceptionHelp.IgnoreCertificate:
437+
_info.WriteLine("Ensure you trust the server certificate or try using the --ignore-certificate flag");
438+
break;
439+
}
440+
}
441+
442+
ExceptionHelp WriteException(Exception ex, int depth)
418443
{
419444
if (depth > 0)
420445
{
421446
_info.Write(new string('\t', depth));
422447
}
423448
_info.WriteLine(ex.Message);
424449

450+
var exceptionHelp = ExceptionHelp.None;
451+
switch (ex.Message)
452+
{
453+
case "The SSL connection could not be established, see inner exception.":
454+
exceptionHelp = ExceptionHelp.IgnoreCertificate;
455+
break;
456+
}
457+
425458
if (ex.InnerException != null)
426459
{
427-
WriteException(ex.InnerException, depth + 1);
460+
var otherHelp = WriteException(ex.InnerException, depth + 1);
461+
if (otherHelp != ExceptionHelp.None)
462+
{
463+
return otherHelp;
464+
}
428465
}
466+
467+
return exceptionHelp;
468+
}
469+
470+
enum ExceptionHelp
471+
{
472+
None = 0,
473+
IgnoreCertificate = 1,
429474
}
430475
}
431476

0 commit comments

Comments
 (0)