public HomeModule()
        {
            Get("/",
                x => Response.AsText("{\"owner\": \"Milkey\",\"message\": \"Hello,World!\"}", "application/json"));

            Get("/{host}/{port}",
                x =>
            {
                try
                {
                    List <int> list = MPing.Tcping(x.host.ToString(), Convert.ToInt32(x.port.ToString()));
                    return(Response.AsText(
                               "{" +
                               $"\"status\": {(list.Max() != 0).ToString().ToLower()}," +
                               $"\"time\": {(list.Average() == 0 ? "null" : list.Average().ToString(CultureInfo.InvariantCulture))}" +
                               "}", "application/json"));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(e);
                }
            });

            Get("/{host}",
                x =>
            {
                try
                {
                    List <int> list = MPing.Ping(x.host.ToString());
                    return(Response.AsText(
                               "{" +
                               $"\"status\": {(list.Max() != 0).ToString().ToLower()}," +
                               $"\"time\": {(list.Average() == 0 ? "null" : list.Average().ToString(CultureInfo.InvariantCulture))}" +
                               "}", "application/json"));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    return(e);
                }
            });
        }
Example #2
0
        public static void Switchip(string ipName, string regionName, string attachedTo)
        {
            using (var pStart = new Process
            {
                StartInfo = new ProcessStartInfo("python")
                {
                    Arguments =
                        modStr +
                        $"release-static-ip --static-ip-name {ipName} --region={regionName}"
                }
            })
            {
                pStart.Start();
                pStart.WaitForExit();
            }

            Console.WriteLine("release static ip");

            using (var pStart = new Process
            {
                StartInfo = new ProcessStartInfo("python")
                {
                    Arguments =
                        modStr +
                        $"allocate-static-ip --static-ip-name {ipName} --region={regionName}"
                }
            })
            {
                pStart.Start();
                pStart.WaitForExit();
            }

            Console.WriteLine("allocate static ip");

            using (var pStart = new Process
            {
                StartInfo = new ProcessStartInfo("python")
                {
                    Arguments = modStr + $"attach-static-ip --static-ip-name {ipName}" +
                                $" --instance-name {attachedTo} --region={regionName}"
                }
            })
            {
                pStart.Start();
                pStart.WaitForExit();
            }

            Console.WriteLine("attached");

            var p = new Process
            {
                StartInfo = new ProcessStartInfo("python")
                {
                    Arguments              = modStr + "get-static-ip --static-ip-name={ipName} --region={region}",
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true
                }
            };

            p.Start();
            p.WaitForExit();
            var        jMsgOip    = JsonConvert.DeserializeObject <dynamic>(p.StandardOutput.ReadToEnd());
            List <int> pingInts   = MPing.Ping(jMsgOip.staticIp.ipAddress.ToString());
            List <int> tcPingInts = MPing.Tcping(jMsgOip.staticIp.ipAddress.ToString(), 22);

            if (pingInts.Max() == 0 || tcPingInts.Max() == 0)
            {
                Switchip(ipName, regionName, attachedTo);
            }
        }
