From 58293b9817ed08fd3dc91e7f48c22e27b9859d23 Mon Sep 17 00:00:00 2001 From: ProxySU Date: Wed, 1 Apr 2020 12:14:33 +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=BD2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ProxySU/MainWindow.xaml.cs | 86 ++++++++++++--- ProxySU/ProofreadTimeWindow.xaml | 18 ++-- ProxySU/ProofreadTimeWindow.xaml.cs | 161 ++++++++++++++++++++++++++-- 3 files changed, 234 insertions(+), 31 deletions(-) diff --git a/ProxySU/MainWindow.xaml.cs b/ProxySU/MainWindow.xaml.cs index c1deecf..fc5ed1e 100644 --- a/ProxySU/MainWindow.xaml.cs +++ b/ProxySU/MainWindow.xaml.cs @@ -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(); - } + } } diff --git a/ProxySU/ProofreadTimeWindow.xaml b/ProxySU/ProofreadTimeWindow.xaml index 517a66b..ac8bdbb 100644 --- a/ProxySU/ProofreadTimeWindow.xaml +++ b/ProxySU/ProofreadTimeWindow.xaml @@ -17,18 +17,20 @@ + - - + + - - + + - - + + - + + diff --git a/ProxySU/ProofreadTimeWindow.xaml.cs b/ProxySU/ProofreadTimeWindow.xaml.cs index cb24480..2eb5cd4 100644 --- a/ProxySU/ProofreadTimeWindow.xaml.cs +++ b/ProxySU/ProofreadTimeWindow.xaml.cs @@ -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()); + } } + + /// + /// 网络时间 代码从网上复制,原网址:https://www.codeleading.com/article/23791981303/ + /// + public class NetTime + { + /// + /// 从国家授时中心获取标准GMT时间,读取https://www.tsa.cn + /// GMT时间与UTC时间没有差别,可以UTC=GMT + /// + /// 返回网络时间 + 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; + } + + } + + /// + /// 更新系统时间,代码从网上复制,原网址:https://www.open-open.com/code/view/1430552965599 + /// + 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; + } + + /// + /// 设置系统时间 + /// + /// 需要设置的时间 + /// 返回系统时间设置状态,true为成功,false为失败 + 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; + } + } + }