How to use Xterm.js to connect to a container

I wish to use Xterm.js to connect to a LXC container, however I am not really understanding the steps to connect this and how to do this via the API. Just looking at it makes my head spin…

Can anybody give me an idea on how I can implement this, what steps i must take etc?

Thanks

somewhere in there I worked it all out

obviously if you use my code please attribute it

Many thanks, the amount of work you have done is quite impressive. I will take a look at it.

I mean I dunno what your trying todo but I assume its executing commands on lots of containers, I built this the other weekend and may even more appropriate to either give some input on or a base for your research (it uses PHP and the exec API (but not xterm.js - its not needed) which i prefer to node everday)

Id be happy to give more specific guidance than look at my code but I dont know what your trying todo you’ll need to get more specific

I am scratching an itch, building a web app to manage linux containers, and simplifies the process.

As for the action series, i already have a queuing library part of my framework, which can handle that quite easily using success or error callbacks. It doesnt have to be complex. Unless you want to diagram it that is.

Anyone got this working?

Simple example I am trying to get to work:
https://pastebin.com/dhHkGwmE

Even looking at the source of the lxd-demo server code, I am still sitting at a blank terminal, despite the websocket connected fine.

Using the latest xterm.js 4.18

You have to use attach addon to attach the websocket to xterm

I tried that one too: ws = new WebSocket(endpoint); const terminal = new Terminal({ - Pastebin.com

Same result, the terminal is not responsive, whatever I type does never give me any result.
Randomly I end up with "Error: Failed running forkconsole: “attaching to the container failed” but this happens on booth versions.

So the Socket is open and LXD can talk to xterm.js however xterm.js is not responsive at all.

Some tips for you:

  • use {binary:true} if your proxying it through a backend
  • make sure you connect to control socket too

I don’t use a reverse proxy but thanks for the tip.
Connecting booth websockets leads to booth failing to connect.

Beforehand, the data socket would connect fine.
The ID’s are correct the and the fd’s, I don’t get why it fails now.

since youre not proxying it, your certs should be passed to the websocket for auth. though the built in websocket client in webpages dont allow that.

Okay, but if the auth is not completed, why does LXD start send stuff like this:
“Error: Failed running forkconsole: “attaching to the container failed”

My brain went like the auth is fine, LXD is talking to the websocket.
I don’t have any other means to debug this since I did got no error message and apparently I forgot the part completely with the certificates.

From the looks of it, LXD seems to sending messages but never reacts or ignores messages send to the websocket endpoint.

Boot sequence and Shutdown sequence all come through however any input send to the websocket is ignored. Is there any specific format that it needs to be?

I see nothing in the documentation that does mention anything related to that.

edit: When sending messages in the shutdown/boot seq. the websocket gets terminated.
Is that normal? Usually it runs through.

addendum:

I used /1.0/operations/{id}/websocket before
After switching to /1.0/operations/{id}/websocket?public it works without issues.
Since the auth runs over the secret.
Thanks @Wolfo-Gaming

However, still the issue ,that LXD seems to ignore the messages that are getting send to the LXD API.
Receiving is no issue, I tried sending just strings to binary formatted data as LXD is sending, however no success.

Anyone got an idea how to build the payload so, LXD does not ignore it? (javascript)

could you show me the code that you are using?

Sure <div id="terminal"></div> <script> control = '<?php echo $ - Pastebin.com
Nothing really changed, besides your suggestions to use the Control Socket too.

What it does for me, is properly closing the Attached Console if the Browser window is closed, happens apparently automatically which is neat. Without it, the Attached old Session stays open.

Im not currently seeing why you should have issues, by the way attachAddon is a way to attach a ws for i/o to xtermjs so adding xtermjs and ws listeners in not needed

This code runs for you fine?
Can you show me what your Google Chrome Dev. Outputs? e.g Messages.

Whatever I try, LXD keeps ignoring it.

edit: @Wolfo-Gaming could you maybe share your “example” snippet?

Most of the code that i use is similar to what lxdmosaic does, i havent run tour code in my browser yet. i can try to make a working example but it will be diffcult since youre connecting straight through the browser wich seems weird to me because of lxd auth with certificates

@stgraber according to swagger, this Endpoint Swagger UI runs the auth over the secret instead/without the certificates. Is this correct?

@Wolfo-Gaming I am going to setup a nginx config as proxy and let it attach the certificates just for a test. Its wired since I still get output. Thanks.