Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite IPAddr.new.include? with pure Ruby bit operation #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
50 changes: 44 additions & 6 deletions lib/uri/generic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

require_relative 'common'
autoload :IPSocket, 'socket'
autoload :IPAddr, 'ipaddr'

module URI

Expand Down Expand Up @@ -1564,15 +1563,54 @@ def self.use_proxy?(hostname, addr, port, no_proxy) # :nodoc:
return false if dothostname.end_with?(".#{p_host.downcase}")
end
if addr
begin
return false if IPAddr.new(p_host).include?(addr)
rescue IPAddr::InvalidAddressError
next
end
return false if addr == p_host || in_ip_range?(addr, p_host)
end
end
}
true
end

private

def self.expand_ipv6(ip)
if ip.include?('::')
parts = ip.split('::')
left = parts[0].split(':')
right = parts[1] ? parts[1].split(':') : []
middle = ['0'] * (8 - left.size - right.size)
(left + middle + right).join(':')
else
ip
end
end

def self.ip_to_i(ip)
if ip.include?(':')
expanded_ip = expand_ipv6(ip)
segments = expanded_ip.split(':').map { |seg| seg.rjust(4, '0') }
hex_ip = segments.join
hex_ip.to_i(16)
else
ip.split('.').map(&:to_i).pack('C*').unpack1('N')
end
end

def self.in_ip_range?(ip, range)
return false unless range.include?('/')

base, bits = range.split('/')
bits = bits.to_i

if base.include?(':')
# IPv6
expanded_base = expand_ipv6(base)
mask = (1 << 128) - (1 << (128 - bits))
(ip_to_i(ip) & mask) == (ip_to_i(expanded_base) & mask)
else
# IPv4
mask = (0xFFFFFFFF << (32 - bits)) & 0xFFFFFFFF
(ip_to_i(ip) & mask) == (ip_to_i(base) & mask)
end
end
end
end