From 1bb1827e1c1915aad6a8043bb2a6428c8cfc95e2 Mon Sep 17 00:00:00 2001 From: ProxySU Date: Wed, 1 Apr 2020 20:59:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=A0=A1=E5=AF=B9=E6=97=B6?= =?UTF-8?q?=E9=97=B4=E5=8A=9F=E8=83=BD3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxySU/MainWindow.xaml.cs | 23 +++++++--- ProxySU/ProofreadTimeWindow.xaml | 2 +- ProxySU/ProofreadTimeWindow.xaml.cs | 68 +++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 25 deletions(-) diff --git a/ProxySU/MainWindow.xaml.cs b/ProxySU/MainWindow.xaml.cs index fc5ed1e..2ce56dd 100644 --- a/ProxySU/MainWindow.xaml.cs +++ b/ProxySU/MainWindow.xaml.cs @@ -861,9 +861,12 @@ namespace ProxySU MessageBox.Show("远程主机连接信息有误,请检查"); return; } + ProofreadTimeWindow proofreadTimeWindow = new ProofreadTimeWindow(); ProofreadTimeWindow.ProfreadTimeReceiveConnectionInfo = connectionInfo; + proofreadTimeWindow.ShowDialog(); + } private void ButtonGuideConfiguration_Click(object sender, RoutedEventArgs e) { @@ -1003,13 +1006,21 @@ namespace ProxySU private void Button_Click_5(object sender, RoutedEventArgs e) { //获取本地时间戳 - TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); - long timeStampLocal = Convert.ToInt64(ts.TotalSeconds); - MessageBox.Show("本地时间戳"+timeStampLocal.ToString()); + //TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); + //long timeStampLocal = Convert.ToInt64(ts.TotalSeconds); + //MessageBox.Show("本地时间戳"+timeStampLocal.ToString()); //获取网络时间戳 - TimeSpan utcTS = NetTime.GetUTCTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); - long timeStampVPS = Convert.ToInt64(utcTS.TotalSeconds); - MessageBox.Show("网络时间戳"+timeStampVPS.ToString()); + //Thread.Sleep(1000); + try + { + TimeSpan utcTS = NetTime.GetUTCTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); + long timeStampVPS = Convert.ToInt64(utcTS.TotalSeconds); + MessageBox.Show("网络时间戳" + timeStampVPS.ToString()); + } + catch(Exception ex1) + { + MessageBox.Show(ex1.ToString()); + } //string netDatetime = NetTime.GetUTCTime().AddHours(8).ToString(); //MessageBox.Show(netDatetime); diff --git a/ProxySU/ProofreadTimeWindow.xaml b/ProxySU/ProofreadTimeWindow.xaml index ac8bdbb..d6bcfeb 100644 --- a/ProxySU/ProofreadTimeWindow.xaml +++ b/ProxySU/ProofreadTimeWindow.xaml @@ -26,7 +26,7 @@ - + diff --git a/ProxySU/ProofreadTimeWindow.xaml.cs b/ProxySU/ProofreadTimeWindow.xaml.cs index 2eb5cd4..75e68dd 100644 --- a/ProxySU/ProofreadTimeWindow.xaml.cs +++ b/ProxySU/ProofreadTimeWindow.xaml.cs @@ -75,16 +75,13 @@ namespace ProxySU private void ButtonProofreading_Click(object sender, RoutedEventArgs e) { - using (var client = new SshClient(ProfreadTimeReceiveConnectionInfo)) + if (RadioButtonUpDateLocalTime.IsChecked == true) { - client.Connect(); - //设置vps为UTC时区 - client.RunCommand("rm -f /etc/localtime"); - client.RunCommand("ln -s /usr/share/zoneinfo/UTC /etc/localtime"); - if (RadioButtonUpDateLocalTime.IsChecked==true) + //将本机电脑与网络时间同步 + DateTime netUTCtime = NetTime.GetUTCTime(); + if (!DateTime.Equals(netUTCtime, new DateTime(1970, 1, 1, 0, 0, 0, 0))) { - //将本机电脑与网络时间同步 - DateTime localTime = NetTime.GetUTCTime().ToLocalTime(); + DateTime localTime = netUTCtime.ToLocalTime(); bool setD = UpdateTime.SetDate(localTime); if (setD == true) { @@ -94,9 +91,17 @@ namespace ProxySU { MessageBox.Show("更新失败,请重试。"); } - } - else if (RadioButtonLocalTime.IsChecked == true) + return; + } + using (var client = new SshClient(ProfreadTimeReceiveConnectionInfo)) + { + client.Connect(); + //设置vps为UTC时区 + client.RunCommand("rm -f /etc/localtime"); + client.RunCommand("ln -s /usr/share/zoneinfo/UTC /etc/localtime"); + + if (RadioButtonLocalTime.IsChecked == true) { //以本地时间为准,校正远程主机时间 //获取本地时间戳 @@ -108,7 +113,8 @@ namespace ProxySU //string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @1489739011)\""; //MessageBox.Show(sshCmd); client.RunCommand(sshCmd); - MessageBox.Show("校时完毕"); + MessageBox.Show("同步本地时间校时完毕"); + } else @@ -116,14 +122,24 @@ namespace ProxySU //以网络时间为准,校正远程主机时间 TimeSpan utcTS = NetTime.GetUTCTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0); long timeStampVPS = Convert.ToInt64(utcTS.TotalSeconds); - //MessageBox.Show(timeStampVPS.ToString()); - string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{timeStampVPS.ToString()})\""; - //MessageBox.Show(sshCmd); - client.RunCommand(sshCmd); - MessageBox.Show("校时完毕"); + if (timeStampVPS!=0) + { + //MessageBox.Show(timeStampVPS.ToString()); + string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{timeStampVPS.ToString()})\""; + //MessageBox.Show(sshCmd); + client.RunCommand(sshCmd); + MessageBox.Show("同步网络时间校时完毕"); + } + //else + //{ + // MessageBox.Show(timeStampVPS.ToString()); + // MessageBox.Show("同步网络时间失败"); + //} + } client.Disconnect(); } + } private void ButtonTEST_Click(object sender, RoutedEventArgs e) @@ -151,6 +167,10 @@ namespace ProxySU /// 返回网络时间 public static DateTime GetUTCTime() { + DateTime time; + ////Thread.Sleep(5000); + try + { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.tsa.cn"); request.Method = "HEAD"; request.AllowAutoRedirect = false; @@ -158,10 +178,22 @@ namespace ProxySU string cc = reponse.GetResponseHeader("date"); reponse.Close(); - DateTime time; bool s = GMTStrParse(cc, out time); return time; - + } + catch (Exception ex1) + { + if (ex1.ToString().Contains("403")) + { + MessageBox.Show("校时操作太频繁,请稍等片刻再操作!"); + } + else + { + MessageBox.Show(ex1.Message); + } + return time = new DateTime(1970, 1, 1, 0, 0, 0, 0); + } + //return time.AddHours(8); //GMT要加8个小时才是北京时间 } public static bool GMTStrParse(string gmtStr, out DateTime gmtTime) //抓取的date是GMT格式的字符串,这里转成datetime