There is a mechanism in node to get the remote IP by calling req.connection.remoteAddress.
I tried the below code to get the remote IP of the client but the result is undefined.
var ipAddr = req.headers["x-forwarded-for"];
if (ipAddr){
var list = ipAddr.split(",");
ipAddr = list[list.length-1];
} else {
ipAddr = req.connection.remoteAddress;
}
console.log("req.connection.remoteAddress:" + ipAddr);
Does anyone know if I can get the remoteIP in Parse.com cloud code?
Related
I have a service hosted on Google Cloud Run. The service uses socket io whenever the service is up and running.
When a socket client connects to the service I have the following function that gets the ip address of the connected client from the socket as shown below and then I am hitting this GeoPlugin Link with the retrieved IP
async getSocketIP(socket) {
let { headers, address } = socket.handshake;
let { origin } = headers;
let ip = headers['x-forwarded-for'];
let userAgent = headers['user-agent'];
try {
let locationPointUrl = `http://www.geoplugin.net/json.gp?ip=${ip}`;
let { data: location } = await axios.get(locationPointUrl);
} catch (e) {
console.log(`Error get client online IP on Socket IO`);
}
}
Unfortunately, irrespective of the User's Location the IP always resolves to US.
I have a custom domain mapped to the cloud run service via Domain Mapping.
What could be the reason the IP of the Client is always US IP?
Please note that this same service when hosted on Heroku gets the correct IP address of the connected client.
So, I'm very certain that it has something to do with Cloud Run.
All my services on Cloud Run are on US-CENTRAL1
For anyone who may experience something like this in the future.
We had Cloudflare sitting in front of Cloud Run.
So, to get the correct Client's IP address all we had to do was retrieve it from cf-connecting-ip header instead of x-forwarded-for.
So, the modified and working code now becomes:
async getSocketIP(socket) {
let { headers, address } = socket.handshake;
let { origin } = headers;
let ip = headers['cf-connecting-ip'] ?? headers['x-forwarded-for']; //Notice the difference
let userAgent = headers['user-agent'];
try {
let locationPointUrl = `http://www.geoplugin.net/json.gp?ip=${ip}`;
let { data: location } = await axios.get(locationPointUrl);
} catch (e) {
console.log(`Error get client online IP on Socket IO`);
}
}
I'm using https://github.com/farhadi/node-smpp in order to create a smpp server.
I'm going to forbid connection if the client's IP address is not in the allowed ips list. For that when a new connection is open I have to check if the credentials are ok and if the IP address is a good one.
The question is how and where can I get the client (ESME's) IP address?
session.on('bind_transceiver', function(pdu) {
session.pause();
const username = pdu.system_id;
const password = pdu.password;
const ipAddress = ''; // WHERE TO GET IT??
if (credentialsAreOk(username, password, ipAddress)) {
session.send(pdu.response());
session.resume();
} else {
session.close();
}
});
When an ESME is connecting to your server, a session is created.
The network socket used by this TCP connection, which is a net.Socket class (https://nodejs.org/api/net.html#net_class_net_socket), is stored inside this session in the socket property.
const socket = session.socket;
So you can easily access this socket property of the session and get the remoteAddress (the clients IP) from there (https://nodejs.org/api/net.html#net_socket_remoteaddress).
const ipAddress = session.socket.remoteAddress;
I'm trying to expose a JSON as a POST request, where I'm trying to append the base-url with another value.
How could I get the base url value?
I tried using:
var root = RED.settings.httpNodeRoot;
but then it returned only /, where as I'm expecting something like http://localhost:1880.
Is it possible to get the base url by using any node-red api? Any help could be appreciated.
It's not directly available, but you can assemble it from some of the available parts.
Have a look at the subscribe function in the Wemo nodes
Basically you can get the port and the path from the RED.settings object, but the IP address very much depends on the machine you are running on. By default Node-RED binds to 0.0.0.0 (which is short hand for all available IP addresses).
If you are running on NodeJS newer than 0.12.x then you can get hold of the IP address of the default route which is normally a fair guess. For NodeJS 0.10.x you pretty much just have to guess.
var ipAddr;
//device.ip
var interfaces = os.networkInterfaces();
var interfaceNames = Object.keys(interfaces);
for (var name in interfaceNames) {
if (interfaceNames.hasOwnProperty(name)) {
var addrs = interfaces[interfaceNames[name]];
for (var add in addrs) {
if (addrs[add].netmask) {
//node 0.12 or better
if (!addrs[add].internal && addrs[add].family == 'IPv4') {
if (ip.isEqual(ip.mask(addrs[add].address,addrs[add].netmask),ip.mask(device.ip,addrs[add].netmask))) {
ipAddr = addrs[add].address;
break;
}
}
} else {
//node 0.10 not great but best we can do
if (!addrs[add].internal && addrs[add].family == 'IPv4') {
ipAddr = addrs[add].address;
break;
}
}
}
if (ipAddr) {
break;
}
}
}
var callback_url = 'http://' + ipAddr + ':' + settings.uiPort;
if (settings.httpAdminRoot) {
callback_url += settings.httpAdminRoot;
}
Looking at this code reminds me I have to add a fix for if HTTPS has been enabled....
Tried the following as per one suggestion;
static void Main()
{
string url = "http://localhost:8080/";
StartOptions options = new StartOptions();
options.Urls.Add("http://localhost:8080");
options.Urls.Add("http://127.0.0.1:8080");
options.Urls.Add(string.Format("http://{0}:8080", Environment.MachineName));
using (WebApp.Start<Startup>(options))
{
HttpClient client = new HttpClient();
var response = client.GetAsync(url + "api/Values").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
Console.ReadLine();
}
}
Can connect with local browser, but remote machines get 'webpage is not available'.
Set reservation with 'add urlacl url=http://*:8080/ user=EVERYONE'
Port 8080 is open, tried many other ports with same results.
tried suggestion below, same result;
var options = new StartOptions("http://*:8080")
{
ServerFactory = "Microsoft.Owin.Host.HttpListener"
};
Nothing is working remotely, only locally - is there anything else I can try?
I've wrote a self-hosted servicestack server and a client, both desktop applications.
In my very basic PING test service I'm trying to retrieve the IP of the client.
Server is on 192.168.0.87:82, client I tried on the same computer and on another computer, but RemoteIp and UserHostAddress always return 192.168.0.87:82. XRealIp is null.
I also tried base.Request.RemoteIp, but still is 192.168.0.87:82.
What am I doing wrong?
public RespPing Any(ReqPing request)
{
string IP = base.RequestContext.Get<IHttpRequest>().RemoteIp;
string MAC = request.iTransactionInfo.MAC;
Log(MAC,IP, base.RequestContext.Get<IHttpRequest>().RemoteIp + base.RequestContext.Get<IHttpRequest>().XRealIp + base.RequestContext.Get<IHttpRequest>().UserHostAddress);
RespPing response = new RespPing { Result = "PONG" };
return response;
}
Thanks!
Made it with:
HttpListenerRequest iHttpListenerRequest = (HttpListenerRequest)base.RequestContext.Get<IHttpRequest>().OriginalRequest;
string IP = iHttpListenerRequest.RemoteEndPoint.ToString().Split(':')[0];
Request.RemoteIP kept giving me the server address.
ServiceStack.4.0.38
string IP = base.Request.RemoteIp;
fwiw I had to do something just a little bit different:
HttpListenerRequest iHttpListenerRequest = (HttpListenerRequest)base.RequestContext.Get<IHttpRequest>().OriginalRequest;
string ip = iHttpListenerRequest.RemoteEndPoint.Address.ToString();