node.js how to get client hostname by using dns - node.js

i'm trying to get client hostname by getting their ip and then use dns.reverse to do the thing, it works, i'm getting the ip but i don't client hostname. Am i misunderstand something or is there the better way to do.
queue2: function(req, res){
var mac = req.param('mac');
var ip = req.connection.remoteAddress;
dns.lookup(ip,function onLookup(err, addresses){
console.log('Address :' + addresses);
dns.reverse(addresses, function(err, hostname){
Q_list.query("CALL GetQueuePlay('00')", function (err, result) {
if (err){
return res.send(err);
}
console.log(result);
console.log(addresses+ ' : ' + JSON.stringify(hostname));
return res.view('kue/kue',{q_play_list:result});
});
});
});
},

Related

softlayer-client: unable to update VPN for User_Customer via REST API

I am experimenting with the softlayer-client api wrapper in my Node Express application. My goal is to update the VPN password of a User_Customer by calling the updateVpnPassword method on a specific user.
I can construct a call to achieve a VPN password update using request, but I'm not sure it's the best way to achieve the desired result.
Can the softlayer-client module be used to make an similar call to this:
function updateVpnPassword(req, res, next) {
// Construct URL to update VPN
myURL = 'https://' + <userIDAdmin> + ':' + <apiKeyAdmin> + '#api.softlayer.com/rest/v3/SoftLayer_User_Customer/' + <softLayerID> + '/updateVpnPassword/' + <newPassword> + '.json';
request.get({url: myURL}, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
next();
}
My initial attempts have been to try variations on this:
function updateVpnPassword(req, res, next) {
// Assuming var client = new SoftLayer();
client
.auth(<userIDAdmin>, <apiKeyAdmin>)
.path('User_Customer', <softLayerID>,'updateVpnPassword')
.parameters(<newPassword>)
.put(function(err,result){
console.log(result);
if (err) {
next(err); // Pass errors to Express.
}
else {
// update successful
}
});
next();
}
But the console log gives an error response like
{ message: { error: 'Internal Error', code: 'SoftLayer_Exception_Public' } }.
I expect a TRUE or FALSE response, to indicate the whether the update is successful.
A similar python client can be found here but I require an implementation in JS.
I'm not familiar with nodejs but I installed the package softlayer-node and run your second code and it worked.
I also created the following script and I got TRUE
var username = 'set me';
var apikey = 'set me';
var userId = 1111111;
var SoftLayer = require('softlayer-node');
var client = new SoftLayer();
client
.auth(username, apikey)
.path('User_Custome', userId, 'updateVpnPassword')
.parameters('P#ssword123')
.put()
.then(function(result) {
console.log(result);
}, function(error) {
console.log(error);
});
node command:
$ node updateVpnPassword.js
true
Did you tried by sending that request using curl or any other REST client like postman?
If you get the same error then I recommend you submit a ticket and provide information like the id of users you are trying to update the vpn password and the user with which you are sending the request.

Q: Hot to change Ip in request using tor-request npm

i'm trying to change Ip by using tor
According to Tor-Request Documentation I should be able to do this simply by using
newTorSession
But ip not changing. What is wrong in my code?
var tr = require('tor-request');
requestIP();
tr.setTorAddress('localhost', 9050);
tr.newTorSession( (err) =>
{
console.log (err);
requestIP();
return;
});
//console.log (tr.TorControlPort)
function requestIP() {
tr.request('https://api.ipify.org', function (err, res, body) {
if (!err && res.statusCode == 200) {
console.log("Your public (through Tor) IP is: " + body);
}
})
}
From the doc
You need to enable the Tor ControlPort if you want to programmatically
refresh the Tor session (i.e., get a new proxy IP address) without
restarting your Tor client.
so you need to follow these steps to enable ControlPort then pass that password to tor-request
tr.TorControlPort.password = 'PASSWORD'

received status code 400 invalid value 'ipaddress' for no_key

i use chef-api for express js and i wanna get only the ip address of node " server 1 " from a chef-server
im sending a request like this
Code :
chef.partialSearch("node", "name:server1",{"ip":"ipaddress"} ,function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Or
chef.partialSearch("node", { q: "name:server1"} ,{"ip":"ipaddress"} ,function(err, res){
....
});
=> Response :
received status code 400 invalid value 'ipaddress' for no_key
function in code source :
partialSearch: function(index, qs, data, fn){
http_methods.post([config.host_url, "search", index].join("/"), qs, data, function(err, response){
return fn(err, response);
});
}
and i cant understand the correct syntax for the request (http) from the official website doc api_chef_server
Can you please give a valid syntax with example .
Thanks
What you probably want is something like this:
chef.getNode('server1', function(err, node) {
if(err) throw err;
console.log(node.automatic.ipaddress);
});
Finaly i found the correct syntax for both request
Simple Search :
chef.search("node", {q: "name:server1" }, function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Partial Search :
chef.partialSearch("node", "chef_environment:prod", { name: ['name'] , 'ipaddress': ['ipaddress'] }, function(err, res){
if (err){console.log(error);}
else{console.log(res);}
});
Hope this can help someone else who is still looking .

Openshift - auth: couldn't find user while connecting mongodb via mongojs

I am following this tutorial to host a Rest API at my openshift account:
Day 27: Restify–Build Correct REST Web Services in Node.js
As per my requirements I made few changes, below is the code:
server.js
var restify = require('restify');
var mongojs = require('mongojs');
var ip_addr = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
var server = restify.createServer({name: 'bemeta'});
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.use(restify.CORS());
server.listen(port, ip_addr, function(){
console.log(server.name + ' listening at url ' + server.url);
});
var connectionString = '127.0.0.1:27017/bemeta';
if(process.env.OPENSHIFT_MONGODB_DB_PASSWORD){
connectionString = process.env.OPENSHIFT_MONGODB_DB_USERNAME + ':' +
process.env.OPENSHIFT_MONGODB_DB_PASSWORD + '#' +
process.env.OPENSHIFT_MONGODB_DB_HOST + ':' +
process.env.OPENSHIFT_MONGODB_DB_PORT + '/' +
process.env.OPENSHIFT_APP_NAME;
}
var db = mongojs(connectionString, ['bemeta']);
var beRecords = db.collection("be_records");
var PATH = '/be_records';
// get all records
server.get({path: PATH, version:'0.0.1'}, findAllRecords);
// create new record
server.post({path: PATH, version:'0.0.1'}, createRecord);
// defining call backs
function findAllRecords(req, res, next){
res.setHeader('Access-Control-Allow-Origin', '*');
beRecords.find(function(err, docs){
console.log('Response docs ' + docs);
console.log('Error '+ err);
if(docs){
res.send(200, docs);
return next();
}
return next(err);
});
}
function createRecord(req, res, next){
res.setHeader('Access-Control-Allow-Origin', '*');
var beRecord = {};
beRecord.be_id = req.params.be_id;
beRecord.item_title = req.params.item_title;
beRecord.item_description = req.params.item_description;
beRecord.item_link = req.params.item_link;
beRecords.save(beRecord, function(err, docs){
console.log('Response docs ' + docs);
console.log('Error '+ err);
if(docs){
res.send(201, beRecord);
return next();
}
else {
next(err);
}
});
}
When I run it at local and fire a get request via postman by typing below url:
http://127.0.0.1:8080/be_records
I get response as: [], which is correct since I have not inserted any document in respective collection.
But when I run it at my openshift account and fire a get request via postman by typing below url:
http://bemeta-{domain-name}.rhcloud.com/be_records
I get response as:
{
"code": "InternalError",
"message": "auth fails"
}
In my rhc console I see below messages:
==> app-root/logs/mongodb.log <==
Wed Feb 24 01:32:23.543 [conn3] authenticate db: bemeta { authenticate: 1, user: "admin", nonce: "some nonce", key: "some key" }
Wed Feb 24 01:32:23.544 [conn3] auth: couldn't find user admin#bemeta, bemeta.system.users
I am not sure why is it trying to find user - admin#bemeta, might be I am missing something over here, I do not have any such user but I do have an admin user which is used to connect to mongodb.
Here is the snapshot of my db (as shown in RockMongo):
Any ideas?
The auth error was caused by not having the admin user authenticated for accessing the bemeta database (see question comments above for details).
Users can be authenticated using the RockMongo interface as follows:
Select a DB -> More -> Authentication -> Add user.

Cannot insert record to PostgreSQL in Bluemix

I try to get postgresql work with Node.js in Bluemix platform. However, there is a problem. I tried to use the code described in https://www.ng.bluemix.net/docs/#starters/nodejs/index.html#PostgreSQL.
I can run the app I written successfully in local environment. However, it is not functioning properly in Bluemix platform.
Following is the code describing what I have done:
var recordVisit = function(req, res) {
/* Connect to the DB and auth */
pg.connect(psql,
function(err, client, done) {
if (err) {
return console.error('Error requesting client', err);
}
console.log(req.ip);
client.query('insert into ips(ip, ts) values($1, $2)', [req.ip, new Date()],
function(err, result) {
if (err) {
done();
return console.error('Error inserting ip', err);
}
client.query('select count(ip) as count from ips where ip=$1',
[req.ip],
function(err, result) {
done();
if (err) {
return console.error('Error querying count', err);
}
console.log("You have visited " + result.rows[0].count + " times")
// res.writeHead(200, {'Content-Type': 'text/html'});
// res.end();
}
);
}
);
}
);
};
It is following the code from the bluemix document. The result of the visit count in Bluemix always be zero (0). But the result in local environment is okay. And I did not get any error from the Bluemix platform. So I assume that the visit record can be inserted. However, seems that it is not.
Can anyone have any pointer for me to solve this problem?
I tried to insert more logging into the insert function. I found that the variable req.ip becomes undefined in the result of INSERT SQL in Bluemix platform.
So I added a local variable to store the ip of the request in order to keep the content.

Resources