Cần người giúp đỡ về code C#

kegiaumat

Junior
Joined
Aug 4, 2013
Messages
272
Reactions
60
MR
0.016
em mới nhập môn C# xin được ít code của anh em về change SSH. em sửa được một ít nhưng đến chỗ cho tool check 24/7 nếu con SSH bị die thì change con khác nhưng ko biết cách nào các bác cho em xin ý kiến.
em cảm ơn nhiều.


Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.IO;

//using MSTSCLib;
using System.Threading;
using System.Management;

using System.Net.NetworkInformation;
using System.Net;
using System.Runtime.InteropServices;
//using ManagedWinapi.Windows;
using System.Text.RegularExpressions;
//using System.Windows.Forms;


namespace ChangerSSH
{
    public partial class Form1 : Form
    {
        public int thread=0;
        public Form1()
        {
            InitializeComponent();
            this.FormClosing += Form1_FormClosing;
        }
       
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            string PlinkCommand = "Taskkill /IM plink.exe /F";

            string CheckLiveCommand = "Taskkill /IM CheckC#.exe /F";

            Process proc = new Process();
            proc.StartInfo.FileName = "CMD.exe";
            proc.StartInfo.Arguments = "/c " + PlinkCommand;
            proc.StartInfo.Arguments = "/c " + CheckLiveCommand;
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            proc.Start();
        }

        private int porti = 1080;
        private int top = 100;
        Label[] l1 = new Label[50];
        int[,] x = new int[50, 4];
        Random ranSSH = new Random();
        int sshi = 1;
           
       
        internal bool CheckSocksAlive(string host, int port, int TryTimes)
        {
            TryTimes++;
            Status.Text = "Thử lần: " + TryTimes.ToString();
            TcpClient tcpClient = new TcpClient();
            try
            {
                tcpClient.Connect(host, port);
                Status.Text = "Connected";
                Status.Refresh();
                return true;
            }
            catch (Exception)
            {
                Status.Refresh();
                if (TryTimes > 5) return tcpClient.Connected;
                return CheckSocksAlive(host, port, TryTimes);
                //if (tcpClient.Connected) return true;
                //else return false;
            }
        }
       
        //List<Button> buttons = new List<Button>();
        private void NewBtn_Click(object sender, EventArgs e)
        {
            //int t = thread;
            porti = Int32.Parse(StartPortTbx.Text);
            //MessageBox.Show(porti.ToString());
           // MessageBox.Show(thread.ToString());
            for (int i = thread; i < Int32.Parse(ThreadsTbx.Text) + thread; i++)
            {
               
                Button newButton = new Button();
                //buttons.Add(newButton);
                newButton.Name = porti.ToString();
                newButton.Height = 23;
                newButton.Width = 90;
                newButton.Left = 300;
                newButton.Top = top;
                newButton.FlatStyle = 0;
                newButton.Text = "Change_" + newButton.Name;
                this.Controls.Add(newButton);
                newButton.Click += new EventHandler(ButtonClickOneEvent);
                l1[i] = new Label();
                l1[i].Name = porti.ToString();
                l1[i].Width = 300;
                l1[i].Left = 10;
                l1[i].Top = top + 5;
                l1[i].Text = newButton.Name;
                this.Controls.Add(l1[i]);
                top = top + 30;
                porti++;
                x[i,0] = i;
                x[i,1] = porti;
            }
            thread = thread + Int32.Parse(ThreadsTbx.Text);
            StartPortTbx.Text = porti.ToString();
            StartPortTbx.Refresh();
        }

