Beispiel #1
0
    public async Task <IPAddress> GetMyIpAsync(IPVersion ver = IPVersion.IPv4, CancellationToken cancel = default)
    {
        string url;

        if (ver == IPVersion.IPv4)
        {
            url = Consts.Urls.GetMyIpUrl_IPv4;
        }
        else
        {
            url = Consts.Urls.GetMyIpUrl_IPv6;
        }

        Exception error = new CoresLibException("Unknown Error");

        IPAddress?ret = null;

        for (int i = 0; i < CoresConfig.GetMyIpClientSettings.NumRetry; i++)
        {
            try
            {
                WebRet webRet = await Web.SimpleQueryAsync(WebMethods.GET, url, cancel);

                string str = webRet.Data._GetString_Ascii()._OneLine("");

                if (str.StartsWith("IP=", StringComparison.OrdinalIgnoreCase))
                {
                    ret = IPAddress.Parse(str.Substring(3));
                    break;
                }
                else
                {
                    throw new ApplicationException($"Invalid IP str: \"{str}\"");
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
        }

        if (ret == null)
        {
            throw error !;
        }

        return(ret);
    }
Beispiel #2
0
    public async Task <GetMyIpInfoResult> GetMyIpInfoAsync(IPVersion ver = IPVersion.IPv4, CancellationToken cancel = default)
    {
        string url;

        if (ver == IPVersion.IPv4)
        {
            url = Consts.Urls.GetMyIpUrl_IPv4;
        }
        else
        {
            url = Consts.Urls.GetMyIpUrl_IPv6;
        }

        url += "?port=true&fqdn=true";

        Exception error = new CoresLibException("Unknown Error");

        GetMyIpInfoResult?ret = null;

        for (int i = 0; i < CoresConfig.GetMyIpClientSettings.NumRetry; i++)
        {
            try
            {
                WebRet webRet = await Web.SimpleQueryAsync(WebMethods.GET, url, cancel);

                string str = webRet.Data._GetString_UTF8();

                IPAddress?ip   = null;
                string    fqdn = "";
                int       port = 0;

                foreach (string line in str._GetLines(true))
                {
                    if (line._GetKeyAndValue(out string key, out string value, "="))
                    {
                        key = key.ToUpperInvariant();

                        switch (key)
                        {
                        case "IP":
                            ip = value._NonNullTrim()._ToIPAddress(ver == IPVersion.IPv4 ? AllowedIPVersions.IPv4 : AllowedIPVersions.IPv6);
                            break;

                        case "FQDN":
                            fqdn = value._NonNullTrim();
                            break;

                        case "PORT":
                            port = value._NonNullTrim()._ToInt();
                            break;
                        }
                    }
                }

                if (ip != null && port != 0)
                {
                    if (fqdn._IsEmpty())
                    {
                        fqdn = ip.ToString();
                    }
                    ret = new GetMyIpInfoResult(ip, port, fqdn, null !);
                    break;
                }
                else
                {
                    throw new ApplicationException($"Invalid GetMyIp Server reply str: \"{str}\"");
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
        }

        if (ret == null)
        {
            throw error !;
        }

        return(ret);
    }