1
0
mirror of https://github.com/proxysu/ProxySU.git synced 2025-02-16 14:43:14 +03:00

完善校对时间功能2

This commit is contained in:
ProxySU 2020-04-01 12:14:33 +08:00
parent 9754dab3cf
commit 58293b9817
3 changed files with 234 additions and 31 deletions

View File

@ -21,6 +21,11 @@ using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Drawing;
using QRCoder;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Runtime;
using System.Globalization;
namespace ProxySU
{
@ -847,6 +852,19 @@ namespace ProxySU
WindowTemplateConfiguration windowTemplateConfiguration = new WindowTemplateConfiguration();
windowTemplateConfiguration.ShowDialog();
}
//打开系统工具中的校对时间窗口
private void ButtonProofreadTime_Click(object sender, RoutedEventArgs e)
{
ConnectionInfo connectionInfo = GenerateConnectionInfo();
if (connectionInfo == null)
{
MessageBox.Show("远程主机连接信息有误,请检查");
return;
}
ProofreadTimeWindow proofreadTimeWindow = new ProofreadTimeWindow();
ProofreadTimeWindow.ProfreadTimeReceiveConnectionInfo = connectionInfo;
proofreadTimeWindow.ShowDialog();
}
private void ButtonGuideConfiguration_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("尚未完善,敬请期待");
@ -868,8 +886,11 @@ namespace ProxySU
private void Button_Click_1(object sender, RoutedEventArgs e)
{
ResultClientInformation resultClientInformation = new ResultClientInformation();
resultClientInformation.ShowDialog();
//ResultClientInformation resultClientInformation = new ResultClientInformation();
//resultClientInformation.ShowDialog();
string stampTime = "1489739011";
string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{stampTime})\"";
MessageBox.Show(sshCmd);
}
private void Button_Click_2(object sender, RoutedEventArgs e)
@ -981,9 +1002,51 @@ namespace ProxySU
private void Button_Click_5(object sender, RoutedEventArgs e)
{
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timeStamp = Convert.ToInt64(ts.TotalSeconds);
MessageBox.Show(timeStamp.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());
//string netDatetime = NetTime.GetUTCTime().AddHours(8).ToString();
//MessageBox.Show(netDatetime);
//TimeZoneInfo localZone = TimeZoneInfo.Local;
////MessageBox.Show($"Local Time Zone ID: {localZone.Id}");
//MessageBox.Show($"Display Name is: {localZone.DisplayName}.");
//MessageBox.Show($"Standard name is: {localZone.StandardName}.");
//MessageBox.Show($" Daylight saving name is: {localZone.DaylightName}.");
//HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.tsa.cn");
//request.Method = "HEAD";
//request.AllowAutoRedirect = false;
//HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
//string cc = reponse.GetResponseHeader("date");
//reponse.Close();
//MessageBox.Show(cc);
//DateTime time;
//bool s = GMTStrParse(cc, out time);
//return time.AddHours(8); //GMT要加8个小时才是北京时间
//WebRequest wrt = null;
//WebResponse wrp = null;
//wrt = WebRequest.Create("https://www.tsa.cn/");
//wrp = wrt.GetResponse();
//string html = string.Empty;
//using (Stream stream = wrp.GetResponseStream())
//{
// using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
// {
// html = sr.ReadToEnd();
// using (StreamWriter sw = new StreamWriter(@"html.txt"))
// {
// sw.Write(html.ToString());
// }
// }
//}
//TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
//long timeStamp = Convert.ToInt64(ts.TotalSeconds);
//MessageBox.Show(timeStamp.ToString());
//DateTime dateNow = DateTime.Now;
//MessageBox.Show(TimeZoneInfo.ConvertTimeToUtc(dateNow).ToString());
@ -991,18 +1054,7 @@ namespace ProxySU
// TimeZoneInfo.ConvertTimeToUtc(dateNow));
}
private void ButtonProofreadTime_Click(object sender, RoutedEventArgs e)
{
ConnectionInfo connectionInfo = GenerateConnectionInfo();
if (connectionInfo == null)
{
MessageBox.Show("远程主机连接信息有误,请检查");
return;
}
ProofreadTimeWindow proofreadTimeWindow = new ProofreadTimeWindow();
ProofreadTimeWindow.ProfreadTimeReceiveConnectionInfo = connectionInfo;
proofreadTimeWindow.ShowDialog();
}
}
}

