-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
148 lines (136 loc) · 4.69 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
142
143
144
145
146
147
148
using Microsoft.Win32;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace ConsoleApp6
{
class Program
{
private static ConcurrentDictionary<Guid, HyperGuestConnection> _clients;
private static Dictionary<Guid, string> clientMap;
static void Main(string[] args)
{
Guid serviceGuid = new Guid("{B17A9D47-5580-48F4-9E64-99AF86C8C9EE}");
// are we running as a hypverv guest with integration services enabled?
Guid clientVmkID = Guid.Empty;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Virtual Machine\Guest\Parameters", false))
{
if (key != null)
{
string value = (string)key.GetValue("VirtualMachineId");
if (!string.IsNullOrEmpty(value) && Guid.TryParse(value, out var candidate))
{
clientVmkID = candidate;
}
}
}
if (clientVmkID == Guid.Empty)
{
// assuming we're the server do we have the service we need registered?
bool integrationServiceRegistered = false;
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Virtualization\GuestCommunicationServices", false))
{
if (key != null)
{
string[] subKeys = key.GetSubKeyNames();
if (subKeys != null && subKeys.Length > 0)
{
foreach (string subKey in subKeys)
{
if (!string.IsNullOrEmpty(subKey) && Guid.TryParse(subKey, out Guid candidateServiceGuid))
{
if (candidateServiceGuid == serviceGuid)
{
integrationServiceRegistered = true;
break;
}
}
}
}
}
}
if (!integrationServiceRegistered)
{
Console.WriteLine("Error: integration service is not registered");
return;
}
_clients = new ConcurrentDictionary<Guid, HyperGuestConnection>();
ManagementObjectSearcher searcher = new ManagementObjectSearcher(
@"root\virtualization\v2",
@"select Name,ElementName from Msvm_ComputerSystem where description <>""Microsoft Hosting Computer System"""
);
clientMap = new Dictionary<Guid, string>();
foreach (var result in searcher.Get())
{
if (Guid.TryParse(result.Properties["Name"]?.Value as string, out Guid vmid))
{
clientMap.Add(vmid, (string)result.Properties["ElementName"].Value);
}
}
EndPoint endPoint = new HyperVEndPoint(HyperV.Children, serviceGuid);
using (Socket socket = new Socket(HyperV.HyperVAddressFamily, SocketType.Stream, HyperV.HyperVProtocolType))
{
socket.Bind(endPoint);
socket.Listen(1);
Console.WriteLine("server waiting for connections");
HyperVHost host = new HyperVHost();
while (true)
{
Socket clientSocket = socket.Accept();
HyperGuestConnection client = new HyperGuestConnection(clientSocket, host);
client.Connected += Client_Connected;
client.Disconnecting += Client_Disconnecting;
client.Start();
}
}
}
else
{
EndPoint endPoint = new HyperVEndPoint(HyperV.Parent, serviceGuid);
Socket socket = new Socket(HyperV.HyperVAddressFamily, SocketType.Stream, HyperV.HyperVProtocolType);
socket.Connect(endPoint);
HyperVGuest guest = new HyperVGuest(clientVmkID);
guest.GuestEvent += (sender, e) => { Console.WriteLine($"guest event from host"); };
HyperVHostConnection hostConnection = new HyperVHostConnection(socket, guest);
hostConnection.Start();
Console.WriteLine($"connected to host as {clientVmkID}");
string line = null;
while (!string.IsNullOrEmpty(line = Console.ReadLine()))
{
if (string.Equals(line, "event", StringComparison.InvariantCultureIgnoreCase))
{
guest.FireGuestEvent();
}
}
hostConnection.Stop();
Console.WriteLine("stopped");
Console.ReadLine();
}
}
private static void Client_Disconnecting(object sender, EventArgs e)
{
HyperGuestConnection client = (HyperGuestConnection)sender;
_clients.Remove(client.ID, out _);
client.Connected -= Client_Connected;
client.Disconnected -= Client_Disconnecting;
client.Remote.GuestEvent -= client.Local.GuestEventHandler;
Console.WriteLine($"client {client.ID} disconnected, friendlyname is {client.Name}");
}
private static void Client_Connected(object sender, EventArgs e)
{
HyperGuestConnection client = (HyperGuestConnection)sender;
client.Remote.GuestEvent += client.Local.GuestEventHandler;
if (clientMap.TryGetValue(client.ID, out string name))
{
client.Name = name;
Console.WriteLine($"client {client.ID} connected, friendlyname is {client.Name}");
}
}
}
}