private bool DDNS(DNSPodClient client, string domainName, string subDomain) { if (!isLock) { isLock = true; Logger.Write("获取本地IP"); var ip = DNSHelper.GetLocalIP(); Logger.Write("本地IP为:{0},IP比对中...", ip); try { Domain domain = client.GetDomain(domainName); Record record = null; try { record = client.GetRecord(domain.Id.ToString(), subDomain); } catch (DNSPodException ex) { // 如果记录不存在则创建一个 if (ex.Code == 22) { Logger.Write("主机头不存在,创建记录"); record = client.CreateRecord(domain.Id.ToString(), subDomain, ip); client.Clear(); Logger.Write("已创建记录,ID为:{0}", record.Id); } else { throw ex; } } // 如果本地IP与服务器不一样则更新 if (ip != record.Value) { Logger.Write("IP变动,刷新DNS。IP地址为:{0}", ip); client.DDNS(domain.Id.ToString(), subDomain, record.Id); client.Clear(); Logger.Write("已更换IP:{0}", ip); } else { Logger.Write("本地IP与服务器IP一致,无需更新"); } isLock = false; return(true); } catch (DNSPodException ex) { Logger.Write("出错:{0}", ex.Message); isLock = false; return(false); } } return(true); }
private bool DDNS(DNSPodClient client, string domainName, string subDomain) { try { Domain domain = client.GetDomain(domainName); Record record = null; try { record = client.GetRecord(domain.Id.ToString(), subDomain); } catch (DNSPodException ex) { // 如果记录不存在则创建一个 if (ex.Code == 22) { record = client.CreateRecord(domain.Id.ToString(), subDomain, DNSHelper.GetLocalIP()); client.Clear(); Logger.Write("主机头不存在,创建记录"); } else { throw ex; } } // 如果本地IP与服务器不一样则更新 var ip = DNSHelper.GetLocalIP(); if (ip != record.Value) { client.DDNS(domain.Id.ToString(), subDomain, record.Id); client.Clear(); Logger.Write("IP变动,刷新DNS。IP地址为:{0}", ip); } else { Logger.Write("本地IP与服务器IP一致,无需更新"); } return(true); } catch (DNSPodException ex) { AppHelper.Alert(ex.Message); Logger.Write("出错:{0}", ex.Message); return(false); } }
protected override void OnStart(string[] args) { Logger.Write("服务启动!"); if ((string.IsNullOrWhiteSpace(token) || string.IsNullOrWhiteSpace(tokenID)) && (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))) { Logger.Write("至少需要一种验证方式!"); this.Stop(); return; } if (string.IsNullOrWhiteSpace(domain)) { Logger.Write("Missing Domain"); this.Stop(); return; } if (string.IsNullOrWhiteSpace(subDomain)) { Logger.Write("Missing SubDomain"); this.Stop(); return; } if (!string.IsNullOrWhiteSpace(token) && !string.IsNullOrWhiteSpace(tokenID)) { client = new DNSPodClient(tokenID + "," + token); } else { client = new DNSPodClient(email, password); } timer.Interval = updateInterval * 60 * 1000; timer.Start(); AppHelper.SetTimeout(() => { DDNS(client, domain, subDomain); }, 1000); }
protected override void OnStart(string[] args) { Logger.Write("服务启动!"); if (string.IsNullOrWhiteSpace(email)) { Logger.Write("Missing Email"); this.Stop(); return; } if (string.IsNullOrWhiteSpace(password)) { Logger.Write("Missing Password"); this.Stop(); return; } if (string.IsNullOrWhiteSpace(domain)) { Logger.Write("Missing Domain"); this.Stop(); return; } if (string.IsNullOrWhiteSpace(subDomain)) { Logger.Write("Missing SubDomain"); this.Stop(); return; } client = new DNSPodClient(email, password); timer.Interval = updateInterval * 60 * 1000; timer.Start(); AppHelper.SetTimeout(() => { DDNS(client, domain, subDomain); }, 1000); }