Skip to content

Commit

Permalink
some documentation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
orellabacCR committed Nov 26, 2019
1 parent 2ae9057 commit 5e8a29a
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 36 deletions.
3 changes: 2 additions & 1 deletion DesktopAgent2/DesktopAgentForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () =>
Expand Down
Binary file added Images/AfterDesktopAgent2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/AgentWithClient.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/BeforeDesktopAgent2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/DesktopAgent2.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Images/testpage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
109 changes: 102 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<string>();
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
75 changes: 47 additions & 28 deletions testsockets.html → test.html
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<title>Desktop Agent Tests</title>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>DesktopAgent2 Test</h1>
<p class="lead">
This sample page connects to a DesktopAgent2 and allows the execution of some tests
</p>
</div>

<!doctype html>
<style>
textarea { vertical-align: bottom; }
#output { overflow: auto; }
#output > p { overflow-wrap: break-word; }
#output span { color: blue; }
#output span.error { color: red; }
</style>
<h2>DesktopAgent2 Test</h2>
<p>
This sample page connects to a DesktopAgent2 and allows the execution of some tests
</p>
<div class="btn-group-vertical">
<button type="button" class="btn btn-primary" onclick="doConnect()"> Connect </button>
<button type="button" class="btn btn-primary" onclick="doGetComputerName()"> Get Computer Name </button>
<button type="button" class="btn btn-primary" onclick="doStartWord()"> Start Word </button>
<button type="button" class="btn btn-primary" onclick="doStartExcel()"> Start Excel </button>
<button type="button" class="btn btn-primary" onclick="doStartExcel()"> Run Program </button>
</div>

<button onclick="doConnect()"> Connect </button>

<button onclick="doGetComputerName()"> Get Computer Name </button>

<button onclick="doStartWord()"> Start Word </button>

<button onclick="doStartExcel()"> Start Excel </button>

<button onclick="doStartExcel()"> Run Program </button>


<div id=output>NOT CONNECTED</div>
<p>STATUS: <div class="badge badge-info" id=output>NOT CONNECTED</div></p>
<script>

function DesktopAgent2Client()
{
this.wsUri = "ws://127.0.0.1:996/";
this.wsUri = "wss://localhost:996/";
this.websocket = undefined;
this.OnConnected = function(e) {};
this.OnClosed = function(e) {};
Expand Down Expand Up @@ -86,14 +90,29 @@ <h2>DesktopAgent2 Test</h2>
})
}

function doGetComputerName() {
var msg = {'action':'WSH','command':'ComputerName'};
agent.Send(JSON.stringify(msg)).then(function(e) {
var data = JSON.parse(e.data);
alert("Computer Name is " + data.result);
});
}


function doStartExcel() {
var msg = {'action':'Excel','command':'open'};
agent.Send(JSON.stringify(msg)).then(function() {
alert('excel was opened');
})
});
}

function doStartWord() {

var msg = {'action':'Word','command':'open'};
agent.Send(JSON.stringify(msg)).then(function() {
alert('word was opened');
});
}
</script>
</script>
</div>
</body>
</html>

0 comments on commit 5e8a29a

Please sign in to comment.