NodeJS https proxy don't work with request module - https

when I do :
curl https://httpbin.org/ip --proxy https://74.208.146.112:80
I get {"origin": "74.208.146.112"} so it's work but with request module like that :
request({url: 'https://httpbin.org/ip', proxy: 'https://74.208.146.112:80'}, function (error, response, body)
{
if (!error && response.statusCode == 200) {
console.log(body)
}
else {
console.log(error)
}
})
I get this error : { [Error: tunneling socket could not be established, cause=write EPROTO 140735132950528:error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol:../deps/openssl/openssl/ssl/s23_clnt.c:794:
] code: 'ECONNRESET' }
I have try with this params pool: true, strictSSL: false, tunnel: true all of a sudden, with different value or one by one but I get the same error.
I have try with other proxies and with VPS and I get the same error ...
Anyone have idea ?

Related

Why does "instanceof" return true on the server side but return false on the client side?

I am using Yup to validate register inputs on the server side using a Next.js API route.
Although the validate function returns an error on the server side which is an instance of ValidationError, when sending the same error to the client side and checking again if it is an instance of ValidationError the same error returns false.
Server side code:
try {
const validation = await registerYupSchema.validate(registerData)
} catch (error) {
if (error instanceof ValidationError) {
return res.status(400).send({ error })
}
return res.status(500).send({ error: 'Validation failed' })
}
Client side code:
const response: Response = await fetch(endpoint, options)
const { error } = await response.json()
if (error && error instanceof ValidationError) {
} else if (error) {
setError(error)
}
I checked with console.logs where and what exactly is sent back to the client. The status code was 400 and the console.log within the if-statement on the server side was triggered successfully. Still, on the client side the if-statement returned false and the else-if-statement ran instead of it.

Nuxt Proxy changes POST to GET when proxy is set to true

