sending http request with proxy ip in node.js - node.js

I am trying to send an http get request from a different ip, such as hardcoding the proxy ip to use.
I have a working http get request in node.js:
const https = require('https')
//get access token
let accessToken = await spotifyAuth.getAccessToken();
//create data
const data = JSON.stringify({
offset: 0,
})
//create options data obj
const options = {
hostname: 'api.spotify.com',
path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',
method: 'GET',
headers: {
'Authorization': 'Bearer ' + accessToken,
},
}
const req = https.request(options, res => {
console.log(`proxyiprequest() statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error('proxyiprequest() err=', error)
})
req.write(data)
req.end()
So I'm trying to add an https proxy ip address/port combo I found from here like so:
//create options data obj
const options = {
host: "220.135.165.38",
port: 8080,
hostname: 'api.spotify.com',
path: '/v1/artists/1XqqyIQYMonHgllb1uysL3/albums',
method: 'GET',
headers: {
'Authorization': 'Bearer ' + accessToken,
},
}
Would this method work? If I run the request it errors out like:
proxyiprequest() err= { Error: write EPROTO 18:error:1B:SSL routines:ssl3_get_record:wrong version number:../ssl/record/ssl3_record.c:331:
at WriteWrap.afterWrite [as oncomplete] (net.js:789:14) errno: 'EPROTO', code: 'EPROTO', syscall: 'write' }

Related

Error: socket hang up - Lambda nodejs function http post request

I have a very simple function in Lambda using Nodejs.
The purpose of that function is to triggered a third party api every 1 minute.
So for that I have setup Cloud Watch Cron based event.
But that Api is throwing this error:
START RequestId: 54d90c9d-0a5b-4e5e-a26a-857d9bb6dd4e Version: $LATEST
2022-08-29T11:11:59.112Z 54d90c9d-0a5b-4e5e-a26a-857d9bb6dd4e ERROR Error: socket hang up
at connResetException (node:internal/errors:692:14)
at TLSSocket.socketOnEnd (node:_http_client:478:23)
at TLSSocket.emit (node:events:539:35)
at endReadableNT (node:internal/streams/readable:1345:12)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
It works fine in a simple nodejs code on my local machine.
Here is my Lambda Function code:
const https = require('https');
exports.handler = async (event) => {
var postData = JSON.stringify({
"client_id": "abnbfye9-qtfnf1cj-abhrhzfyf7-m2tup-6x9kk2kc5688",
"client_secret": "fpghfh329-polk80s-ye043465p1yy-45hxnfd874z06",
"inTime": new Date(),
"outTime": new Date()
});
var options = {
hostname: 'example.com',
path: '/api/updateDataLambda',
method: 'POST',
port: 443, // 👈️ replace with 80 for HTTP requests
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiJ0ekpFTlNxcDJXeXh4YlNGbSIsImFjY2Vzc1Rva2VuRXhwIjp7ImRhdGUiOiIyMDIyLTA5LTI4IiwidGltZSI6IjA2OjIwOjQ1IiwidGltZVN0YW1wIjoxNjY0MzQ2MDQ1NjQ2LCJnbXQiOiIrMDAwMCJ9LCJyZWZyZXNoVG9rZW5FeHAiOnsiZGF0ZSI6IjIwMjItMDktMjgiLCJ0aW1lIjoiMDY6MjA6NDUiLCJ0aW1lU3RhbXAiOjE2NjQzNDYwNDU2NDYsImdtdCI6IiswMDAwIn0sImlhdCI6MTY2MTc1NDA0NSwiZXhwIjoxNjYxNzU0MTA1fQ.g1e5S15Q1qxB5_s4j3LFfFf6spU8gwgBUyVNLVuWNWk'
}
};
var req = https.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (e) => {
console.error(e);
});
req.write(postData);
req.end();
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};
Am I doing something wrong here?

Netlify & Faunadb Client network socket disconnected before secure TLS connection was established

I get the following error only in production mode calling a Netlify AWS serverless function
ERROR FetchError: request to https://registry.npmjs.org/faunadb failed, reason: Client network socket disconnected before secure TLS connection was established
at ClientRequest. (/var/task/node_modules/node-fetch/lib/index.js:1461:11)
at ClientRequest.emit (events.js:314:20)
at TLSSocket.socketErrorListener (_http_client.js:427:9)
at TLSSocket.emit (events.js:314:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
type: 'system',
errno: 'ECONNRESET',
code: 'ECONNRESET'
}
Here is the code:
const faunadb = require('faunadb');
q = faunadb.query;
var fauna_client = new faunadb.Client({ secret: '[..]' });
const redirect_uri = "/.netlify/functions/follow_spotify_callback";
const client_id = "[..]";
const client_secret = "[..]";
const basic = Buffer.from(client_id + ":" + client_secret).toString('base64');
const qs = require('querystring');
const axios = require('axios');
exports.handler = async function(event, context) {
const state = JSON.parse(event.queryStringParameters.state)
axios.post(
'https://accounts.spotify.com/api/token',
qs.stringify({
'grant_type': 'authorization_code',
'code': event.queryStringParameters.code,
'redirect_uri': process.env.URL + redirect_uri
}),
{
headers: {
'Authorization': `Basic ${basic}`,
'Content-Type': 'application/x-www-form-urlencoded'
}
}
).then(res => {
const access_token = res.data.access_token;
const headers = {
'Authorization': `Bearer ${res.data.access_token}`,
'Content-Type': 'application/json'
}
axios.get(
"https://api.spotify.com/v1/me",
{
headers: headers,
}
).then(result => {
fauna_client.query(
q.Exists(
q.Match(
q.Index('users_by_id'),
result.data.id
)
)
).then(ret => {
if (ret == false){
fauna_client.query(
q.Create(
q.Collection('users'),
{ data: {
display_name: result.data.display_name,
id: result.data.id,
access_token: access_token,
campaign: state.artisturi,
playlist: state.playlisturi,
referred_by: state.referrer
} }
)
)
}else{
console.log("user already exists")
}
})
.catch(err => console.log(err));
}
).catch(err =>{
console.log(err.message)
});
axios.put('https://api.spotify.com/v1/me/following?type=artist', {
'ids': [state.artisturi],
}, {
headers: headers
});
axios.put(`https://api.spotify.com/v1/playlists/${state.playlisturi}/followers`, {
'public': true,
}, {
headers: headers,
});
axios.put('https://api.spotify.com/v1/me/tracks', {
'ids': [state.trackuri],
}, {
headers: headers,
})
}).catch(err =>{
console.log(err.message)
})
return{
statusCode: 302,
headers: {
Location: 'https://open.spotify.com/playlist/'+state.playlisturi
}
}
}
Everything works locally running netlify dev, but not in production mode, and it is on https with SSL / TLS encryption
It looks like you are running into the problem described here: https://docs.fauna.com/fauna/current/drivers/known_issues
If you move the code that instantiates a Fauna client into your handler logic, the problem might go away.

