I'm having a very hard time sending commands to an interactive bash shell using a node.js websockets client. I'm running a cluster with kops and I'm able to establish the connection with a pod and get its prompt but I am unable to send commands and receive a response.
So my connection looks like:
const WebSocket = require('ws');
const fs = require('fs');
const readline = require('readline');
const unescape = require('querystring').unescape;
const escape = require('querystring').escape;
const channel = '0';
const access_token = "[the_api_token]";
const pod_name = 'nginx-726417742-s1nv2';
const host_address = 'wss://[the_api_url]';
const cmd = `${escape('/bin/bash')}&command=-i`;
const params = `stdout=1&stdin=1&stderr=1&tty=1&container=nginx&command=${cmd}`;
const url = `${host_address}/api/v1/namespaces/default/pods/${pod_name}/exec?${params}`;
const options = {
headers: {
'Authorization': `Bearer ${access_token}`
},
ca: fs.readFileSync('ca.crt'),
}
const ws = new WebSocket(url, options);
ws.on('open', () => {
console.log('connected');
});
ws.on('message', (data, flags) => {
process.stdout.write(data);
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', function (cmd) {
const data = channel + cmd;
if (ws && ws.readyState === 1) {
ws.send(data);
}
});
The ws.on('message', (data, flags) part prints the shell prompt perfectly root#nginx-726417742-s1nv2:/# but then I can type whatever and it goes trough ws.send(data) but no message is ever received and no error is generated. I have already tried to convert the data to base64 and send char by char instead of the whole line, but the result and behaviour is always the same.
If I proxy the API through kubectl proxy --disable-filter=true --port=8080 and use https://github.com/kubernetes-ui/container-terminal pointing to ws://localhost:8080 and using /api/v1/namespaces/default/pods/${pod_name}/exec I can get a working terminal, but local proxying is not an option for me.
Any help will be much appreciated.
I know that my answer comes late. But maybe I can help someone else with this. I have noticed that often the Kubernetes API requires to set an origin when using websockets. Otherwise the API returns strange errors sometimes. In your case you could try to complete your options as follows:
const options = {
headers: {
'Authorization': `Bearer ${access_token}`
},
ca: fs.readFileSync('ca.crt'),
origin: 'https://<the_api_url>:<port>'
}
Furthermore I think that it is not required to set wss as protocol (in your host_address variable). Usually the protocol should be automatically upgraded from https to wss during the connection process.
Related
I am trying to initiate a websocket request using nodeJS. I am new to WS, and I am quite stuck with this, and all examples I could find seem to be the same one with building a chat, repeated over and over in every media lol, and it didn't help me better understand.
I have an API that provides real-time data (forex). I am able to successfully subscribe to sources of real-time data on the front-end, and do what I want. BTW, I don't get any CORS issue doing that, for some reason I don't understand.... :-o
However, i'd like to get this real-time data through my server, so my API key doesn't appear clearly.
So, I want to initiate a request to my server; this request open a channel to the supplier of forex data's API. Then I open a channel to receive this real-time data from my server.
I have thought of the following, but that doesn't work.
var enableWs = require('express-ws');
enableWs(app);
const WebSocket = require('ws');
const WebSocketServer = require('ws').Server;
const URL = 'wss://API_ENDPOINT?api_key=MY_KEY';
app.ws('/ws', function (ws, req) {
const wss = new WebSocket(URL);
let msg = { action: 'subscribe', symbol : 'EUR-USD'};
wss.onopen = (event) => {
wss.send(JSON.stringify(msg));
};
wss.onmessage = function (event) {
jsonData = JSON.parse(event.data);
// now, i want to send the RT data received in jsonData to another channel so I can display the data on the FE
const wssReact = new WebSocketServer({ port: 7100 });
wssReact.send(jsonData);
}
wss.onclose = function (event) {
wss.terminate();
};
});
On the front-end, I expected to get a result when opening a socket as follows :
const socket = new WebSocket('ws://localhost:7100');
ok, I figured how to do what i wanted to achieve. I was close :-) And now it seems obvious lol.
My incoming WS data from the data provider is inconsistent, that's one reason why I couldn't figure the solution earlier.
app.ws('/ws', function (ws, req) {
const wss = new WebSocket(URL);
let msg = { action: 'subscribe', symbol : 'EUR-USD'};
wss.onopen = (event) => {
wss.send(JSON.stringify(msg));
};
wss.onmessage = function (event) {
ws.send(event.data);
}
wss.onclose = function (event) {
wss.terminate();
};
});
I hope it can be of any help to someone else :)
I have a Koajs node app in a docker container on an EC2 instance. The app is behind an AWS Application Load Balancer.
The app simply takes a POSTed file and responds with a stream that the client can view events on.
So my server is doing the right thing (sending file data), and my client is doing the right thing (receiving file data and sending back progress), but the ALB is timing out. I don't understand why it's timing out. Both client and server are sending and receiving data to/from each other, so I would think that would qualify as keep alive traffic.
Here's the code that each is running.
Client:
const request = require('request-promise');
const fs = require('fs');
const filePath = './1Gfile.txt';
const file = fs.createReadStream(filePath);
(async () => {
// PUT File
request.put({
uri: `http://server/test`,
formData: { file },
headers: { Connection: 'keep-alive' },
timeout: 200000,
})
.on('data', (data) => {
const progressString = data.toString();
console.log({ progressString });
});
})();
Server:
const { Readable } = require('stream');
const Koa = require('koa');
const router = require('koa-router')();
(async () => {
const app = module.exports = new Koa();
router.get('/healthcheck', async (ctx) => {
ctx.status = 200;
});
router.put('/test', test);
async function test(ctx) {
const read = new Readable({
objectMode: true,
read() { },
});
ctx.body = read;
let i = 1;
setInterval(() => {
read.push(`${process.hrtime()}, ${i}`);
ctx.res.write('a');
i++;
}, 3000);
}
app.use(router.routes());
app.use(router.allowedMethods());
app.listen(3000, (err) => {
if (err) throw err;
console.info(`App started on port 3000 with environment localhost`);
});
})();
Both server and client are logging the correct things, but the ALB just times out at whatever I set it's idle timeout to. Is there some trick to tell the ALB that traffic is really flowing?
Thanks so much for any light you can shed on it.
Just a quick guess, you need to enable keepAlive when using the request-promise. add forever: true in options. Try this:
request.put({
uri: `http://server/test`,
formData: { file },
headers: { Connection: 'keep-alive' },
timeout: 200000,
forever: true,
})
We have a similar issue about timeout when using request-promise-native. We fixed by adding this option. Hopfully it works out for you.
I want to close an issue matching the file name pushed with Issue title (My source files are named with unique integers, e.g. 34521.cpp and there are corresponding issues on Gitlab e.g. Problem #34521).
How can I do so?
The default pattern is not suitable as I have 2000+ issues and I do not want to refer issues with the issue ID's each time. I want it to be automated. So I was checking the page :
Change the issue closing pattern.
It says I need to have access to the server where gitlab is installed. Does that mean I cannot change the issue closing pattern for Gitlab cloud's user account hosted at http://gitlab.com ?
You can't define a custom closing pattern on gitlab.com, only on your own hosted gitlab instance. But what you can do is to use webhooks to listen on push events on a remote server. You can then parse the commit messages yourself and take decision on closing issues. You can use Gitlab API to close issue on your server instance (with a hard coded access token)
This can be tested locally using an http tunnel like ngrok
The following nodejs script starts a server serving a /webhook endpoint. This webhook endpoint is called when any push occurs on your repo.
const express = require('express');
const bodyParser = require('body-parser');
const axios = require('axios');
const to = require('await-to-js').to;
const port = 3000;
const projectId = "4316159";
const accessToken = "YOUR_ACCESS_TOKEN";
const app = express();
app.use(bodyParser.json())
app.post('/webhook', async function(req, res) {
console.log("received push event");
let result, err, closeRes;
for (var i = 0; i < req.body.commits.length; i++) {
for (var j = 0; j < req.body.commits[i].added.length; j++) {
filenameWithoutExt = req.body.commits[i].added[j].split('.').slice(0, -1).join('.');
[err, result] = await to(axios({
url: `https://gitlab.com/api/v4/projects/${projectId}/issues?search=#${filenameWithoutExt}`,
method: 'GET',
headers: {
"PRIVATE-TOKEN": accessToken
}
}));
if (err) {
console.log(err);
} else {
if (result.data.length !== 0) {
//close those issues
for (item in result.data) {
console.log(`closing issue #${result.data[item].iid} with title ${result.data[item].title}`);
[err, closeRes] = await to(axios({
url: `https://gitlab.com/api/v4/projects/${projectId}/issues/${result.data[item].iid}?state_event=close`,
method: 'PUT',
headers: {
"PRIVATE-TOKEN": accessToken
}
}));
if (err) {
console.log(err);
} else {
console.log(`closing status : ${closeRes.status}`);
}
}
} else {
console.log("no issue were found");
}
}
}
}
res.sendStatus(200);
});
app.listen(port, () => console.log(`listening on port ${port}!`))
In the above you need to change the access token value & projectId. Also note that above code will check only added file, you can modify it to include updated or deleted file matching your requirements.
Launch ngrok on port 3000 ngrok http 3000 & copy the given url in integrations sections of your repo :
Now when you add any file it will check for the filename without extension and search all issue with within title #filename_without_extension and close it right away
I am tring to write a simple bot that after some task reply with a text.
The bot offline with app.polling is working perfectly. But when I deploy it on heroku, if I write only one ctx.reply(), no message is sent. If I write it twice, one message is sent on telegram. You can see the code on the snippet below, I included only the necessary code (the log show me with console.log that all the function are working and the final text is ready to be sent, I also commented the code a little to exlpain better the situation).
So this appear strange to me,can anyone explain why?
const Telegraf = require('telegraf');
const request = require('request');
const date = require('date-and-time');
const API_TOKEN = process.env.API_TOKEN || ''; //the api token is into env var
const PORT = process.env.PORT || 3000;
const URL = process.env.URL || 'url of my heroku account';
const app = new Telegraf(API_TOKEN);
app.telegram.setWebhook(`${URL}/bot${API_TOKEN}`);
app.startWebhook(`/bot${API_TOKEN}`, null, PORT);
function getdata(ctx,stazione1,stazione2){
let tempo = gettempo();
let linkget = '....';
var options = {
url: linkget,
headers: {
'Referer':'http://m.trenord.it/site-lite/index.html',
'secret': '...'
}
};
let linkget1 = '...';
var options1 = {
url: linkget1,
headers: {
'Referer':'...',
'secret': '...'
}
};
request(options, function(error, response, body){
request(options1, async function(error1, response1, body1){
let text = await gettext(body,stazione1,stazione2);//return text
let text1 = await gettext(body1,stazione2,stazione1);//return text
let final = await text+"\r\n\r\n"+text1;
console.log(ctx);
//here is the problem, if i write only one reply no message is sent on the bot, but if i wrtite it twice one message is sent.
ctx.replyWithMarkdown(final);
ctx.replyWithMarkdown(final);
});
});
}//getdata
app.command('pl', function(ctx){
getdata(ctx,stazione1,stazione2);
});
NEWS
I want to add some feedback while the server works, so I add a ctx.reply("searching...") after the command right before the function getdata is launched. Now all two messages are sent to telegram. On the previous case is possible that heroku "shut down the webhook" and at the first ctx.reply was wake up and than at the second ctx.reply the message was sent?
I'm using node-telegram-bot-api for node.js/Heroku and this config work well for me:
const options = {
webHook: {
// Port to which you should bind is assigned to $PORT variable
// See: https://devcenter.heroku.com/articles/dynos#local-environment-variables
port: process.env.PORT,
// you do NOT need to set up certificates since Heroku provides
// the SSL certs already (https://<app-name>.herokuapp.com)
// Also no need to pass IP because on Heroku you need to bind to 0.0.0.0
},
};
// okk
// Heroku routes from port :443 to $PORT
// Add URL of your app to env variable or enable Dyno Metadata
// to get this automatically
// See: https://devcenter.heroku.com/articles/dyno-metadata
const url = process.env.APP_URL || 'https://my-bot.herokuapp.com:443';
const bot = new Tg(config.BOT_TOKEN, options);
// This informs the Telegram servers of the new webhook.
// Note: we do not need to pass in the cert, as it already provided
bot.setWebHook(`${url}/bot${config.BOT_TOKEN}`);
Regular client initiated requests to the node server are captured fine in Fiddler. However, requests sent from node to a web service are not captured. It did not help to pass in config for proxy (127.0.0.1:8888) to the request method. How can I route the request messages through Fiddler?
var http = require('http');
var request = require('request');
request.get(webserviceURL, { "auth" : {"user": "user", "pass" = "pass", sendImmediately: true },
"proxy" : { "host" : "127.0.0.1", "port" : 8888 }},
function (error, response) { console.log( "response received" );
});
Request repo: https://github.com/mikeal/request
I just tried to do this myself (using Fiddler and the request library from npm). Here's how I got it working:
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'; // Ignore 'UNABLE_TO_VERIFY_LEAF_SIGNATURE' authorization error
// Issue the request
request(
{
method: "GET",
uri: "https://secure.somewebsite.com/",
proxy: "http://127.0.0.1:8888" // Note the fully-qualified path to Fiddler proxy. No "https" is required, even for https connections to outside.
},
function(err, response, body) {
console.log("done");
});
This is with Fiddler2 using the default port and proxy options (and no proxy authentication).
Fiddler works by setting your "Internet Options" (from start menu) "Connections" > "LAN Settings" > "Proxy Server" to its port, thus making all HTTP traffic (clients which obey this setting) go through it.
You should point your node.js client lib to use a proxy, the settings are written in that options dialog after you start Fiddler.
The proxy option should be a full url, like this:
proxy : "http://127.0.0.1:8888"
To do this on an ad-hoc basis, without changing your code, you can use environment variables.
Request respects:
HTTP_PROXY
HTTPS_PROXY
NO_PROXY
So, to proxy just set these in your console before running your process.
For example, to setup http and https proxy use:
set HTTP_PROXY="http://127.0.0.1:8888"
set HTTPS_PROXY="http://127.0.0.1:8888"
set NODE_TLS_REJECT_UNAUTHORIZED=0
The latter line stops issues with SSL through the fiddler proxy.
I've been wanting the same... an equivalent of the Network tab in chrome DevTools, only for Nodejs. Unfortunately, it doesn't appear as though one exists. I don't have Fiddler on macos, so this is how I went about stubbing the require('http') methods to log and pass though. Leaving this here in case I need it again or someone else finds it helpful. You can turn it on by attaching a debugger and require('filename')() the file containing this script.
module.exports = () => {
const http = require('http');
http._request = http.request;
global.DO_LOG_AJAX = true;
const log = str => {
if (global.DO_LOG_AJAX) {
console.debug(str);
}
};
const flushLog = (requestLines, responseLines) => {
if (global.DO_LOG_AJAX) {
log([
'----------------Begin Request-----------------------------------',
...requestLines,
'----------------End Request / Begin Response--------------------',
...responseLines,
'----------------End Reponse-------------------------------------',
].join('\n'));
}
};
let write;
let end;
http.request = (...requestParams) => {
const req = http._request(...requestParams);
const { method, path, headers, host, port } = requestParams[0];
const requestLogLines = [];
requestLogLines.push(`${method} ${path}`);
requestLogLines.push(`Host: ${host}:${port}`);
for (const header of Object.keys(headers)) {
requestLogLines.push(`${header}: ${headers[header]}`);
}
write = write || req.write;
end = end || req.end;
req.on('error', err => {
log({ err });
});
req._write = write;
req._end = end;
const requestBody = [];
req.write = (...writeParams) => {
requestBody.push(writeParams[0].toString());
return req._write(...writeParams);
};
req.end = (...endParams) => {
if (endParams[0]) {
requestBody.push(endParams[0].toString());
}
requestLogLines.push('');
requestLogLines.push(requestBody.join(''));
return req._end(...endParams);
};
const responseLogLines = [];
req.once('response', response => {
const responseBody = [];
responseLogLines.push(`${response.statusCode} ${response.statusMessage}`);
for (const header of Object.keys(response.headers)) {
responseLogLines.push(`${header}: ${response.headers[header]}`);
}
const onData = chunk => {
responseBody.push(chunk.toString());
};
const onClose = err => {
responseLogLines.push('');
responseLogLines.push(responseBody.join(''));
responseLogLines.push('');
responseLogLines.push(`--- ERROR --- ${err.toString()}`);
flushLog(requestLogLines, responseLogLines);
req.removeListener('data', onData);
};
const onEnd = () => {
responseLogLines.push('');
responseLogLines.push(responseBody.join(''));
flushLog(requestLogLines, responseLogLines);
req.removeListener('data', onData);
};
response.on('data', onData);
response.once('close', onClose);
response.once('end', onEnd);
});
return req;
};
};