In my universal nuxt app, I have setted proxy at true and rewritte my url to avoid CORS issue.
But when I'm setting proxy to true, all my post requests are changed to get request. Don't understand why and how to configure it no to have this transformation.
Here is my nuxt.config.js :
/*
** Axios module configuration
*/
axios: {
proxy: true
},
proxy: {
'/apicore/': { target: 'http://blablabla.fr', pathRewrite: { '^/apicore/': '' }, changeOrigin: true }
}
My call:
async createJoueur({ state, dispatch, commit }, data) {
const URL = '/apicore/joueur'
await this.$axios
.post(
URL,
data, {
headers: {
'Content-Type': 'application/json'
}
}
)
.then((response) => {
console.log('JOUEUR LOGGED : ')
if (response.status === 200) {
} else {
console.log('Login failed / Not found')
}
}
)
.catch((error) => {
console.log('ERROR')
})
With this proxy set to true, my post-call becomes a get one.
Do I have forgotten something in my configuration?
Thanks for your help.
I was with the same problem! i solved it using changeOrigin: false.
I know that it must be the default value (Look at changeOrigin session ),
but it seems like in nuxtjs proxy implementation this value default is true (Look at Options session) .
I had the same issue and after some logging using the onProxyReq option I found out that the issue was the Cloudflare proxy, not the nuxt proxy. Cloudflare was forwarding HTTPS requests to HTTP and this forces POST requests to become GET requests as is common with 301/302 redirects.
As far as I know, it's not possible to configure Cloudflare to do 308 redirects, which would not alter the HTTP method/body.

Install Service Worker on apex.oracle.com

Can I have some help installing a Service Worker on an application stored on the server apex.oracle.com.
Im currently installing the server worker with this code running on page load:
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js', {
scope : './'
}).then(function (registration) {
var serviceWorker;
if (registration.installing) {
serviceWorker = registration.installing;
printState('installing');
} else if (registration.waiting) {
serviceWorker = registration.waiting;
printState('waiting');
} else if (registration.active) {
serviceWorker = registration.active;
printState('active');
}
if (serviceWorker) {
printState(serviceWorker.state);
serviceWorker.addEventListener('statechange', function (e) {
printState(e.target.state);
});
}
}).catch (function (error) {
printState(error);
});
}
And i get the errors:
A bad HTTP response code (404) was received when fetching the script.
/pls/apex/apex//mvx/r/72250/files/static/v5/sw.js Failed to load resource: net::ERR_INVALID_RESPONSE
I have to get this application with service worker because i need it to run offline.
Any ideas?
Thanks in advance.
Edit:
Changed the lines:
navigator.serviceWorker.register('/sw.js', {
scope : './'
to
navigator.serviceWorker.register('#WORKSPACE_IMAGES#sw.js', {
scope : './#WORKSPACE_IMAGES#'
And it worked.

proxy with hapi.js and h2o2, connection close

I try to configure hapi.js to proxy requests from /{params*} path to http://localhost:3000. It works fine for root '/' but when I try call /login I receive Cannot GET /login and on request I can see 'GET /login HTTP/1.1\r\nHost: localhost:3000\r\nConnection: close\r\n\r\n'. On my UI server http://localhost:3000/login works fine.
this is my proxy configuration
proxy: {
mapUri: (request, callback) => {
//loaded from a configuration file
let url = `http://localhost:3000${request.path}`;
callback(null, url);
}
}
Anyone know how to configure hapi proxy to pass custom routes?
Acctually it started to work. This is my current route
{
method: 'GET',
path: '/{param*}',
config: {
handler: {
proxy: {
mapUri: (request, callback) => {
let tls = conf.ui.tls;
let host = conf.ui.host;
let port = conf.ui.port;
let url = `${tls ? 'https://' : 'http://'}${host}:${port}${request.path}`;
callback(null, url);
}
}
}
}
}

How can I send an AJAX request to a node.js server via HTTPS?

I have the following node.js server set up listening to port 9001
var https = require('https');
var fs = require('fs');
var qs = require('querystring');
var options = {
key: fs.readFileSync('privatekey.pem'),
cert: fs.readFileSync('certificate.pem')
};
https.createServer(options, function (req, res) {
res.writeHead(200);
console.log('Request Received!');
console.log(req.method);
if (true || req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
console.log(body);
var POST = qs.parse(body);
console.log(POST);
});
}
res.end("hello, world\n");
}).listen(9001);
and I am trying to get this server to respond to an AJAX call
function form_save()
{
console.log("submitted!");
var data_obj = {
data1: "item1",
data2: "item2"
}
$.ajax({
url: 'https://adam.testserver.com:9001/',
type: "POST",
dataType: "json",
data: data_obj,
success: function() {
console.log("success!");
},
complete: function() {
console.log("complete!");
}
});
}
There are two problems occurring with my arrangement. The first is that if I start the server and then click the button that triggers my form_save() the node server does not respond and I get the following error:
submitted!
OPTIONS https://adam.testserver.com:9001/ Resource failed to load
jQuery.extend.ajaxjquery.js:3633
$.ajaxjquery.validate.js:1087
form_savew_worksheet.php:64
confirm_deletew_worksheet.php:95
jQuery.event.handlejquery.js:2693
jQuery.event.add.handlejquery.js:2468
w_worksheet.php:73
complete!
At this point if I access that url directy (https://adam.testserver.com:9001/) I get the expected "hello, world" output as well as the console message "Request Received!
GET". From this point on if I click the button to trigger my AJAX call I get a new error.
submitted!
XMLHttpRequest cannot load https://adam.testserver.com:9001/. Origin
https://adam.testserver.com is not allowed by Access-Control-Allow-Origin.
w_worksheet.php:73
complete!
I don't understand why I get this message as both my form and node server reside on the same server. Thanks for taking the time to read, I appreciate any help I can get on this. I've been stuck for a while now!
You've run into the Cross-Origin Resource Sharing (CORS) specification.
Note the OPTIONS in your output. The OPTIONS HTTP Verb is used by the browser to query the web server about the URL, not to GET its contents or POST data to it.
Your server doesn't respond with the correct header data on a CORS request, so your browser assumes it has no rights to access the data, and refuses to GET or POST to the URL.
If you truly want to let any website in the world run that AJAX request, you can do something similar to the following:
function handleOptions(request, response) {
response.writeHead(200, {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Method": "POST, GET, OPTIONS",
"Access-Control-Allow-Headers": request.headers["access-control-request-headers"]
});
response.end();
}
function server(request, response) {
if(request.method == "POST") {
handlePost(request, response);
} else if(request.method == "OPTIONS") {
handleOptions(request, response);
} else {
handleOther(response);
}
}
https.createServer(sslObj, server).listen(9001);
You can fill in the details and whether you should handle GET separately, and so on (handleOther should return an appropriate error code for each request method you don't support).

Resources