构建获取IP的中间件



我正在尝试编写我的第一个机架中间件。需要帮助。我希望中间件能够找到请求者的IP,如果它在允许的IP列表中,则继续请求,否则它将终止。

棘手的部分是我需要这个工作在heroku,你不能使用请求。知识产权:http://developerhemal.tumblr.com/post/3958107290/client-ip-addresses-on-heroku

所以我有以下内容:

class RestrictIP
  def initialize(app, message = "Hello")
    @app = app
    @message = message
  end
  def call(env)
    dup._call(env)
    @ip = env['HTTP_X_REAL_IP'] ||= env['REMOTE_ADDR']
  end
  def each(&block)
    block.call("<!-- #{@ip} -->n")
    @response.each(&block)
  end
end

错误:

NoMethodError (You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]=):

对于我的第一次迭代,我只想确保我可以抓取并将请求者的IP放在html文档上。

有机架中间件专家吗?由于

根据这篇文章,你似乎可以使用以下内容:

ip = env[‘HTTP_X_REAL_IP’] ||= env[‘REMOTE_ADDR’]

我还没有测试过这个,因为我不在Heroku上。

最新更新