Skip to content

Commit 896a99f

Browse files
Merge pull request #3 from ZeroAbility/ipv6-compressed-address
Add support for compressed form of IPv6
2 parents 120dbfb + 6548477 commit 896a99f

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

ip2location.lua

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,34 @@ function ip2location:checkip(ip)
404404
-- check for ipv6 format, should be 8 'chunks' of numbers/letters
405405
-- without leading/trailing chars
406406
-- or fewer than 8 chunks, but with only one `::` group
407-
chunks = {ip:match("^"..(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$")))}
407+
-- chunks = {ip:match("^"..(("([a-fA-F0-9]*):"):rep(8):gsub(":$","$")))}
408408
-- if #chunks == 8
409409
-- or #chunks < 8 and ip:match('::') and not ip:gsub("::","",1):match('::') then
410410

411+
local isIPv6 = false;
412+
local hextets = 8;
413+
414+
if #chunks == 0 then
415+
-- parse the ipv6 string using the expected pattern
416+
for hextet in ip:gmatch("[a-fA-F0-9]*") do
417+
table.insert(chunks, hextet)
418+
end
419+
if #chunks > 0 then isIPv6 = true; end
420+
end
421+
422+
-- expand the ipv6 address and add zeroes
423+
if isIPv6 == true then
424+
for i = 1, #chunks do
425+
if chunks[i] == "" then
426+
for j = 1 , hextets - (#chunks - 1) - 1 do
427+
if j == 1 then chunks[i] = "0" end
428+
table.insert(chunks, i, "0")
429+
end
430+
end
431+
end
432+
if chunks[hextets] == "" then chunks[hextets] = "0" end
433+
end
434+
411435
-- only support full IPv6 format for now
412436
if #chunks == 8 then
413437
local ipnum = bn.ZERO

0 commit comments

Comments
 (0)