写了一个端口映射工具, 代码贴出来下

时间:2021-08-01 03:25:48

这类工具挺实用的, 嗯有必要写一个, 放到github上  https://github.com/nsnail/PortMapNet

名字随便起的就叫PortMapNet好了

希望有好心人接着弄个界面啥的 搞个完整版的portmap tools for .net

主要代码如下, 也可以直接下载编译版使用(.net2.0) , ...怎么找不着上传文件在哪?   保存这张图片改扩展名7z吧 ---->   http://pic002.cnblogs.com/images/2012/16981/2012011613360243.jpg

代码比较简单 注释啥的就木时间弄了有了

 

对了 截图上一个 

写了一个端口映射工具, 代码贴出来下

  1 using System;
2 using System.Collections.Generic;
3 using System.Configuration;
4 using System.Net;
5 using System.Net.Sockets;
6 using System.Runtime.InteropServices;
7 using System.Text;
8 using System.Threading;
9 using log4net;
10
11 namespace portmap_net
12 {
13 internal class program
14 {
15 #region Fields (6)
16
17 private static StringBuilder _console_buf = new StringBuilder();
18 private static int _id_plus;
19 private static readonly ILog _l4n = LogManager.GetLogger(typeof(program));
20 private static readonly Dictionary<int, stat_obj> _stat_info = new Dictionary<int, stat_obj>();
21 public const string product_version = "0.1.113";
22 public const string product_version_addl = "beta";
23
24 #endregion Fields
25
26 #region Methods (8)
27
28 // Private Methods (8) 
29
30 private static List<work_group> load_maps_cfg()
31 {
32 string maps_cfg = ConfigurationManager.AppSettings["portmaps"];
33 if (string.IsNullOrEmpty(maps_cfg))
34 throw new Exception("配置文件错误: 缺少PortMap配置");
35
36 string[] tmp1 = maps_cfg.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
37 if (tmp1.Length == 0)
38 throw new Exception("配置文件错误: 缺少PortMap配置");
39
40 List<work_group> rtn = new List<work_group>();
41 foreach (string tmp2 in tmp1)
42 {
43 string[] tmp3 = tmp2.Split(new[] { '|' });
44 if (tmp3.Length != 2)
45 throw new Exception("配置文件错误: 每组PortMap配置必须为2个节点");
46 work_group rtn_item = new work_group { _id = ++_id_plus };
47 for (int i = 0; i != 2; ++i)
48 {
49 string[] tmp4 = tmp3[i].Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
50 if (tmp4.Length != 2)
51 throw new Exception("配置文件错误: IP节点格式错误");
52 IPAddress ip = IPAddress.Any;
53 if (i == 0 && tmp4[0] != "*" && !IPAddress.TryParse(tmp4[0], out ip))
54 throw new Exception("配置文件错误: IP节点格式错误");
55 ushort port;
56 if (!ushort.TryParse(tmp4[1], out port))
57 throw new Exception("配置文件错误: IP节点格式错误");
58 if (i == 0)
59 rtn_item._point_in = new IPEndPoint(ip, port);
60 if (i != 1) continue;
61 rtn_item._point_out_host = tmp4[0];
62 rtn_item._point_out_port = port;
63 }
64 rtn.Add(rtn_item);
65 }
66 return rtn;
67 }
68
69 private static void Main()
70 {
71 List<work_group> maps_list;
72 try
73 {
74 maps_list = load_maps_cfg();
75 }
76 catch (Exception exp)
77 {
78 Console.WriteLine(program_ver);
79 Console.WriteLine(exp.Message);
80 system("pause");
81 return;
82 }
83 foreach (var map_item in maps_list)
84 {
85 map_start(map_item);
86 }
87
88 Console.CursorVisible = false;
89 while (true)
90 {
91 show_stat();
92 Thread.Sleep(2000);
93 }
94 }
95
96 private static void map_start(work_group work)
97 {
98 Socket sock_svr = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
99 bool start_error = false;
100 try
101 {
102 sock_svr.Bind(work._point_in);
103 sock_svr.Listen(10);
104 sock_svr.BeginAccept(on_local_connected, new object[] { sock_svr, work });
105 }
106 catch (Exception exp)
107 {
108 _l4n.Error(exp.Message);
109 start_error = true;
110 }
111 finally
112 {
113 _stat_info.Add(work._id, new stat_obj(work._point_in.ToString(), work._point_out_host + ":" + work._point_out_port, !start_error, 0, 0, 0));
114 }
115 }
116
117 private static void on_local_connected(IAsyncResult ar)
118 {
119 object[] ar_arr = ar.AsyncState as object[];
120 Socket sock_svr = ar_arr[0] as Socket;
121 work_group work = (work_group)ar_arr[1];
122
123 ++_stat_info[work._id]._connect_cnt;
124 Socket sock_cli = sock_svr.EndAccept(ar);
125 sock_svr.BeginAccept(on_local_connected, ar.AsyncState);
126 Socket sock_cli_remote = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
127 try
128 {
129 sock_cli_remote.Connect(work._point_out_host, work._point_out_port);
130 }
131 catch (Exception exp)
132 {
133 _l4n.Warn(exp.Message);
134 try
135 {
136 sock_cli.Shutdown(SocketShutdown.Both);
137 sock_cli_remote.Shutdown(SocketShutdown.Both);
138 sock_cli.Close();
139 sock_cli_remote.Close();
140 }
141 catch (Exception)
142 { }
143 --_stat_info[work._id]._connect_cnt;
144 return;
145 }
146 Thread t_send = new Thread(recv_and_send_caller) { IsBackground = true };
147 Thread t_recv = new Thread(recv_and_send_caller) { IsBackground = true };
148 t_send.Start(new object[] { sock_cli, sock_cli_remote, work._id, true });
149 t_recv.Start(new object[] { sock_cli_remote, sock_cli, work._id, false });
150 t_send.Join();
151 t_recv.Join();
152 --_stat_info[work._id]._connect_cnt;
153 }
154
155 private static void recv_and_send(Socket from_sock, Socket to_sock, Action<int> send_complete)
156 {
157 byte[] recv_buf = new byte[4096];
158 int recv_len;
159 while ((recv_len = from_sock.Receive(recv_buf)) > 0)
160 {
161 to_sock.Send(recv_buf, 0, recv_len, SocketFlags.None);
162 send_complete(recv_len);
163 }
164 }
165
166 private static void recv_and_send_caller(object thread_param)
167 {
168 object[] param_arr = thread_param as object[];
169 Socket sock1 = param_arr[0] as Socket;
170 Socket sock2 = param_arr[1] as Socket;
171 try
172 {
173 recv_and_send(sock1, sock2, bytes =>
174 {
175 stat_obj stat = _stat_info[(int)param_arr[2]];
176 if ((bool)param_arr[3])
177 stat._bytes_send += bytes;
178 else
179 stat._bytes_recv += bytes;
180 });
181 }
182 catch (Exception exp)
183 {
184 _l4n.Info(exp.Message);
185 try
186 {
187 sock1.Shutdown(SocketShutdown.Both);
188 sock2.Shutdown(SocketShutdown.Both);
189 sock1.Close();
190 sock2.Close();
191 }
192 catch (Exception) { }
193 }
194 }
195
196 private static void show_stat()
197 {
198 StringBuilder curr_buf = new StringBuilder();
199 curr_buf.AppendLine(program_ver);
200 curr_buf.AppendLine(stat_obj._print_head);
201 foreach (KeyValuePair<int, stat_obj> item in _stat_info)
202 {
203 curr_buf.AppendLine(item.Value.ToString());
204 }
205 if (_console_buf.Equals(curr_buf))
206 return;
207 Console.Clear();
208 Console.WriteLine(curr_buf);
209 _console_buf = curr_buf;
210 }
211
212 [DllImport("msvcrt.dll")]
213 private static extern bool system(string str);
214
215 #endregion Methods
216
217 private const string program_ver = @"[PortMapNet(0.1) http://www.beta-1.cn]
218 --------------------------------------------------";
219 }
220 }