From cab2bc4452f0f0e9129d3b59fabee9583cf63d0a Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 5 Feb 2019 20:39:35 +0700 Subject: [PATCH] fix IP --- lib/auto_linker/parser.ex | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 3d13ec2..0f02039 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -29,10 +29,14 @@ defmodule AutoLinker.Parser do @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/ @match_url ~r{^[\w\.-]+(?\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} - @match_scheme ~r{^(?:http(s)?:\/\/)?[\w.-]+(?\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} + @match_scheme ~r{^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~:/?#[\]@!\$&'\(\)\*\+,;=.]+$} @match_phone ~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))" + @match_hostname ~r{^(?:https?:\/\/)?(?:[^@\n]+@)?(?[^:#~\/\n?]+)} + + @match_ip ~r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" + @default_opts ~w(url)a @tlds "./priv/tlds.txt" |> File.read!() |> String.trim() |> String.split("\n") @@ -185,7 +189,7 @@ defmodule AutoLinker.Parser do if Regex.match?(@invalid_url, buffer) do false else - Regex.run(@match_scheme, buffer, capture: [:tld]) |> is_valid_tld?() + Regex.match?(@match_scheme, buffer) |> is_valid_tld?(buffer) end end @@ -193,12 +197,26 @@ defmodule AutoLinker.Parser do if Regex.match?(@invalid_url, buffer) do false else - Regex.run(@match_url, buffer, capture: [:tld]) |> is_valid_tld?() + Regex.match?(@match_url, buffer) |> is_valid_tld?(buffer) end end - def is_valid_tld?(["." <> tld]), do: tld in @tlds - def is_valid_tld?(_), do: false + def is_valid_tld?(true, buffer) do + [host] = Regex.run(@match_hostname, buffer, capture: [:host]) + + if is_ip?(host) do + true + else + tld = host |> String.split(".") |> List.last() + tld in @tlds + end + end + + def is_valid_tld?(false, _), do: false + + def is_ip?(buffer) do + Regex.match?(@match_ip, buffer) + end @doc false def match_phone(buffer) do