ERROR connecting to remote database server(NGINX) via tunneled localhost Node JS - node.js

error connecting: Error: Connection lost: The server closed the connection.
at Protocol.end (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:109:13)
at Socket.<anonymous> (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\Connection.js:109:28)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
--------------------
at Protocol._enqueue (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:141:48)
at Protocol.handshake (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\protocol\Protocol.js:52:41)
at Connection.connect (C:\Users\Tony\Documents\TestServer\node_modules\mysql\lib\Connection.js:130:18)
at Object.<anonymous> (C:\Users\Tony\Documents\TestServer\app.js:13:12)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
using this code
var express = require('express');
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
port: '8888',
user: 'root',
password: 'xxx',
database: 'shop'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
server is BITNAMI using google cloud
localhost is on windows, i can connect to the phpmyadmin through the browser but the script does not work. Thank you

Hi Bitnami developer here,
As you mentioned, you can access phpMyAdmin using the 8888 port because you opened a tunnel to access Apache and that application through that port. You would need to open a new tunnel to access MySQL directly because we configure the server to only listen to localhost for security reasons.
The source port of this new tunnel would be a different port (i.e 8989) and the destination would be localhost:3306 (3306 is the MySQL default port). You would need to modify your code to use the 8989 port instead of the 8888 one.
I hope this information helps.
Jota

To find out if the tunnel is really set up you can run.
sudo lsof -i -n | egrep '\<ssh\>'
you should get some lines out of that. If one of them is around these lines :
ssh 11704 root 3u IPv4 47283826 0t0 TCP [localip]:46286->[remoteip]:ssh (ESTABLISHED)
then the connection is on. In this case you should make sure you are connecting your nodejs application to the correct local port. The code above seems correct just make sure that the tunnel is then from port :8888 to the remote database port, maybe :3607 or whatever the port is.
Otherwise, if your tunnel is not set up properly. There are quite a few tutorials out there explaining how to set an ssh tunnel such as this one :
https://support.cloud.engineyard.com/hc/en-us/articles/205408088-Access-Your-Database-Remotely-Through-an-SSH-Tunnel
Make sure that you also use autossh to set up the tunnel. SSH tunnels are unstable and they tend to fall. Autossh will make sure that the connection is brought back up and that is run when the computer boots up.
https://www.everythingcli.org/ssh-tunnelling-for-fun-and-profit-autossh/

Related

PORT 3306 Already In use when i rn nodemon in NodeJS app

I have a nodejs app which was running just fine all this time until I opened MySQL Workbench and also included .env file in my project. I was in the process of deploying the system to Digital Ocean managed database service when I opened MySQL Workbench to visualise the process and not use the mysql shell. Everything worked fine and I migrated my db on to the DO database cluster.
I also wanted to make my app more secure, so I bumped into the .env file method and tried my best to follow through and I came up with this:
Step 1:
npm i dotenv --save
Step 2: Added require('dotenv').config() to my server.js file
Step 3: Update my DB connection file
const mysql = require("mysql");
const conn = mysql.createConnection({
host: process.env.HOST,
user: process.env.USERNAME,
password: process.env.PASSWORD,
//rsport : (process.env.PORT),
database: process.env.DATABASE_NAME,
multipleStatements: true,
});
conn.connect((err) => {
if (err) {
console.log("Oops!, Failed to connect to the database.");
} else {
console.log("Database connection succesfull!");
}
});
module.exports = conn;
Step 4: I set my local .env file and remote .env file accordingly
Step 5: I run nodemon the it return the following error:
body-parser deprecated undefined extended: provide extended option server.js:27:17
events.js:292
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3306
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (G:\Maxiko Payment System\Systems\Management Apps\Microservices\mx-core\server.js:47:5)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 3306
}
[nodemon] app crashed - waiting for file changes before starting...
What am I doing wrong? honestly I do not think .env has anything to do with this, but i definately know MySQL Workbench made its own connection. So am I to believe that only one application can connect to a database at one time? That doesnt sound right to me either.
Try running your node js server on a different port or run this command in command prompt (as administrator)
netstat -ano | findstr :<PORT>
Replace with your port number that is in use(in your case 3306)
Then it will show you the PID of your process
Something like this
TCP 0.0.0.0:3306 0.0.0.0:0 LISTEN 77777
77777 is the PID
then run this command
taskkill /PID <PID> /F
Replace with the PID you got in the last command
Its look like some other process already running on your port 3306
You can check it by using below command
lsof -i tcp:3306
It will list you process that is already on the port 3306
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 12012 user 20u IPv6 86535 0t0 TCP *:3306 (LISTEN)
That shows a process with PID 12012 already using your port 3306
If this process is redundant you can kill it by following command:
sudo kill -9 PID
Replace PID with your process id which in my case is 12012
otherwise you can use some other port to run your node server
I have found the problem. There was indeed another process running and using the same port. In app server initialization script , I was setting the port like this:
app.use('port', process.env.PORT || 5000)
Where at the the fdatabase connection file, I set the ports also as
const conn = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASS,
port : (process.env.DB_PORT),
database: process.env.DB_NAME,
multipleStatements: true,
});
So, as you can see, there is supposed to be a port conflict since i only had on PORT=3006 variable in the .env file.
SOLUTION
All I had to do was specify the database port as
DB_PORT = 3006
APP_PORT = 5000
in the environment variables.
Thank you and Goog luck.

