Ejemplo n.º 1
0
 public static void CheckSysctl(bool isActive)
 {
     if (isActive)
     {
         Sysctl.WriteConfig();
         Sysctl.LoadConfig();
         ConsoleLogger.Log("    sysctl -> loaded");
     }
     else
     {
         ConsoleLogger.Log("    sysctl -> skipped");
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Ottiene un Master valorizzato coi valori ottenuti direttamente dal sistema operativo
        /// Potrebbe non corrispondere al Master salvato
        /// Viene utilizzato per ottenere le differenze tra i parametri salvati e quelli effettivamente applicati
        /// </summary>
        /// <returns></returns>
        public static MachineStatus GetRunning()
        {
            //var STOPWATCH = new System.Diagnostics.Stopwatch();
            //STOPWATCH.Start();
            var master = new MachineStatus();

            master.Info.Uptime    = Uptime.Get();
            master.Info.CpuInfo   = CpuInfo.Get();
            master.Info.MemInfo   = MemInfo.Get();
            master.Info.Free      = Free.Get();
            master.Info.Losetup   = Losetup.Get();
            master.Info.DiskUsage = DiskUsage.Get();
            master.Info.Versions  = Versioning.Get();

            master.Host     = Hostnamectl.Get();
            master.TimeDate = new TimeDate()
            {
                Timezone = Timedatectl.Get().Timezone
            };
            master.Boot = new Boot();
            var modules     = Mod.Get();
            var bootModules = new SystemModule[modules.Length];

            for (var i = 0; i < modules.Length; i++)
            {
                bootModules[i] = new SystemModule()
                {
                    Module = modules[i].Module, Active = true
                };
            }
            master.Boot.Modules           = bootModules;
            master.Boot.Services          = cmds.Systemctl.GetAll();
            master.Boot.Parameters        = Sysctl.Get();
            master.Users.SystemUsers      = Passwd.Get();
            master.Users.ApplicativeUsers = new ApplicativeUser[] { new ApplicativeUser()
                                                                    {
                                                                        Active = true, Type = AuthenticationType.simple, Id = "master", Claims = new[] { SHA.Generate("master") }
                                                                    } };

            master.Network.KnownDns          = Dns.GetResolv();
            master.Network.KnownHosts        = Dns.GetHosts();
            master.Network.KnownNetworks     = Dns.GetNetworks();
            master.Network.Bridges           = Brctl.Get();
            master.Network.Bonds             = Bond.Get();
            master.Network.NetworkInterfaces = cmds.Network.Get();
            master.Network.RoutingTables     = Route.GetRoutingTable();
            master.Network.Routing           = Route.Get();

            master.NsSwitch = NS.Switch.Get();

            master.Storage.Mounts       = Mount.Get();
            master.Storage.Zpools       = Zpool.GetPools();
            master.Storage.ZfsDatasets  = Zfs.GetDatasets();
            master.Storage.ZfsSnapshots = Zfs.GetSnapshots();

            master.Services.Ssh.PublicKey     = Ssh.GetRootPublicKey();
            master.Services.Ssh.PrivateKey    = Ssh.GetRootPrivateKey();
            master.Services.Ssh.AuthorizedKey = Ssh.GetAuthorizedKey();

            master.Services.Virsh.Domains = Virsh.GetDomains();

            //ConsoleLogger.Log($"[conf] loaded running conf ({STOPWATCH.ElapsedMilliseconds})");
            return(master);
        }
Ejemplo n.º 3
0
        public SystemModule()
            : base("/system")
        {
            this.RequiresAuthentication();

            Get["/"] = x => {
                dynamic vmod = new ExpandoObject();
                vmod.Hostname           = Command.Launch("hostname", "").output;
                vmod.Domainname         = Command.Launch("hostname", "-f").output;
                vmod.Timezone           = Command.Launch("timedatectl", "").output;
                vmod.Timeserver         = "time.server.net";
                vmod.Language           = "English";
                vmod.TCPport            = "";
                vmod.MaxProcesses       = "2";
                vmod.AlternateHostnames = "";
                vmod.SSHPort            = "22";
                return(View["_page-system", vmod]);
            };

            Get["/mounts"] = x => {
                dynamic vmod = new ExpandoObject();
                vmod.MountRunning = Mount.Running;
                vmod.MountAntd    = Mount.Antd;
                return(View["_page-system-mounts", vmod]);
            };

            Get["/sysctl"] = x => {
                dynamic vmod = new ExpandoObject();
                vmod.Sysctl = VHStatus.Sysctl(Sysctl.Stock, Sysctl.Running, Sysctl.Antd);
                return(View["_page-system-sysctl", vmod]);
            };

            Get["/conf"] = x => {
                dynamic vmod = new ExpandoObject();
                HashSet <DirItemModel> etcList = new DirectoryLister("/etc", true).FullList2;
                HashSet <DirItemModel> cfgList = new DirectoryLister("/antd/etc", true).FullList2;
                List <dynamic>         nl      = new List <dynamic>()
                {
                };
                foreach (DirItemModel dir in etcList)
                {
                    dynamic imod = new ExpandoObject();
                    imod.isFile  = dir.isFile;
                    imod.etcPath = dir.path;
                    bool   hasCfg;
                    string cfgPath;
                    string cfgName;
                    string p = dir.path.ConvertPathToFileName().Replace("D:", "");
                    string c = (from i in cfgList
                                where i.name == p
                                select i.path).FirstOrDefault();
                    if (c == null)
                    {
                        hasCfg  = false;
                        cfgPath = "";
                        cfgName = "";
                    }
                    else
                    {
                        hasCfg  = true;
                        cfgPath = c;
                        cfgName = Path.GetFileName(c);
                    }
                    imod.hasCfg  = hasCfg;
                    imod.cfgPath = cfgPath;
                    imod.cfgName = cfgName;
                    nl.Add(imod);
                }
                vmod.Conf = nl;
                return(View["_page-system-conf", vmod]);
            };

            Post["/export/file/{path*}"] = x => {
                string path = x.path;
                ConfigEtc.Export(path);
                return(Response.AsJson("done"));
            };

            Get["/read/file/{path*}"] = x => {
                string path = x.path;
                string text = FileSystem.ReadFile(path.RemoveDriveLetter());
                return(Response.AsJson(text));
            };

            Post["/file"] = x => {
                string path    = this.Request.Form.FilePath;
                string content = this.Request.Form.FileContent;
                ConfigEtc.EditFile(path, content);
                return(Response.AsRedirect("/system"));
            };

            Post["/sysctl/{param}/{value}"] = x => {
                string param  = x.param;
                string value  = x.value;
                var    output = Sysctl.Config(param, value);
                return(Response.AsJson(output));
            };

            Get["/wizard"] = x => {
                dynamic vmod = new ExpandoObject();
                return(View["page-wizard", vmod]);
            };
        }
Ejemplo n.º 4
0
 private static void SetParameters()
 {
     Sysctl.SaveDefaultValues();
     RunningConfiguration.Boot.Parameters = Sysctl.Get();
     Sysctl.Set();
 }
Ejemplo n.º 5
0
        public BootModule() : base("/boot")
        {
            Get["/parameters"] = x => {
                var current = Application.CurrentConfiguration.Boot.Parameters;
                var running = Application.RunningConfiguration.Boot.Parameters;
                var result  = new BootParameterJoin[current.Length];
                for (var i = 0; i < current.Length; i++)
                {
                    var param = current[i];
                    var value = running.FirstOrDefault(_ => _.Key == param.Key) == null ? string.Empty : running.FirstOrDefault(_ => _.Key == param.Key).Value;
                    result[i] = new BootParameterJoin()
                    {
                        Key     = param.Key,
                        Value   = param.Value,
                        Running = value
                    };
                }
                return(JsonConvert.SerializeObject(result));
            };

            Get["/modules"] = x => {
                return(JsonConvert.SerializeObject(Application.CurrentConfiguration.Boot.Modules));
            };

            Get["/modules/list"] = x => {
                return(JsonConvert.SerializeObject(Mod.GetList()));
            };

            Get["/services"] = x => {
                return(JsonConvert.SerializeObject(Application.CurrentConfiguration.Boot.Services));
            };

            Get["/services/list"] = x => {
                return(JsonConvert.SerializeObject(Systemctl.GetList()));
            };

            Get["/commands"] = x => {
                return(JsonConvert.SerializeObject(Application.CurrentConfiguration.SetupCommands));
            };

            Post["/save/parameters"] = x => {
                string data    = Request.Form.Data;
                var    objects = JsonConvert.DeserializeObject <SystemParameter[]>(data);
                Application.CurrentConfiguration.Boot.Parameters = objects;
                ConfigRepo.Save();
                return(HttpStatusCode.OK);
            };

            Post["/save/modules"] = x => {
                string data    = Request.Form.Data;
                var    objects = JsonConvert.DeserializeObject <SystemModule[]>(data);
                Application.CurrentConfiguration.Boot.Modules = objects;
                ConfigRepo.Save();
                return(HttpStatusCode.OK);
            };

            Post["/save/services"] = x => {
                string data    = Request.Form.Data;
                var    objects = JsonConvert.DeserializeObject <SystemService[]>(data);
                Application.CurrentConfiguration.Boot.Services = objects;
                ConfigRepo.Save();
                return(HttpStatusCode.OK);
            };

            Post["/save/commands"] = x => {
                string data    = Request.Form.Data;
                var    objects = JsonConvert.DeserializeObject <Command[]>(data);
                Application.CurrentConfiguration.SetupCommands = objects;
                ConfigRepo.Save();
                return(HttpStatusCode.OK);
            };

            Post["/apply/parameters"] = x => {
                Sysctl.Set();
                return(HttpStatusCode.OK);
            };

            Post["/apply/modules"] = x => {
                Mod.Set();
                return(HttpStatusCode.OK);
            };

            Post["/apply/services"] = x => {
                Systemctl.Set();
                return(HttpStatusCode.OK);
            };

            Post["/apply/commands"] = x => {
                SetupCommands.Set();
                return(HttpStatusCode.OK);
            };
        }
Ejemplo n.º 6
0
        public SystemModule()
        {
            this.RequiresAuthentication();

            Post["/system/cctable"] = x => {
                var    label         = (string)Request.Form.Label;
                var    inputLabel    = (string)Request.Form.InputLabel;
                var    notes         = (string)Request.Form.Notes;
                var    inputId       = "New" + CctableContextName.UppercaseAllFirstLetters().RemoveWhiteSpace() + label.UppercaseAllFirstLetters().RemoveWhiteSpace();
                string inputLocation = "CCTable" + Request.Form.TableName;
                switch ((string)Request.Form.InputType.Value)
                {
                case "hidden":
                    var directCommand = (string)Request.Form.CommandDirect;
                    CCTableRepository.New.CreateRowForDirectCommand(CctableContextName, label, inputLabel, directCommand,
                                                                    notes, inputId, inputLocation);
                    break;

                case "text":
                    var setCommand = (string)Request.Form.CommandSet;
                    var getCommand = (string)Request.Form.CommandGet;
                    CCTableRepository.New.CreateRowForTextInputCommand(CctableContextName, label, inputLabel, setCommand,
                                                                       getCommand, notes, inputId, inputLocation);
                    break;

                case "checkbox":
                    var enableCommand  = (string)Request.Form.CommandTrue;
                    var disableCommand = (string)Request.Form.CommandFalse;
                    CCTableRepository.New.CreateRowForBooleanPairCommand(CctableContextName, label, inputLabel,
                                                                         enableCommand, disableCommand, notes, inputId, inputLocation);
                    break;
                }
                return(Response.AsRedirect("/"));
            };

            Get["/system/auth/disable"] = x => {
                ApplicationSetting.DisableTwoFactorAuth();
                return(Response.AsJson(true));
            };

            Get["/system/auth/enable"] = x => {
                ApplicationSetting.EnableTwoFactorAuth();
                return(Response.AsJson(true));
            };

            Get["/system/mounts"] = x => {
                dynamic vmod = new ExpandoObject();
                return(View["_page-system-mounts", vmod]);
            };

            Post["/system/mount/unit"] = x => {
                var    guid       = Request.Form.Guid;
                string unit       = Request.Form.Unit;
                var    unitsSplit = unit.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToArray();
                if (unitsSplit.Length > 0)
                {
                    MountRepository.AddUnit(guid, unitsSplit);
                }
                return(Response.AsRedirect("/"));
            };

            Delete["/system/mount/unit"] = x => {
                var guid = Request.Form.Guid;
                var unit = Request.Form.Unit;
                MountRepository.RemoveUnit(guid, unit);
                return(Response.AsJson(true));
            };

            Get["/system/sysctl"] = x => {
                dynamic vmod = new ExpandoObject();
                vmod.Sysctl = VhStatus.Sysctl(Sysctl.Stock, Sysctl.Running, Sysctl.Antd);
                return(View["_page-system-sysctl", vmod]);
            };

            Post["/system/sysctl/{param}/{Value}"] = x => {
                string param  = x.param;
                string value  = x.value;
                var    output = Sysctl.Config(param, value);
                return(Response.AsJson(output));
            };

            Post["/system/import/info"] = x => {
                SystemInfo.Import();
                return(Response.AsJson(true));
            };
        }