Esempio n. 1
0
        public static async Task <Response> VerifyGlobalRequirements(this DownloadPluginInfo pinfo, Dictionary <string, object> globalmetadata)
        {
            Response r = VerifyRequiredKeys(globalmetadata, pinfo.GlobalRequirements);

            if (r.Status != ResponseStatus.Ok)
            {
                return(r);
            }
            if (VerifyRequirementsList(pinfo.GlobalRequirements, DownloadPluginInfo.ProxyEnabled, DownloadPluginInfo.ProxyAddress, DownloadPluginInfo.ProxyPort, DownloadPluginInfo.ProxyUsername, DownloadPluginInfo.ProxyPassword))
            {
                bool?enabled = globalmetadata.GetBoolFromMetadata(DownloadPluginInfo.ProxyEnabled);
                if (enabled.HasValue && enabled.Value)
                {
                    string address = globalmetadata.GetStringFromMetadata(DownloadPluginInfo.ProxyAddress);

                    if (string.IsNullOrEmpty(address))
                    {
                        return new Response {
                                   ErrorMessage = "'" + DownloadPluginInfo.ProxyAddress + "' is invalid", Status = ResponseStatus.MissingRequirement
                        }
                    }
                    ;
                    int?port = globalmetadata.GetIntFromMetadata(DownloadPluginInfo.ProxyPort);
                    if (!port.HasValue)
                    {
                        return new Response {
                                   ErrorMessage = "'" + DownloadPluginInfo.ProxyPort + "' is invalid", Status = ResponseStatus.MissingRequirement
                        }
                    }
                    ;
                    if (port.Value < 1 || port.Value > 65534)
                    {
                        return new Response {
                                   ErrorMessage = "'" + DownloadPluginInfo.ProxyPort + "' is invalid", Status = ResponseStatus.InvalidArgument
                        }
                    }
                    ;
                    IpInfo    ipnfo = null;
                    IWebProxy proxy = pinfo.ProxyFromGlobalRequirements(globalmetadata);
                    if (proxy == null)
                    {
                        return new Response {
                                   ErrorMessage = "Unable to create proxy", Status = ResponseStatus.InvalidArgument
                        }
                    }
                    ;
                    WebStream s = await WebStream.Get("http://ipinfo.io/json", null, null, null, null, 10000, false, null, proxy);

                    if (s != null && s.StatusCode == HttpStatusCode.OK)
                    {
                        StreamReader reader = new StreamReader(s, Encoding.UTF8);

                        string json = reader.ReadToEnd();
                        ipnfo = JsonConvert.DeserializeObject <IpInfo>(json);
                        reader.Dispose();
                    }
                    s?.Dispose();
                    if ((s == null) || (s.StatusCode != HttpStatusCode.OK))
                    {
                        return new Response {
                                   ErrorMessage = "Unable to Connect", Status = ResponseStatus.InvalidArgument
                        }
                    }
                    ;
                    r.ErrorMessage = "IP: " + ipnfo.ip + " " + ipnfo.city + "/" + ipnfo.country;
                }
            }
            return(r);
        }