diff --git a/lib/geocoder/ip_address.rb b/lib/geocoder/ip_address.rb index 0e49a05684cd8268790f891341400405a841e700..3eca0af32b1ebfdbadcec08b1c5303c9dd2245bd 100644 --- a/lib/geocoder/ip_address.rb +++ b/lib/geocoder/ip_address.rb @@ -6,7 +6,14 @@ module Geocoder end def valid? - !!self.match(/\A(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/) + ipregex = %r{ + \A( # String Starts + ((::ffff:)?((\d{1,3})\.){3}\d{1,3}) # Check for IPv4 + | # .... Or + (\S+?(:\S+?){6}\S+) # Check for IPv6 + )\z + }x + !!self.match(ipregex) end end end diff --git a/test/unit/ip_address_test.rb b/test/unit/ip_address_test.rb index 4de712b94cbd53dffe01e88b97df42742e5cd6eb..cefccf32f2b9f181bba802276c7e26ffbf252115 100644 --- a/test/unit/ip_address_test.rb +++ b/test/unit/ip_address_test.rb @@ -8,6 +8,7 @@ class IpAddressTest < GeocoderTestCase assert Geocoder::IpAddress.new("232.65.123.94").valid? assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid? + assert Geocoder::IpAddress.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").valid? assert !Geocoder::IpAddress.new("232.65.123.94.43").valid? assert !Geocoder::IpAddress.new("232.65.123").valid? assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid? diff --git a/test/unit/query_test.rb b/test/unit/query_test.rb index 0b38903840bb11266f6ff8d69b93612093982c6c..3b63b462dc98e9deeed9c46d2c0e4da00c4039d5 100644 --- a/test/unit/query_test.rb +++ b/test/unit/query_test.rb @@ -6,6 +6,7 @@ class QueryTest < GeocoderTestCase def test_ip_address_detection assert Geocoder::Query.new("232.65.123.94").ip_address? + assert Geocoder::Query.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").ip_address? assert !Geocoder::Query.new("232.65.123.94.43").ip_address? assert !Geocoder::Query.new("::ffff:123.456.789").ip_address? end