Exemplo n.º 1
0
        public AntdTimeModule()
        {
            Get["/time/info"] = x => {
                const StringSplitOptions ssoree = StringSplitOptions.RemoveEmptyEntries;
                var bash              = new Bash();
                var launcher          = new CommandLauncher();
                var hostConfiguration = new HostConfiguration();
                var timezones         = bash.Execute("timedatectl list-timezones --no-pager").SplitBash();
                var timedatectl       = launcher.Launch("timedatectl").ToList();
                var ntpd              = launcher.Launch("cat-etc-ntp").ToArray();
                var model             = new PageTimeModel {
                    Timezones = timezones,
                    LocalTime = timedatectl.First(_ => _.Contains("Local time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    UnivTime  = timedatectl.First(_ => _.Contains("Universal time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    RtcTime   = timedatectl.First(_ => _.Contains("RTC time:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Timezone  = timedatectl.First(_ => _.Contains("Time zone:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Nettimeon = timedatectl.First(_ => _.Contains("Network time on:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Ntpsync   = timedatectl.First(_ => _.Contains("NTP synchronized:")).Split(new[] { ":" }, 2, ssoree)[1],
                    Rtcintz   = timedatectl.First(_ => _.Contains("RTC in local TZ:")).Split(new[] { ":" }, 2, ssoree)[1],
                    NtpServer = hostConfiguration.Host.NtpdateServer.StoredValues["$server"],
                    Ntpd      = ntpd.JoinToString("<br />"),
                    NtpdEdit  = ntpd.JoinToString(Environment.NewLine)
                };
                return(JsonConvert.SerializeObject(model));
            };

            Post["/host/timezone"] = x => {
                string timezone          = Request.Form.Timezone;
                var    hostconfiguration = new HostConfiguration();
                hostconfiguration.SetTimezone(timezone);
                hostconfiguration.ApplyTimezone();
                return(HttpStatusCode.OK);
            };

            Post["/host/synctime"] = x => {
                var hostconfiguration = new HostConfiguration();
                hostconfiguration.SyncClock();
                return(HttpStatusCode.OK);
            };

            Post["/host/ntpdate"] = x => {
                string ntpdate           = Request.Form.Ntpdate;
                var    hostconfiguration = new HostConfiguration();
                hostconfiguration.SetNtpdate(ntpdate);
                hostconfiguration.ApplyNtpdate();
                return(HttpStatusCode.OK);
            };

            Post["/host/ntpd"] = x => {
                string ntpd = Request.Form.Ntpd;
                var    hostConfiguration = new HostConfiguration();
                hostConfiguration.SetNtpd(ntpd.Contains("\n")
                  ? ntpd.SplitToList("\n").ToArray()
                  : ntpd.SplitToList(Environment.NewLine).ToArray());
                hostConfiguration.ApplyNtpd();
                return(HttpStatusCode.OK);
            };
        }