Ruby-on-rails 在本地 Rails 开发环境中获取真实 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3887943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 23:21:05  来源:igfitidea点击:

Get real IP address in local Rails development environment

ruby-on-railsip-addressgeocoding

提问by gkolan

I have Rails 2.3.8, Ruby 1.8.7, Mongrel Web Server and MySQL database.

我有 Rails 2.3.8、Ruby 1.8.7、Mongrel Web Server 和 MySQL 数据库。

I am in the development modeand I need to find the real IP address

我在开发模式,我需要找到真实的IP地址

When I use request.remote_ipI am getting the IP as 127.0.0.1

当我使用时,request.remote_ip我获得的 IP 为127.0.0.1

I know I am getting 127.0.0.1because I am developing on the local machine.. but is there a way to get the real ip-address even if I am on the local machine?

我知道我得到了,127.0.0.1因为我在本地机器上开发..但是即使我在本地机器上,有没有办法获得真正的 ip 地址?

I am using these mentioned below in my controller and all I get is 127.0.0.1with all of them in the view.

我在我的控制器中使用了下面提到的这些,我得到的只是127.0.0.1在视图中所有这些。

request.remote_ip
request.env["HTTP_X_FORWARDED_FOR"]
request.remote_addr
request.env['REMOTE_ADDR']

采纳答案by Mischa

As far as I can see there is no standard method for getting the remote address of your local machine. If you need the remote address for (testing) your geocoding, I suggest adding 127.0.0.1 to your database table that matches IP addresses with locations.

据我所知,没有获取本地机器远程地址的标准方法。如果您需要远程地址来(测试)您的地理编码,我建议将 127.0.0.1 添加到您的数据库表中,以将 IP 地址与位置相匹配。

As a last resort you could also hard code your remote address. E.g. like this:

作为最后的手段,您还可以硬编码您的远程地址。例如像这样:

class ApplicationController < ActionController::Base
  def remote_ip
    if request.remote_ip == '127.0.0.1'
      # Hard coded remote address
      '123.45.67.89'
    else
      request.remote_ip
    end
  end
end

class MyController < ApplicationController
  def index
    @client_ip = remote_ip()
  end
end

回答by Swanand

I don't think you will get your 'real-ip' on localhost and the reason for this actually lies in TCP/IP.

我认为您不会在本地主机上获得“真实 IP”,其原因实际上在于 TCP/IP。

The remote ip address depends upon the network the request is being sent to. Since it is a part of TCP/IP to send your IP address when communicating, it always translates to relative IP address that the destination computer may understand. When the request is within your subnet, it is your local IP. The moment it goes out to the network through your service provider, it assumes a global IP, which can be tracked back to the service provider (Note: this is arguable and depends on your network setup).

远程 IP 地址取决于请求发送到的网络。由于在通信时发送 IP 地址是 TCP/IP 的一部分,因此它始终转换为目标计算机可以理解的相对 IP 地址。当请求在您的子网内时,它是您的本地 IP。当它通过您的服务提供商进入网络时,它假定一个全局 IP,可以追溯到服务提供商(注意:这是有争议的,取决于您的网络设置)。

So if you are testing locally then it would be 127.0.0.1 as you have experienced. If you are sending it over your local network, it'll be your IP within that network. For example, my machine's IP in my office network is 192.168.1.7, and if I access my app at 3000 port through another computer in the same network, say from 192.168.1.13, then I get the request.remote_ip as 192.168.1.13

因此,如果您在本地进行测试,那么它将是您所经历的 127.0.0.1。如果您通过本地网络发送它,它将是您在该网络中的 IP。例如,我的机器在我的办公网络中的 IP 是 192.168.1.7,如果我通过同一网络中的另一台计算机在 3000 端口访问我的应用程序,比如说从 192.168.1.13,那么我得到的 request.remote_ip 为 192.168.1.13

What this is means is, while you are on localhost, 127.0.0.1 isyour real IP. And likewise in the example I mentioned, 192.168.1.13 is the real IP for the machine that sent the request.

这意味着,当您在本地主机上时,127.0.0.1您的真实 IP。同样在我提到的示例中,192.168.1.13 是发送请求的机器的真实 IP。

回答by BSB

This is what I normally do nowadays:

这就是我现在通常做的:

if Rails.env.production?
  request.remote_ip
else
  Net::HTTP.get(URI.parse('http://checkip.amazonaws.com/')).squish
end

回答by letronje

Depends on how you access the url.

取决于您访问网址的方式。

If you access the url using http://127.0.0.1/....or http://localhost/...., you'll get 127.0.0.1as the remote ip. If you are on a LAN, use http://{lan-ip}/....

如果您使用http://127.0.0.1/....或访问网址http://localhost/....,您将获得127.0.0.1远程 IP。如果您在 LAN 上,请使用 http://{lan-ip}/....

for e.g. http://172.20.25.210/.......

例如 http://172.20.25.210/.......

回答by Mitch VanDuyn

I had to do this:

我不得不这样做:

require 'socket'
Socket.ip_address_list.detect(&:ipv4_private?)&.ip_address

回答by Tsvetelina Borisova

I am testing with Rspec and what I did was:

我正在使用 Rspec 进行测试,我所做的是:

request.stub(:remote_ip).and_return('198.172.43.34')
Instance.stub(:find_by_key).and_return(@instance)
before = @instance.weekly_statistic.times_opened_by['198.172.43.34']

get :key, :key=>"314C00"

@instance.weekly_statistic.times_opened_by['198.172.43.34'].should == before+1

And it worked like a charm! : )

它就像一个魅力!:)

回答by Trevor Elwell

Problem:

问题:

We were trying to do a similar thing for a project recently and were having trouble using request.remote_ipin our code. It kept returning 127.0.0.1or ::1. When combined with the Geocoder gem it was giving us an empty array and was basically useless.

我们最近试图为一个项目做类似的事情,但request.remote_ip在我们的代码中使用时遇到了问题。它不断返回127.0.0.1::1。当与 Geocoder gem 结合时,它给了我们一个空数组,基本上没用。

Solution:

解决方案:

There's a really nice API at telize.comwhich returns the IP address of whatever hits it. The code we implemented looked something like this:

telize.com 上有一个非常好的 API,它返回任何命中它的 IP 地址。我们实现的代码如下所示:

location = Geocoder.search(HTTParty.get('http://www.telize.com/ip').body)

location = Geocoder.search(HTTParty.get('http://www.telize.com/ip').body)

It's not the most rails-y way to do it, but I think this is a pretty good solution since it will work locally and on production as well. The only potential problem is if you hit their API limit, but for small projects this should be a non-issue.

这不是最棒的方式,但我认为这是一个很好的解决方案,因为它可以在本地和生产环境中工作。唯一的潜在问题是您是否达到了他们的 API 限制,但对于小型项目,这应该不是问题。

回答by Jan M

If you access the development machine with your real IP you should get your real IP, so don't use localhost, but your use your real IP. Not all routers are will allow LAN access to a machine to an external IP address that is actually on that LAN, but most will.

如果你使用真实IP访问开发机器,你应该得到你的真实IP,所以不要使用localhost,而是使用你的真实IP。并非所有路由器都允许 LAN 访问机器到实际位于该 LAN 上的外部 IP 地址,但大多数路由器都允许。