Example #1
0
        public static IWebProxy ProxyFromGlobalRequirements(this DownloadPluginInfo pinfo, Dictionary <string, object> globalmetadata)
        {
            if (globalmetadata == null)
            {
                return(null);
            }
            NetworkCredential nt = null;
            bool?enabled         = globalmetadata.GetBoolFromMetadata(DownloadPluginInfo.ProxyEnabled);

            if (enabled.HasValue && enabled.Value)
            {
                string proxyaddress  = globalmetadata.GetStringFromMetadata(DownloadPluginInfo.ProxyAddress);
                int?   proxyport     = globalmetadata.GetIntFromMetadata(DownloadPluginInfo.ProxyPort);
                string proxyusername = globalmetadata.GetStringFromMetadata(DownloadPluginInfo.ProxyUsername);
                string proxypassword = globalmetadata.GetStringFromMetadata(DownloadPluginInfo.ProxyPassword);
                if (!string.IsNullOrEmpty(proxyusername) || !string.IsNullOrEmpty(proxypassword))
                {
                    nt = new NetworkCredential(proxyusername, proxypassword);
                }
                UriBuilder bld = new UriBuilder(proxyaddress);
                bld.Port = proxyport.Value;
                return(new WebProxy(bld.Uri, false, null, nt));
            }
            return(null);
        }
Example #2
0
 public static async Task <Response> VerifyBaseAuthentication(this DownloadPluginInfo pinfo, Dictionary <string, object> authmetadata)
 {
     return(await Task.Run(() =>
     {
         Response r = VerifyRequiredKeys(authmetadata, pinfo.AuthenticationRequirements);
         if (r.Status != ResponseStatus.Ok)
         {
             return r;
         }
         if (VerifyRequirementsList(pinfo.AuthenticationRequirements, DownloadPluginInfo.Username, DownloadPluginInfo.Password))
         {
             string username = authmetadata.GetStringFromMetadata(DownloadPluginInfo.Username);
             if (string.IsNullOrEmpty(username))
             {
                 return new Response {
                     ErrorMessage = "'" + DownloadPluginInfo.Username + "' is required", Status = ResponseStatus.MissingRequirement
                 }
             }
             ;
             string password = authmetadata.GetStringFromMetadata(DownloadPluginInfo.Password);
             if (string.IsNullOrEmpty(password))
             {
                 return new Response {
                     ErrorMessage = "'" + DownloadPluginInfo.Password + "' is required", Status = ResponseStatus.MissingRequirement
                 }
             }
             ;
         }
         return r;
     }));
 }
        public DownloadPluginHandler()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string   dirname  = System.IO.Path.GetDirectoryName(assembly.GetName().CodeBase);

            if (dirname != null)
            {
                if (dirname.StartsWith(@"file:\"))
                {
                    dirname = dirname.Substring(6);
                }
                AggregateCatalog catalog = new AggregateCatalog();
                catalog.Catalogs.Add(new AssemblyCatalog(assembly));
                catalog.Catalogs.Add(new DirectoryCatalog(dirname));
                var container = new CompositionContainer(catalog);
                container.ComposeParts(this);
            }
            Plugins     = new Dictionary <string, IDownloadPlugin>();
            PluginInfos = new Dictionary <string, DownloadPluginInfo>();
            foreach (IDownloadPlugin f in List)
            {
                DownloadPluginInfo pinfo = f.Information();
                Plugins.Add(pinfo.Name, f);
                PluginInfos.Add(pinfo.Name, pinfo);
            }
        }
Example #4
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);
        }