Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions lib/ipinfo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ def details_v6(ip_address = nil)
details_base(ip_address, :v6)
end

def resproxy(ip_address)
cache_key_str = "resproxy:#{ip_address}"
res = @cache.get(cache_key(cache_key_str))
return Response.new(res) unless res.nil?

response = @httpc.get("/resproxy/#{CGI.escape(ip_address)}", :v4)

if response.status.eql?(429)
raise RateLimitError,
RATE_LIMIT_MESSAGE
end

details = JSON.parse(response.body, symbolize_names: true)
@cache.set(cache_key(cache_key_str), details)
Response.new(details)
end

def get_map_url(ips)
if !ips.kind_of?(Array)
return JSON.generate({:error => 'Invalid input. Array required!'})
Expand Down
31 changes: 23 additions & 8 deletions test/ipinfo_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class IPinfoTest < Minitest::Test
TEST_IPV4 = '8.8.8.8'
TEST_IPV6 = '2001:240:2a54:3900::'
TEST_RESPROXY_IP = '175.107.211.204'

def assert_ip6(resp)
assert_equal(resp.ip, TEST_IPV6)
Expand Down Expand Up @@ -52,14 +53,8 @@ def assert_ip6(resp)
phone: '+000000000'
}
)
assert_equal(
resp.domains,
{
page: 0,
total: 0,
domains: []
}
)
assert_equal(resp.domains[:total], 0)
assert_equal(resp.domains[:domains], [])
end

def assert_ip4(resp)
Expand Down Expand Up @@ -181,6 +176,26 @@ def test_lookup_ip4
end
end

def test_resproxy
ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])

# multiple checks for cache
(0...5).each do |_|
resp = ipinfo.resproxy(TEST_RESPROXY_IP)
assert_equal(resp.ip, TEST_RESPROXY_IP)
refute_nil(resp.last_seen)
refute_nil(resp.percent_days_seen)
refute_nil(resp.service)
end
end

def test_resproxy_empty
ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])

resp = ipinfo.resproxy(TEST_IPV4)
assert_equal(resp.all, {})
end

# # Requires IPv6 support
# def test_lookup_ip4_on_host_v6
# ipinfo = IPinfo.create(ENV['IPINFO_TOKEN'])
Expand Down