-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
108 lines (100 loc) · 3.73 KB
/
Program.cs
File metadata and controls
108 lines (100 loc) · 3.73 KB
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
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Principal;
using System.Threading.Tasks;
namespace NegotiateStreamClient
{
class Program
{
static void Main(string[] args)
{
var controlPort = 54321;
var controlIp = IPAddress.IPv6Any;
if (args.Length > 0)
{
controlPort = int.Parse(args[0]);
}
if (args.Length > 1)
{
controlIp = IPAddress.Parse(args[1]);
}
RunControlChannel(new IPEndPoint(controlIp, controlPort));
}
private static void RunControlChannel(IPEndPoint controlEndPoint)
{
try
{
Console.WriteLine($"Control channel listening on {controlEndPoint}");
var listener = new TcpListener(controlEndPoint);
if (controlEndPoint.AddressFamily == AddressFamily.InterNetworkV6)
{
listener.Server.DualMode = true;
}
listener.Start();
while (true)
{
Console.WriteLine("Accepting control channel...");
var client = listener.AcceptTcpClient();
// Fire and forget, accept the next incoming.
ProcessIncoming(client);
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
private static async void ProcessIncoming(TcpClient client)
{
using (client)
{
try
{
var serverEndPoint = (IPEndPoint)client.Client.RemoteEndPoint;
var dns = await Dns.GetHostEntryAsync(serverEndPoint.Address);
string serverSpn = "HOST/" + dns.HostName;
await ClientAuth(client, serverSpn);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
private static async Task ClientAuth(TcpClient tcpClient, string serverSpn)
{
try
{
using (var clientAuth = new NegotiateStream(tcpClient.GetStream(), leaveInnerStreamOpen: false))
{
Console.WriteLine($"Authenticating to {serverSpn}");
await clientAuth.AuthenticateAsClientAsync(
CredentialCache.DefaultNetworkCredentials,
serverSpn,
ProtectionLevel.EncryptAndSign,
TokenImpersonationLevel.Identification);
Console.WriteLine("Authenticated");
Console.WriteLine($"IsAuthenticated: {clientAuth.IsAuthenticated}");
Console.WriteLine($"IsEncrypted: {clientAuth.IsEncrypted}");
Console.WriteLine($"IsMutuallyAuthenticated: {clientAuth.IsMutuallyAuthenticated}");
Console.WriteLine($"IsSigned: {clientAuth.IsSigned}");
Console.WriteLine($"AuthType: {clientAuth.RemoteIdentity.AuthenticationType}");
Console.WriteLine($"Name: {clientAuth.RemoteIdentity.Name}");
// Send a message to the server.
var message = "Hello from the client.";
using (var writer = new StreamWriter(clientAuth))
{
await writer.WriteAsync(message);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}