Geocoder,当ip为127.0.0.1时如何在本地测试?

发布于 2024-11-09 04:31:54 字数 153 浏览 4 评论 0原文

我无法让地理编码器正常工作,因为我的本地 IP 地址是 127.0.0.1,因此它无法正确定位我所在的位置。

request.location.ip 显示“127.0.0.1”

我如何使用不同的 IP 地址(我的互联网连接 IP),以便它会带来更多相关数据?

I can't get geocoder to work correct as my local ip address is 127.0.0.1 so it can't located where I am correctly.

The request.location.ip shows "127.0.0.1"

How can I use a different ip address (my internet connection ip) so it will bring break more relevant data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

晚雾 2024-11-16 04:31:54

一个很好的干净方法是使用 MiddleWare。将此类添加到您的 lib 目录:

# lib/spoof_ip.rb

class SpoofIp
  def initialize(app, ip)
    @app = app
    @ip = ip
  end

  def call(env)
    env['HTTP_X_FORWARDED_FOR'] = nil
    env['REMOTE_ADDR'] = env['action_dispatch.remote_ip'] = @ip
    @status, @headers, @response = @app.call(env)
    [@status, @headers, @response]
  end
end

然后找到您想要用于开发环境的 IP 地址并将其添加到您的development.rb 文件中:

config.middleware.use('SpoofIp', '64.71.24.19')

A nice clean way to do it is using MiddleWare. Add this class to your lib directory:

# lib/spoof_ip.rb

class SpoofIp
  def initialize(app, ip)
    @app = app
    @ip = ip
  end

  def call(env)
    env['HTTP_X_FORWARDED_FOR'] = nil
    env['REMOTE_ADDR'] = env['action_dispatch.remote_ip'] = @ip
    @status, @headers, @response = @app.call(env)
    [@status, @headers, @response]
  end
end

Then find an IP address you want to use for your development environment and add this to your development.rb file:

config.middleware.use('SpoofIp', '64.71.24.19')
清旖 2024-11-16 04:31:54

为此,我通常使用 params[:ip] 或正在开发的东西。这使我能够测试其他 IP 地址的功能并假装我在世界任何地方。

例如

class ApplicationController < ActionController::Base
  def request_ip
    if Rails.env.development? && params[:ip]
      params[:ip]
    else
      request.remote_ip
    end 
  end
end

For this I usually use params[:ip] or something in development. That allows me to test other ip addresses for functionality and pretend I'm anywhere in the world.

For example

class ApplicationController < ActionController::Base
  def request_ip
    if Rails.env.development? && params[:ip]
      params[:ip]
    else
      request.remote_ip
    end 
  end
end
薄暮涼年 2024-11-16 04:31:54

我的实现略有不同,这对我的情况很有效。

application_controller.rb中,我有一个查找方法,它调用Geocoder IP查找,直接传入request.remote_ip的结果。

def lookup_ip_location
  if Rails.env.development?
    Geocoder.search(request.remote_ip).first
  else
    request.location
  end
end

然后在 config/environments/development.rb 中,我对remote_ip 调用进行了猴子修补:

class ActionDispatch::Request
  def remote_ip
    "71.212.123.5" # ipd home (Denver,CO or Renton,WA)                                                                                                                                                                                                                                                                        
    # "208.87.35.103" # websiteuk.com -- Nassau, Bahamas                                                                                                                                                                                                                                                                      
    # "50.78.167.161" # HOL Seattle, WA                                                                                                                                                                                                                                                                                       
  end
end

我只是硬编码了一些地址,但您可以在这里做任何您想做的事情。

I implemented this slightly different, and this works well for my case.

In application_controller.rb i have a lookup method which calls the Geocoder IP lookup directly passing in the results of request.remote_ip.

def lookup_ip_location
  if Rails.env.development?
    Geocoder.search(request.remote_ip).first
  else
    request.location
  end
end

Then in config/environments/development.rb i monkey-patched the remote_ip call:

class ActionDispatch::Request
  def remote_ip
    "71.212.123.5" # ipd home (Denver,CO or Renton,WA)                                                                                                                                                                                                                                                                        
    # "208.87.35.103" # websiteuk.com -- Nassau, Bahamas                                                                                                                                                                                                                                                                      
    # "50.78.167.161" # HOL Seattle, WA                                                                                                                                                                                                                                                                                       
  end
end

I just hard code some addresses, but you could do whatever you'd like here.

忘羡 2024-11-16 04:31:54

我也有同样的问题。这是我使用地理编码器实现的方法。

#gemfile
gem 'httparty', :require => 'httparty', :group => :development 

#application_controller
def request_ip
  if Rails.env.development? 
     response = HTTParty.get('http://api.hostip.info/get_html.php')
     ip = response.split("\n")
     ip.last.gsub /IP:\s+/, ''      
   else
     request.remote_ip
   end 
end

#controller
ip = request_ip
response = Geocoder.search(ip)

(使用 geo_magic gem 中的 hostip.info 进行代码部分,并基于此问题的其他答案。)

现在您可以执行类似 response.first.state 的操作

I had the same question. Here is how I implemented with geocoder.

#gemfile
gem 'httparty', :require => 'httparty', :group => :development 

#application_controller
def request_ip
  if Rails.env.development? 
     response = HTTParty.get('http://api.hostip.info/get_html.php')
     ip = response.split("\n")
     ip.last.gsub /IP:\s+/, ''      
   else
     request.remote_ip
   end 
end

#controller
ip = request_ip
response = Geocoder.search(ip)

( code part with hostip.info from geo_magic gem, and based on the other answer to this question. )

now you can do something like response.first.state

倾`听者〃 2024-11-16 04:31:54

这是地理编码器 1.2.9 的更新答案,为开发和测试环境提供硬编码 IP。只需将其放在 config/initilizers/geocoder.rb 的底部即可:

if %w(development test).include? Rails.env
  module Geocoder
    module Request
      def geocoder_spoofable_ip_with_localhost_override
        ip_candidate = geocoder_spoofable_ip_without_localhost_override
        if ip_candidate == '127.0.0.1'
          '1.2.3.4'
        else
          ip_candidate
        end
      end
      alias_method_chain :geocoder_spoofable_ip, :localhost_override
    end
  end
end

This is an updated answer for geocoder 1.2.9 to provide a hardcoded IP for development and test environments. Just place this at the bottom of your config/initilizers/geocoder.rb:

if %w(development test).include? Rails.env
  module Geocoder
    module Request
      def geocoder_spoofable_ip_with_localhost_override
        ip_candidate = geocoder_spoofable_ip_without_localhost_override
        if ip_candidate == '127.0.0.1'
          '1.2.3.4'
        else
          ip_candidate
        end
      end
      alias_method_chain :geocoder_spoofable_ip, :localhost_override
    end
  end
end
り繁华旳梦境 2024-11-16 04:31:54

你也可以这样做

request.safe_location

you may also do this

request.safe_location
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文