using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using TravelRepublic.DnsClient;
using TravelRepublic.DnsClient.Records;
namespace Nslookup.Net
{
class Program
{
static void Main(string[] args)
{
Console.Title = "设置Chrome更新服务器地址 by 福建-兮";
Console.SetWindowSize(70, 10);
Console.SetBufferSize(70, 10);
IntPtr hWin = GetConsoleWindow();
RECT rc;
GetWindowRect(hWin, out rc);
Screen scr = Screen.FromPoint(new Point(rc.left, rc.top));
int x = scr.WorkingArea.Left + (scr.WorkingArea.Width - (rc.right - rc.left)) / 2;
int y = scr.WorkingArea.Top + (scr.WorkingArea.Height - (rc.bottom - rc.top)) / 2;
MoveWindow(hWin, x, y, rc.right - rc.left, rc.bottom - rc.top, true);
GetChromeUpdateServerIP();
Console.ReadKey();
}
private struct RECT { public int left, top, right, bottom; }
[DllImport("kernel32.dll", SetLastError = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", SetLastError = true)]
private static extern bool GetWindowRect(IntPtr hWnd, out RECT rc);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
/// <summary>
/// 获取chrome浏览器更新服务器地址ip
/// </summary>
private static void GetChromeUpdateServerIP()
{
Console.Write("请输入解析DNS地址(默认为:114.114.114.114):");
string dns = Console.ReadLine();
if (string.IsNullOrEmpty(dns))
{
dns = "114.114.114.114";
}
var client = new DnsClientBuilder().WithDnsServer(new IPEndPoint(IPAddress.Parse(dns), 53)).Build();
try
{
var response = client.Query("tools.google.com", NsType.A, NsClass.INET, ProtocolType.Udp);
var record = (ARecord)response.Answers[2];
Console.Write("使用DNS:");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write(dns);
Console.ResetColor();
Console.Write("解析chrome更新服务器地址");
Console.WriteLine();
Console.Write("chrome更新服务器地址为:");
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.Write(record.Address);
Console.ResetColor();
//===
Console.WriteLine();
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("开始修改host");
Console.ResetColor();
Console.Write(record.Address);
Console.WriteLine();
Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.StandardInput.WriteLine("attrib -r -a -s -h %windir%\\system32\\drivers\\etc\\hosts & exit");
process.WaitForExit();
process.Close();
string hostspath = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers\\etc\\hosts");
List<string> list = File.ReadAllLines(hostspath).ToList();
list.RemoveAll(z => z.Contains("tools.google.com"));
foreach (var item in list.ToArray())
{
if (item.Contains("dl.google.com"))
{
list.Remove(item);
}
}
list.Add($"{record.Address} tools.google.com");
list.Add($"{record.Address} dl.google.com");
//File.WriteAllLines(System.Environment.CurrentDirectory + "\\hosts", list.ToArray());
File.WriteAllLines(hostspath, list.ToArray());
Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("host修改完成");
Console.ResetColor();
//===
Console.ReadLine();
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.StackTrace, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
}
设置Chrome更新服务器地址
于 2022-03-04 16:21:34 首次发布