Пример #1
0
        static void Main(string[] args)
        {
            Dictionary <string, string> Configargs = Readargs(args);
            string ConfigPath = null;

            foreach (var Var in Configargs)
            {
                if (Var.Key == "")
                {
                    ConfigPath = Var.Value;
                }
            }
            DDNSState = true;
            string AccessKeyId     = "";
            string AccessKeySecret = "";

            ConsoleX.WriteLine("开始读取配置文件");
            string[] ConfigFile;
            if (ConfigPath != "" && ConfigPath != null)
            {
                ConfigFile = File.ReadAllLines(ConfigPath);
            }
            else
            {
                if (File.Exists("Config.json"))
                {
                    ConfigFile = File.ReadAllLines("Config.json");
                }
                else
                {
                    ConfigFile = File.ReadAllLines(AppContext.BaseDirectory + "Config.json");
                }
            }
            string ConfigContent = "";

            ConsoleX.WriteLine("屏蔽注释");
            foreach (var Da in ConfigFile)
            {
                if (Da.First() != '/')
                {
                    ConfigContent += Da;
                }
            }
            string pattern = "[\u4e00-\u9fbb]";

            if (Regex.IsMatch(ConfigContent, pattern))
            {
                Console.WriteLine("config文件中出现了中文字符串,请检查,本软件暂时不支持中文域名");
                goto ccLever;
            }
            ConsoleX.WriteLine("反序列化解析配置数据");
            JObject       jsonObj = JObject.Parse(ConfigContent);
            JArray        jlist   = JArray.Parse(jsonObj["data"].ToString());
            List <string> Config  = new List <string>();

            ConsoleX.WriteLine("载入解析配置数据");
            foreach (var jk in jlist)
            {
                Config.Add(jk.ToString());
                Console.WriteLine(jk.ToString());
            }
            ConsoleX.WriteLine("载入AccessKeyId/AccessKeySecret数据");
            JToken record = JObject.Parse(ConfigContent)["Key"];

            foreach (JProperty jp in record)
            {
                if (jp.Name == "AccessKeyId")
                {
                    AccessKeyId = jp.Value.ToString();
                }
                else if (jp.Name == "AccessKeySecret")
                {
                    AccessKeySecret = jp.Value.ToString();
                }
            }
            ConsoleX.WriteLine("开始启动DDNS");
            List <DDNS> DdnsList = new List <DDNS>();

            for (int i = 0; i < Config.Count; i++)
            {
                DDNS ddns = new DDNS();
                ddns.InitializeDict(AccessKeyId, AccessKeySecret, Config[i].Split(',')[0], Config[i].Split(',')[1], Config[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();
        }
Пример #2
0
        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);
                while (Program.DDNSState)
                {
                    string IP = "";
                    try { IP = GetlocalhostIP(); } catch { 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);
                                Add(IP);
                                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);
        }