vi / websocat

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

mixed text/binary client hex representation #182

Closed alien999999999 closed 1 year ago

alien999999999 commented 1 year ago

I'm using rlwrap websocat --binary-prefix "B:" -k --protocol default wss://domain.tld to connect to a lws (libwebsocket) server, and I have a web javascript client, which can send and receive text and binary; and it seems there is a flag somewhere to say if it's binary or not.

I have commands which are text based and would return binary data; this seems not to work, (or i am doing something wrong).

Also, it seems the binary data has to be sent as binary (or base64), it would be great if there's a hex representation too? where binary would be converted to hexdump like output (2 hex digits and space), same thing with inputting binary, accepting hex?

failing that, is there a way to hook input and output conversion functions where the binary in/out conversion is handled by somthing of my choice? (could be bash scripts)?

vi commented 1 year ago

You should probably use --base64 together with --binary-prefix. That would enable you to both send and receive binary messages in otherwise text stream. If you type a line starting with B:, it is interpreted as base64 to be decoded and sent as a binary WebSocket message. If incoming WebSocket message is binary then it would be base64-encoded and B: prepended.

a hex representation too

Currently it is not implemented.

is there a way to hook input and output conversion functions where the binary in/out conversion is handled by somthing of my choice?

You can, for example, use base64 everywhere (i.e. --base64 --base64-text --binary-prefix='B:' --text-prefix='T:') and use custom script that would re-encode things to/from base64 from/to hex.

-k ... wss://domain.tld

Usage of --insecure with real domains (not just internal or localhost ports) is not recommended outside of some quick tests. If you use self-signed certificates, you can specify certificate of your CA as SSL_CERT_FILE environment variable.

alien999999999 commented 1 year ago

You should probably use --base64 together with --binary-prefix. That would enable you to both send and receive binary messages in otherwise text stream. If you type a line starting with B:, it is interpreted as base64 to be decoded and sent as a binary WebSocket message. If incoming WebSocket message is binary then it would be base64-encoded and B: prepended.

atm, I've made a simple python script that popen's this one, and i'm doing the base64 with binary prefix in order to handle this, and it starts to work well, and I noticed that my text mode commands fail, and now i understand why;

It seems that the newline is also passed on, eg: i'm trying a websocket server command device list which is 11 bytes long, but 12 bytes is sent (i'm assuming the newline is also sent), which fails with the command comparison functions in the websocket server. is there a way to not send the newline from the input to the websocket?

a hex representation too

Currently it is not implemented.

is there a way to hook input and output conversion functions where the binary in/out conversion is handled by somthing of my choice?

You can, for example, use base64 everywhere (i.e. --base64 --base64-text --binary-prefix='B:' --text-prefix='T:') and use custom script that would re-encode things to/from base64 from/to hex.

-k ... wss://domain.tld

Usage of --insecure with real domains (not just internal or localhost ports) is not recommended outside of some quick tests. If you use self-signed certificates, you can specify certificate of your CA as SSL_CERT_FILE environment variable.

this is for internal use only, and I have a wildcard signed by a cacert (which is selfsigned), but not all of my machines have the cacert CA installed...

vi commented 1 year ago

11 bytes long, but 12 bytes is sent (i'm assuming the newline is also sent)

You may want to use --binary mode, --no-fixups to avoid automatic insertion of overlays and adding msg2line: / line2msg: overlays manually.

vi commented 1 year ago

not send the newline from the input to the websocket

--base64-text is supposed to enable base64 for text messages, so you can control whitespace in them precisely.

alien999999999 commented 1 year ago

i ended up using --linemode-strip-newlines in the --text mode with --base64 and --binary-prefix B: , that seemed to work