module EricWeixin::Ip

Constants

IPLIST

Public Class Methods

is_ip_exist?(options) click to toggle source

判断给定 ip 是否是正确的微信服务器 ip.

参数说明

  • ip # ip地址

  • public_account_id #公众号 id

调用示例

Ip.is_ip_exist? ip: “127.0.0.1”, public_account_id: 1

# File lib/eric_weixin/modules/ip.rb, line 11
def self.is_ip_exist? options
  return true    #TODO 腾讯ip有时候不能获取正常的列表,需要调整研究
  options[:ip].to_s.to_debug
  return true if IPLIST[:iplist].include? options[:ip]
  get_ip_list options
  return true if IPLIST[:iplist].include? options[:ip]
  return false
end

Private Class Methods

get_ip_list(options) click to toggle source

根据公众号 ID 获取微信服务器 IP 列表.

参数说明

  • public_account_id #公众账号 ID

调用示例

EricWeixin::Ip.get_ip_list public_account_id: 1

# File lib/eric_weixin/modules/ip.rb, line 26
def self.get_ip_list options
  return if IPLIST[:update_at] > 1.minutes.ago
  token = ::EricWeixin::AccessToken.get_valid_access_token public_account_id: options[:public_account_id]
  ips = RestClient.get "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=#{token}"
  ips = JSON.parse ips.body
  ips.to_debug
  IPLIST[:iplist] =  ips["ip_list"]
  IPLIST[:update_at] = Time.now
end