Last active
March 29, 2023 15:04
-
-
Save CalebEverett/bed94582b437ffe88f650819d772b682 to your computer and use it in GitHub Desktop.
Lxd api example: lxc exec and operations websocket via nodejs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const WebSocket = require('ws'); | |
const wsoptions = { | |
cert: fs.readFileSync('../../.config/lxc/client.crt'), | |
key: fs.readFileSync('../../.config/lxc/client.key'), | |
rejectUnauthorized: false | |
} | |
var ws = new WebSocket('wss://127.0.0.1:8443/1.0/events?type=operation', wsoptions); | |
ws.on('open', function open() { | |
console.log('open for notifications') | |
}); | |
ws.on('error', (error) => { | |
console.log(error) | |
}); | |
ws.on('message', function(data, flags) { | |
var buf = Buffer.from(data) | |
console.log(JSON.stringify(JSON.parse(data.toString()), null, 4)) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require('https'); | |
const fs = require('fs'); | |
const WebSocket = require('ws'); | |
const options = { | |
method: 'POST', | |
host: '127.0.0.1', | |
port: 8443, | |
path: '/1.0/containers/lxd-websock/exec', | |
cert: fs.readFileSync('../../.config/lxc/client.crt'), | |
key: fs.readFileSync('../../.config/lxc/client.key'), | |
rejectUnauthorized: false | |
} | |
const body = JSON.stringify({ | |
"command":["/bin/bash"], | |
"environment":{"MY_KEY":"my_value"}, | |
"interactive":true, | |
"wait-for-websocket":true | |
}) | |
const req = https.request(options, res => { | |
res.on('data', d => { | |
const output = JSON.parse(d); | |
const wsoptions = { | |
cert: options.cert, | |
key: options.key, | |
rejectUnauthorized: false | |
} | |
const ws = new WebSocket('wss://' + | |
options.host + ':' + options.port + output.operation + | |
'/websocket?secret=' + output.metadata.metadata.fds['0'], | |
wsoptions | |
); | |
ws.on('open', () => { | |
console.log('connection opened'); | |
ws.send('printenv \r', { binary: true }, () => { | |
setTimeout(() => ws.send('exit \r', { binary: true }), 100); | |
setTimeout(() => process.exit(0), 200); | |
}); | |
}); | |
ws.on('error', error => console.log(error)); | |
ws.on('message', data => { | |
const buf = Buffer.from(data); | |
console.log(buf.toString()); | |
}); | |
}); | |
}); | |
req.write(body); | |
req.end(); | |
req.on('error', (e) => { | |
console.error(e); | |
}); |
Your the best for this ❤️ I wasted some much time and went through so many web socket libraries ❤️
Hello guys
I want to connect my container LXD to xterm.js for a terminal bash on my website
my IP server LXD is 192.168.1.207:8443
my certificates path /var/www/html/plateforme/client.crt and client.key
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this,
If you're connecting to local LXD with node, you don't need to setup certificates if you use.
require('child_process').exec
and use it to call this little gem:
lxc query -X POST -d '{"command":["/bin/bash"],"environment":{"MY_KEY":"my_value"},"interactive":true,"wait-for-websocket":true}' /1.0/containers/c1/exec
That will the give you the operation id and the secret which is required for the websocket.