diff --git a/DesktopAgent2/DesktopAgentForm.cs b/DesktopAgent2/DesktopAgentForm.cs index bfb8a03..c56f44e 100644 --- a/DesktopAgent2/DesktopAgentForm.cs +++ b/DesktopAgent2/DesktopAgentForm.cs @@ -260,7 +260,8 @@ private void RunOnSTATread(IWebSocketConnection webSocket, JObject action, Actio private void SetupServer() { lblStatus.Text = "0 clients connected"; - var server = new WebSocketServer("ws://127.0.0.1:996"); + var server = new WebSocketServer("wss://0.0.0.0:996"); + server.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(@"M:\Program Files\OpenSSL-Win64\bin\secondtest.pfx", "test"); server.Start(c => { c.OnOpen = () => diff --git a/Images/AfterDesktopAgent2.PNG b/Images/AfterDesktopAgent2.PNG new file mode 100644 index 0000000..a65e9b6 Binary files /dev/null and b/Images/AfterDesktopAgent2.PNG differ diff --git a/Images/AgentWithClient.png b/Images/AgentWithClient.png new file mode 100644 index 0000000..ccd0274 Binary files /dev/null and b/Images/AgentWithClient.png differ diff --git a/Images/BeforeDesktopAgent2.PNG b/Images/BeforeDesktopAgent2.PNG new file mode 100644 index 0000000..da4c553 Binary files /dev/null and b/Images/BeforeDesktopAgent2.PNG differ diff --git a/Images/DesktopAgent2.PNG b/Images/DesktopAgent2.PNG new file mode 100644 index 0000000..c51d1c6 Binary files /dev/null and b/Images/DesktopAgent2.PNG differ diff --git a/Images/testpage.png b/Images/testpage.png new file mode 100644 index 0000000..a571dc5 Binary files /dev/null and b/Images/testpage.png differ diff --git a/README.md b/README.md index 3903cd2..f036eab 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,12 @@ # DesktopAgent2 -The Web keep progressing and each day there are more an more things you can do from a browser. +The Web keeps progressing and each day there are more things you can do from a browser. -However sometimes you need to access things that are only on the machine were your browser is running. For example opening local office apps or accessing local devices. +However, sometimes you need to access things that are only on the machine where your browser is running. For example, opening local office apps or accessing local devices. -Sometime ago I had provided a similar project called DesktopAgent. That project works fine but there were some lessons learned from it: +![BeforeDesktopAgent](Images\BeforeDesktopAgent2.png) + +Some time ago I had provided a similar project called [DesktopAgent](https://github.com/orellabac/WebMap.DesktopAgent). That project works fine but there were some lessons learned from it: * That project was meant as a too general approach. That project had a concept of plugins for extending functionality and I think that is too complicated. So this project is provided more like a template that provides just the basics, and then you are free @@ -15,9 +17,102 @@ I also switched for a more general communication approach. * Web Request can be too slow and not that reliable When I wrote the old desktop agent, IE6 was still around (it is still around I know but let's say its dead), and now all browsers support WebSockets. -So I decided to switch for an implementation based on websockets. +So I decided to switch for an implementation based on WebSockets. + +Ok, all those learned lessons bring then DesktopAgent2. + +![AfterDesktopAgent](Images\AfterDesktopAgent2.png) + +Desktop Agent2 is a simple template to help you get started on creating your DesktopAgent. +DesktopAgent2 targets .NET Framework 4.5, so it can be used even from Windows 7. + +Building +======== + +To build the code just open `DesktopAgent2.csproj` in Visual Studio + +Testing +======= + +If you just want to test *DesktopAgent2* you can build it from source of download the binary from the Releases link. + +First you need to run the Agent ![Desktop Agent](Images\DesktopAgent2.png) + +Next open the `test.html' page. +![TestPage](Images\testpage.png) + +When you press connect, the page will send a connection request to the agent. Once connected the agent will indicate that it has a new client + +![One client](Images\AgentWithClient.png) + +Now you can press the test buttons. For example press `Start Word` + +The Agent will display the json message it received + +```json +{"action":"Word","command":"open"} +``` + +In general the Agent usage is pretty simple: + +```js + // create a JSON + var msg = {'action':'Excel','command':'open'}; + //Send the JSON message + agent.Send(JSON.stringify(msg)). + // react to the response + then(function() { + alert('excel was opened'); + }); +``` + +On the Agent you just modify the code to insert your custom actions: + +```C# + private void ExecuteActions(IWebSocketConnection webSocket, string message) + { + Invoke(new Action(() => txtLog.AppendText(message))); + + try + { + var action = JObject.Parse(message); + var command = action["action"].Value(); + switch (command) + { + //Add your actions in this switch + case "WSH": + this.RunWSH(webSocket, action); + break; + case "ExecProgram": + this.RunProgram(webSocket, action); + break; + case "Excel": + this.ExcelAction(webSocket, action); + break; + case "Word": + this.WordAction(webSocket, action); + break; + } + } + catch + { + // Just ignore + } + } +``` + +Secure communication +===================== + +Enabling secure connections requires two things: using the scheme wss instead of ws, and pointing to an x509 certificate containing public and private key + +The changes in code are easy. Just two lines: + +```C# + var server = new WebSocketServer("wss://0.0.0.0:996"); + server.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(@"M:\Program Files\OpenSSL-Win64\bin\secondtest.pfx", "test"); +``` + +To create a certificate you can follow this guide: https://github.com/statianzo/Fleck/issues/214#issuecomment-364413879 -Ok, all those lessons learned bring then DesktopAgent2. -Desktop Agent2 is a simple template to help you get started on creating your own DesktopAgent. -DesktopAgent2 targets .NET Framework 4.5 \ No newline at end of file diff --git a/testsockets.html b/test.html similarity index 50% rename from testsockets.html rename to test.html index 9ad4a24..2ae469a 100644 --- a/testsockets.html +++ b/test.html @@ -1,34 +1,38 @@ + + + + + Desktop Agent Tests + + +
+
+

DesktopAgent2 Test

+

+ This sample page connects to a DesktopAgent2 and allows the execution of some tests +

+
- - -

DesktopAgent2 Test

-

- This sample page connects to a DesktopAgent2 and allows the execution of some tests -

+
+ + + + + +
- - - - - - - - - - - -
NOT CONNECTED
+

STATUS:

NOT CONNECTED

\ No newline at end of file + +
+ + \ No newline at end of file