Beispiel #1
0
        private static void RegisterPhpDefaultDocument(ConfigurationSection defaultDocument)
        {
            ConfigurationElement defaultFiles = defaultDocument.GetChildElement("files");
            ConfigurationElementCollection filesCollection = defaultFiles.GetCollection();

            // search index.php in default documents
            bool indexPhpPresent = false;
            foreach (ConfigurationElement configurationElement in filesCollection)
            {
                string value = configurationElement.GetAttributeValue("value") as string;
                if (!string.IsNullOrEmpty(value))
                {
                    if (string.Equals(value, "index.php", StringComparison.OrdinalIgnoreCase))
                    {
                        indexPhpPresent = true;
                        break;
                    }
                }
            }

            if (!indexPhpPresent)
            {
                // add index.php
                ConfigurationElement indexPhp = filesCollection.CreateElement();
                indexPhp.SetAttributeValue("value", "index.php");
                filesCollection.AddAt(0, indexPhp);
            }
        }
Beispiel #2
0
        public async Task SaveAsync(Application application)
        {
            var variables = new SortedDictionary <string, List <string> >();

            foreach (var item in application.Extra)
            {
                variables.Add(item.Key, item.Value);
            }

            var vDir = application.VirtualDirectories[0];

            Configuration                  config = application.GetWebConfiguration();
            ConfigurationSection           defaultDocumentSection = config.GetSection("system.webServer/defaultDocument");
            ConfigurationElementCollection filesCollection        = defaultDocumentSection.GetCollection("files");
            ConfigurationSection           httpLoggingSection     = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/httpLogging", application.Location);
            ConfigurationSection           ipSecuritySection      = application.Server.GetApplicationHostConfiguration().GetSection("system.webServer/security/ipSecurity", application.Location);

            ConfigurationSection           requestFilteringSection  = config.GetSection("system.webServer/security/requestFiltering");
            ConfigurationElement           hiddenSegmentsElement    = requestFilteringSection.GetChildElement("hiddenSegments");
            ConfigurationElementCollection hiddenSegmentsCollection = hiddenSegmentsElement.GetCollection();

            ConfigurationSection           httpErrorsSection    = config.GetSection("system.webServer/httpErrors");
            ConfigurationElementCollection httpErrorsCollection = httpErrorsSection.GetCollection();

            var urlCompressionSection = config.GetSection("system.webServer/urlCompression");

            ConfigurationSection httpProtocolSection = config.GetSection("system.webServer/httpProtocol");

            ConfigurationSection           rewriteSection    = config.GetSection("system.webServer/rewrite/rules");
            ConfigurationElementCollection rewriteCollection = rewriteSection.GetCollection();

            variables.Add("usehttps", new List <string> {
                (application.Site.Bindings[0].Protocol == "https").ToString()
            });
            variables.Add("addr", new List <string> {
                application.Site.Bindings[0].EndPoint.Address.ToString()
            });
            variables.Add("port", new List <string> {
                application.Site.Bindings[0].EndPoint.Port.ToString()
            });
            variables.Add("hosts", new List <string> {
                application.Site.Bindings[0].Host
            });
            variables.Add("root", new List <string> {
                string.Format("{0} {1}", vDir.Path, vDir.PhysicalPath)
            });
            variables.Add("nolog", new List <string> {
                httpLoggingSection["dontLog"].ToString()
            });
            variables.Add("keep_alive", new List <string> {
                httpProtocolSection["allowKeepAlive"].ToString()
            });

            var indexes = new StringBuilder();

            foreach (ConfigurationElement item in filesCollection)
            {
                indexes.AppendFormat("{0},", item.RawAttributes["value"]);
            }

            if (indexes.Length > 0)
            {
                indexes.Length--;
            }

            variables.Add("indexes", new List <string> {
                indexes.ToString()
            });

            var allows = new List <string>();
            var denys  = new List <string>();

            foreach (ConfigurationElement item in ipSecuritySection.GetCollection())
            {
                string element = string.IsNullOrEmpty((string)item["subnetMask"])
                    ? (string)item["ipAddress"]
                    : string.Format("{0}/{1}", item["ipAddress"], item["subnetMask"]);
                if ((bool)item["allowed"])
                {
                    allows.Add(element);
                }
                else
                {
                    denys.Add(element);
                }
            }

            variables.Add("allowfrom", allows);
            variables.Add("denyfrom", denys);

            var segments = new StringBuilder();

            foreach (ConfigurationElement item in hiddenSegmentsCollection)
            {
                segments.AppendFormat("{0},", item["segment"]);
            }

            if (segments.Length > 0)
            {
                segments.Length--;
            }

            variables.Add("denydirs", new List <string> {
                segments.ToString()
            });

            foreach (ConfigurationElement item in httpErrorsCollection)
            {
                if ((uint)item["statusCode"] == 404 && (int)item["subStatusCode"] == 0 &&
                    (string)item["prefixLanguageFilePath"] == @"%SystemDrive%\inetpub\custerr" &&
                    (long)item["responseMode"] == 1)
                {
                    variables.Add("nofile", new List <string> {
                        item["path"].ToString()
                    });
                }
            }

            variables.Add("usegzip", new List <string> {
                urlCompressionSection["doStaticCompression"].ToString()
            });

            var rules = new List <string>();

            foreach (ConfigurationElement item in rewriteCollection)
            {
                var action = item.GetChildElement("action");
                var match  = item.GetChildElement("match");
                if ((long)action["type"] == 2)
                {
                    rules.Add(string.Format("{0}{2} {1}", match["url"], action["url"], (bool)match["ignoreCase"] ? "/i" : string.Empty));
                }
            }

            variables.Add("rewrite", rules);

            if (string.IsNullOrEmpty(application.Server.HostName))
            {
                var rows = new List <string>();
                foreach (var item in variables)
                {
                    foreach (var line in item.Value)
                    {
                        rows.Add(string.Format("{0}={1}", item.Key, line));
                    }
                }

                var fileName = Path.Combine("siteconf", application.ToFileName());
                File.WriteAllLines(fileName, rows);
            }
            else
            {
                using (var client = GetClient())
                {
                    HttpResponseMessage response = await client.PutAsJsonAsync(string.Format("api/site/{0}", application.ToFileName()), variables);

                    if (response.IsSuccessStatusCode)
                    {
                    }
                }
            }
        }
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            string targetDir  = Context.Parameters[Argument.TargetDir].TrimEnd('\\');
            string configFile = Path.Combine(targetDir, Assembly.GetExecutingAssembly().Location + ".config");

            System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(configFile);

            System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);

            AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(delegate(object sender, ResolveEventArgs args)
            {
                return(Assembly.LoadFile(Path.Combine(targetDir, args.Name + ".dll")));
            });

            UhuruSection section = (UhuruSection)config.GetSection("uhuru");

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.Capacity]))
            {
                section.Service.Capacity = int.Parse(Context.Parameters[Argument.Capacity], CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.BaseDir]))
            {
                section.Service.BaseDir = Context.Parameters[Argument.BaseDir];
                if (!Directory.Exists(section.Service.BaseDir))
                {
                    Directory.CreateDirectory(section.Service.BaseDir);
                }
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.Index]))
            {
                section.Service.Index = int.Parse(Context.Parameters[Argument.Index], CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.StatusPort]))
            {
                int port = Convert.ToInt32(Context.Parameters[Argument.StatusPort], CultureInfo.InvariantCulture);
                section.Service.StatusPort = port;
                if (port != 0)
                {
                    FirewallTools.OpenPort(port, "FileService Status");
                }
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.LocalDb]))
            {
                section.Service.LocalDB = Context.Parameters[Argument.LocalDb];
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.LocalRoute]))
            {
                section.Service.LocalRoute = Context.Parameters[Argument.LocalRoute];
            }
            else
            {
                string ip = string.Empty;
                foreach (IPAddress address in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
                {
                    if (address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    {
                        ip = address.ToString();
                        break;
                    }
                }

                section.Service.LocalRoute = ip;
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.Mbus]))
            {
                section.Service.MBus = Context.Parameters[Argument.Mbus];
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.MigrationNfs]))
            {
                section.Service.MigrationNFS = Context.Parameters[Argument.MigrationNfs];
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.NodeId]))
            {
                section.Service.NodeId = Context.Parameters[Argument.NodeId];
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.ZInterval]))
            {
                section.Service.ZInterval = int.Parse(Context.Parameters[Argument.ZInterval], CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.Plan]))
            {
                section.Service.Plan = Context.Parameters[Argument.Plan];
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.UseVhd]))
            {
                section.Service.Uhurufs.UseVHD = bool.Parse(Context.Parameters[Argument.UseVhd]);
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.MaxStorageSize]))
            {
                section.Service.Uhurufs.MaxStorageSize = long.Parse(Context.Parameters[Argument.MaxStorageSize], CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(Context.Parameters[Argument.VhdFixedSize]))
            {
                section.Service.Uhurufs.VHDFixedSize = bool.Parse(Context.Parameters[Argument.VhdFixedSize]);
            }

            section.DEA = null;
            config.Save();

            int lowPort  = 5000;
            int highPort = 6000;

            using (ServerManager serverManager = new ServerManager())
            {
                Microsoft.Web.Administration.Configuration iisConfig = serverManager.GetApplicationHostConfiguration();

                Microsoft.Web.Administration.ConfigurationSection firewallSupportSection = iisConfig.GetSection("system.ftpServer/firewallSupport");
                firewallSupportSection["lowDataChannelPort"]  = lowPort;
                firewallSupportSection["highDataChannelPort"] = highPort;

                Microsoft.Web.Administration.ConfigurationSection sitesSection           = iisConfig.GetSection("system.applicationHost/sites");
                Microsoft.Web.Administration.ConfigurationElement siteDefaultsElement    = sitesSection.GetChildElement("siteDefaults");
                Microsoft.Web.Administration.ConfigurationElement ftpServerElement       = siteDefaultsElement.GetChildElement("ftpServer");
                Microsoft.Web.Administration.ConfigurationElement firewallSupportElement = ftpServerElement.GetChildElement("firewallSupport");
                firewallSupportElement["externalIp4Address"] = @"0.0.0.0";

                serverManager.CommitChanges();
            }

            FirewallTools.OpenPortRange(lowPort, highPort, "UhuruFS Ports");
        }