-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
141 lines (126 loc) · 4.1 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Authentication;
using System.Threading.Tasks;
using myoddweb.commandlineparser;
using myoddweb.commandlineparser.Rules;
namespace myoddweb.ssltest
{
internal class Program
{
/// <summary>
/// The lock we will use for wr
/// </summary>
private static readonly object Lock = new object();
private static async Task Main(string[] args)
{
var arguments = new CommandlineParser(args, new CommandlineArgumentRules
{
new HelpCommandlineArgumentRule( new []{"help", "h"} ) ,
new RequiredCommandlineArgumentRule( "host" ),
new OptionalCommandlineArgumentRule( "port", "443" ),
new OptionalCommandlineArgumentRule( "scheme", "http" )
});
var uriBuilder = new UriBuilder(arguments.Get<string>( "scheme"),
arguments.Get<string>("host"),
arguments.Get<int>( "port"));
var uri = uriBuilder.Uri;
var sslTest = new SslTester(uriBuilder.Uri);
Console.WriteLine($"Checking: {uri} (IP resolved as {await sslTest.GetIpAddressAsync().ConfigureAwait(false) ?? IPAddress.None})");
// test the prefered/expected protocols
await TestProtocols(
sslTest,
"Prefered:",
new[] {
SslProtocols.None,
SslProtocols.Tls12,
SslProtocols.Tls13
}
).ConfigureAwait(false);
// then test the obsolete protocols
await TestProtocols(
sslTest,
"Obsolete:",
new[] {
// we know it is obsolete, this is why we are testing for it
#pragma warning disable 618
SslProtocols.Ssl2,
SslProtocols.Ssl3,
#pragma warning restore 618
SslProtocols.Tls,
SslProtocols.Tls11
}
).ConfigureAwait(false);
}
private static async Task TestProtocols(
SslTester sslTester,
string message,
IEnumerable<SslProtocols> protocols
)
{
Console.WriteLine( message );
// wait for all to be done.
await Task.WhenAll(protocols.Select(protocol => WriteResponseAsync(protocol, sslTester)).ToArray()).ConfigureAwait(false);
// add a new line
Console.WriteLine();
}
private static async Task WriteResponseAsync(SslProtocols ssl, SslTester sslTester)
{
var supported = await sslTester.IsSupportedAsync(ssl).ConfigureAwait(false);
lock (Lock)
{
var color = Console.ForegroundColor;
try
{
const string warning = "[Warning]";
const string good = "[Good] ";
const string bad = "[Bad] ";
ConsoleColor newColor;
string message;
switch (ssl)
{
case SslProtocols.None:
newColor = ConsoleColor.Blue;
// the default is kind of pexpected
message = supported ? good : warning;
break;
// we know it is obsolete, this is why we are testing for it
#pragma warning disable 618
case SslProtocols.Ssl2:
case SslProtocols.Ssl3:
#pragma warning restore 618
case SslProtocols.Tls:
case SslProtocols.Tls11:
newColor = supported? ConsoleColor.Yellow: ConsoleColor.Green;
message = supported ? warning : good;
break;
case SslProtocols.Tls12:
case SslProtocols.Tls13:
newColor = supported ? ConsoleColor.Green : ConsoleColor.Red;
message = supported ? good : warning;
break;
default:
newColor = ConsoleColor.Gray;
message = bad;
break;
}
Console.ForegroundColor = newColor;
if (supported)
{
Console.WriteLine($" {message}: {ssl} is supported");
}
else
{
Console.WriteLine($" {message}: {ssl} is not supported");
}
}
finally
{
Console.ForegroundColor = color;
}
}
}
}
}