403 Forbidden from bitcoind -server run on docker - node.js

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

Related

how to correctly configure ngrok docker container for node or react app?

I'm trying to use ngrok for exposing my localhost to share my node backend with a friend.
The problem I face is that whenever I start node backend the ngrok container couldn't work cause it trys to map the same port as being used by node.
node:events:371
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (node:net:1319:16)
at listenInCluster (node:net:1367:12)
at Server.listen (node:net:1454:7)
at Function.listen (C:\Users\928941\node_backends\express-for-react\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\928941\node_backends\express-for-react\app.js:27:5)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12)
Emitted 'error' event on Server instance at:
at emitErrorNT (node:net:1346:8)
code: 'EADDRINUSE',
errno: -4091,
syscall: 'listen',
address: '::',
port: 5000
}
[nodemon] app crashed - waiting for file changes before starting...
the above error simply means that port 5000 is currently in use(i.e., by ngrok docker container).
If I run the node app first and try to run the docker container after that I get this error from docker.
[+] Running 1/0
- Container docker-compose_for_ngrok_ngrok_1 Created 0.0s
Attaching to ngrok_1
Error response from daemon: Ports are not available: listen tcp 0.0.0.0:5000: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
This is my docker-compose.yml file
version: '3'
services:
ngrok:
image: ngrok/ngrok:alpine
ports:
- 4040:4040
- 5000:5000
environment:
- NGROK_AUTHTOKEN=my_authentication_token
command: http 5000
the compose file is same as this docker run command so anyone can be used.
docker run -d -p 4040:4040 -p 5000:5000 -e NGROK_AUTHTOKEN=my_token ngrok/ngrok:alpine http 5000
Port 4040 is default ngrok port for dashboard and it works fine for me.
I'm also facing this issue with react app as well.
I'm not sure if you are running on linux, but you might not need to do any port forwarding if you use the --net=host option. See the examples in https://hub.docker.com/r/ngrok/ngrok/
The equivalent for a docker compose file is network_mode:https://docs.docker.com/compose/compose-file/compose-file-v3/#network_mode
So your docker compose would be:
version: '3'
services:
ngrok:
image: ngrok/ngrok:alpine
environment:
- NGROK_AUTHTOKEN=my_authentication_token
command: http 5000
network_mode: "host"
Assuming your node is running on localhost:5000, if you start a docker container and use the same network, then you don't need to open any ports.
If you are using mac or windows, you will need to use the workaround here:
https://docs.docker.com/desktop/mac/networking/#use-cases-and-workarounds
Which is to use command: http host.docker.internal:5000 instead.

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.

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.

ERROR connecting to remote database server(NGINX) via tunneled localhost 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/

How to start node.js on port 80 on a linux server?

When I try to start node on port 80, the error tells me that the port is in use. I imagine that's Apache.
What is the proper way to "take over" port 80, and keep it that way after a server restart?
(Linux xxxx.__.com 2.6.32-5-amd64 #1 SMP Tue Jun 14 09:42:28 UTC 2011 x86_64 GNU/Linux)
you can use ip tables to map port 80 to 8000
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8000
to make it permanent
sudo sh -c "iptables-save > /etc/iptables.rules"
and add
pre-up iptables-restore < /etc/iptables.rules
to your /etc/network/interfaces
To take over port 80 when another process is listening on it, you must kill the process (or somehow tell it to stop listening). To ensure that Apache doesn't try to listen on port 80 again the next time it starts, you need to edit its configuration or prevent it from starting up.
To see which process is listening on port 80, run sudo netstat -ntap and look for the row with Local Address ending in port :80. The PID of the process (and the name) is in the far right column.
you can use node.js with node-http-proxy check this link How to use vhosts alongside node-http-proxy? and
How do I run Node.js on port 80?
Thanks & Regards,
Alok
A constantly running unused apache maybe a security hole, in any case no sense in running unused services.
On the chance you're on ubuntu, this what I used..
sudo service apache2 stop
sudo update-rc.d apache2 remove
You can access port 80 once you stop the service currently using it.
In your case, follow these steps:
1) Use systemctl to stop apache2:
sudo systemctl stop apache2
2) Check apache2 status:
sudo systemctl status apache2
Or just by entering http://localhost in your browser. If you get an error, you are good to go.
ERR_CONNECTION_REFUSED
3) Now start your NodeJS server on port 80.
4) You can access your server at http://localhost
UPDATE
If you are seeing errors on your console, try node preceding with sudo
For eg. sudo node server.js
Here are the errors
events.js:137
throw er; // Unhandled 'error' event
^
Error: listen EACCES 0.0.0.0:80
at Object._errnoException (util.js:1003:13)
at _exceptionWithHostPort (util.js:1024:20)
at Server.setupListenHandle [as _listen2] (net.js:1349:19)
at listenInCluster (net.js:1407:12)
at Server.listen (net.js:1495:7)
at Object.<anonymous> (/home/abdus/Desktop/voice-recognition/test.js:7:4)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)

Resources