Example #3
0
        static void Main()
        {
            if (!File.Exists("./log.log"))
            {
                File.Create("./log.log").Close();
            }

            while (true)
            {
                for (int i = 0; i < 5; i++)
                {
                    foreach (var item in File.ReadAllLines("svr.txt"))
                    {
                        var svr    = item.Split(',');
                        var ipName = svr[0];
                        var region = svr[1];
                        var ddns   = svr[2];

                        var p = new Process
                        {
                            StartInfo = new ProcessStartInfo("python")
                            {
                                Arguments              = "-m awscli",
                                UseShellExecute        = false,
                                RedirectStandardOutput = true,
                                CreateNoWindow         = true
                            }
                        };
                        p.Start();
                        p.WaitForExit();

                        if (p.ExitCode != 1)
                        {
                            p.StartInfo.Arguments =
                                modStr + $"get-static-ip --static-ip-name={ipName} --region={region}";
                            Console.WriteLine("Connecting...");
                            p.Start();
                            p.WaitForExit();
                            Console.Clear();

                            var jMsgOip = JsonConvert.DeserializeObject <dynamic>(p.StandardOutput.ReadToEnd());
                            Console.WriteLine(jMsgOip.staticIp.name);
                            Console.WriteLine(jMsgOip.staticIp.ipAddress);
                            Console.WriteLine(jMsgOip.staticIp.attachedTo);

                            Console.WriteLine("Ping...");
                            List <int> pingInts   = MPing.Ping(jMsgOip.staticIp.ipAddress.ToString());
                            List <int> tcPingInts = MPing.Tcping(jMsgOip.staticIp.ipAddress.ToString(), 22);
                            if (pingInts.Max() == 0 || tcPingInts.Max() == 0)
                            {
                                Console.Clear();
                                Console.WriteLine("Inaccessible:" + jMsgOip.staticIp.ipAddress);
                                Console.WriteLine("Switching IP...");
                                File.WriteAllText("./log.log",
                                                  File.ReadAllText("./log.log") + DateTime.Now + ":Inaccessible " + ipName + " " +
                                                  jMsgOip.staticIp.ipAddress + Environment.NewLine);

                                Switchip(jMsgOip.staticIp.name.ToString(), region,
                                         jMsgOip.staticIp.isAttached.ToString());
                                Thread.Sleep(500);
                                p.Start();
                                p.WaitForExit();
                                jMsgOip = JsonConvert.DeserializeObject <dynamic>(p.StandardOutput.ReadToEnd());
                                Console.WriteLine("NewIP:" + jMsgOip.staticIp.ipAddress);

                                try
                                {
                                    //using (var pStart = new Process
                                    //{
                                    //    StartInfo = new ProcessStartInfo("curl")
                                    //        { Arguments = ddns.Replace("0.0.0.0", jMsgOip.staticIp.ipAddress.ToString()) }
                                    //})
                                    //{
                                    //    pStart.Start();
                                    //    pStart.WaitForExit();
                                    //}

                                    var uri      = new Uri(ddns.Replace("0.0.0.0", jMsgOip.staticIp.ipAddress.ToString()));
                                    var userinfo = uri.UserInfo.Split(':');
                                    var http     = new HttpClient(new HttpClientHandler()
                                    {
                                        Credentials = new NetworkCredential(userinfo[0], userinfo[1])
                                    });
                                    Console.WriteLine(http.GetStringAsync(uri).Result);
                                }
                                catch (Exception e)
                                {
                                    Console.WriteLine(e);
                                }

                                Console.WriteLine("OK");
                                File.WriteAllText("./log.log",
                                                  File.ReadAllText("./log.log") + DateTime.Now + ":SwitchIP " + ipName + " " +
                                                  jMsgOip.staticIp.ipAddress + Environment.NewLine);
                            }
                            else
                            {
                                File.WriteAllText("./log.log",
                                                  File.ReadAllText("./log.log") + DateTime.Now + ":Accessible " + ipName + " " +
                                                  jMsgOip.staticIp.ipAddress + Environment.NewLine);
                                Console.WriteLine("Accessible");
                                Console.WriteLine();
                                var uri      = new Uri(ddns.Replace("0.0.0.0", jMsgOip.staticIp.ipAddress.ToString()));
                                var userinfo = uri.UserInfo.Split(':');
                                var http     = new HttpClient(new HttpClientHandler()
                                {
                                    Credentials = new NetworkCredential(userinfo[0], userinfo[1])
                                });
                                Console.WriteLine(http.GetStringAsync(uri).Result);
                                Console.WriteLine();
                                Console.WriteLine("OK");
                            }
                        }
                        else
                        {
                            Console.WriteLine("maybe awscli not installed");
                            Console.WriteLine("please pip install awscli and try again");
                        }

                        Thread.Sleep(1000);
                        Console.Clear();
                    }
                }

                Thread.Sleep(60000 * 10);
            }
        }
