Native messaging from chrome extension to native host written in C# - c#

I'm trying to receive a message from my chrome extension via native messaging. The popup.html console indicates that the message is being sent, but my host is not receiving it for some reason. I can see that the host native.exe is being launched in task manager, but the host is not receive the sent data.
popup.js
document.addEventListener('DOMContentLoaded', function() {
var downloadButton= document.getElementById('Button');
downloadButton.addEventListener('click', function () {
chrome.tabs.query({currentWindow: true, active: true}, function (tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: "myScript.js"}, function (data) {
sendNativeMessage(data[0]);
});
});
});
});
function sendNativeMessage(msg) {
var hostName = "com.google.example";
console.log("Connecting to host: " + hostName);
port = chrome.runtime.connectNative(hostName);
message = {"text": msg};
port.postMessage(message);
console.log("Sent message: " + JSON.stringify(message));
}
Now, when I send the message from my Chrome Extension, I can see the console log for popup.html and the message is correct.
I tried to test everything by using some code that someone claimed to have worked, from Native Messaging Chrome. Specifically, I have
native.exe
static void Main(string[] args) {
while (OpenStandardStreamIn() != null || OpenStandardStreamIn() != "")
{
Console.WriteLine(OpenStandardStreamIn());
}
}
private static string OpenStandardStreamIn()
{
//Read first four bytes for length information
System.IO.Stream stdin = Console.OpenStandardInput();
int length = 0;
byte[] bytes = new byte[4];
stdin.Read(bytes, 0, 4);
length = System.BitConverter.ToInt32(bytes, 0);
string input = "";
for (int i = 0; i < length; i++)
input += (char)stdin.ReadByte();
return input;
}
I'm still trying to wrap my head around nativeMessaging, but since I can see the message being sent via the console logs, I have to assume the error is with my .exe. I have created the registry keys, HKCU, and created the host's manifest.json file. Since the extension is launching the .exe, I'm pretty sure all of that is working fine as well.
If anyone could provide some assistance with this, that would be much appreciated. Thanks!
EDIT: After changing absolutely nothing, I cannot get my native.exe to launch anymore from my extension. In an effort to fix this, I tried rewriting my sender to not open a port:
...
chrome.tabs.executeScript(tabs[0].id, {file: "myScript.js"}, function (data) {
chrome.runtime.sendNativeMessage('com.google.example', {"text":data[0]}, function(response) {
if (chrome.runtime.lastError)
console.log("ERROR");
} else {
console.log("Response: " + response);
}
});
});
...
This doesn't work at all either. I have no idea what I'm doing wrong, or why my host .exe stopped launching when I sent a message from my extension.
EDIT #2
Good news!
I deleted my registry key and then added it again, this time into the Local Machine registry (i.e. HKLM). Again, I used the code from Native Messaging Chrome to send and receive from my host. Now, when I look at my extension's log, I can see the correct response from my host. I am using the "no-port" method by simply calling chrome.runtime.sendNativeMessage(...), which is fine for my purposes. Unfortunately, I am not receiving the message FROM the extension TO the host. Also, my host.exe never exits, even after the message has been received. I'm not sure why, but if anyone can help out, I would appreciate it.
In my host, I'm attempting to write the incoming message to a file just to test that I'm getting the message:
System.IO.File.WriteAllText(#"path_to_file.txt", OpenStandardStreamIn());
Note: The message that I'm passing FROM the extension TO the host is around 250KB (it varies from message to message).
The problem is:
Nothing is written to the file. It's completely blank.
The file takes a long time to create (~4 seconds).
My host.exe instance never terminates. It's still running (I can view it in the task manager).

In order to get my extension working, I followed the following steps:
I deleted my registry key and then re-added it in my HKLM registry.
Although unnecessary,host.exe really only needed to receive one message from the extension, so I changed my previous "open port" messaging to a "no port" message, i.e.
chrome.runtime.sendNativeMessage(...);
Importantly, when I changed the text I was sending from {"text":data[0]} to a simple string {"text":"Hello from Chrome!"}, everything began working smoothly. data[0] was a string of about 250KB, which should definitely be an acceptable message size according to the developer's site. However, I have created a different question for this problem, as I was able to solve the general message sending and receiving problem I was experiencing.

Related

how to get real time log via perforce api similar to p4v log

I am facing issue with perforce api (.net), as i am unable to pull sync logs in real time.
- What am I trying to do
I am trying to pull real time logs as Sync is triggered using the
Perforce.P4.Client.SyncFiles() command. Similar to the P4V GUI Logs, which update when we try to sync any files.
- What is happening now
As the output is generated only after the command is done execution its not something intended for.
Also tried looking into Perforce.P4.P4Server.RunCommand() which does provide detailed report but only after the execution of the command.
Looked into this
Reason is -
I am trying to add a status update to the Tool i am working on which shows which Perforce file is currently being sync'd.
Please advise. Thanks in Advance.
-Bharath
In the C++ client API (which is what P4V is built on), the client receives an OutputInfo callback (or OutputStat in tagged mode) for each file as it begins syncing.
Looking over the .NET documentation I think the equivalents are the P4CallBacks.InfoResultsDelegate and P4CallBacks.TaggedOutputDelegate which handle events like P4Server.InfoResultsReceived etc.
I ended up with the same issue, and I struggled quite a bit to get it to work, so I will share the solution I found:
First, you should use the P4Server class instead of the Perforce.P4.Connection. They are two classes doing more or less the same thing, but when I tried using the P4.Connection.TaggedOutputReceived events, I simply got nothing back. So instead I tried with the P4Server.TaggedOutputReceived, and there, finally, I got the TaggedOutput just like I wanted.
So, here is a small example:
P4Server p4Server = new P4Server(cwdPath); //In my case I use P4Config, so no need to set user or to login, but you can do all that with the p4Server here.
p4Server.TaggedOutputReceived += P4ServerTaggedOutputEvent;
p4Server.ErrorReceived += P4ServerErrorReceived;
bool syncSuccess=false;
try
{
P4Command syncCommand = new P4Command(p4Server, "sync", true, syncPath + "\\...");
P4CommandResult rslt = syncCommand.Run();
syncSuccess=true;
//Here you can read the content of the P4CommandResult
//But it will only be accessible when the command is finished.
}
catch (P4Exception ex) //Will be caught only when the command has failed
{
Console.WriteLine("P4Command failed: " + ex.Message);
}
And the method to handle the error messages or the taggedOutput:
private void P4ServerErrorReceived(uint cmdId, int severity, int errorNumber, string data)
{
Console.WriteLine("P4ServerErrorReceived:" + data);
}
private void P4ServerTaggedOutputEvent(uint cmdId, int ObjId, TaggedObject Obj)
{
Console.WriteLine("P4ServerTaggedOutputEvent:" + Obj["clientFile"]); //Write the synced file name.
//Note that I used this only for a 'Sync' command, for other commands, I guess there might not be any Obj["clientFile"], so you should check for that.
}

Displaying C# console app output in real-time on a website using PHP [duplicate]

I am trying to run a process on a web page that will return its output in realtime. For example if I run 'ping' process it should update my page every time it returns a new line (right now, when I use exec(command, output) I am forced to use -c option and wait until process finishes to see the output on my web page). Is it possible to do this in php?
I am also wondering what is a correct way to kill this kind of process when someone is leaving the page. In case of 'ping' process I am still able to see the process running in the system monitor (what makes sense).
This worked for me:
$cmd = "ping 127.0.0.1";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("pipe", "w") // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
echo "</pre>";
This is a nice way to show real time output of your shell commands:
<?php
header("Content-type: text/plain");
// tell php to automatically flush after every output
// including lines of output produced by shell commands
disable_ob();
$command = 'rsync -avz /your/directory1 /your/directory2';
system($command);
You will need this function to prevent output buffering:
function disable_ob() {
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);
// Implicitly flush the buffer(s)
ini_set('implicit_flush', true);
ob_implicit_flush(true);
// Clear, and turn off output buffering
while (ob_get_level() > 0) {
// Get the curent level
$level = ob_get_level();
// End the buffering
ob_end_clean();
// If the current level has not changed, abort
if (ob_get_level() == $level) break;
}
// Disable apache output buffering/compression
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}
}
It doesn't work on every server I have tried it on though, I wish I could offer advice on what to look for in your php configuration to determine whether or not you should pull your hair out trying to get this type of behavior to work on your server! Anyone else know?
Here's a dummy example in plain PHP:
<?php
header("Content-type: text/plain");
disable_ob();
for($i=0;$i<10;$i++)
{
echo $i . "\n";
usleep(300000);
}
I hope this helps others who have googled their way here.
Checked all answers, nothing works...
Found solution Here
It works on windows (i think this answer is helpful for users searching over there)
<?php
$a = popen('ping www.google.com', 'r');
while($b = fgets($a, 2048)) {
echo $b."<br>\n";
ob_flush();flush();
}
pclose($a);
?>
A better solution to this old problem using modern HTML5 Server Side Events is described here:
http://www.w3schools.com/html/html5_serversentevents.asp
Example:
http://sink.agiletoolkit.org/realtime/console
Code: https://github.com/atk4/sink/blob/master/admin/page/realtime/console.php#L40
(Implemented as a module in Agile Toolkit framework)
For command-line usage:
function execute($cmd) {
$proc = proc_open($cmd, [['pipe','r'],['pipe','w'],['pipe','w']], $pipes);
while(($line = fgets($pipes[1])) !== false) {
fwrite(STDOUT,$line);
}
while(($line = fgets($pipes[2])) !== false) {
fwrite(STDERR,$line);
}
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
return proc_close($proc);
}
If you're trying to run a file, you may need to give it execute permissions first:
chmod('/path/to/script',0755);
try this (tested on Windows machine + wamp server)
header('Content-Encoding: none;');
set_time_limit(0);
$handle = popen("<<< Your Shell Command >>>", "r");
if (ob_get_level() == 0)
ob_start();
while(!feof($handle)) {
$buffer = fgets($handle);
$buffer = trim(htmlspecialchars($buffer));
echo $buffer . "<br />";
echo str_pad('', 4096);
ob_flush();
flush();
sleep(1);
}
pclose($handle);
ob_end_flush();
I've tried various PHP execution commands on Windows and found that they differ quite a lot.
Don't work for streaming: shell_exec, exec, passthru
Kind of works: proc_open, popen -- "kind of" because you cannot pass arguments to your command (i.e. wont' work with my.exe --something, will work with _my_something.bat).
The best (easiest) approach is:
You must make sure your exe is flushing commands (see printf flushing problem). Without this you will most likely receive batches of about 4096 bytes of text whatever you do.
If you can, use header('Content-Type: text/event-stream'); (instead of header('Content-Type: text/plain; charset=...');). This will not work in all browsers/clients though! Streaming will work without this, but at least first lines will be buffered by the browser.
You also might want to disable cache header('Cache-Control: no-cache');.
Turn off output buffering (either in php.ini or with ini_set('output_buffering', 'off');). This might also have to be done in Apache/Nginx/whatever server you use in front.
Turn of compression (either in php.ini or with ini_set('zlib.output_compression', false);). This might also have to be done in Apache/Nginx/whatever server you use in front.
So in your C++ program you do something like (again, for other solutions see printf flushing problem):
Logger::log(...) {
printf (text);
fflush(stdout);
}
In PHP you do something like:
function setupStreaming() {
// Turn off output buffering
ini_set('output_buffering', 'off');
// Turn off PHP output compression
ini_set('zlib.output_compression', false);
// Disable Apache output buffering/compression
if (function_exists('apache_setenv')) {
apache_setenv('no-gzip', '1');
apache_setenv('dont-vary', '1');
}
}
function runStreamingCommand($cmd){
echo "\nrunning $cmd\n";
system($cmd);
}
...
setupStreaming();
runStreamingCommand($cmd);
First check whether flush() works for you. If it does, good, if it doesn't it probably means the web server is buffering for some reason, for example mod_gzip is enabled.
For something like ping, the easiest technique is to loop within PHP, running "ping -c 1" multiple times, and calling flush() after each output. Assuming PHP is configured to abort when the HTTP connection is closed by the user (which is usually the default, or you can call ignore_user_abort(false) to make sure), then you don't need to worry about run-away ping processes either.
If it's really necessary that you only run the child process once and display its output continuously, that may be more difficult -- you'd probably have to run it in the background, redirect output to a stream, and then have PHP echo that stream back to the user, interspersed with regular flush() calls.
If you're looking to run system commands via PHP look into, the exec documentation.
I wouldn't recommend doing this on a high traffic site though, forking a process for each request is quite a hefty process. Some programs provide the option of writing their process id to a file such that you could check for, and terminate the process at will, but for commands like ping, I'm not sure that's possible, check the man pages.
You may be better served by simply opening a socket on the port you expect to be listening (IE: port 80 for HTTP) on the remote host, that way you know everything is going well in userland, as well as on the network.
If you're attempting to output binary data look into php's header function, and ensure you set the proper content-type, and content-disposition. Review the documentation, for more information on using/disabling the output buffer.
Try changing the php.ini file set "output_buffering = Off". You should be able to get the real time output on the page
Use system command instead of exec.. system command will flush the output
why not just pipe the output into a log file and then use that file to return content to the client. not quite real time but perhaps good enough?
I had the same problem only could do it using Symfony Process Components ( https://symfony.com/doc/current/components/process.html )
Quick example:
<?php
use Symfony\Component\Process\Process;
$process = new Process(['ls', '-lsa']);
$process->run(function ($type, $buffer) {
if (Process::ERR === $type) {
echo 'ERR > '.$buffer;
} else {
echo 'OUT > '.$buffer;
}
});
?>

SSH Connection via C#

This is the situation. I have a Windows machine and a Linux machine. There is a shared drive between these two machines (which is mapped to Q:). I am trying to figure out how to create an SSH session at the Q: drive (shared drive) from C#. I am trying to use the SharpSsh library to do this.
This is what I have so far, however, it is giving me an error:
try
{
ssh = new SshStream(host, username, password);
Console.WriteLine("OK ({0}/{1})", ssh.Cipher, ssh.Mac);
Console.WriteLine("Server version={0}, Client version={1}", ssh.ServerVersion, ssh.ClientVersion);
Console.WriteLine("-Use the 'exit' command to disconnect.");
Console.WriteLine();
//Sets the end of response character
ssh.Prompt = "#";
//Remove terminal emulation characters
ssh.RemoveTerminalEmulationCharacters = true;
//Reads the initial response from the SSH stream
Console.Write(ssh.ReadResponse()); // Blocking here
while (true)
{
string command = Console.ReadLine();
if (command.ToLower().Equals("exit"))
break;
//Write command to the SSH stream
ssh.Write(command);
//Read response from the SSH stream
Console.Write(ssh.ReadResponse());
}
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
if(ssh != null)
{
ssh.Close();
}
I have added the Tamir.SharpSSH.dll as a reference to the project, and I am using it in the project. There are two other dll's that are included with SharpSSH, do I need to add them as well to the references? The examples I have seen only have the Tamir.SharpSSH.dll as a reference.
I am not sure how to initiate the connection in the correct location, and how to submit commands properly to the ssh.
UPDATE
I realized I needed to close the SSH connection before ending the program. The error does not exist anymore, however, I am still not getting any information from my "ls" command.
UPDATE
I updated the code with what I have now. It seems like the ssh.ReadResponse() is blocking, which leads me to believe the server is not responding. Is that correct?

Configuring client and server using OSC in Unity

I'm trying to utilize a library for Unity called UnityOSC, which allows you to receive OSC messages, presumably, from other applications. I have an application which transmits data via UDP at the following address:
host: 127.0.0.1
port: 33433
I now need to configure UnityOSC to listen for that data. The library has a script called OSCHandler.cs, and in the init function, you can set up your client and server. I'm just not sure how to set this up.
Currently, I'm attempting this:
public void Init()
{
CreateClient("FaceClient", IPAddress.Parse("127.0.0.1"), 33433);
CreateServer("FaceServer", 6666);
}
I've matched the client parameters to those of the application transmiting the data, and the server is just random - but honestly I'm not sure what to put in either one of these. Theoretically, if I set the client / server up properly, I should be able to register information in my update function like this:
void Update() {
OSCHandler.Instance.UpdateLogs();
//clients = OSCHandler.Instance.Clients;
servers = OSCHandler.Instance.Servers;
foreach(KeyValuePair<string, ServerLog> item in servers)
{
// If we have received at least one packet,
// show the last received from the log in the Debug console
if(item.Value.log.Count > 0)
{
int lastPacketIndex = item.Value.packets.Count - 1;
UnityEngine.Debug.Log(String.Format("SERVER: {0} ADDRESS: {1} VALUE 0: {2}",
item.Key, // Server name
item.Value.packets[lastPacketIndex].Address, // OSC address
item.Value.packets[lastPacketIndex].Data[0].ToString())); //First data value
}
}
}
But so far nothing is registering to the debugger. Any idea what I'm doing wrong?
Two things, keeping in mind I did this from the command line and not in the Unity editor so things might be different.
First, line 50 in OSCPacket.cs throws an exception in the command line version. Not sure if Unity picks up Trace.Assert or not but nothing is hurt by removing the line:
Trace.Assert(string.IsNullOrEmpty(_address) == false);
I'm guessing the UnityOSC author meant to do something like:
Trace.Assert(string.IsNullOrEmpty(value) == false);
Otherwise I'm not sure how that assertion would pass since OSCMessage, a derived class of OSCPacket, directly calls the Address property in its ctor.
Secondly, if you're trying to test this on a local machine, your ports need to match:
public void Init()
{
CreateServer("Server", 5555);
CreateClient("Client", IPAddress.Parse("127.0.0.1"), 5555);
}
Lastly, my manual creation:
OSCHandler handler = new OSCHandler();
handler.Init();
handler.SendMessageToClient<string>("Client", "127.0.0.1", "TestMessage");
handler.UpdateLogs();
I had to slightly modify the authors code to get it to work outside of Unity but only slightly.
I went a different route to get this working in Unity - based off of an older project called openTSPS from James George. You can download it here - it accepts all OSC data:
https://github.com/heaversm/UnityOSC_FS

Problem with WebSockets Draft

I'm having some trobule with Web Sockets and Microsoft's Draft implementation. I'm using the API provided by them for the server back and as well as the Silverlight fix for browsers that don't natively support Web Sockets. The information i'm working off of comes from http://channel9.msdn.com/Events/MIX/MIX11/HTM10 and http://html5labs.interoperabilitybridges.com/prototypes/websockets/websockets/info
My code compiles okay, it starts to open the connection, then fails. Here's my backend server code (in a C# Console App)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ServiceModel.WebSockets;
namespace ChatBackend
{
class Program
{
static void Main(string[] args)
{
var host = new WebSocketsHost<MessageHandler>();
host.AddWebSocketsEndpoint("ws://localhost:4502/MessageHandler");
host.Open();
Console.ReadLine();
}
}
}
Here's the MessageHandler class:
class MessageHandler : WebSocketsService
{
public override void OnMessage(string value)
{
string returnMessage = "You just said '" + value + "'";
Console.WriteLine(value);
SendMessage(returnMessage);
}
}
This backend part seems to work fine. Here's the client end:
$(document).ready(function () {
setTimeout(connect, 2050);
});
function connect() {
websocket = new WebSocketDraft("ws://localhost:4502/MessageHandler");
websocket.onopen = function() {
websocket.send("test");
};
websocket.onclose = function () {
alert("DIED");
};
websocket.onmessage = function(event) {
sendMessage(new message(0, event.data, 0));
};
}
EDIT: I checked my firewall and allowed the port i was using. Now the connection just hangs - it doesn't close, it doesn't open. Any ideas?
EDIT 2: I did some more research, turns out i need to remove the clientaccesspolicy.xml. Also turns out i never had it, but i WAS returning a custom 302 redirect instead of a 404 not found. Fixed that, but no progress. It just hangs.
EDIT 3: Tried turning off Anti-Virus and Firewall, no change
Thanks - let me know if i need anymore code or information up here.
Can you use Firebug or something and put a breakpoint on this line in jquery.slws.js (assuming its the one you're using!)
this.slws = slCtl.Content.services.createObject("websocket");
And then step through and see where it fails ?
Your URL definitely looks like it matches the setting on your server side. It's really hard to find any examples of WebSockets actually being used since it's still such a new technology, but from what I could follow of the only working example I could find (it was on the html5labs site), I think maybe you're trying to write to the socket before it's finished opening. In the example I found, it looked like they check the readyState property of the socket, and only try to send if readyState === 1. The socket in that example gets opened by tying into the $(document).ready event, and send gets called from a button click.
Finally got this working! Turns out i had to add clientaccesspolicy.xml to the root folder of the default website on my server. I thought i had to delete it - i misread some information and promptly forgot about it. Moral of the story: read the readme!

Categories