vi / websocat

Command-line client for WebSockets, like netcat (or curl) for ws:// with advanced socat-like functions
MIT License
7.17k stars 278 forks source link

illegal formated jsonrpc ? #133

Open DiSToAGe opened 3 years ago

DiSToAGe commented 3 years ago

It seems there is an illegal formatting of data through jsonrpc: (the test is made through output of debug of chrome browser via websocket as in the doc example)

  1. launch chrome in debug (google-chrome-stable --remote-debugging-port=9222)
  2. get browser version parsed via jsonrpc echo '{"method":"Browser.getVersion","params":{}, "id":1}' | websocat -n1 --text - jsonrpc:ws://127.0.0.1:9222/devtools/page/<youridofwebsocket> and you will get {"jsonrpc":"2.0","id":1, "method":"{"id":1,"result":... !! on value of method you have "{"id"... which seems illegal json, you must have "method":{"id"... without double quote before { (if you parse data through "jq" command (...| jq '.'), it gets errors

another problem, UserAgent value is split after Mozilla value, the rest is in the "params".

other problem, on the params there is an Array with no open double quote, but ending double quote.

On the end there is too inverted "]" and "}"

see complete line : {"jsonrpc":"2.0","id":1, "method":"{"id":1,"result":{"protocolVersion":"1.3","product":"Chrome/92.0.4515.159","revision":"@0185b8a19c88c5dfd3e6c0da6686d799e9bc3b52","userAgent":"Mozilla/5.0", "params":[(X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36","jsVersion":"9.2.230.29"}}]}

if you get data without jsonrpc:, so via echo '{"method":"Browser.getVersion","params":{}, "id":1}' | websocat -n1 --text - ws://127.0.0.1:9222/devtools/page/<youridofwebsocket>

the data is correctly formatted as {"id":1,"result":{"protocolVersion":"1.3","product":"Chrome/92.0.4515.159","revision":"@0185b8a19c88c5dfd3e6c0da6686d799e9bc3b52","userAgent":"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36","jsVersion":"9.2.230.29"}} and parsing it through...| jq '.' is correct.

If you get other chrome debug command, you get same errors of formatting via jsonrpc: (added double quote, wrong of array ending/beggin ...)

DiSToAGe commented 3 years ago

ps: nice software :-)

vi commented 3 years ago

Why do you both specify jsonrpc: and type the JSON manually?

It is supposed to be used like this:

echo Browser.getVersion '{}' | websocat -n1 --text jsonrpc:- ws://127.0.0.1:9222/devtools/page/...

It would convert Browser.getVersion {} into {"jsonrpc":"2.0","id":1, "method":"Browser.getVersion", "params":{}}

jsonrpc: overlay (or --jsonrpc option) is not required for doing JSON RPC over Websocat, it's just a helper to type JSON RPC requests interactively. The overlay is rather simplistic and does not use any actual JSON library. Recommended command line for this is:

rlwrap websocat --jsonrpc ws://127.0.0.1:9222/devtools/page/...

This way you would get line editing and history and also avoid typing a lot of {"":""}-s.