利用C#代码创建、查看、删除端口转发

10 篇文章 0 订阅
3 篇文章 0 订阅

创建端口转发

   private static bool CreateChannle(out string msg)
        {
            msg = "";
            Random random = new Random();
            int port = 0;
            int times = 0;
            do
            {
                port = random.Next(1000, 50000);
                times++;
            } while (!CheckPortAvailable(port) && times < 10);
            if (times >= 10)
            {
                msg = "无可用端口";
                return false;
            }
            string cmd = string.Format("netsh interface portproxy add v4tov4 listenport={0} connectaddress=dtdl.channel.lebaoba.com connectport=11808",port);
            string r = ExecuteCmd(cmd);
            string result = r.Substring(r.IndexOf("exit") + 4);
            if (result.Replace("\r\n", "").Length == 0)
            {
                msg = "127.0.0.1:" + port.ToString();
                return true;
            }
            else
            {
                msg = result;
                return false;
            }
        }

检查端口是否可用

private static bool CheckPortAvailable(int port)
        {
            string cmd = string.Format("netstat -ano | findstr \"{0}\"", port);
            string r = ExecuteCmd(cmd);
            string result = r.Substring(r.IndexOf("exit") + 4);
            if (result.Replace("\r\n", "").Length == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

查看创建的所有转发通道

private static List<string> SearchChannle()
        {
            List<string> list = new List<string>();
            string cmd = "netsh interface portproxy show v4tov4";
            string r = ExecuteCmd(cmd);
            r = r.Substring(r.IndexOf("exit") + 4);
            string[] lines = r.Replace("\r\n", "^").Split('^');
            for (int i = 6; i < lines.Length; i++)
            {
                if (!string.IsNullOrEmpty(lines[i]))
                {
                    string channelString = "";
                   string[] temp = lines[i].Split(' ');
                    foreach (var item in temp)
                    {
                        if (!string.IsNullOrEmpty(item))
                        {
                            channelString += item + "|";
                        }
                    }
                    list.Add(channelString);
                }
            }

            return list;
        }

删除一条转发通道

private static void DeleteChannel(string ip,string port)
        {
            string cmd = "";
            if (ip == "*")
            {
                cmd = string.Format("netsh interface portproxy delete v4tov4 listenaddress={0} listenport={1}", ip, port);
            }
            else
            {
                cmd = string.Format("netsh interface portproxy delete v4tov4 listenport={0}", port);
            }

            ExecuteCmd(cmd);
        }

删除所有转发通道

private static void DeleteAllChannels()
        {
            List<string> channcelList = SearchChannle();
            foreach (var channcel in channcelList)
            {
                DeleteChannel(channcel.Split('|')[0], channcel.Split('|')[1]);
            }
        }

执行cmd指令的方法

private static string ExecuteCmd(string strInput)
        {
            Process p = new Process();
            //设置要启动的应用程序
            p.StartInfo.FileName = "cmd.exe";
            //是否使用操作系统shell启动
            p.StartInfo.UseShellExecute = false;
            // 接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardInput = true;
            //输出信息
            p.StartInfo.RedirectStandardOutput = true;
            // 输出错误
            p.StartInfo.RedirectStandardError = true;
            //不显示程序窗口
            p.StartInfo.CreateNoWindow = true;
            //启动程序
            p.Start();
            //向cmd窗口发送输入信息
            p.StandardInput.WriteLine(strInput + "&exit");
            p.StandardInput.AutoFlush = true;
            //获取输出信息
            string strOuput = p.StandardOutput.ReadToEnd();
            //等待程序执行完退出进程
            p.WaitForExit();
            p.Close();
            return strOuput;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值