Exemplo n.º 1
0
        /// <summary>
        /// Adds a server to the WebFarm
        /// </summary>
        /// <param name="farm">The name of the WebFarm</param>
        /// <param name="address">The address of the server</param>
        /// <returns>If the server was added.</returns>
        public bool AddServer(string farm, string address)
        {
            ConfigurationElement farmElement = this.GetFarm(farm);

            if (farmElement != null)
            {
                ConfigurationElementCollection servers = farmElement.GetCollection();
                ConfigurationElement           server  = servers.FirstOrDefault(f => f.GetAttributeValue("address").ToString() == address);

                if (server == null)
                {
                    ConfigurationElement serverElement = servers.CreateElement("server");
                    serverElement["address"] = server;

                    servers.Add(serverElement);
                    _Server.CommitChanges();

                    _Log.Information("Adding server '{0}'.", address);
                    return(true);
                }
                else
                {
                    _Log.Information("The server '{0}' already exists.", address);
                    return(false);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Checks if a server belongs to the WebFarm
        /// </summary>
        /// <param name="farm">The name of the WebFarm</param>
        /// <param name="address">The address of the server</param>
        /// <returns>If the server belongs to the WebFarm.</returns>
        public bool ServerExists(string farm, string address)
        {
            ConfigurationElement farmElement = this.GetFarm(farm);

            if (farmElement != null)
            {
                ConfigurationElementCollection servers = farmElement.GetCollection();
                ConfigurationElement           server  = servers.FirstOrDefault(f => f.GetAttributeValue("address").ToString() == address);

                if (server != null)
                {
                    _Log.Information("The server '{0}' exists.", address);
                    return(true);
                }
                else
                {
                    _Log.Information("The server '{0}' does not exists.", address);
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 3
0
        public static ConfigurationElement FindWebDavRule(this ConfigurationElementCollection collection, WebDavFolderRule settings)
        {
            return(collection.FirstOrDefault(x =>
            {
                var s = x["users"].ToString();
                if (settings.Users.Any() &&
                    x["users"].ToString() != string.Join(", ", settings.Users.ToArray()))
                {
                    return false;
                }

                if (settings.Roles.Any() &&
                    x["roles"].ToString() != string.Join(", ", settings.Roles.ToArray()))
                {
                    return false;
                }

                if (settings.Pathes.Any() &&
                    x["path"].ToString() != string.Join(", ", settings.Pathes.ToArray()))
                {
                    return false;
                }

                //if ((int)x["access"] != settings.AccessRights)
                //{
                //    return false;
                //}

                return true;
            }));
        }
        private bool ConfigureIsapiRestrictions(ServerManager mgr, string physicalPath, ModeType mode)
        {
            Configuration config = mgr.GetApplicationHostConfiguration();

            ConfigurationSection           isapiCgiRestrictionSection    = config.GetSection("system.webServer/security/isapiCgiRestriction");
            ConfigurationElementCollection isapiCgiRestrictionCollection = isapiCgiRestrictionSection.GetCollection();

            ConfigurationElement addElement =
                isapiCgiRestrictionCollection.FirstOrDefault(elem => string.Compare(elem.Attributes["path"].Value.ToString(), physicalPath, true) == 0);

            if (mode == ModeType.Deploy && addElement == null)
            {
                base.Log.LogMessage("Allowing ISAPI restriction '{0}'...", physicalPath);
                addElement            = isapiCgiRestrictionCollection.CreateElement("add");
                addElement["path"]    = physicalPath;
                addElement["allowed"] = true;
                isapiCgiRestrictionCollection.Add(addElement);
                mgr.CommitChanges();
                base.Log.LogMessage("Allowed ISAPI restriction '{0}'.", physicalPath);
            }
            else if (mode == ModeType.Undeploy && addElement != null)
            {
                base.Log.LogMessage("Disallowing ISAPI restriction '{0}'...", physicalPath);
                isapiCgiRestrictionCollection.Remove(addElement);
                mgr.CommitChanges();
                base.Log.LogMessage("Disallowed ISAPI restriction '{0}'...", physicalPath);
            }

            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates a WebFarm
        /// </summary>
        /// <param name="settings">The settings of the WebFarm</param>
        /// <returns>If the WebFarm was added.</returns>
        public void Create(WebFarmSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException("settings");
            }

            if (string.IsNullOrWhiteSpace(settings.Name))
            {
                throw new ArgumentException("WebFarm name cannot be null!");
            }



            //Get Farm
            ConfigurationElementCollection farms = this.GetFarms();
            ConfigurationElement           farm  = farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == settings.Name);

            if (farm != null)
            {
                _Log.Information("WebFarm '{0}' already exists.", settings.Name);

                if (settings.Overwrite)
                {
                    _Log.Information("WebFarm '{0}' will be overriden by request.", settings.Name);

                    this.Delete(settings.Name);
                }
                else
                {
                    return;
                }
            }



            //Create Farm
            farm         = farms.CreateElement("webFarm");
            farm["name"] = settings.Name;



            //Add Server
            ConfigurationElementCollection servers = farm.GetCollection();

            foreach (string server in settings.Servers)
            {
                ConfigurationElement serverElement = servers.CreateElement("server");
                serverElement["address"] = server;

                servers.Add(serverElement);

                _Log.Information("Adding server '{0}'.", server);
            }

            farms.Add(farm);
            _Server.CommitChanges();

            _Log.Information("WebFarm created.");
        }
Exemplo n.º 6
0
        /// <summary>
        /// Removes a server to the WebFarm
        /// </summary>
        /// <param name="farm">The name of the WebFarm</param>
        /// <param name="address">The address of the server</param>
        /// <returns>If the server was removed.</returns>
        public bool RemoveServer(string farm, string address)
        {
            ConfigurationElement farmElement = this.GetFarm(farm);

            if (farmElement != null)
            {
                ConfigurationElementCollection servers = farmElement.GetCollection();
                ConfigurationElement           server  = servers.FirstOrDefault(f => f.GetAttributeValue("address").ToString() == address);

                if (server != null)
                {
                    servers.Remove(server);
                    _Server.CommitChanges();

                    _Log.Information("Removed server '{0}'.", address);
                    return(true);
                }
                else
                {
                    _Log.Information("The server '{0}' does not exists.", address);
                    return(false);
                }
            }

            return(false);
        }
Exemplo n.º 7
0
        private ConfigurationElement GetFarm(string name)
        {
            ConfigurationElementCollection farms = this.GetFarms();
            ConfigurationElement           farm  = farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == name);

            if (farm == null)
            {
                _Log.Information("The webfarm '{0}' does not exists.", name);
            }

            return(farm);
        }
Exemplo n.º 8
0
        private ConfigurationElement GetServer(ConfigurationElement farm, string address)
        {
            ConfigurationElementCollection servers = farm.GetCollection();
            ConfigurationElement           server  = servers.FirstOrDefault(f => f.GetAttributeValue("address").ToString() == address);

            if (server == null)
            {
                _Log.Information("The server '{0}' does not exists.", address);
            }

            return(server);
        }
Exemplo n.º 9
0
        public static ConfigurationElement GetWebFarm(string name)
        {
            using (var serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection           section = config.GetSection("webFarms");
                ConfigurationElementCollection farms   = section.GetCollection();

                return(farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == name));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Checks if a WebFarm exists
        /// </summary>
        /// <param name="name">The name of the WebFarm</param>
        /// <returns>If the WebFarm exists.</returns>
        public bool Exists(string name)
        {
            ConfigurationElementCollection farms = this.GetFarms();
            ConfigurationElement           farm  = farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == name);

            if (farm != null)
            {
                _Log.Information("The webfarm '{0}' exists.", name);
                return(true);
            }
            else
            {
                _Log.Information("The webfarm '{0}' does not exists.", name);
                return(false);
            }
        }
Exemplo n.º 11
0
        /// <summary>
        /// Adds a server to the WebFarm
        /// </summary>
        /// <param name="farm">The name of the WebFarm</param>
        /// <param name="settings">The settings of the server</param>
        /// <returns>If the server was added.</returns>
        public bool AddServer(string farm, WebFarmServerSettings settings)
        {
            ConfigurationElement farmElement = this.GetFarm(farm);

            if (farmElement != null)
            {
                ConfigurationElementCollection servers = farmElement.GetCollection();
                ConfigurationElement           server  = servers.FirstOrDefault(f => f.GetAttributeValue("address").ToString() == settings.Address);

                if (server == null)
                {
                    ConfigurationElement serverElement = servers.CreateElement("server");
                    serverElement["address"] = settings.Address;

                    var serverSettings = serverElement.ChildElements["applicationRequestRouting"];

                    if (settings.HttpPort.HasValue)
                    {
                        serverSettings["httpPort"] = settings.HttpPort.Value;
                    }

                    if (settings.HttpsPort.HasValue)
                    {
                        serverSettings["httpsPort"] = settings.HttpsPort.Value;
                    }

                    if (settings.Weight.HasValue)
                    {
                        serverSettings["weight"] = settings.Weight.Value;
                    }

                    servers.Add(serverElement);
                    _Server.CommitChanges();

                    _Log.Information("Adding server '{0}'.", settings.Address);
                    return(true);
                }
                else
                {
                    _Log.Information("The server '{0}' already exists.", settings.Address);
                    return(false);
                }
            }

            return(false);
        }
Exemplo n.º 12
0
        public static void DeleteWebFarm(string name)
        {
            using (var serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection           section = config.GetSection("webFarms");
                ConfigurationElementCollection farms   = section.GetCollection();

                ConfigurationElement farm = farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == name);

                if (farm != null)
                {
                    farms.Remove(farm);
                    serverManager.CommitChanges();
                }
            }
        }
Exemplo n.º 13
0
        /// <summary>
        /// Delete an WebFarm
        /// </summary>
        /// <param name="name">The name of the WebFarm</param>
        /// <returns>If the WebFarm was deleted.</returns>
        public bool Delete(string name)
        {
            ConfigurationElementCollection farms = this.GetFarms();
            ConfigurationElement           farm  = farms.FirstOrDefault(f => f.GetAttributeValue("name").ToString() == name);

            if (farm != null)
            {
                farms.Remove(farm);
                _Server.CommitChanges();

                _Log.Information("WebFarm deleted.");
                return(true);
            }
            else
            {
                _Log.Information("The webfarm '{0}' does not exists.", name);
                return(false);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Verify if the ftp site exists.
        /// </summary>
        /// <param name="name">The name of the virtual directory.</param>
        /// <returns>True if the ftp site exists.</returns>
        public static bool Exists(string name)
        {
            using (ServerManager serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");

                ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

                ConfigurationElement fileServiceSite = sitesCollection.FirstOrDefault(c => (string)c.Attributes["name"].Value == name);

                if (fileServiceSite == null)
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 15
0
        /// <summary>
        /// Gets the mimetype for the extension provided, using IIS to lookup the mimetype, or this fails,
        /// a dictionary lookup of common extensions to mimetypes.
        /// </summary>
        /// <param name="fileExtension">The file extension to lookup, which should include the "." e.g. ".jpg"</param>
        /// <returns>The mimetype for the extension, or "application/octet-stream" if the mimetype cannot be found.</returns>
        public static string GetMimeType(string fileExtension)
        {
            if (string.IsNullOrEmpty(fileExtension))
            {
                return("application/octet-stream");
            }

            fileExtension = fileExtension.ToLower();

#if MONO || DEBUG
            return(MimeTypes.GetMimeMapping(fileExtension));
#endif
            try
            {
                using (ServerManager serverManager = new ServerManager())
                {
                    string mimeType = "application/octet-stream";

                    Microsoft.Web.Administration.Configuration config   = serverManager.GetApplicationHostConfiguration();
                    ConfigurationSection           staticContentSection = config.GetSection("system.webServer/staticContent");
                    ConfigurationElementCollection mimemaps             = staticContentSection.GetCollection();

                    ConfigurationElement element = mimemaps.FirstOrDefault(m => m.Attributes["fileExtension"].Value.ToString() == fileExtension);

                    if (element != null)
                    {
                        mimeType = element.Attributes["mimeType"].Value.ToString();
                    }

                    return(mimeType);
                }
            }
            catch (Exception)
            {
                // Shared hosting won't have access to the applicationhost.config file (UnauthorizedAccessException)
                // also IIS Express doesn't have ServerManager registered as a COM type (COMException)
                return(MimeTypes.GetMimeMapping(fileExtension));
            }
        }
Exemplo n.º 16
0
        /// <summary>
        /// Deletes an FTP virtual directory.
        /// </summary>
        /// <param name="name">The name of the virtual directory.</param>
        public static void DeleteFtpSite(string name)
        {
            using (ServerManager serverManager = new ServerManager())
            {
                Configuration config = serverManager.GetApplicationHostConfiguration();

                ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");

                ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

                ConfigurationElement fileServiceSite = sitesCollection.FirstOrDefault(c => (string)c.Attributes["name"].Value == name);

                if (fileServiceSite != null)
                {
                    sitesCollection.Remove(fileServiceSite);
                }

                ConfigurationElementCollection bindingsCollection = fileServiceSite.GetCollection("bindings");

                ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
                bindingElement["protocol"] = @"ftp";
                string bindingInformation = bindingElement["bindingInformation"] as string;

                if (!string.IsNullOrEmpty(bindingInformation))
                {
                    string[] bindingParts = bindingInformation.Split(':');
                    if (bindingParts.Length > 2)
                    {
                        int port = 0;
                        if (int.TryParse(bindingParts[1], out port))
                        {
                            Uhuru.Utilities.FirewallTools.ClosePort(port);
                        }
                    }
                }

                serverManager.CommitChanges();
            }
        }
Exemplo n.º 17
0
        public virtual void UpdateSvcConfig()
        {
            ServerManager sm = new ServerManager();

            #region 添加MIEI
            ConfigurationElementCollection staticContentCollection = sm.GetApplicationHostConfiguration()
                                                                     .GetSection("system.webServer/staticContent")
                                                                     .GetCollection();
            ConfigurationElement mimeTypeEl = staticContentCollection.FirstOrDefault(a => (string)a.Attributes["fileExtension"].Value == ".svc");
            if (mimeTypeEl != null)
            {
                staticContentCollection.Remove(mimeTypeEl);
            }
            ConfigurationElement mimeMapElement = staticContentCollection.CreateElement("mimeMap");
            mimeMapElement["fileExtension"] = ".svc";
            mimeMapElement["mimeType"]      = "application/octet-stream";
            staticContentCollection.Add(mimeMapElement);
            #endregion
            #region 添加处理程序映射
            ConfigurationElementCollection handlersCollection = sm.GetApplicationHostConfiguration()
                                                                .GetSection("system.webServer/handlers")
                                                                .GetCollection();
            ConfigurationElement handleEl = handlersCollection.FirstOrDefault(a => (string)a.Attributes["path"].Value == "*.svc" &&
                                                                              (string)a.Attributes["name"].Value == "svc-Integrated");
            if (handleEl != null)
            {
                handlersCollection.Remove(handleEl);
            }
            ConfigurationElement addElement = handlersCollection.CreateElement("add");
            addElement["path"] = "*.svc";
            addElement["name"] = "svc-Integrated";
            addElement["verb"] = "*";
            addElement["type"] = "System.ServiceModel.Activation.HttpHandler";
            handlersCollection.Add(addElement);
            #endregion
            sm.CommitChanges();
        }
Exemplo n.º 18
0
        static void Main(string[] args)
        {
            Console.WriteLine("Indique la IP con la que desea trabajar: ");
            string IP = Console.ReadLine();

            if (string.IsNullOrEmpty(IP))
            {
                Console.WriteLine("No se ingreso una IP, por tanto termino el programa. Gracias por hacerme perder mi tiempo ¬¬");
                return;
            }

            //Probar si la IP es una IP valida
            if (IsValidateIP(IP) == false)
            {
                Console.WriteLine("Tss, ingresaste una IP valida. Gracias por hacerme perder mi tiempo ¬¬");
                return;
            }

            //Preguntamos si se quiere agregar o eliminar
            Console.WriteLine("Deseas eliminar(1) o agregar (2) la IP?");
            string opcionEliminar = Console.ReadLine();

            bool agregar = opcionEliminar == "2";

            using (ServerManager serverManager = new ServerManager())
            {
                Configuration configuration = serverManager.GetApplicationHostConfiguration();

                List <string> sitiosIIs = new List <string>()
                {
                    "adminEcommerce", "clientesEcomm", "disponibilidadecom", "exploracionecom", "menuecom", "notificacionesecom", "pedidosecom"
                };



                foreach (string sitioIis in sitiosIIs)
                {
                    ConfigurationSection           ipSecuritySection    = configuration.GetSection("system.webServer/security/ipSecurity", sitioIis);
                    ConfigurationElementCollection ipSecurityCollection = ipSecuritySection.GetCollection();

                    ConfigurationElement existingElement = ipSecurityCollection.FirstOrDefault(x => x.Attributes["ipAddress"].Value.ToString() == IP);

                    //Si quiere agregar y no existe el elemento, lo agrego
                    if (agregar && existingElement == null)
                    {
                        ConfigurationElement addElement = ipSecurityCollection.CreateElement("add");
                        addElement["ipAddress"] = IP;
                        addElement["allowed"]   = true;
                        Console.WriteLine($"[ADD] Se agrega el elemento {IP} en el sitio: {sitioIis}");
                        ipSecurityCollection.Add(addElement);
                    }
                    else
                    {
                        ConfigurationElement removeElement = ipSecurityCollection.FirstOrDefault(x => x.Attributes["ipAddress"].Value.ToString() == IP);

                        //Si ya esta, lo elimino
                        if (removeElement != null)
                        {
                            Console.WriteLine($"[DELETE] Se encuentra el elemento {IP} en el sitio: {sitioIis}, se remueve");
                            ipSecurityCollection.Remove(removeElement);
                        }
                    }
                }

                serverManager.CommitChanges();
            }
        }
        private bool ConfigureHandler(ServerManager mgr, string siteName, string virtualPath, string physicalPath, string isapiFileName, ModeType mode)
        {
            Site ste = mgr.Sites[siteName];

            if (ste == null)
            {
                if (mode == ModeType.Undeploy)
                {
                    base.Log.LogWarning("Cannot find IIS site '" + siteName + "'.");
                    return(true);
                }
                else
                {
                    base.Log.LogError("Cannot find IIS site '" + siteName + "'.");
                    return(false);
                }
            }

            Application app = ste.Applications[virtualPath];

            if (app == null)
            {
                if (mode == ModeType.Undeploy)
                {
                    base.Log.LogWarning("Cannot find IIS application '" + virtualPath + "'.");
                    return(true);
                }
                else
                {
                    base.Log.LogError("Cannot find IIS application '" + virtualPath + "'.");
                    return(false);
                }
            }

            Configuration config = app.GetWebConfiguration();

            ConfigurationSection           handlersSection    = config.GetSection("system.webServer/handlers");
            ConfigurationElementCollection handlersCollection = handlersSection.GetCollection();

            ConfigurationElement addElement =
                handlersCollection.FirstOrDefault(elem => string.Compare(elem.Attributes["scriptProcessor"].Value.ToString(), physicalPath, true) == 0);

            if (mode == ModeType.Deploy)
            {
                base.Log.LogMessage("Adding handler mapping '{0}'...", physicalPath);
                if (addElement == null)
                {
                    addElement                    = handlersCollection.CreateElement("add");
                    addElement["name"]            = isapiFileName;
                    addElement["path"]            = isapiFileName;
                    addElement["verb"]            = "*";
                    addElement["modules"]         = "IsapiModule";
                    addElement["scriptProcessor"] = physicalPath;
                    handlersCollection.Clear();
                    handlersCollection.AddAt(0, addElement);
                }
                else
                {
                    addElement["name"]            = isapiFileName;
                    addElement["path"]            = isapiFileName;
                    addElement["verb"]            = "*";
                    addElement["modules"]         = "IsapiModule";
                    addElement["scriptProcessor"] = physicalPath;
                }
                mgr.CommitChanges();
                base.Log.LogMessage("Added handler mapping '{0}'.", physicalPath);
            }
            else if (mode == ModeType.Undeploy && addElement != null)
            {
                base.Log.LogMessage("Removing handler mapping '{0}'...", physicalPath);
                handlersCollection.Remove(addElement);
                mgr.CommitChanges();
                base.Log.LogMessage("Removed handler mapping '{0}'...", physicalPath);
            }

            return(true);
        }