Intercepting data from WebSockets in WebBrowser - c#

I have to develop a WinForms app with embedded web browser. The browser could be either WebBrowser control or WebView2. Once I supply IP address to the address line the browser starts getting data from the server (of that IP) over WebSocket. I think the browser is the one that creates the WebSocket and connects to the server.
My question is: how to I intercept the data that comes over the WebSocket from the server? Somehow I need to hookup to this web socket. Redundant to say that my code in the app is C#. I need to do some calculations on that data.
I was thinking creating my own WebSocket or Socket in C# and connecting to the server with that IP, but I am not sure if the server accepts multiple connections...

Apparently it is possible to launch a browser with the page and then create CDP session (Chrome DevTools Protocol) that allows interacting with browser API and thus intercept a WebSocket communication. CDP is the foundation for Microsoft Edge DevTools, which is now in Preview. Meanwhile CDP can be used... Alternatively, I can also use open source PuppeteerSharp library that I can get via NuGet and it gives access to CDP... Anyway, that's the direction that I will be be digging in...

Related

How to get the web socket (WS) tab data programmatically?

I want to get information from a web socket running on a website. however since I am not aware of handshaking process I am unable to establish a successful connection to the websocket and retrieve the information being published on the websocket. I assume this falls on the website confidentiality and unless they provide me the documents it is highly unlikely I can get the data directly from the websocket (please correct me if I am wrong).
However since I can view the data from "WS" tab in network section of the chrome (developer mode). I was wondering if there is a way to replicate this programmatically using selenium or any headless browser and I get this information inside my program. my main objective is to get the information using C# however I am open to any other way to get this data.
This is the sample picture of a WS
I have also found this extension "chrome.devtools.network" but I am not sure if it can do the thing I want. I am still searching any advice or help is highly appreciated
With new versions of Selenium webdrive (version 4 - It's beta version) you can create a devtool session of the driver
driver.getDevTools().createSession();
With wich you can send any devtool commands. You should create a web socket event and send the request through it. You can refer this link for Network Domain Chrome DevTools Protocol.
Also, you can use logging preferences properties and intercept performance logs to fetch messages sent and received through websocket.
driver.manage().logs().get(LogType.PERFORMANCE);
I tried the last solution using Java ang get this result for requesting this link
Complete source code is provided here
Note that you should wait until the chrome driver quit. Then in console you'll see the sent and received messages

Application send data to web page

I have a web page for data processing. Web page waits for data to process.
And I have a C# application for data. I want to send data to open web page. But I don't want to use socket, Post/Get methods or any web request.
Web page and C# application are client side. They run in same Windows at the same time.
I want to send data to web page from C# app. This operation need to be done with Windows OS or some command line based trigger mechanism.
Web page (Chrome tab or Firefox tab; it doesn't matter) should have tab id the work on. With using this id I may be able to send data to web page from C# app.
But I couldn't really find anything useful.
Is there any way to do this? Is it even possible?
Any advice appreciated.
The way I would approach this is this, assuming you are using either WinForms or WPF:
In your application, embed a web browser:
WPF: https://www.wpf-tutorial.com/misc-controls/the-webbrowser-control/
WinForms: https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/webbrowser-control-windows-forms?view=netframeworkdesktop-4.8
Load the web page in that browser
Establish a two-way communication between your client application and the web page:
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/controls/implement-two-way-com-between-dhtml-and-client?view=netframeworkdesktop-4.8
If using other technologies let me know and I'll update answer.
One solution is Raw TCP/IP connection on browser but it's not yet fully supported and implemented. If by Web requests you mean online requests then what #DevRacker said is the best I know, too.
However consider TCP/IP, Web sockets and even REST-APIs are frequently used for Inter-Process Communication (IPC) too, when there is no online transmission of data and the data/command is only transmitted over a local machine.
If I were you, I would use Web sockets or maybe a simpler solution such as Socket-IO.

Use Microsoft.Interop on server application for multiple Windows users

I'm creating a server (PHP/JS) application which is communicating with a client application (C#) over Websockets (WS). The C# application is listening to port 4001 and the server application will create for each request (in javascript) a websocket to the C# application.
In the server application it is possible for the end-user to create a new e-mail, which will be send over the websocket so the client application creates the e-mail in Outlook (using Microsoft.Office.Interop.Outlook). So far so good, everything is working fine.
I need to run this client application for each user on the remote desktop envoirment, because it's using the Microsoft.Office.Interop.Outlook libary. This is impossible, because there can only be 1 instance of the application listening to port 4001. I read on various sources that it is impossible to use Microsoft.Office.Interop.Outlook as a C# service.
My question is, how can I achieve that there is only one active instance of the client application on the server side, which can use the Microsoft.Office.Interop.Outlook libary off the current user.

How to use cassia to Create Remote Session and send input?

I know I can use Remote Desktop ActiveX control, but I am trying to implement solution browser based solution just like browserstack.com etc, they have implemented same by using Flash which connects to their server which in turn might be rdp gateway.
I have looked at library Cassia, https://code.google.com/p/cassia/source/browse/trunk/Samples/SessionInfo/Source/SessionInfo/Program.cs, but I am not able to understand how to actually grab the screen and send input after we call WTSStartRemoteControlSession method.
I want to call these methods from an ASP.NET MVC Web Application, which basically stores scripts for automated UI testing. Most of the time, no body will be keeping any UI open, our test server will automatically create RDP session, run everything and then email results.
Only for recording tests, programmers will use Web UI which will communicate with server purely through web sockets and download images of screen. Using ActiveX control is not possible as we use UI Automation to capture text/UI element information etc.
Yeah, Cassia and the WTS API won't help you much here, since they're more for administering servers than interacting with individual sessions.
You could try an HTML5 RDP client like Guacamole. It has a JavaScript client API that allows you to send key and mouse events.
As an alternative, very comparable to Guacamole, you can try Myrtille, also open source and using FreeRDP as rdp client through an HTTP gateway. The main difference is it's done in .NET (C#) and thus is more intended for Windows Servers.

Server listen to Client Serial Ports

I manage a web site for company that hired me after being unsatisfied with the original developer of the site. One function of this site is to add new patients. Part of adding the patient is to receive data from a USB port on the client machine via an applet. Since the Java 7 update 21 the applet does not work and the company has asked to remove the applet and replace it with a different solution. I am having trouble doing this, however, due to the applet listening to the client USB ports from the server to retrieve the data. I Have thought of a few ideas to solve this but am unsure if it is possible and haven't been able to find the answer online. I was hoping someone here could tell me if my idea is possible or point me in the right direction.
When the user adds a new patient the web site will call a web service on the server. This web service will then call a WCF hosted Windows Service on the client and active its serial port listener. Once the client's Windows Service has received the data the WCF will respond to the Web Service on the server and parse the data.
What I'm having trouble with is finding a way that the web service can call the WCF downloaded by the client and have it begin listening to the USB ports. If anyone could tell me how to do this, or suggest a better way to have the server initiate the client listening to the serial port I would appreciate it.
Do you have any system requirements?
When entering new patient, what kind of browsers are expected?
Will this be deployed as one solution, or clients expect that your application
can support any modern browser?
The main problem is that browsers are very restrictive in terms of what
kind of code you are allowed to execute. By default, browser won't allow
to execute any plugins / read any usb ports, let alone execution / installation
of arbitrary wcf service.
So, supposing that you have 'any modern browser' in requirements - you have following options:
Java / activex applet plugin. I believe this is the only sure-way to have access to usb
on a client machine. Also plugin must be signed with trusted certificate, or client must allow
execution of untrusted plugin (again, check requirements). I though silverlight would also
be a valid alternative, but according to my research (please correct if I am wrong) - its security
model does not allow any kind of USB access.
Prompt user to download and install browser plugin / helper object with access to usb.
And then communicate with your web page through this plugin.
Installing an entire wcf service on a client side would be an overkill in my opinion.
You would have to manage issues like firewalls, closed ports, security,
writing a self-hosting wcf solution, etc.
But if you managed to install and host a wcf service on a client side:
When the user adds a new patient the web site will call a web service on the server.
This web service will then call a WCF hosted Windows Service on the client and active its serial port listener.
Why not save a roundtrip and call client service directly from page?
Server would need to know client address to call it, which is not always possible.
Probably your web page can attempt to access localhost, at predefined port,
where your wcf service is listening.
Once the client's Windows Service has received the data the WCF
will respond to the Web Service on the server and parse the data.
It would be easier to respond on call from your web page (use polling if usb reading is slow),
and only then send to server. Browser already knows where to send data, and have permission
to do so. But if your server address is well known and can be accessed with domain name,
you can try to connect to it from your wcf on client.
Also there is a very similar topic, discussing connection to usb from client.

Categories