Here's my setup
Server
var webrtcServer = new PeerServer({
port: 9000,
path: "/wrtc"
});
Peer 1 (running inside a node-webkit application)
var peer = new Peer( 'masterName', {
host: 'localhost',
port: 9000,
path: '/wrtc'
});
peer.on( 'connection', function(conn) {
conn.on( 'open', function() {
console.log( "peer connected" ); // This fires as expected
conn.send( "helo" );
});
});
Peer 2 (running inside a browser)
conn = peer.connect('masterName');
conn.on('open', function(){
console.log( "WebRTC connection open" ); // This fires as expected
});
conn.on('data', function(data) {
debugger;
console.log("data"); // This never hits
});
If I set breakpoints in peer.js, no data is being received. Nothing changes if I set the connection to reliable. Is there anything else I can try?
I ran into a similar problem and adding a STUN server solved it for me. Also have a look this discussion in the peerjs Google group
peer = new Peer(
{
host: 'localhost',
port: 9000,
debug: true,
config: { 'iceServers': [
{ 'url': 'stun:stun.l.google.com:19302' }
] }
});
Related
I can connect to ssh2 without the privateKey
I am trying to enter a server with SFTP but when I get the following error ...
Timed out while waiting for handshake
I'm looking for an example and almost everyone uses the privateKey, is it mandatory? and how is one generated?
My code is the following ...
var Client = require ('ssh2'). Client;
var conn = new Client ();
conn.on ('error', function (err) {
console.log ('SSH - Connection Error:' + err);
});
conn.on ('end', function () {
console.log ('SSH - Connection Closed');
});
conn.on ('ready', function () {
console.log ("------ enter ------");
// code to work with SSH
});
conn.connect ({
host: 'host',
username: 'user',
port: 22
password: 'password',
});
Strange behavior in Node with TLSSocket and tls.connect.
var port = 7000;
var host = '94.125.182.252'; //freenode
var tls = require('tls');
var net = require('net');
var socket = new net.Socket();
var secure;
secure = new tls.TLSSocket( socket, {
isServer: false,
rejectUnauthorized: false
});
// edit (left out of original post, but present in my test code, whoops)
secure.connect( {
port: port,
host: host
});
secure.setEncoding( 'utf8' );
secure.on( 'connect' , function() {
console.log( 'connected' );
})
.on( 'secureConnect', function() {
console.log( 'secure connect' );
})
.on( 'error', function( e ) {
console.log( 'error', e );
})
.on( 'data', function( data ) {
console.log( data );
});
if ( secure.isPaused() ) {
console.log( 'socket was paused' );
secure.resume();
}
This doesn't even attempt to connect and no error messages are produced. I have wireshark monitoring and there is no activity captured.
A different approach:
secure = tls.connect( {
rejectUnauthorized: false,
host: host,
port: port,
socket: socket
});
Same story, nothing captured, no errors. If I remove the socket: socket aspect above it will connect. This makes some sense as the docs state that if the socket option is specified it will ignore port and host. The above works on my previous Node version( 0.12.7).
If I want to use the existing socket I have to tell it to connect before calling tls.connect.
socket.connect( {
port: port,
host: host
});
secure = tls.connect( {
rejectUnauthorized: false,
socket: socket
});
This doesn't seem proper.
Passing a connecting socket to tls.TLSSocket( socket, ...) seems to have no effect.
The 'connect' event is fired but I imagine that is not related to TLSSocket.
I could not get tls.TLSSocket(...) to work on previous Node iterations.
Stepping through with node debug did not expose any obvious problems.
The options for net.Socket([options]) don't seem to accept a port or host for configuring until you try to connect, and trying to connect before passing to tls.connect seems counter intuitive. It would suggest that is not the intended usage.
So my questions would be:
What am I doing wrong with tls.TLSSocket() or perhaps is it a bug?
Am I correct to assume that passing an existing socket into tls.connect() is for already established connections switching protocol? If not, whats the proper way to assign a port and host?
Edit:
As per suggestion:
secure = tls.connect( {
rejectUnauthorized: false,
socket: socket
});
socket.connect( {
port: port,
host: host
});
This works.
secure = new tls.TLSSocket( socket , {
isServer: false,
rejectUnauthorized: false
});
socket.connect( {
port: port,
host: host
});
Unfortunately this does not work. A 'connect' event is emitted, never a 'secureConnect' and never any other events or data.
In your first two (non-working) examples, you only created a socket and never started actually connected it. Add a socket.connect(); at the end of your original code and it should work fine.
tls.connect() when passed a plain socket, does not actually call socket.connect(); internally, it merely sets up to start listening for data on the socket so it can decrypt incoming data properly.
I'm learning Angular js and trying to run deployd server. Followed http://terraltech.com/how-to-setup-deployd-on-ubuntu-server/ steps mentioned here.
When I try to add a row from deployd dashboard am getting below error as a response
{"message":"Database connection error","status":400}
Here is my production.js server configuration.
var deployd = require('deployd');
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'SAMLP15070001',
port: 27017,
name: 'deployd',
credentials: {
username: 'deployd',
password: 'deployd'
}
}
});
server.listen();
server.on('listening', function() {
console.log("Server is listening");
});
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
Here is my mongo information which I configured in production.js
> getHostName( )
SAMLP15070001
> show dbs;
admin 0.078GB
local 0.078GB
test 0.078GB
I'm not sure whey is my deployd db is not getting displayed here.
Can someone help me in resolving this issue ?
From your mongo you have to execute the following commands:
mongo shell
use admin
db.addUser( { user: "deployd", pwd: "deployd", roles: [ "userAdminAnyDatabase" ] } )
use deployd
db.addUser( { user: "deployd", pwd: "deployd", roles: [ "readWrite", "dbAdmin" ] } )
I'm trying to set up a node.js application which uses node-orm2. However our cloud hosted DB can only be connected via SSH tunnel. Checking the ORM doc I can not see any config option to connect to the DB via SSH tunnel. Is there any way to set up this or I need to find some way to connect without SSH?
I updated the code example for tunnel-ssh 1.1.0, because it's actually the only working example on the internet (for so far i searched..).
It was quite a hassle to get this new tunnel-ssh configured...
var mysql = require('mysql');
var Tunnel = require('tunnel-ssh');
module.exports = function (server) {
return new Object({
tunnelPort: 33333, // can really be any free port used for tunneling
/**
* DB server configuration. Please note that due to the tunneling the server host
* is localhost and the server port is the tunneling port. It is because the tunneling
* creates a local port on localhost
*/
dbServer: server || {
host: '127.0.0.1',
port: 33333,
user: 'username',
password: 'yourpwd',
database: 'yourdb'
},
/**
* Default configuration for the SSH tunnel
*/
tunnelConfig: {
remoteHost: '127.0.0.1', // mysql server host
remotePort: 3306, // mysql server port
localPort: 33333, // a available local port
verbose: true, // dump information to stdout
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections)
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2)
host: 'your_tunneling_host',
port: 22,
username: 'user_on_tunneling',
password: 'pwd'
//privateKey: require('fs').readFileSync('<pathToKeyFile>'),
//passphrase: 'verySecretString' // option see ssh2 config
}
},
/**
* Initialise the mysql connection via the tunnel. Once it is created call back the caller
*
* #param callback
*/
init: function (callback) {
/* tunnel-ssh < 1.0.0
//
// SSH tunnel creation
// tunnel-ssh < 1.0.0
var me = this;
me.tunnel = new Tunnel(this.tunnelConfig);
me.tunnel.connect(function (error) {
console.log('Tunnel connected', error);
//
// Connect to the db
//
me.connection = me.connect(callback);
});
*/
/* tunnel-ssh 1.1.0 */
//
// SSH tunnel creation
//
var me = this;
// Convert original Config to new style config:
var config = this.tunnelConfig;
var newStyleConfig = {
username: config.sshConfig.username,
port: config.sshConfig.port,
host: config.sshConfig.host,
// SSH2 Forwarding...
dstPort: config.remotePort,
dstHost: config.remoteHost,
srcPort: config.localPort,
srcHost: config.localHost,
// Local server or something...
localPort: config.localPort,
localHost: config.localHost,
privateKey: config.privateKey
}
me.tunnel = tunnel(newStyleConfig, function (err) {
console.log('Tunnel connected', err);
if (err) {
return callback(err);
}
me.connection = me.connect(callback);
});
},
/**
* Mysql connection error handling
*
* #param err
*/
errorHandler: function (err) {
var me = this;
//
// Check for lost connection and try to reconnect
//
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('MySQL connection lost. Reconnecting.');
me.connection = me.connect();
} else if (err.code === 'ECONNREFUSED') {
//
// If connection refused then keep trying to reconnect every 3 seconds
//
console.log('MySQL connection refused. Trying soon again. ' + err);
setTimeout(function () {
me.connection = me.connect();
}, 3000);
}
},
/**
* Connect to the mysql server with retry in every 3 seconds if connection fails by any reason
*
* #param callback
* #returns {*} created mysql connection
*/
connect: function (callback) {
var me = this;
//
// Create the mysql connection object
//
var connection = mysql.createConnection(me.dbServer);
connection.on('error', me.errorHandler);
//
// Try connecting
//
connection.connect(function (err) {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
if (callback) callback();
});
return connection;
}
}
);
};
Finally it was resolved by dropping orm2 and using node-mysql and tunnel-ssh modules as in the code below.
var mysql = require('mysql');
var Tunnel = require('tunnel-ssh');
module.exports = function (server) {
return new Object({
tunnelPort: 33333, // can really be any free port used for tunneling
/**
* DB server configuration. Please note that due to the tunneling the server host
* is localhost and the server port is the tunneling port. It is because the tunneling
* creates a local port on localhost
*/
dbServer: server || {
host: '127.0.0.1',
port: 33333,
user: 'username',
password: 'yourpwd',
database: 'yourdb'
},
/**
* Default configuration for the SSH tunnel
*/
tunnelConfig: {
remoteHost: '127.0.0.1', // mysql server host
remotePort: 3306, // mysql server port
localPort: 33333, // a available local port
verbose: true, // dump information to stdout
disabled: false, //set this to true to disable tunnel (useful to keep architecture for local connections)
sshConfig: { //ssh2 configuration (https://github.com/mscdex/ssh2)
host: 'your_tunneling_host',
port: 22,
username: 'user_on_tunneling',
password: 'pwd'
//privateKey: require('fs').readFileSync('<pathToKeyFile>'),
//passphrase: 'verySecretString' // option see ssh2 config
}
},
/**
* Initialise the mysql connection via the tunnel. Once it is created call back the caller
*
* #param callback
*/
init: function (callback) {
//
// SSH tunnel creation
//
var me = this;
me.tunnel = new Tunnel(this.tunnelConfig);
me.tunnel.connect(function (error) {
console.log('Tunnel connected', error);
//
// Connect to the db
//
me.connection = me.connect(callback);
});
},
/**
* Mysql connection error handling
*
* #param err
*/
errorHandler: function (err) {
var me = this;
//
// Check for lost connection and try to reconnect
//
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
console.log('MySQL connection lost. Reconnecting.');
me.connection = me.connect();
} else if (err.code === 'ECONNREFUSED') {
//
// If connection refused then keep trying to reconnect every 3 seconds
//
console.log('MySQL connection refused. Trying soon again. ' + err);
setTimeout(function () {
me.connection = me.connect();
}, 3000);
}
},
/**
* Connect to the mysql server with retry in every 3 seconds if connection fails by any reason
*
* #param callback
* #returns {*} created mysql connection
*/
connect: function (callback) {
var me = this;
//
// Create the mysql connection object
//
var connection = mysql.createConnection(me.dbServer);
connection.on('error', me.errorHandler);
//
// Try connecting
//
connection.connect(function (err) {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
if (callback) callback();
});
return connection;
}
}
);
};
You can use the settings parameters to node-orm2 in order pass an options object to the underlying driver. For example, if you are using a mysql then you can pass an ssl option. See https://github.com/felixge/node-mysql#ssl-options in this case.
Thanks for the existing answers in this thread. The following solution worked for me:
function connect() {
return new Promise(async resolve => {
let tunnelPort = 33000 + Math.floor(Math.random() * 1000);
Tunnel({
//First connect to this server over ssh
host: '6.6.6.6',
username: 'vagrant',
privateKey: await fs.readFile('path/to/private_key'),
//And forward the inner dstPort (on which mysql is running) to the host (where your app is running) with a random port
dstPort: 3306,
localPort: tunnelPort
}, (err) => {
if (err) throw err;
console.log('Tunnel connected');
let connection = mysql.createConnection({
//Now that the tunnel is running, it is forwarding our above "dstPort" to localhost/tunnelPort and we connect to our mysql instance.
host: '127.0.0.1',
port: tunnelPort,
user: 'root',
password: 'password',
database: 'dbName'
});
connection.on('error', err => { throw err; });
connection.connect((err) => {
if (err) throw err;
console.log('Mysql connected as id ' + connection.threadId);
resolve(connection);
});
});
})
}
I'm trying to get a persistent connection from my socket.io-client (running on Node.js) to a remote websocket. I do not have control over the remote socket, and sometimes it can go down entirely. I would like to attempt to reconnect() whenever an error or disconnect occurs. In the following example, I'm trying to test the case where the remote host is refusing a connection. In this case, I would like to attempt to reconnect after 1 second. It calls a second time, and exits.
Here's the code:
var events = require('events'),
util = require('util'),
io = require('socket.io-client'),
url = "ws://localhost:12345", // intentionally an unreachable URL
socketOptions = {
"transports" : [ "websocket" ],
"try multiple transports" : false,
"reconnect" : false,
"connect timeout" : 5000
};
// The goal is to have this socket attempt to connect forever
// I would like to do it without the built in reconnects, as these
// are somewhat unreliable (reconnect* events not always firing)
function Test(){
var self = this;
events.EventEmitter.call(self);
var socket;
function reconnect(){
setTimeout(go, 1000);
}
function go(){
console.log("connecting to", url, socketOptions);
socket = io.connect(url, socketOptions);
socket.on('connect', function(){
console.log("connected! wat.");
});
socket.on('error', function(err){
console.log("socket.io-client 'error'", err);
reconnect();
});
socket.on('connect_failed', function(){
console.log("socket.io-client 'connect_failed'");
reconnect();
});
socket.on('disconnect', function(){
console.log("socket.io-client 'disconnect'");
reconnect();
});
}
go();
}
util.inherits(Test, events.EventEmitter);
var test = new Test();
process.on('exit', function(){
console.log("this should never end");
});
When running it under node 0.11.0 I get the following:
$ node socketio_websocket.js
connecting to ws://localhost:12345 { transports: [ 'websocket' ],
'try multiple transports': false,
reconnect: false,
'connect timeout': 5000 }
socket.io-client 'error' Error: connect ECONNREFUSED
at errnoException (net.js:878:11)
at Object.afterConnect [as oncomplete] (net.js:869:19)
connecting to ws://localhost:12345 { transports: [ 'websocket' ],
'try multiple transports': false,
reconnect: false,
'connect timeout': 5000 }
this should never end
The ECONNREFUSED is an exception you don't manage.
Try with this:
process.on('uncaughtException', function(err) {
if(err.code == 'ECONNREFUSED'){
reconnect();
}
}
Edit
Modify the options like this:
socketOptions = {
"transports" : [ "websocket" ],
"try multiple transports" : false,
"reconnect" : false,
'force new connection': true, // <-- Add this!
"connect timeout" : 5000
};
and the reconnect function (look in the comments for the explanation)
function reconnect(){
socket.removeAllListeners();
setTimeout(go, 1000);
}
Probably socket.io reuse the same connection without creating a new one, forcing it the app works