View File

@ -17,18 +17,20 @@
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<RadioButton x:Name="RadioButtonNetworkTime" Content="网络时" Grid.Column="0" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将本地时间与远程主机时间同时校正为网络时间" TextWrapping="Wrap" Grid.Column="0" Grid.Row="1" Margin="30"></TextBlock>
<RadioButton x:Name="RadioButtonNetworkTime" IsChecked="True" Content="同步网络时" Grid.Column="0" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将远程主机时间校正为网络时间注意本机电脑的时间必须也是与网络时间同步的否则本机电脑V2ray有可能连接出错&#x0a;&#x0a;推荐:优先使用此方法校对时间" TextWrapping="Wrap" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Margin="20"></TextBlock>
<RadioButton x:Name="RadioButtonVPStime" Content="远程主机时间" Grid.Column="1" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将本地时间校正为远程主机时间" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Margin="30"></TextBlock>
<RadioButton x:Name="RadioButtonLocalTime" Content="同步本机时间" Grid.Column="1" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将远程主机时间同步为本地时间,注意:本机电脑的时间必须是准确的,与网络时间同步的,否则其他客户端中,有可能连接出错&#x0a;&#x0a;使用网络校时无法校对时间时,使用此种方法" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Grid.RowSpan="2" Margin="20"></TextBlock>
<RadioButton x:Name="RadioButtonLocalTime" Content="本地时间" Grid.Column="2" Grid.Row="0" Margin="40"></RadioButton>
<TextBlock Text="将远程主机时间校正为本地时间" TextWrapping="Wrap" Grid.Column="2" Grid.Row="1" Margin="30"></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>
<Button x:Name="ButtonTestTime" Content="检测时间误差" Grid.Column="0" Grid.Row="2" Margin="40" Click="ButtonTestTime_Click"/>
<Button x:Name="ButtonProofreading" Content="校对时间" Grid.Column="2" Grid.Row="2" Margin="40"></Button>
<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>
<Button x:Name="ButtonTEST" Content="测试命令" Grid.Column="1" Grid.Row="3" Margin="30" Click="ButtonTEST_Click"></Button>
</Grid>
</Grid>
</Window>

View File