        private void ButtonClickOneEvent(object sender, EventArgs e)
        {
            int iii;

           
           
            Button button = sender as Button;
            Label label = sender as Label;
            //MessageBox.Show(button.Name);
            for (iii = 0; iii < thread; iii++)
            {

                if (l1[iii].Name == button.Name)
                {
                    Console.WriteLine(iii);
                    Console.WriteLine("Xử lý SSH cổng: " + l1[iii].Name);
                    Console.WriteLine("ProcID =" + x[iii, 3]);
                    if (x[iii, 3] != 0)
                    {
                        string PlinkCommand4 = "Taskkill /F /PID " + x[iii, 3] + " /T";
                        Process proc4 = new Process();
                        proc4.StartInfo.FileName = "CMD.exe";
                        proc4.StartInfo.Arguments = "/c " + PlinkCommand4;
                        proc4.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        proc4.Start();
                    }
                    int port = Int32.Parse(button.Name);
                CheckSSHline:

                    if (RandomSSH.Checked)
                    {
                        sshi = ranSSH.Next(1, SshTbx.Lines.Length);
                    }
                    if (SshTbx.Lines[sshi - 1] == "")
                    {
                        Console.WriteLine(SshTbx.Lines[sshi - 1]);
                        sshi++;
                        goto CheckSSHline;
                    };
                    if (sshi <= SshTbx.Lines.Length)
                    {
                        //Back:
                        string[] SshInfo = SshTbx.Lines[sshi - 1].Split('|');
                        l1[iii].Text = l1[iii].Name + ": Connecting to " + SshInfo[0];
                        l1[iii].Refresh();
                        bool conn = false;
                        string PlinkCommand = "echo y | plink.exe -ssh -D " + port + " " + SshInfo[1] + "@" + SshInfo[0] + " -N -pw " + SshInfo[2];
                        Process proc = new Process();
                        proc.StartInfo.FileName = "CMD.exe";
                        proc.StartInfo.Arguments = "/c " + PlinkCommand;
                        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                        proc.Start();
                        //await Task.Delay(1500);
                   
                        conn = CheckSocksAlive("127.0.0.1", port, 0);
                        if (conn)
                        {
                            x[iii, 3] = proc.Id;
                            l1[iii].Text = l1[iii].Name + ": Connected " + SshInfo[0];
                            l1[iii].Refresh();
                           
                        }
                        else
                        {
                            string PlinkCommand4 = "Taskkill /F /PID " + proc.Id + " /T";
                            Process proc4 = new Process();
                            proc4.StartInfo.FileName = "CMD.exe";
                            proc4.StartInfo.Arguments = "/c " + PlinkCommand4;
                            //proc4.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                            proc4.Start();
                            l1[iii].Text = l1[iii].Name + ": Connect Failed. Next SSH =============>>>>>> ";
                            l1[iii].Refresh();
                            sshi++;
                            goto CheckSSHline;
                        }
                        //break;
                        Console.WriteLine("ProcIDAfter =" + x[iii, 3]);
                    }
                    else
                    {
                        MessageBox.Show("Hết SSH ");
                    }

                };

               

            }
            sshi++;
            label5.Text = (Int32.Parse(label5.Text) + 1).ToString();
            if (Aoto247.Checked)
            {

               Process[] processlist = Process.GetProcesses();

                foreach(Process theprocess in processlist)
                {

                    if(theprocess.ProcessName.StartsWith("CheckC#"))
                         theprocess.Kill(); 
                }
                Process proc2 = new Process();
                proc2.StartInfo.FileName = "CheckC#.exe";
                proc2.Start();

            }
        }


        private void PopulateList(string filePath, RichTextBox A, Label L)
        {

            try
            {
                this.Invoke((MethodInvoker)delegate()
                { A.Text = File.ReadAllText(filePath); });
                // show_log2(0,0,"ok2");
            }
            catch (Exception)
            {
                MessageBox.Show("Please Load List");
            }
            this.Invoke((MethodInvoker)delegate()
            {
                L.Text = A.Lines.Count().ToString();
            });


        }
        private void FillSSH_Click(object sender, EventArgs e)
        {
            string path = null;
            OpenFileDialog choofdlog = new OpenFileDialog();
            choofdlog.Filter = "All Files (*.*)|*.*";
            choofdlog.FilterIndex = 1;
            choofdlog.Multiselect = true;
            if (choofdlog.ShowDialog() == DialogResult.OK)
            {
                string sFileName = choofdlog.FileName;
                string[] arrAllFiles = choofdlog.FileNames; //used when Multiselect = true       
                path = sFileName;
                try
                {
                    PopulateList(path, SshTbx, label7);
                    textBoxPathFile.Text = path;
                    //  richTextBox1.Text = string.Join(Environment.NewLine, richTextBox1.Lines.Distinct());//remove near duplicate
                }
                catch { }
            }
        }
       
        private void Random_CheckedChanged(object sender, EventArgs e)
        {
       
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SshTbx.AllowDrop = true;
            SshTbx.DragDrop += (ss, ee) =>
            {
                string[] files = (string[])ee.Data.GetData(DataFormats.FileDrop);

                SshTbx.Text = SshTbx.Text + File.ReadAllText(files[0]);

                label7.Text = SshTbx.Lines.Count().ToString();

            };
        } 
    }
}

}
 

Attachments

Announcements

Today's birthdays

Forum statistics

Threads
434,194
Messages
7,316,971
Members
185,635
Latest member
keowinfootball

Most viewed of week

Most viewed of week

Back
Top Bottom