Accessing local nodejs server from salesforce.com - node.js

I have set up a local server in my machine using nodejs. something like this:
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
var host = config.host;
var port = config.port;
var express = require("express");
var app = express();
app.get("/",function(request,response){
response.send('thanks for visiting my site');
});
app.listen(port, host);
Now, If I send a http request to this server, I get the 502 seervice unavailable response.
HttpRequest req = new HttpRequest();
req.setEndpoint('http://localhost:4555/');
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
I have also added the localhost url in the remote site setting, but still I am unable make any calls. Any idea how to go about it?

localhost is a relative address, which machine it resolves to depend on where it used, so when you use localhost in apex code (which is running on a salesforce server), its trying to talk to itself not your nodejs host. You need to give your host a publicly resolvable name or IP address and use that in your apex code.

Related

Nodejs SSL using CloudFlare not working over https

So the problem I'm having is that the client won't connect with the server.js when the server.js is using https.
if I go to "https://mydomainame.com" I get this error in the console of every other browser than brave browser
index.js:83 GET https://serverip:8081/socket.io/?EIO=3&transport=polling&t=NK0oCD6 net::ERR_CERT_AUTHORITY_INVALID
(The blacked out is the IP address of the server)
the weird thing is that in the brave browser the domain changes to "http://mydomainame.com" and the client then is connected to server.js
I'm using free Cloudflare with Full end to end encryption
server.js code:
var express = require('express'),
https = require('https');
var app = express();
var fs = require('fs');
var httpsOptions = {
key: fs.readFileSync('/var/www/ssl/sitename.com.key'),
cert: fs.readFileSync('/var/www/ssl/sitename.com.pem')};
var server = https.createServer(httpsOptions,app);
var io = require('socket.io').listen(server);
const port = 8081;
server.listen(port);
And client.js connection code:
socket = io.connect('https://serverip:8081', {secure: true});
I am using the same Origin Certificates for the server and for the nodejs code.
The server is using Apache2 with PHPMyAdmin and is configured to make the domain only work using https.
I read somewhere something Cloudflare not being able to use other ports than 443 and some other but I did not really understand it, And I can't get the server.js to work over port 443.
I'm thankful for any information or help I can get! :)
So I figured it out, big thanks to Eric Wong for pointing out the biggest problem that I was trying to connect to the server using its IP there for not going thru Cloudflare.
Then in this article Identifying network ports compatible with Cloudflare's proxy
you can see what ports Cloudflare allows connections on then, I changed my code to used the https port 8443.
socket = io.connect('https://domainname.com:8443',{secure: true});
then the only thing I had to do was to port forward the new port and everything worked fine!

Creating A HTTP proxy server with https support and use another proxy server to serve the response using nodejs

I need help creating a proxy server using node js to use with firefox.
the end goal is to create a proxy server that will tunnel the traffic through another proxy server (HTTP/SOCKS) and return the response back to firefox. like this
I wanna keep the original response received from the proxy server and also wanna support https websites as well.
Here is the code I came up with.
var http = require('http');
var request = require("request");
http.createServer(function(req, res){
const resu = request(req.url, {
// I wanna Fetch the proxy From database and use it here
proxy: "<Proxy URL>"
})
req.pipe(resu);
resu.pipe(res);
}).listen(8080);
But it has 2 problems.
It does not support https requests.
It also does not supports SOCKS 4/5 proxies.
EDIT: I tried to create a proxy server using this module. https://github.com/http-party/node-http-proxy
but the problem is we cannot specify any external proxy server to send connections through.
I have found a really super simple solution to the problem. We can just forward all packets as it is to the proxy server. and still can handle the server logic with ease.
var net = require('net');
const server = net.createServer()
server.on('connection', function(socket){
var laddr = socket.remoteAddress;
console.log(laddr)
var to = net.createConnection({
host: "<Proxy IP>",
port: <Proxy Port>
});
socket.pipe(to);
to.pipe(socket);
});
server.listen(3000, "0.0.0.0");
You have to use some middleware like http-proxy module.
Documentation here: https://www.npmjs.com/package/http-proxy
Install it using npm install node-http-proxy
This might help too: How to create a simple http proxy in node.js?

404 - from nginx application to remote servers localhost?

I am new to react, and wanted to deploy a site to my domain with Nginx. I need to make the application to be able to fetch from client side, to the localhost of the remote server hosting the site with Nginx. I know exposing this many details might make security experts and hackers either drool or shake their heads. But I am losing my sanity from this.
This is a filtered version of my Node.js express service running on the remote server:
const express = require("express")
const cors = require("cors")
const app = express();
const PORT = 1234;
const spawn = require("child_process").spawn;
app.use(cors())
app.listen(PORT, function(){
console.log(`listening on port:${PORT}...`)
})
app.get("/api/play/:choice", function(req,res){
pythonProcess = spawn('python',["./script.py", req.params.choice]);
pythonProcess.stdout.on('data', function(data) {
res.status(200).send(data.toString('utf-8'))})
})
this is how I am fetching from the deployed react application. The public IP of the droplet I am using
fetch(`104.248.28.88/1234/api/play/rock`)
Change the fetch to replace the / with a : to indicate port, rather than directory
fetch("104.248.28.88:1234/api/play/rock")

Running http-webserver and ws-socket-server on same port

The Heroku tutorial on creating/deploying a web- and a socketserver has the following piece of code setting up those servers:
const server = express()
//...
const wss = new SocketServer({ server });
In other words, one server runs for example at http://localhost:8080 and the other one at ws://localhost:8080. So the http server is referencing the socket server by:
var HOST = location.origin.replace(/^http/, 'ws')
var ws = new WebSocket(HOST);
//...
Now while I did not use the exactly same code, running both servers on the same address and port with the only difference being the protocol (http and ws) makes the websocket-server unreachable:
Firefox can’t establish a connection to the server at ws://localhost:8080/.
Changing the port to anything else fixes the problem. Why is that? And how should I implement my application, considering that Heroku recommends using static ports only as a fallback like
var port = process.env.PORT || 8080;
instead?

Using VHOST for subdomains on Express

I have 2 separated site (static web and application). I've tried to use Express vhost middleware but I couldn't manage.
For the below codes I configured my hosts file as;
127.0.0.1 localhost
127.0.0.1 process.localhost
my server.js codes
var connect = require('connect')
var express = require('express')
var vhost = require('vhost')
var app = require('./app')
var static = require('./static')
var server = connect()
server
.use(vhost('localhost', static.service))
.use(vhost('process.localhost', app.service))
.listen(1337, function(){
console.log('Server is listening')
})
Then if I write my address bar localhost:1337 static page comes this is good. However, if I write process.localhost:1337 nothing comes.
What should I do?
Edit
If I add the below middleware to my codes when I write the address bar localhost:1337 console write localhost:1337 but is I write process.localhost:1337 console write nothing.
Actually problem was the change of hosts file permissions.
Solution is here;
https://serverfault.com/a/452269/277517

Resources