Error when sending https request to ROBLOX API

I'm writing a simple API request to Roblox so I can retrieve the X-CSRF-TOKEN to do POST requests. The issue I'm facing is "Error: socket hang up".
I tried to just run the link in my browser and it displays a JSON table, but when I do the request through node.js it errors out.
const https = require("https")
const options = {
hostname: "groups.roblox.com",
path: "/v1/groups/5307563",
method: "GET",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Cookie': '.ROBLOSECURITY=' + cookie
}
}
const request = https.request(options, res => {
res.on('data', data => {
console.log("data received")
})
});
request.on('error', error => {
console.log(error)
})
You need to end the request with request.end().
const https = require("https")
const options = {
hostname: "groups.roblox.com",
path: "/v1/groups/5307563",
method: "GET",
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Cookie': '.ROBLOSECURITY=' + cookie
}
}
const request = https.request(options, res => {
res.on('data', data => {
console.log("data received")
})
});
request.on('error', error => {
console.log(error)
})
request.end()

How to send post request to iotdata service with authorization from a node js lambda function

I have the following post request that works in insomnia, but am not sure how to send it in node js lambda function, specifically I don't know how to do the authorization.
Here is my setup in insomnia
post request
https://obsf-ats.iot.us-east-1.amazonaws.com/things/esp8266_7F3B95/shadow
json
{
"state" :{
"desired":{
"on": true
}
}
}
auth
What I would do, but don't know where to put the auth, I am also not sure where to put the body of the message. Any help would be greatly appreciated.
const https = require('https')
const data = JSON.stringify({
todo: 'Buy the milk'
})
const options = {
hostname: 'https://obsf-ats.iot.us-east-1.amazonaws.com',
port: 443,
path: '/things/esp8266_7F3B95/shadow',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()
UPDATE: I have tried the following but it error with no console, is there a way to get the request error
var load = JSON.stringify({
state: {
desired: {
on: false,
},
},
});
request(
aws4.sign(
{
hostname: "https://obsf-ats.iot.us-east-1.amazonaws.com",
service: "iotdata",
region: "us-east-1",
method: "POST",
path: "/things/esp8266_7F3B95/shadow",
headers: {
"Content-Type": "application/x-amz-json-1.0",
},
body: load,
},
{
secretAccessKey: "obsf/obsf/x5Hpej0I",
accessKeyId: "obsf",
}
)
);
UPDATE: I am getting the following error and a 403
etaddrinfo ENOTFOUND https://obsf-ats.iot.us-east-1.amazonaws.com
Had to change this from
hostname: 'https://obsf-ats.iot.us-east-1.amazonaws.com',
to this
hostname: 'obsf-ats.iot.us-east-1.amazonaws.com',

Getting error on hitting Google Vision Api

const options = {
hostname: 'https://vision.googleapis.com/v1/images:annotate?key=<some key>',
method: 'POST',
headers: {
'Content-Type' : 'application/json'
}
};
const req = http.request(options, (res : any) => {
res.on('data', (chunk : any) => {
console.log(`BODY: ${chunk}`);
});
});
req.on('error', (e) => {
console.log(e)
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.write(JSON.stringify(body))
req.end()
I am trying to use one of the google vision feature i.e. Text Detection. But when ever I am hitting that api I am getting this error. I double checked the url and other data.
{ Error: getaddrinfo ENOTFOUND https://vision.googleapis.com/v1/images:annotate?key=<> https://vision.googleapis.
com/v1/images:annotate?key=<key>:80
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:56:26)
errno: 'ENOTFOUND',
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname:
'https://vision.googleapis.com/v1/images:annotate?key=<key>',
host:
'https://vision.googleapis.com/v1/images:annotate?key=<key>',
port: 80 }
This code should work, there are only a couple of changes necessary, for example we'll use the https module rather than the http module.
const https = require('https');
const options = {
hostname: 'vision.googleapis.com',
path: '/v1/images:annotate?key=' + API_KEY,
method: 'POST',
headers: {
'Content-Type' : 'application/json'
}
};
let data = "";
const req = https.request(options, (res: any) => {
res.on('data', (chunk: any) => {
data += chunk;
});
res.on('end', (chunk) => {
console.log(`BODY: ${data}`);
});
});
req.on('error', (e) => {
console.log(e)
console.error(`problem with request: ${e.message}`);
});
// Write data to request body
req.write(JSON.stringify(body))
req.end()
Try modifying the request to:
const options = {
method: 'POST',
headers: {
'Content-Type' : 'application/json'
}
};
const req = http.request(`https://vision.googleapis.com/v1/images:annotate?key=<some key>`, options, (res : any) => {
res.on('data', (chunk : any) => {
console.log(`BODY: ${chunk}`);
});
});
because https://vision.googleapis.com/v1/images:annotate?key=<some key> is a full URL, not a valid hostname.

Resources