Express server errors with external IP, works fine otherwise

I have an Express server - very small at the moment, this is my whole code:
const express = require('express');
const app = express();
const port = 8080
app.get('/', function(req, res) {
res.send('Hey!')
})
app.listen(port, 'my.ip.address')
It works just fine when my.ip.address is localhost/0.0.0.0/127.0.0.1/192.168.0.28 (my network IP.) But when my external IP is provided, I get this:
events.js:298
throw er; // Unhandled 'error' event
^
Error: listen EADDRNOTAVAIL: address not available myip:8080
at Server.setupListenHandle [as _listen2] (net.js:1292:21)
at listenInCluster (net.js:1357:12)
at doListen (net.js:1496:7)
at processTicksAndRejections (internal/process/task_queues.js:85:21)
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1336:8)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
code: 'EADDRNOTAVAIL',
errno: -99,
syscall: 'listen',
address: myip,
port: 8080
}
I know that 8080 is correctly forwarded, as another example server (Apache) works just fine, and can be connected to from my external IP.
TLDR:
Just listen to the port and remove the IP address:
app.listen(port)
What's happening:
When you do the following:
app.listen(port, 'my.ip.address')
What you are doing is telling your OS that you want to listen to incoming packet from the port on the network card that owns that IP address.
If the OS cannot find any network card (ethernet, wifi etc.) that has that IP address assigned then your OS will error out saying it cannot find the hardware you want to listen to.
Since your external IP is owned by the external network device of your router your OS cannot find the hardware that owns the IP address.
Altenate solution
You can just listen to the IP address assigned to the network card that is connected to your router:
app.listen(port, 'my.local.ip.address')

Amazon EC2 Error: listen EACCES 0.0.0.0:80

