This guide shows you how to detect VPN, proxy, TOR, and bot IP addresses in Ruby using the standard library's Net::HTTP and the Focsec API.

Works great with Rails, Sinatra, or any Ruby application where you need to identify anonymous visitors.

1

Check an IP for VPN or proxy

Use Ruby's built-in Net::HTTP to query the Focsec API.

Create your free account to get an API key.

ruby
require 'net/http'
require 'json'

API_KEY = 'your-api-key-here'

def check_ip(ip_address)
  uri = URI("https://api.focsec.com/v1/ip/#{ip_address}")

  request = Net::HTTP::Get.new(uri)
  request['Authorization'] = API_KEY

  response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
    http.request(request)
  end

  JSON.parse(response.body)
end

# Usage
result = check_ip('82.40.11.200')
puts result

API Response

The response is a hash containing threat flags and geolocation details:

Response
{
  "ip": "82.40.11.200",
  "is_vpn": false,
  "is_proxy": false,
  "is_bot": false,
  "is_tor": false,
  "is_datacenter": false,
  "city": "London",
  "country": "United Kingdom",
  "iso_code": "gb",
  "is_in_european_union": false,
  "flag": "šŸ‡¬šŸ‡§",
  "autonomous_system_number": 5089,
  "autonomous_system_organization": "Virgin Media Limited"
}
2

Detect suspicious IPs

Create a helper method to identify VPN, proxy, TOR, or bot connections:

ruby
def suspicious?(data)
  data['is_vpn'] ||
    data['is_proxy'] ||
    data['is_tor'] ||
    data['is_bot'] ||
    data['is_datacenter']
end

# Usage
result = check_ip('82.40.11.200')

if suspicious?(result)
  puts "VPN/Proxy detected: #{result['ip']}"
else
  puts "Clean IP: #{result['ip']}"
end
3

Rails controller example

Block VPN users from sensitive actions in a Rails controller:

ruby
class CheckoutController < ApplicationController
  before_action :block_vpn_users, only: [:create]

  private

  def block_vpn_users
    result = check_ip(request.remote_ip)

    if suspicious?(result)
      render json: {
        error: 'VPN or proxy detected. Please disable to continue.'
      }, status: :forbidden
    end
  end

  def check_ip(ip_address)
    uri = URI("https://api.focsec.com/v1/ip/#{ip_address}")
    request = Net::HTTP::Get.new(uri)
    request['Authorization'] = ENV['FOCSEC_API_KEY']

    response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
      http.request(request)
    end

    JSON.parse(response.body)
  end

  def suspicious?(data)
    data['is_vpn'] || data['is_proxy'] || data['is_tor'] ||
      data['is_bot'] || data['is_datacenter']
  end
end

Next steps

Your Ruby application is ready to detect VPN and proxy traffic. Head over to the API Reference for the complete documentation.

Ready to get started?

Sign up for a free account and start detecting VPNs, proxies, and bots in your application.

Get your API key »