public static string GetlocalhostIP() { string url = "https://api-ipv4.ip.sb/jsonip"; string IP = "Error"; HttpClient client = new HttpClient(); try { HttpResponseMessage response = client.PostAsync(url, null).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); string responseBody = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); IPJson iPJson = JsonConvert.DeserializeObject <IPJson>(responseBody); if (!iPJson.ip.Equals("")) { IP = iPJson.ip; } else { IP = "Error"; } } catch (HttpRequestException e) { ConsoleX.WriteLine("IP获取发生错误", $"IP获取函数 报错\r\nMessges:{e.Message}"); } return(IP); }
public static string CreateGetHttpResponse(string url) { string result = string.Empty; HttpClient client = new HttpClient(); try { HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult(); response.EnsureSuccessStatusCode(); result = response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); } catch (Exception ex) { ConsoleX.WriteLine("发生错误:" + ex.Message, "HTTP"); throw; } return(result); }
static void Main(string[] args) { var builder = new ConfigurationBuilder() .AddCommandLine(args); var configuration = builder.Build(); string[] ConfigFile; if (configuration["config"] != null) { ConsoleX.WriteLine("开始读取配置文件:" + configuration["config"]); if (File.Exists(configuration["config"])) { ConfigFile = File.ReadAllLines(configuration["config"]); } else { Console.ForegroundColor = ConsoleColor.Red; ConsoleX.WriteLine("配置文件不存在!"); Console.ResetColor(); return; } } else { ConsoleX.WriteLine("开始读取配置文件:" + Path.GetFullPath("config.json"));; if (File.Exists("config.json")) { ConfigFile = File.ReadAllLines("config.json"); } else { ConsoleX.WriteLine($"配置文件{{{Directory.GetCurrentDirectory() + "/config.json"}}}不存在!"); return; } if (File.Exists(AppContext.BaseDirectory + "config.json")) { ConfigFile = File.ReadAllLines(AppContext.BaseDirectory + "config.json"); } else { ConsoleX.WriteLine($"配置文件{{{AppContext.BaseDirectory + "config.json"}}}不存在!"); return; } } ConsoleX.WriteLine("配置文件读取完毕"); DDNSState = true; string AccessKeyId = ""; string AccessKeySecret = ""; string ConfigContent = ""; ConsoleX.WriteLine("屏蔽注释中....."); foreach (var Da in ConfigFile) { if (Da.Trim().First() != '/') { ConfigContent += Da.Trim(); } } string pattern = "[\u4e00-\u9fbb]"; if (Regex.IsMatch(ConfigContent, pattern)) { ConsoleX.WriteLine("config文件中出现了中文字符串,请检查,本软件暂时不支持中文域名"); goto ccLever; } ConsoleX.WriteLine("反序列化解析配置数据"); Config config = JsonConvert.DeserializeObject <Config>(ConfigContent); List <string> DomainConfig = config.data.ToList(); ConsoleX.WriteLine("获取域名配置成功!"); foreach (string domain in DomainConfig) { ConsoleX.WriteLine(domain); } ConsoleX.WriteLine("载入AccessKeyId/AccessKeySecret数据"); AccessKeyId = config.key.AccessKeyId; AccessKeySecret = config.key.AccessKeySecret; string patten = @"(?<=\S)\S(?=\S)"; Regex reg = new Regex(patten); ConsoleX.WriteLine("AccessKeyId:" + reg.Replace(AccessKeyId, "*")); ConsoleX.WriteLine("AccessKeySecret:" + reg.Replace(AccessKeySecret, "*")); ConsoleX.WriteLine("开始启动DDNS"); ConsoleX.WriteLine("开始初始化本机公网地址获取函数"); LocalIP = "0.0.0.0"; ConsoleX.WriteLine("开始第一次获取公网地址:"); try { LocalIP = GetlocalhostIP(); } catch (Exception e) { ConsoleX.WriteLine("IP获取发生错误:\r\n" + e.Message); LocalIP = "Error"; } ConsoleX.WriteLine("IP:" + LocalIP); Thread GetIp = new Thread(new ThreadStart(() => { for (int i = 0; i < 60; i++) { if (Program.DDNSState) { Thread.Sleep(1000); } else { i = 60; } } while (DDNSState) { try { LocalIP = GetlocalhostIP(); } catch (Exception e) { ConsoleX.WriteLine("IP获取发生错误:1分钟后重试\r\n" + e.Message, "IP获取线程"); } for (int i = 0; i < 60; i++) { if (Program.DDNSState) { Thread.Sleep(1000); } else { i = 60; } } } })); GetIp.Start(); List <DDNS> DdnsList = new List <DDNS>(); for (int i = 0; i < DomainConfig.Count; i++) { DDNS ddns = new DDNS(); ddns.InitializeDict(AccessKeyId, AccessKeySecret, DomainConfig[i].Split(',')[0], DomainConfig[i].Split(',')[1], DomainConfig[i].Split(',')[2]); ddns.Start(); Thread.Sleep(1000 * 3); } Console.WriteLine("防手残机制:如果想结束程序请输入Stop"); cc : string Read = Console.ReadLine().ToLower(); if (Read != "stop") { goto cc; } DDNSState = false; ccLever : Console.WriteLine("回车离开"); Console.ReadLine(); }
public DDNS() { ConsoleX.WriteLine("程序启动,DDNS类已经初始化", "DDNS"); }
public void Start() { //ConsoleX.WriteLine(""); State = true; LastIP = "Null"; Thread thread = new Thread(new ThreadStart(() => { reset: ConsoleX.WriteLine("域名:" + HostRecord + "." + DomainName + "的DDNS已启动"); ConsoleX.WriteLine("开始第一次查询IP", HostRecord + "." + DomainName); string IP = ""; if (Program.LocalIP != "Error") { IP = Program.LocalIP; } while (Program.DDNSState) { if (Program.LocalIP != "Error") { IP = Program.LocalIP; } else { ConsoleX.WriteLine("IP获取发生错误,一分钟后即将重试", HostRecord + "." + DomainName); GC.Collect(); goto rewait; } if (IP != LastIP) { try { ConsoleX.WriteLine("已获取IP :" + IP + "与上次获取的IP:" + LastIP + "不符,开始更新DNS", HostRecord + "." + DomainName); LastIP = IP; string DnsIP = null; ConsoleX.WriteLine("开始查询域名解析记录", HostRecord + "." + DomainName); try { DnsIP = Select(); } catch { ConsoleX.WriteLine("未获取到解析记录,准备添加解析记录", HostRecord + "." + DomainName); goto cc; } cc: if (DnsIP == null) { ConsoleX.WriteLine("未获取到解析记录,准备添加解析记录", HostRecord + "." + DomainName); try { Add(IP); } catch (Exception e) { ConsoleX.WriteLine("IP添加解析记录发生错误:" + e.Message, HostRecord + "." + DomainName); } ConsoleX.WriteLine("解析记录添加完毕", HostRecord + "." + DomainName); } else if (DnsIP != IP) { ConsoleX.WriteLine("已获取解析记录:" + DnsIP + " 与本机IP:" + IP + "不符,开始更新解析记录", HostRecord + "." + DomainName); try { Update(IP); } catch { goto reset; } ConsoleX.WriteLine("解析记录更新完毕", HostRecord + "." + DomainName); } else { ConsoleX.WriteLine("已获取解析记录:" + DnsIP + " 与本机IP:" + IP + "相符,不进行操作", HostRecord + "." + DomainName); } } catch (Exception e) {//Message JToken record = JObject.Parse(e.Message); string Error = ""; foreach (JProperty jp in record) { if (jp.Name == "Message") { Error = jp.Value.ToString(); } } Console.Write("[" + HostRecord + "." + DomainName + " "); Console.ForegroundColor = ConsoleColor.Red; Console.Write(DateTime.Now.ToLongTimeString() + " Error]"); Console.ResetColor(); Console.Write("发生访问错误,请检查阿里云Key以及域名、主机记录是否正常,异常多数发生在,阿里云Key以及域名或者TTL过低,如果并未专门购买阿里云云解析的TTL值,请将ttl设为600\r\n这是服务器返回数据:\r\n "); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine(Error + "\r\n"); Console.ResetColor(); } } rewait: for (int i = 0; i < 60; i++) { if (Program.DDNSState) { Thread.Sleep(1000); } else { i = 60; } } } })); thread.Start(); ConsoleX.WriteLine("DDNS线程已启动", HostRecord + "." + DomainName); }