Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8312339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:07:46+00:00 2026-06-08T20:07:46+00:00

I have been trying to work out the correct way of blocking access to

  • 0

I have been trying to work out the correct way of blocking access to our Rails 3 application except for a small number of IP addresses and a couple of IP subnets.

While looking for ways to do this I found this question/answer. The suggested code is as follows:

Application Controller

before_filter :protect

def protect
  @ips = ['127.0.0.1', '203.123.10.1'] #And so on ...]
  if not @ips.include? request.remote_ip
     # Check for your subnet stuff here, for example
     # if not request.remote_ip.include?('127.0,0')
     render :text => "You are unauthorized"
     return
  end
end

This works, so I’ve altered it to redirect to a static page rather than just the text message.

What I would like to do though, is allow access from local IP’s on the same subnet as the Rails application server. The subnet is simply 192.168.1.0/24.

What is the easiest/cleanest way to add the subnet to the accepted IP’s?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:07:47+00:00Added an answer on June 8, 2026 at 8:07 pm

    To test if a given IP address foo is within a network specified by address net and mask mask, you would apply the mask to both the network address and the tested address, and see if the results are equal: foo & mask == net & mask . You have to first convert the IP address and the mask to integers. A mask of /24 is 24 bits set to 1 followed by 8 bits set to 0 – 0xFFFFFF00 or 255.255.255.0 in dotted quad notation.

    before_filter :protect
    
    def protect
      @ips = ['127.0.0.1', '192.168.1.0/24'] #And so on ...]
      allowed = false
      # Convert remote IP to an integer.
      bremote_ip = request.remote_ip.split('.').map(&:to_i).pack('C*').unpack('N').first
      @ips.each do |ipstring|
        ip, mask = ipstring.split '/'
        # Convert tested IP to an integer.
        bip = ip.split('.').map(&:to_i).pack('C*').unpack('N').first
        # Convert mask to an integer, and assume /32 if not specified.
        mask = mask ? mask.to_i : 32
        bmask = ((1 << mask) - 1) << (32 - mask)
        if bip & bmask == bremote_ip & bmask
          allowed = true
          break
        end
      end
    
      if not allowed
         render :text => "You are unauthorized"
         return
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I been trying to work this out but unable too. I have a
I have been pulling my hair out trying to make this work. I have
I have been trying for quite a while to work out how to accomplish
I have been trying all day to find a way to make this work.
I have been really puzzled because I was trying to work out some jQuery
I've been trying to work this out for 2 days now and I have
I have been trying to work with the Projector and Ray classes in order
I have been trying to make it work with either fwrite or fputcsv but
I have been trying to get nServiceBus to work with Ninject 2.0 as the
I have been trying to get this to work correctly and I think I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.