I have already added the HTTP TCP Port 80 to the inbound rules, but I still get the error:
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:992:11)
at _exceptionWithHostPort (util.js:1014:20)
at Server.setupListenHandle [as _listen2] (net.js:1338:19)
at listenInCluster (net.js:1396:12)
at doListen (net.js:1505:7)
at _combinedTickCallback (internal/process/next_tick.js:141:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
at Function.Module.runMain (module.js:695:11)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
You probably have to run your node.js script with sudo as you want to listen on port 80.
You cannot run a process that listens on low ports (below 1024) without root privileges.
You either try to run as sudo, as stated above, or start to use a reverse proxy (nginx for instance), start the process on another port and use the reverse proxy to forward the calls from port 80 to whatever port you started the process on.
The error code EACCES means you don't have proper permissions to run applications on that port. On Linux systems, any port below 1024 requires root access.
you need to use reverse proxy to forward the calls from port 80 to 8080 for example.

Can I listen to website url host? Node JS

I can't understand one thing- does NodeJS allow to listen to custom hostname? Not localhost. Because when I listen to my website url (example.com), I'm getting the following error:
Error: listen EADDRNOTAVAIL example.com ip-address:1000 at
Object.exports._errnoException (util.js:1022:11) at
exports._exceptionWithHostPort (util.js:1045:20) at Server._listen2
(net.js:1246:19) at listen (net.js:1295:10) at net.js:1405:9 at
_combinedTickCallback (internal/process/next_tick.js:77:11) at process._tickCallback (internal/process/next_tick.js:98:9) at
Module.runMain (module.js:606:11) at run (bootstrap_node.js:394:7) at
startup (bootstrap_node.js:149:9)
Why does it happend? And can I listen for POST messages from external site URL?
Why does it happend?
This happens because the hostname and port you requested isn't available to you.
can I listen for POST messages from external site URL?
No, You can't. server.listen() accepts hostname and port
Begin accepting connections on the specified port and hostname. If the hostname is omitted, the server will accept connections on any IPv6 address (::) when IPv6 is available, or any IPv4 address (0.0.0.0) otherwise. Omit the port argument, or use a port value of 0, to have the operating system assign a random port, which can be retrieved by using server.address().port after the 'listening' event has been emitted.
And further digging into list of Node.js Common System Errors docs and exhaustive list, It is clear that, Address not available was the case.
EADDRNOTAVAIL Address not available (POSIX.1).

403 Forbidden from bitcoind -server run on docker

I'm using the new Docker-for-Mac to run the daemon like this:
$docker run -d --name bitcoind -e BITCOIN_DATA=/data \
-v ${PWD}/bitcoind:/data -p 8332:8332 seegno/bitcoind:latest \
-server -rpcuser=test -rpcpassword=nopass -rpcallowip=192.168.1.67 \
-printtoconsole -debug=rpc
where the IP address I allow is my default:
$ ifconfig |grep inet |grep -v inet6
inet 127.0.0.1 nitmask 0xff000000
inet 192.168.1.67 netmask 0xffffff00 broadcast 192.168.1.255
I then run my app:
var BTC = require('bitcoin-core');
var opts = {
host: 'localhost',
port: 8332,
username: 'test',
password: 'nopass'
};
btc = new BTC(opts);
btc.getNewAddress().then(function(s) {
console.log(s);
});
and get:
Unhandled rejection RpcError: 403 Forbidden
at get (/Users/ekkis/Development/Test/www/node_modules/bitcoin-core/dist/src/parser.js:34:11)
at Client.rpc (/Users/ekkis/Development/Test/www/node_modules/bitcoin-core/dist/src/parser.js:81:14)
at Request.self.callback (/Users/ekkis/Development/Test/www/node_modules/request/request.js:200:22)
at emitTwo (events.js:87:13)
at Request.emit (events.js:172:7)
at Request. (/Users/ekkis/Development/Test/www/node_modules/request/request.js:1067:10)
at emitOne (events.js:82:20)
at Request.emit (events.js:169:7)
at IncomingMessage. (/Users/ekkis/Development/Test/www/node_modules/request/request.js:988:12)
From previous event:
at /Users/ekkis/Development/Test/www/node_modules/bitcoin-core/dist/src/index.js:163:21
From previous event:
at Client.command (/Users/ekkis/Development/Test/www/node_modules/bitcoin-core/dist/src/index.js:152:34)
at apply (/Users/ekkis/Development/Test/www/node_modules/lodash/lodash.js:409:27)
at Client.wrapper [as getNewAddress] (/Users/ekkis/Development/Test/www/node_modules/lodash/lodash.js:4837:16)
at Object. (/Users/ekkis/Development/Test/www/t:12:5)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:136:18)
at node.js:972:3
though I seem able to connect to the server:
$ telnet localhost 8332
Trying ::1...
Connected to localhost.
Escape character is '^]'.
it doesn't hang up on me with a connection error... what am I missing?
* edit I *
well... if I run the daemon with -logips and make my attempt I see:
2016-07-09 18:32:26 Received a POST request for / from 172.17.0.1:50262
instead of the IP address I expected. I gather it's the IP address of the VM that Docker runs, that somehow gets routed to the container. so now the question is: how do I discover that IP address from my node app so I can whitelist it?
and... (drumroll) the answer seems to be that I can pick it up from the docker inspect NetworkSettings.Gateway key. but... in fact, I can bake it into the CLI because for this image the gateway is always the same

Resources