How to get the current server IP of request when multiple interfaces are used? - ip

Sorry, but I was not able to find the answer for this simple question after two attempts of googaling:
How can I get the current IP of the server connection a client is handled on? Is it possible to extract this somehow Meteor.status()?
(Background: we have different IPs which are served by the same instance. But we like to distinguish by the IP entry point.)

Meteor.status() don't have the information. But you can publish the IP address to the client or make a method that return it.
This is a pseudo-code and I don't know if this works. It's just an idea:
server.js:
var THIS_IP = '8.8.8.8'; // Try to get it dinamically with node or some npm package
Meteor.methods({
'serverIp': function () {
return THIS_IP;
}
});
client.js:
Meteor.call('serverIp', function (err, serverIp) { //do something with the IP });
EDIT: Sorry about this response. Just saw you have 2 IPs in same server. Maybe this hack can solve problems for now?
how to get external ip of the server from node.js

Related

How to identify the protocol of the Meteor connection

I've wondered how to identify the current protocol if it's using websocket or polling.
-- in the client. (appended for certainty)
I've found a valid information from the debug console.
Meteor.connection._stream.socket.protocol
and it seems to have one value among...
['websocket',
'xdr-streaming',
'xhr-streaming',
'iframe-eventsource',
'iframe-htmlfile',
'xdr-polling',
'xhr-polling',
'iframe-xhr-polling',
'jsonp-polling'];
is there more grace way to identify the current protocol?
and when would it be the fastest timing to detect the protocol?
By the way, I need it to use a different DDP server when the sticky sessions needed since AWS ELB doesn't support sticky sessions and websocket at the same time.
Meteor uses DDP protocol. To connect to a different Meteor server and call its methods, use DDP.connect as follows.
import { DDP } from 'meteor/ddp-client'
DDP.connect(url)
Unfortunately, there is no graceful to get the protocol. onConnection returns an object which has some info.
Meteor.onConnection(obj =>
{ console.log(obj.httpHeaders.x-forwarded-proto); });
This returns 'ws' for websocket. This way of getting the protocol is not graceful!
Meteor.status() gives a reactive data source.
(https://docs.meteor.com/api/connections.html#DDP-connect)
if (Meteor.isClient) {
Tracker.autorun(() => {
const stat = Meteor.status();
if (stat.status === 'connected') {
console.log(Meteor.connection._stream.socket.protocol);
}
});
}
something like that will give the current protocol in the client side.

External use of Meteor method? (to receive SMS from Nexmo)

In my Meteor application I want to receive text messages through Nexmo. How do I create the callback function? I'm thinking of something like
Meteor.methods
'receive_sms': (values) ->
console.log values
But http://hitchticker.meteor.com/receive_sms doesn't really work of course. I can see my method is working when I do Meteor.call('receive_sms', 'test') in my browser, but the network debugger is not really giving me a lot of useful information. The Meteor docs aren't very helpful either.
How do I access the method from elsewhere?
Iron Router and then server side routes. Something like:
Router.route('/download/:file', function () {
// NodeJS request object
var request = this.request;
// NodeJS response object
var response = this.response;
this.response.end('file download content\n');
}, {where: 'server'});
In order to receive sms from nexmo you should make the callback (incoming url) available over the internet. Nexmo won’t be able to call localhost to send the incoming sms messages.
Here are some resources to tunnel request over the internet to localhost.
https://ngrok.com/
http://localtunnel.me/
https://pagekite.net/

Meteor Server Websockets

I am looking to create a websocket on Meteor Server (not client) to connect to an external site. I know the URL I am going to be hitting as well as what data to expect, but I am unclear as to how exactly to create the websocket itself. All the searching I do presents me with solutions for the client, but I have yet to run into anything that serves as a server solution.
Is there anything out there I missed that fills this purpose? Atmosherejs.com doesn't list anything, and searching around on google/github didn't reveal anything either. Is there something built into Meteor that already accomplishes this?
The following code is for opening a Socket in Meteor on Port 3003. It convert the data from the socket (sendet from client) to a JSON-Object. So this means, the following code is a socket, which receive JSON.
Fiber = Npm.require('fibers')
// server
Npm.require('net').createServer(function (socket) {
console.log("connected");
socket.on('data', function (data) {
socket.write("hello!");
var o = JSON.parse(data.toString());
console.log(o);
Fiber(function() {
console.log('Meteor code is executing');
//=> Meteor code
}).run();
//console.log(data.toString());
//socket.close();
});
})
.listen(3003);

Storing client's IP address in Firebase

As far I know there is no information available from the browser to find out the client's IP address without making another call to another resource.
Is there a way to store a client's IP address? I mean in a similar way as the firebase.timestamp placeholder works.
Although I've heard that there are now ways for clients to determine and report their IP address, relying on client side code to report their IP address also opens up the ability for anyone to run custom client side code to report the wrong IP address.
The only secure way of keeping track of the clients' IP addresses would either involve you having a server, or Firebase having a special function, when called by the client, that causes firebase's server to grab the client's IP address and save it for you.
My recommendation would be to run a simple server that can take a POST request. The server only needs to be able to verify what user the request is coming from, and will be able to accurately grab the client's IP address and save it on firebase.
in the following URL
https://firebase.google.com/docs/auth/admin/manage-sessions
In section "Advanced Security: Enforce IP address restrictions"
you'll find a reference to the remoteIpAddress
in this piece of code (the row that the arrow points to):
app.post('/getRestrictedData', (req, res) => {
// Get the ID token passed.
const idToken = req.body.idToken;
// Verify the ID token, check if revoked and decode its payload.
admin.auth().verifyIdToken(idToken, true).then((claims) => {
// Get the user's previous IP addresses, previously saved.
return getPreviousUserIpAddresses(claims.sub);
}).then(previousIpAddresses => {
// Get the request IP address.
const requestIpAddress = req.connection.remoteAddress; <=============================
// Check if the request IP address origin is suspicious relative to previous
// IP addresses. The current request timestamp and the auth_time of the ID
// token can provide additional signals of abuse especially if the IP address
// suddenly changed. If there was a sudden location change in a
// short period of time, then it will give stronger signals of possible abuse.
if (!isValidIpAddress(previousIpAddresses, requestIpAddress)) {
// Invalid IP address, take action quickly and revoke all user's refresh tokens.
revokeUserTokens(claims.uid).then(() => {
res.status(401).send({error: 'Unauthorized access. Please login again!'});
}, error => {
res.status(401).send({error: 'Unauthorized access. Please login again!'});
});
} else {
// Access is valid. Try to return data.
getData(claims).then(data => {
res.end(JSON.stringify(data);
}, error => {
res.status(500).send({ error: 'Server error!' })
});
}
});
});

Flex RTMP client

I'm trying to write an rtmp client with flex. The problem is that I have no idea where to start. Could someone please answer how I would go about doing this, or point me to a site that does? Thanks. I don't even know what classes to use and such.
See Wiki Real Time Messaging Protocol as a starting point
and there are few sample clients
JUV RTMP Client
php-rtmp-client
hopes that works
You don't have to write your own RTMP client because Flash already implements a RTMP client called NetConnection.
To create a basic connection you could do the following:
var nc:NetConnection = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect('rtmp://127.0.0.1/application');
function onNetStatus(e:NetStatusEvent):void
{
switch (e.info.code)
{
case 'NetConnection.Connect.Success':
// Connection with the RTMP server has been established
break;
case '...':
...
}
}
The code 'NetConnection.Connect.Success' you see is one of the codes the server returns, have a look over here for an overview of all the codes.
You should probably read up on the documentation first and then come back with a more precise question.

Resources