Ejemplo n.º 1
0
        public static ConnectionRequest Parse(string input, bool ipv6Mode = false)
        {
            ConnectionRequest request;
            var match      = SplitFormat.Match(input);
            var uriCreated = Uri.TryCreate(input, UriKind.Absolute, out var uri);

            if (uriCreated && !string.IsNullOrEmpty(uri.DnsSafeHost))
            {
                var port = uri.Port == PortLogic.UnsetPort ? PortLogic.DeterminePortByProtocol(uri.Scheme) : uri.Port;
                request = new ConnectionRequest(null, port, uri.DnsSafeHost, ipv6Mode);
            }
            else if (match.Success)
            {
                var host          = match.Groups["host"].Value;
                var port          = match.Groups["port"].Value;
                var convertedPort = PortLogic.StringToPort(port);
                if (convertedPort == PortLogic.UnsetPort)
                {
                    convertedPort = PortLogic.DeterminePortByProtocol(port);
                }
                if (!IPAddress.TryParse(host, out var ip))
                {
                    request = new ConnectionRequest(null, convertedPort, host, ipv6Mode);
                }
                else
                {
                    request = new ConnectionRequest(ip, convertedPort, "", ipv6Mode);
                }

                if (request.Port == PortLogic.UnsetPort || request.Port < 0 || request.Port > ushort.MaxValue)
                {
                    throw new ArgumentException($"The input you provided for the port is not valid, your input: {request.Port}.");
                }
            }
            else
            {
                throw new ArgumentException($"Invalid input: {input} is nether a valid url nor a host:port or host:protocol.");
            }
            return(request);
        }
Ejemplo n.º 2
0
 private void PrintProtocol(int port)
 {
     Console.Write($"{PortLogic.DetermineProtocolByPort(port),-8} | ");
 }