示例#1
0
        public static Data StringLine2Data(string line)
        {
            var s = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (s.Length < 1)
            {
                return(null);
            }

            string    hostname;
            IPAddress ip   = null;
            var       port = 443;

            var sp = s[0].Split(':');

            if (sp.Length == 1 || sp.Length == 2)
            {
                hostname = sp[0];
                if (IPFormatter.IsIPv4Address(hostname))
                {
                    ip = IPAddress.Parse(hostname);
                }
            }
            else
            {
                return(null);
            }
            if (sp.Length == 2)
            {
                try
                {
                    port = Convert.ToInt32(sp[1]);
                }
                catch
                {
                    return(null);
                }

                if (!IPFormatter.IsPort(port))
                {
                    return(null);
                }
            }


            var res = new Data
            {
                HostsName   = hostname,
                Ip          = ip,
                Port        = port,
                Description = string.Empty
            };

            if (s.Length == 2)
            {
                res.Description = s[1];
            }

            return(res);
        }
示例#2
0
        public static Data StringLine2Data(string line)
        {
            var s = line.Split(new[] { ' ' }, 2, StringSplitOptions.RemoveEmptyEntries);

            if (s.Length < 1)
            {
                return(null);
            }

            string    hostname;
            IPAddress ip;
            var       port = 443;

            var sp = IPFormatter.EndPointRegexStr.Match(s[0]).Groups;

            if (sp.Count == 5)
            {
                hostname = string.IsNullOrWhiteSpace(sp[1].Value) ? sp[3].Value : sp[1].Value;
                IPAddress.TryParse(hostname, out ip);

                if (!int.TryParse(string.IsNullOrWhiteSpace(sp[2].Value) ? sp[4].Value : sp[2].Value, out port))
                {
                    return(null);
                }

                if (!IPFormatter.IsPort(port))
                {
                    return(null);
                }
            }
            else if (sp.Count == 1)
            {
                var groups = Regex.Match(s[0], @"^\[(.*)\]$").Groups;
                if (groups.Count == 2)
                {
                    hostname = groups[1].Value;
                    IPAddress.TryParse(hostname, out ip);
                }
                else
                {
                    hostname = s[0];
                    IPAddress.TryParse(hostname, out ip);
                }
            }
            else
            {
                return(null);
            }

            var res = new Data
            {
                HostsName   = hostname,
                Ip          = ip,
                Port        = port,
                Description = string.Empty
            };

            if (s.Length == 2)
            {
                res.Description = s[1];
            }

            return(res);
        }