Example #4
0
        private static void BotOnMessageReceived(object sender, MessageEventArgs e)
        {
            var message = e.Message;

            if (message == null)
            {
                return;
            }
            if (message.Type != MessageType.Text)
            {
                Console.WriteLine("不被支援的媒体类型。");
            }

            Console.WriteLine($"@{e.Message.From.Username}: " + e.Message.Text);

            if (IsIP(message.Text))
            {
                BotClient.SendTextMessageAsync(message.Chat.Id,
                                               $"{message.Text} : {GeoIp(message.Text)} {GeoIsp(message.Text)}");
            }

            if (message.Text.Replace("  ", " ").Split(' ').Length > 1)
            {
                var bgWorker = new BackgroundWorker();
                bgWorker.DoWork += (o, args) =>
                {
                    string msgStr = message.Text.Replace("  ", " ").Split(' ')[1];
                    try
                    {
                        switch (message.Text.Replace("  ", " ").Split(' ')[0])
                        {
                        case "/ip":
                            if (IsIP(msgStr))
                            {
                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"{msgStr} : {GeoIp(msgStr)}");
                            }
                            else
                            {
                                string ipAddr = HttpsDnsHostAddresses(msgStr);
                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"{msgStr}({ipAddr}) : {GeoIp(ipAddr)}");
                            }
                            break;

                        case "/ipv6":
                            BotClient.SendTextMessageAsync(message.Chat.Id,
                                                           $"{msgStr} : {GeoIp(msgStr)} / {GeoIpZXv6(msgStr)}");
                            break;

                        case "/dns":
                            try
                            {
                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"DNSPOD PubDNS : {HttpDnsPodHostAddresses(msgStr)}");

                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"1.1.1.1 DNS : {HttpsDnsHostAddresses(msgStr)}");

                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"Google DNS : {HttpsDnsHostAddresses(msgStr, true)}");
                            }
                            catch (Exception exception)
                            {
                                BotClient.SendTextMessageAsync(message.Chat.Id, exception.Message);
                            }
                            break;

                        case "/ping":
                            var replyPing = MPing.Ping(msgStr);

                            int packetLoss = 0;
                            foreach (var item in replyPing)
                            {
                                if (item == 0)
                                {
                                    packetLoss++;
                                }
                            }

                            if (packetLoss == replyPing.Count)
                            {
                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               "Packet loss : All");
                            }
                            else
                            {
                                List <int> pingList = new List <int>();
                                for (int i = 0; i < replyPing.Count - 1; i++)
                                {
                                    if (replyPing[i] != 0)
                                    {
                                        pingList.Add(replyPing[i]);
                                    }
                                }

                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"{msgStr} : {pingList.Min()} / {pingList.Average():0.00} / {pingList.Max()}ms"
                                                               + $"\n\rPacket loss : {packetLoss} / {replyPing.Count}");
                            }
                            break;

                        case "/tcping":
                            var ipPort = msgStr.Split(':', ':', ' ');
                            Console.WriteLine(ipPort.Count());
                            List <int> replyTcping;

                            replyTcping = ipPort.Length == 2
                                    ? MPing.Tcping(ipPort[0], Convert.ToInt32(ipPort[1]))
                                    : MPing.Tcping(ipPort[1], Convert.ToInt32(ipPort[2]));

                            int packetLossTcp = 0;
                            foreach (var item in replyTcping)
                            {
                                if (item == 0)
                                {
                                    packetLossTcp++;
                                }
                            }

                            if (packetLossTcp == replyTcping.Count)
                            {
                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               "Packet loss : All");
                            }
                            else
                            {
                                List <int> tcpingList = new List <int>();
                                for (int i = 0; i < replyTcping.Count - 1; i++)
                                {
                                    if (replyTcping[i] != 0)
                                    {
                                        tcpingList.Add(replyTcping[i]);
                                    }
                                }

                                BotClient.SendTextMessageAsync(message.Chat.Id,
                                                               $"{msgStr} : {tcpingList.Min()} / {tcpingList.Average():0.00} / {tcpingList.Max()}ms"
                                                               + $"\n\rPacket loss : {packetLossTcp} / {replyTcping.Count}");
                            }
                            break;

                        default:
                            BotClient.SendTextMessageAsync(message.Chat.Id,
                                                           @"意外的指令。");
                            break;
                        }
                    }
                    catch (Exception exception)
                    {
                        Console.WriteLine(exception);
                        BotClient.SendTextMessageAsync(message.Chat.Id,
                                                       @"非常抱歉,可能发生了一些意外的故障,请重试。");
                    }
                };
                bgWorker.RunWorkerAsync();
            }
            else
            {
                BotClient.SendTextMessageAsync(message.Chat.Id,
                                               @"意外的指令。");
            }
        }