예제 #1
0
        static void Main(string[] args)
        {
            var envVars = EnvVars.CheckVars();

            if (envVars == null)
            {
                return;
            }
            CheckIp(envVars);
            Console.CancelKeyPress += new ConsoleCancelEventHandler(OnExit);
            _closing.WaitOne();
        }
예제 #2
0
        static async void CheckIp(EnvVars vars)
        {
            Utility.Log($"{AppDomain.CurrentDomain.FriendlyName} start at [{Utility.Now()}]");
            var ipFetcher = new IpFetcher(vars.IP_ECHO);
            var namesilo  = new Namesilo(vars.API_KEY, vars.DOMAIN, vars.RECORD_NAME);

            Func <string, string, Task> update = async(string ip, string logPrefix) => {
                var recordId = await namesilo.GetRecordId();

                var succeed = await namesilo.UpdateRecordIp(recordId, ip);

                Utility.Log($"{logPrefix} got result: {succeed}");
            };

            string myIp      = "None";
            int    sameCount = 0;


            Func <Task> check = async() => {
                var t     = Utility.Now();
                var newIp = await ipFetcher.GetMyIp();

                //Utility.Log($"ip is [{newIp}]");
                if (newIp != myIp)
                {
                    await update(newIp, $"[{t}] replace [{myIp}] to [{newIp}] after [{sameCount}] times");

                    myIp = newIp;
                }
                else
                {
                    sameCount += 1;
                    if ((sameCount % vars.PRINT_SAME_COUNT_STEP) == 0)
                    {
                        await update(newIp, $"[{t}] make sure it's still [{myIp}] after [{sameCount}] times");
                    }
                }
            };

            while (Running)
            {
                try {
                    await check();
                } catch (Exception ex) {
                    Utility.Log($"[{Utility.Now()}] got exception:[{ex.ToString()}]");
                }

                await Task.Delay(vars.CHECK_INTERVAL_SECONDS * 1000);
            }
            _closing.Set();
        }
예제 #3
0
        public static EnvVars CheckVars()
        {
            var vars = new EnvVars();

            vars.API_KEY     = vars.GetEnvVar(nameof(API_KEY));
            vars.DOMAIN      = vars.GetEnvVar(nameof(DOMAIN));
            vars.RECORD_NAME = vars.GetEnvVar(nameof(RECORD_NAME));
            vars.IP_ECHO     = vars.GetEnvVar(nameof(IP_ECHO), IP_ECHO_DEFAULT);

            vars.GetIntEnvVar(nameof(CHECK_INTERVAL_SECONDS), ref vars.CHECK_INTERVAL_SECONDS, CHECK_INTERVAL_SECONDS_DEFAULT.ToString());
            vars.GetIntEnvVar(nameof(PRINT_SAME_COUNT_STEP), ref vars.PRINT_SAME_COUNT_STEP, PRINT_SAME_COUNT_STEP_DEFAULT.ToString());

            //vars.LogVars();

            if (vars.IsValid == false)
            {
                return(null);
            }

            return(vars);
        }