├── .gitignore ├── LICENSE ├── README.md ├── README_en.md ├── WINtp.cs ├── WINtp.csproj ├── WINtp.exe.config ├── WINtp.manifest ├── WINtp.sln ├── wInstall.bat ├── wUninstall.bat └── wintp.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore 2 | bin/ 3 | .vs/ 4 | obj/ 5 | *.csproj.user 6 | *.xml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WINtp —— NTP 时间同步客户端,更加快速的时间同步实现 2 | 3 | [![Latest Version](https://img.shields.io/github/v/release/lalakii/WINtp?logo=github)](https://github.com/lalakii/WINtp/releases) 4 | [![License: Apache-2.0 (shields.io)](https://img.shields.io/badge/License-Apache--2.0-c02041?logo=apache)](LICENSE) 5 | 6 | [Download WINtp](https://sourceforge.net/projects/wintp/) 7 | 8 | [WINtp Logo](https://sourceforge.net/projects/wintp/) 9 | 10 | [ [中文](./README.md) | [English](./README_en.md) ] 11 | 12 | ## 它是一个简单的时钟同步小工具,目前适用于 Windows 操作系统 13 | 14 | 因为 Windows 默认的时间同步似乎有点缓慢,所以我制作了它, 15 | 16 | 它现在支持通过 TCP 协议,从 HTTP(s) 协议的网站上获取时间。 17 | 18 | ## 下载地址 19 | 20 | - [本地下载](https://github.com/lalakii/WINtp/releases) 21 | - [蓝奏云 1](https://a01.lanzoui.com/ifqXI2orftha) 22 | - [蓝奏云 2](https://a01.lanzout.com/ifqXI2orftha) 23 | - [蓝奏云 3](https://a01.lanzouv.com/ifqXI2orftha) 24 | 25 | ### 如何使用它? 26 | 27 | 通常只需要双击运行即可,软件无 UI 界面,启动后会自动同步系统时间 28 | 29 | 完成后立即退出,无后台 30 | 31 | 推荐将其安装为系统服务,也可以自行设置为开机启动项,可根据自己的喜好决定。 32 | 33 | ### 配置文件示例 34 | 35 | ```conf 36 | # 一个简单的时间同步小工具 WINtp 37 | # 38 | # 此配置文件是必须的,用于存储程序运行所需的配置参数 39 | # 如果需要将其恢复默认值,直接删除它,程序启动后会自动生成一份新的 40 | # 如果你想要自定义 NTP 服务器,按照规律添加即可: 41 | # 42 | Ntps = time.asia.apple.com;ntp.tencent.com;ntp.aliyun.com;rhel.pool.ntp.org; 43 | 44 | # 【自动同步系统时间】开关,如果被注释了,或是值为 false 将不会同步系统时间(防止杀软误报添加这个配置项) 45 | # 46 | AutoSyncTime = true 47 | 48 | # 【定时同步】默认单位是秒,3600 秒即每小时同步一次。默认不开启,以服务运行时才会生效 49 | # 50 | # Delay = 3600 51 | 52 | # 如果因为某些原因,无法访问 UDP 端口 53 | # 这时你可以取消下面的注释,程序将通过 TCP 协议,从指定的网站上获取时间 54 | # 这个选项不会与 Ntps = 冲突,它们可以同时存在,也可以二选一 55 | # 如果 NTP 可以同步时间,就不要启用它。 56 | # 57 | # Urls = www.baidu.com;www.qq.com;www.google.com; 58 | 59 | # 默认从 80 端口获取数据,如需使用 443,取消下面的注释 60 | # 61 | # UseSSL = true 62 | 63 | # 如果【需要打印详细信息】,取消下面的注释 64 | # 65 | Verbose = true 66 | 67 | # 如果【需要自定义超时】,修改下面的参数,单位是毫秒 68 | # 网络较差时可将数值调大一些 69 | # 默认30秒无法获取时间即超时,程序退出,并在事件查看器打印错误信息 70 | # 71 | # Timeout = 30000 72 | 73 | #################### 74 | # by lalaki.cn ## 75 | #################### 76 | ``` 77 | 78 | ### By lalaki.cn 79 | -------------------------------------------------------------------------------- /README_en.md: -------------------------------------------------------------------------------- 1 | # WINtp — NTP time synchronization client, providing a faster implementation for time synchronization. 2 | 3 | [![Latest Version](https://img.shields.io/github/v/release/lalakii/WINtp?logo=github)](https://github.com/lalakii/WINtp/releases) 4 | [![License: Apache-2.0 (shields.io)](https://img.shields.io/badge/License-Apache--2.0-c02041?logo=apache)](LICENSE) 5 | 6 | [Download WINtp](https://sourceforge.net/projects/wintp/) 7 | 8 | [WINtp Logo](https://sourceforge.net/projects/wintp/) 9 | 10 | [ [中文](./README.md) | [English](./README_en.md) ] 11 | 12 | ## It is a simple clock synchronization tool, currently suitable for Windows operating systems 13 | 14 | Because the default time synchronization in Windows seems a bit slow, 15 | 16 | I created this tool. It now supports retrieving time from websites using the TCP protocol over HTTP(s). 17 | 18 | ## Downloads 19 | 20 | - [Github](https://github.com/lalakii/WINtp/releases) 21 | - [Lanzou 1](https://a01.lanzoui.com/ifqXI2orftha) 22 | - [Lanzou 2](https://a01.lanzout.com/ifqXI2orftha) 23 | - [Lanzou 3](https://a01.lanzouv.com/ifqXI2orftha) 24 | 25 | ### How to use it? 26 | 27 | Typically, you just need to double-click to run it. The software has no UI interface; it will automatically synchronize the system time after starting. 28 | 29 | Once completed, it exits immediately and runs no background processes. 30 | 31 | It is recommended to install it as a system service, but you can also set it as a startup item according to your preference. 32 | 33 | ### Configuration 34 | 35 | ```conf 36 | # A simple time synchronization tool: WINtp. 37 | # 38 | # This configuration file is required to store the configuration parameters needed for the program to run. 39 | # If you need to restore it to default values, simply delete it, and a new one will be automatically generated upon program startup. 40 | # If you want to customize the NTP server, just add it following the pattern: 41 | # 42 | Ntps = time.asia.apple.com;ntp.tencent.com;ntp.aliyun.com;rhel.pool.ntp.org; 43 | 44 | # The "Automatic Time Synchronization" switch will not sync the system time if commented out or set to false (to prevent antivirus false positives). 45 | # 46 | AutoSyncTime = true 47 | 48 | # The default unit for "Scheduled Synchronization" is seconds, with 3600 seconds meaning synchronization occurs once per hour. It is disabled by default and will only take effect when the service is running. 49 | # 50 | # Delay = 3600 51 | 52 | # If, for some reason, you cannot access the UDP port, you can uncomment the line below, and the program will retrieve the time via TCP protocol from the specified website. 53 | # This option does not conflict with NTP; both can coexist or be used as alternatives. 54 | # If NTP can synchronize the time, do not enable this option. 55 | # 56 | # Urls = www.baidu.com;www.qq.com;www.google.com; 57 | # 58 | # The default is to retrieve data from port 80. If you need to use port 443, uncomment the line below. 59 | # 60 | # UseSSL = true 61 | 62 | # If you need to print detailed information, uncomment the line below. 63 | # 64 | # Verbose = true 65 | 66 | # If you need to customize the timeout, modify the parameter below, in milliseconds 67 | # You can increase the value if the network is poor 68 | # The default timeout is 30 seconds; if the time cannot be obtained, the program will exit and print an error message in the event viewer. 69 | # 70 | # Timeout = 30000 71 | 72 | #################### 73 | # by lalaki.cn ## 74 | #################### 75 | ``` 76 | 77 | ### By lalaki.cn 78 | -------------------------------------------------------------------------------- /WINtp.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | using System.Net; 3 | using System.Net.Sockets; 4 | using System.Reflection; 5 | using System.Runtime.InteropServices; 6 | 7 | [assembly: AssemblyProduct("WINtp")] 8 | [assembly: AssemblyVersion("1.8.0.0")] 9 | [assembly: AssemblyFileVersion("1.8.0.0")] 10 | [assembly: AssemblyTitle("A Simple NTP Client")] 11 | [assembly: AssemblyCopyright("Copyright (C) 2025 lalaki.cn")] 12 | 13 | [System.ComponentModel.DesignerCategory("")] 14 | public class WINtp : System.ServiceProcess.ServiceBase 15 | { 16 | private const int HttpRequestType = 1; 17 | private const int NetworkConnectionError = -3; 18 | private const int NtpRequestType = 0; 19 | private const int ProcessStartFailure = -2; 20 | private const int TimeoutError = -1; 21 | private static readonly string[] PArray = ["-k", "/k"]; 22 | private static readonly DateTime TimeOf1900 = new(1900, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); 23 | private static readonly Assembly Wintp = typeof(WINtp).Assembly; 24 | private static SystemTime st; 25 | private bool autoSync; 26 | private int delay; 27 | private Timer? timer; 28 | private bool useSSL; 29 | private bool verbose; 30 | 31 | public static void Main(string[] args) 32 | { 33 | using WINtp ntp = new(); 34 | if (args != null && PArray.Contains(args.LastOrDefault(), StringComparer.OrdinalIgnoreCase)) 35 | { 36 | Run(ntp); 37 | } 38 | else 39 | { 40 | ntp.LoadProfile(null); 41 | } 42 | } 43 | 44 | protected override void OnStart(string[]? args) 45 | { 46 | ThreadPool.UnsafeQueueUserWorkItem(this.LoadProfile, args); 47 | } 48 | 49 | protected override void OnStop() 50 | { 51 | this.timer?.Dispose(); 52 | } 53 | 54 | private static void AddAll(List servers, string[] items, bool isNtp) 55 | { 56 | foreach (var it in items) 57 | { 58 | var mHost = it.Trim(); 59 | if (mHost != string.Empty) 60 | { 61 | TimeServer serv = new() 62 | { 63 | HostName = mHost, 64 | RequestType = isNtp ? NtpRequestType : HttpRequestType, 65 | }; 66 | if (!servers.Contains(serv)) 67 | { 68 | servers.Add(serv); 69 | } 70 | } 71 | } 72 | } 73 | 74 | private static string GetFailureMessage(int errorCode) 75 | { 76 | return string.Format("程序路径: {0}\r\n代码: {1}, 可在 https://wintp.sourceforge.io/ 获取帮助。", Wintp.Location, errorCode); 77 | } 78 | 79 | private static DateTime GetNtpTime(IPAddress[] ip) 80 | { 81 | var data = new byte[48]; 82 | data[0] = 0x1B; 83 | using Socket socket = new(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); 84 | socket.SendTo(data, new IPEndPoint(ip.FirstOrDefault(), 123)); 85 | socket.Receive(data); 86 | unsafe 87 | { 88 | fixed (byte* ptr = data) 89 | { 90 | var intPart = (ulong*)(ptr + 40); 91 | var fractPart = (ulong*)(ptr + 44); 92 | return TimeOf1900.AddMilliseconds((SwapEndianness(*fractPart) >> 32) + SwapEndianness(*intPart)); 93 | } 94 | } 95 | } 96 | 97 | // stackoverflow.com/a/20847931/28134812 98 | [DllImport("kernel32.dll")] 99 | private static extern bool SetSystemTime(ref SystemTime st); 100 | 101 | // stackoverflow.com/a/3294698/162671 102 | private static long SwapEndianness(ulong x) 103 | { 104 | return IPAddress.NetworkToHostOrder((long)x) * 1000L; 105 | } 106 | 107 | private static void Win32SetSystemTime(DateTime t) 108 | { 109 | st.Year = (short)t.Year; 110 | st.Month = (short)t.Month; 111 | st.Day = (short)t.Day; 112 | st.Hour = (short)t.Hour; 113 | st.Minute = (short)t.Minute; 114 | st.Second = (short)t.Second; 115 | st.Millisecond = (short)t.Millisecond; 116 | SetSystemTime(ref st); 117 | } 118 | 119 | private DateTime GetHttpTime(IPAddress[] ip, string host) 120 | { 121 | using Socket socket = new(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 122 | socket.Connect(ip, this.useSSL ? 443 : 80); 123 | socket.Send(System.Text.Encoding.UTF8.GetBytes(string.Format("HEAD / HTTP/1.1\r\nHost: {0}\r\nConnection: close\r\n\r\n", host))); 124 | StreamReader rdr = new(new NetworkStream(socket)); 125 | while (rdr.Peek() != -1) 126 | { 127 | var line = rdr.ReadLine(); 128 | if (line.StartsWith("Date:", StringComparison.OrdinalIgnoreCase)) 129 | { 130 | return Convert.ToDateTime(line.Substring(5)).ToUniversalTime(); 131 | } 132 | } 133 | 134 | throw new SocketException(); 135 | } 136 | 137 | private void GetNetTime(object obj) 138 | { 139 | if (obj is TimeServer serv) 140 | { 141 | var sh = serv.ResetEvent.SafeWaitHandle; 142 | IPAddress[]? ip = null; 143 | DateTime? time = null; 144 | while (!sh.IsClosed) 145 | { 146 | try 147 | { 148 | ip = Dns.GetHostAddresses(serv.HostName); 149 | time = serv.RequestType == NtpRequestType ? GetNtpTime(ip) : this.GetHttpTime(ip, serv.HostName); 150 | } 151 | catch 152 | { 153 | if (!sh.IsClosed && ip == null) 154 | { 155 | Thread.Sleep(1000); 156 | continue; 157 | } 158 | else if (this.verbose) 159 | { 160 | using EventLog log = new("Application", ".", string.Format("{0} cannot connect to \"{1}\"", Wintp.GetName().Name, serv)); 161 | log.WriteEntry(GetFailureMessage(NetworkConnectionError), EventLogEntryType.Error); 162 | } 163 | 164 | Thread.CurrentThread.Abort(); 165 | } 166 | 167 | break; 168 | } 169 | 170 | if (!sh.IsClosed) 171 | { 172 | serv.ResetEvent.Set(); 173 | } 174 | 175 | if (time is DateTime local && !sh.IsClosed) 176 | { 177 | serv.ResetEvent.Close(); 178 | if (this.autoSync) 179 | { 180 | Win32SetSystemTime(local); 181 | } 182 | 183 | if (this.verbose) 184 | { 185 | var msg = string.Format("/c echo {0} Get datetime from \"{1}\", AutoSyncTime: {2} && pause", local.ToLocalTime(), serv.HostName, this.autoSync); 186 | try 187 | { 188 | Process.Start("cmd.exe", msg); 189 | } 190 | catch 191 | { 192 | Environment.FailFast(GetFailureMessage(ProcessStartFailure)); 193 | } 194 | } 195 | } 196 | } 197 | } 198 | 199 | private void LoadProfile(object? args) 200 | { 201 | List servers = []; 202 | var cfgPath = Path.Combine(Path.GetDirectoryName(Wintp.Location), "WINtp.exe.config"); 203 | if (!File.Exists(cfgPath)) 204 | { 205 | using var pCfg = File.Create(cfgPath); 206 | Wintp.GetManifestResourceStream(Wintp.GetManifestResourceNames().FirstOrDefault()).CopyTo(pCfg); 207 | } 208 | 209 | var reader = new System.Configuration.AppSettingsReader(); 210 | _ = bool.TryParse($"{reader.GetValue("Verbose", typeof(bool))}", out verbose); 211 | _ = bool.TryParse($"{reader.GetValue("AutoSyncTime", typeof(bool))}", out autoSync); 212 | _ = bool.TryParse($"{reader.GetValue("UseSsl", typeof(bool))}", out useSSL); 213 | _ = int.TryParse($"{reader.GetValue("Delay", typeof(int))}", out delay); 214 | _ = int.TryParse($"{reader.GetValue("Timeout", typeof(int))}", out int timeout); 215 | AddAll(servers, $"{reader.GetValue("Ntps", typeof(string))}".Split(';'), true); 216 | AddAll(servers, $"{reader.GetValue("Urls", typeof(string))}".Split(';'), false); 217 | if (servers.Count != 0) 218 | { 219 | ManualResetEvent evt = new(false); 220 | servers.ForEach(it => 221 | { 222 | it.ResetEvent = evt; 223 | ThreadPool.UnsafeQueueUserWorkItem(this.GetNetTime, it); 224 | }); 225 | if (!evt.WaitOne(timeout < 1 ? 30000 : timeout)) 226 | { 227 | Environment.FailFast(GetFailureMessage(TimeoutError)); 228 | } 229 | 230 | if (args != null && this.timer == null) 231 | { 232 | if (this.delay < 1) 233 | { 234 | Thread.Sleep(3000); 235 | this.Stop(); 236 | } 237 | else 238 | { 239 | this.timer = new(_ => this.OnStart(null), null, 0, this.delay * 1000); 240 | } 241 | } 242 | } 243 | } 244 | 245 | [StructLayout(LayoutKind.Sequential)] 246 | private struct SystemTime 247 | { 248 | public short Year; 249 | public short Month; 250 | public short DayOfWeek; 251 | public short Day; 252 | public short Hour; 253 | public short Minute; 254 | public short Second; 255 | public short Millisecond; 256 | } 257 | 258 | private struct TimeServer 259 | { 260 | public string HostName; 261 | public int RequestType; 262 | public ManualResetEvent ResetEvent; 263 | } 264 | } -------------------------------------------------------------------------------- /WINtp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | $([System.DateTime]::Now.ToString(yyyy-MM-dd)) 4 | false 5 | WINtp 6 | .\bin\WINtp\$(GetDate) 7 | WinExe 8 | preview 9 | net481 10 | enable 11 | enable 12 | true 13 | none 14 | 9999 15 | False 16 | latest 17 | False 18 | SA1633;SA0001;RCS1110;S3903;SA1600;SA1101;S2486;S108;S1075;SA1307;CA1050; 19 | false 20 | AnyCPU 21 | WINtp.manifest 22 | True 23 | False 24 | True 25 | 26 | 27 | 28 | 29 | 30 | PreserveNewest 31 | 32 | 33 | all 34 | runtime; build; native; contentfiles; analyzers; buildtransitive 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /WINtp.exe.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /WINtp.manifest: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /WINtp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.12.35424.110 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WINtp", "WINtp.csproj", "{D4A656D9-E8D8-4EAA-8EC3-0C4DD9A45EE6}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {D4A656D9-E8D8-4EAA-8EC3-0C4DD9A45EE6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {D4A656D9-E8D8-4EAA-8EC3-0C4DD9A45EE6}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {D4A656D9-E8D8-4EAA-8EC3-0C4DD9A45EE6}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {D4A656D9-E8D8-4EAA-8EC3-0C4DD9A45EE6}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {62361611-4D14-41AB-8692-83D0D03EB614} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /wInstall.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | cd "%~dp0" 3 | set WINtpEXE=%~dp0 4 | sc create WINtp binPath= "\"%WINtpEXE%WINtp.exe\" -k" start= auto depend= "tcpip"/"nsi" 5 | sc failure WINtp reset= 60 actions= restart/60000 6 | sc config WINtp obj= "LocalSystem" 7 | sc description WINtp "WINtp is a simple NTP client." 8 | sc start WINtp 9 | 10 | pause -------------------------------------------------------------------------------- /wUninstall.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | sc stop WINtp 3 | sc delete WINtp 4 | 5 | pause -------------------------------------------------------------------------------- /wintp.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/lalakii/WINtp/7135a29f5f4b2229aea027346969b99ac11d16f2/wintp.jpg --------------------------------------------------------------------------------