Ejemplo n.º 1
0
        public static Proxy ParseProxy(String proxySpecifier, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxyForProxy)
        {
            // format
            // http:<ip-or-host>:<port>
            // socks4:<ip-or-host>:<port>
            if (proxySpecifier == null || proxySpecifier.Length <= 0)
            {
                return(default(Proxy));
            }

            String[] splitStrings = proxySpecifier.Split(':');
            if (splitStrings.Length != 3)
            {
                throw new FormatException(String.Format("Invalid proxy '{0}', expected 'http:<host>:<port>', 'socks4:<host>:<port>' or 'socks5:<host>:<port>'", proxySpecifier));
            }

            String proxyTypeString = splitStrings[0];
            String ipOrHost        = splitStrings[1];
            String portString      = splitStrings[2];

            UInt16 port;

            if (!UInt16Parser.TryParse(portString, out port))
            {
                throw new FormatException(String.Format("Invalid port '{0}'", portString));
            }
            InternetHost proxyHost = new InternetHost(ipOrHost, port, dnsPriorityQuery, proxyForProxy);

            if (proxyTypeString.Equals("socks4", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new Socks4Proxy(proxyHost, null));
            }
            else if (proxyTypeString.Equals("socks5", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new Socks5NoAuthenticationConnectSocket(proxyHost));
            }
            else if (proxyTypeString.Equals("http", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new HttpProxy(proxyHost));
            }
            else if (proxyTypeString.Equals("gateway", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new GatewayProxy(proxyHost));
            }
            else if (proxyTypeString.Equals("httpconnect", StringComparison.CurrentCultureIgnoreCase))
            {
                return(new HttpConnectProxyProxy(proxyHost));
            }

            throw new FormatException(String.Format("Unexpected proxy type '{0}', expected 'gateway', 'http', 'httpconnect', 'socks4' or 'socks5'", proxyTypeString));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes and parses the ':port' prefix from the given string.
        /// </summary>
        /// <param name="ipOrHostAndPort">IP or Hostname with a ':port' postfix</param>
        /// <param name="port">The port number parsed from the host ':port' postfix.</param>
        /// <returns><paramref name="ipOrHostAndPort"/> with the ':port' postix removed.
        /// Also returns the ':port' postix in the form of a parsed port number.</returns>
        /// <exception cref="FormatException">If no ':port' postfix or if port is invalid.</exception>
        public static String SplitIPOrHostAndPort(String ipOrHostAndPort, out UInt16 port)
        {
            Int32 colonIndex = ipOrHostAndPort.IndexOf(':');

            if (colonIndex < 0)
            {
                throw new FormatException(String.Format("Missing colon to designate the port on '{0}'", ipOrHostAndPort));
            }

            String portString = ipOrHostAndPort.Substring(colonIndex + 1);

            if (!UInt16Parser.TryParse(portString, out port))
            {
                throw new FormatException(String.Format("Port '{0}', could not be parsed as a 2 byte unsigned integer", portString));
            }
            return(ipOrHostAndPort.Remove(colonIndex));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// It will check if <paramref name="ipOrHostOptionalPort"/>
        /// has a ':port' postfix.  If it does, it will parse that port number and use that instead of <paramref name="defaultPort"/>.
        /// Then tries to parse the string as an IPAddress.  If that fails, it will resolve
        /// it as a host name and select the best address using the given priority query.
        /// </summary>
        /// <param name="ipOrHostOptionalPort">IP or Hostname with an optional ':port' postfix</param>
        /// <param name="defaultPort">The default port to use if there is no ':port' postfix</param>
        /// <param name="dnsPriorityQuery">Callback that determines priority to select an address from Dns record.
        ///   Use DnsPriority.(QueryName) for standard queries.</param>
        /// <returns></returns>
        public static IPEndPoint ParseIPOrResolveHostWithOptionalPort(String ipOrHostOptionalPort, UInt16 defaultPort, PriorityQuery <IPAddress> dnsPriorityQuery)
        {
            Int32 colonIndex = ipOrHostOptionalPort.IndexOf(':');

            if (colonIndex >= 0)
            {
                // NOTE: It would be nice to parse this without creating another string
                String portString = ipOrHostOptionalPort.Substring(colonIndex + 1);
                if (!UInt16Parser.TryParse(portString, out defaultPort))
                {
                    throw new FormatException(String.Format("Port '{0}' could not be parsed as a 2 byte unsigned integer", portString));
                }
                ipOrHostOptionalPort = ipOrHostOptionalPort.Remove(colonIndex);
            }

            return(new IPEndPoint(ParseIPOrResolveHost(ipOrHostOptionalPort, dnsPriorityQuery), defaultPort));
        }
Ejemplo n.º 4
0
        public static InternetHost FromIPOrHostWithPort(String ipOrHostWithPort, PriorityQuery <IPAddress> dnsPriorityQuery, Proxy proxy)
        {
            Int32 colonIndex = ipOrHostWithPort.IndexOf(':');

            if (colonIndex < 0)
            {
                throw new FormatException(String.Format("Missing colon to designate the port on '{0}'", ipOrHostWithPort));
            }

            // NOTE: I could parse this without creating another string
            String portString = ipOrHostWithPort.Substring(colonIndex + 1);
            UInt16 port;

            if (!UInt16Parser.TryParse(portString, out port))
            {
                throw new FormatException(String.Format("Port '{0}' could not be parsed as a 2 byte unsigned integer", portString));
            }
            String ipOrHost = ipOrHostWithPort.Remove(colonIndex);

            return(new InternetHost(ipOrHost, port, dnsPriorityQuery, proxy));
        }
Ejemplo n.º 5
0
        public static SortedNumberSet ParsePortSet(String ports, UInt32 offset, UInt32 limit)
        {
            if (limit < offset)
            {
                throw new FormatException(String.Format("limit ({0}) cannot be less then offset ({1})", limit, offset));
            }

            SortedNumberSet portSet = new SortedNumberSet();

            UInt32 lastStartOffset = offset;

            while (true)
            {
                if (offset >= limit)
                {
                    if (offset > lastStartOffset)
                    {
                        String portString = ports.Substring((int)lastStartOffset, (int)(offset - lastStartOffset));
                        UInt16 port;
                        if (!UInt16Parser.TryParse(portString, out port))
                        {
                            throw new FormatException(String.Format("Failed to parse '{0}' as a port", portString));
                        }
                        portSet.Add(port);
                    }

                    return(portSet);
                }

                Char c = ports[(int)offset];
                if (c == ',')
                {
                    String portString = ports.Substring((int)lastStartOffset, (int)(offset - lastStartOffset));
                    UInt16 port;
                    if (!UInt16Parser.TryParse(portString, out port))
                    {
                        throw new FormatException(String.Format("Failed to parse '{0}' as a port", portString));
                    }
                    portSet.Add(port);
                    offset++;
                    lastStartOffset = offset;
                }
                else if (c == '-')
                {
                    throw new NotImplementedException();

                    /*
                     * String portString = ports.Substring((int)lastStartOffset, (int)(offset - lastStartOffset));
                     * UInt16 port;
                     * if (!UInt16.TryParse(portString, out port))
                     * {
                     *  throw new FormatException(String.Format("Failed to parse '{0}' as a port", portString));
                     * }
                     * portSet.Add(port);
                     */
                }
                else
                {
                    offset++;
                }
            }
        }