@ -20,6 +20,12 @@ using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
using System.Drawing;
using QRCoder;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Runtime;
using System.Globalization;
namespace ProxySU
{
@ -30,7 +36,8 @@ namespace ProxySU
{
public static ConnectionInfo ProfreadTimeReceiveConnectionInfo { get; set; }
//ProfreadTimeReceiveParameters
public ProofreadTimeWindow()
public ProofreadTimeWindow()
{
InitializeComponent();
@ -41,16 +48,19 @@ namespace ProxySU
using (var client = new SshClient(ProfreadTimeReceiveConnectionInfo))
{
client.Connect();
client.RunCommand("rm -f /etc/localtime");
client.RunCommand("ln -s /usr/share/zoneinfo/UTC /etc/localtime");
//获取远程主机的时间戳
long timeStampVPS = Convert.ToInt64(client.RunCommand("date +%s").Result.ToString());
//MessageBox.Show(timesStampVPS.ToString());
//获取本地时间戳
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timeStampLocal = Convert.ToInt64(ts.TotalSeconds);
client.Disconnect();
if (Math.Abs(timeStampLocal - timeStampVPS) >= 90)
{
MessageBox.Show("本地时间与远程主机时间相差超过限制(90秒)请先用\"系统工具-->时间校对\"校对时间后再设置");
MessageBox.Show("本地时间与远程主机时间相差超过限制(90秒)V2ray无法建立连接");
//currentStatus = "时间较对失败......";
//textBlockName.Dispatcher.BeginInvoke(updateAction, textBlockName, progressBar, currentStatus);
//Thread.Sleep(1000);
@ -58,14 +68,153 @@ namespace ProxySU
}
else
{
MessageBox.Show("误差为:"+Math.Abs(timeStampLocal - timeStampVPS).ToString());
MessageBox.Show("误差为:" + Math.Abs(timeStampLocal - timeStampVPS).ToString()+" 可以连接");
}
}
}
private void ButtonProofreading_Click(object sender, RoutedEventArgs e)
{
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 (RadioButtonUpDateLocalTime.IsChecked==true)
{
//将本机电脑与网络时间同步
DateTime localTime = NetTime.GetUTCTime().ToLocalTime();
bool setD = UpdateTime.SetDate(localTime);
if (setD == true)
{
MessageBox.Show("本机时间已经更新为网络时间(国家授时中心获取)");
}
else
{
MessageBox.Show("更新失败,请重试。");
}
}
else if (RadioButtonLocalTime.IsChecked == true)
{
//以本地时间为准,校正远程主机时间
//获取本地时间戳
TimeSpan ts = DateTime.Now.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
long timeStampLocal = Convert.ToInt64(ts.TotalSeconds);
//string stampTime = timeStampLocal.ToString();
string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @{timeStampLocal.ToString()})\"";
//MessageBox.Show(sshCmd);
//string sshCmd = $"date --set=\"$(date \"+%Y-%m-%d %H:%M:%S\" -d @1489739011)\"";
//MessageBox.Show(sshCmd);
client.RunCommand(sshCmd);
MessageBox.Show("校时完毕");
//MainWindow.ConnectionInfo
//using (var client = new SshClient(MainWindow.ConnectionInfo))
}
else
{
//以网络时间为准,校正远程主机时间
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("校时完毕");
}
client.Disconnect();
}
}
private void ButtonTEST_Click(object sender, RoutedEventArgs e)
{
//NetTime netTime = new NetTime();
string netDatetime = NetTime.GetUTCTime().ToString();
MessageBox.Show(netDatetime);
//NetTime netTime = new NetTime();
//UpdateTime updateTime = new UpdateTime();
//DateTime netDateTime = netTime.GetBeijingTime();
//MessageBox.Show(netDateTime.ToString());
}
}
/// <summary>
/// 网络时间 代码从网上复制原网址https://www.codeleading.com/article/23791981303/
/// </summary>
public class NetTime
{
/// <summary>
/// 从国家授时中心获取标准GMT时间读取https://www.tsa.cn
/// GMT时间与UTC时间没有差别可以UTC=GMT
/// </summary>
/// <returns>返回网络时间</returns>
public static DateTime GetUTCTime()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.tsa.cn");
request.Method = "HEAD";
request.AllowAutoRedirect = false;
HttpWebResponse reponse = (HttpWebResponse)request.GetResponse();
string cc = reponse.GetResponseHeader("date");
reponse.Close();
DateTime time;
bool s = GMTStrParse(cc, out time);
return time;
//return time.AddHours(8); //GMT要加8个小时才是北京时间
}
public static bool GMTStrParse(string gmtStr, out DateTime gmtTime) //抓取的date是GMT格式的字符串这里转成datetime
{
CultureInfo enUS = new CultureInfo("en-US");
bool s = DateTime.TryParseExact(gmtStr, "r", enUS, DateTimeStyles.None, out gmtTime);
return s;
}
}
/// <summary>
/// 更新系统时间代码从网上复制原网址https://www.open-open.com/code/view/1430552965599
/// </summary>
public class UpdateTime
{
//设置系统时间的API函数
[DllImport("kernel32.dll")]
private static extern bool SetLocalTime(ref SYSTEMTIME time);
[StructLayout(LayoutKind.Sequential)]
private struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}
/// <summary>
/// 设置系统时间
/// </summary>
/// <param name="dt">需要设置的时间</param>
/// <returns>返回系统时间设置状态true为成功false为失败</returns>
public static bool SetDate(DateTime dt)
{
SYSTEMTIME st;
st.year = (short)dt.Year;
st.month = (short)dt.Month;
st.dayOfWeek = (short)dt.DayOfWeek;
st.day = (short)dt.Day;
st.hour = (short)dt.Hour;
st.minute = (short)dt.Minute;
st.second = (short)dt.Second;
st.milliseconds = (short)dt.Millisecond;
bool rt = SetLocalTime(ref st);
return rt;
}
}
}