1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2024-11-22 13:16:09 +03:00

完善校对时间功能3

This commit is contained in:
ProxySU 2020-04-01 20:59:34 +08:00
parent 58293b9817
commit 1bb1827e1c
3 changed files with 68 additions and 25 deletions

View File

@ -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);

View File

@ -26,7 +26,7 @@
<TextBlock Text="将远程主机时间同步为本地时间,注意:本机电脑的时间必须是准确的,与网络时间同步的,否则其他客户端中,有可能连接出错&#x0a;&#x0a;使用网络校时无法校对时间时,使用此种方法" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Margin="20"></TextBlock>
<RadioButton x:Name="RadioButtonUpDateLocalTime" Content="更新本机时间" Grid.Column="2" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将本机电脑时间与网络时间同步。" TextWrapping="Wrap" Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" Margin="20"></TextBlock>
<TextBlock Text="将本机电脑时间与网络时间同步。&#x0a;&#x0a;注意:使用此项功能,必须以管理员身份运行本程序" TextWrapping="Wrap" Grid.Column="2" Grid.Row="1" Grid.RowSpan="2" Margin="20"></TextBlock>
<Button x:Name="ButtonTestTime" Content="检测时间误差" Grid.Column="0" Grid.Row="3" Margin="30" Click="ButtonTestTime_Click"/>
<Button x:Name="ButtonProofreading" Content="校对时间" Grid.Column="2" Grid.Row="3" Margin="30" Click="ButtonProofreading_Click"></Button>

View File

@ -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
/// <returns>返回网络时间</returns>
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,9 +178,21 @@ 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个小时才是北京时间
}