forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClass1.cs
More file actions
214 lines (188 loc) · 7.23 KB
/
Class1.cs
File metadata and controls
214 lines (188 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace NSmartProxy
{
public static class NetworkUtil
{
private const string PortReleaseGuid = "C086DE94-2C45-4247-81E2-2E5248F5A769";
private static HashSet<int> _usedPorts = new HashSet<int>();
public static bool ReleasePort(int port)
{
return _usedPorts.Remove(port);
}
/// <summary>
/// 查找一个端口
/// </summary>
/// <param name="startPort"></param>
/// <returns></returns>
public static int FindOneAvailableTCPPort(int startPort)
{
return NetworkUtil.FindAvailableTCPPorts(startPort, 1)[0];
}
/// <summary>
/// Check if startPort is available, incrementing and
/// checking again if it's in use until a free port is found
/// </summary>
/// <param name="startPort">The first port to check</param>
/// <returns>The first available port</returns>
public static int[] FindAvailableTCPPorts(int startPort, int PortCount)
{
int[] arrangedPorts = new int[PortCount];
int port = startPort;
bool isAvailable = true;
var mutex = new Mutex(false,
PortReleaseGuid
/*string.Concat("./Global/", PortReleaseGuid)*/);
mutex.WaitOne();
try
{
for (int i = 0; i < PortCount; i++)
{
IPGlobalProperties ipGlobalProperties =
IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints =
ipGlobalProperties.GetActiveTcpListeners();
do
{
if (!isAvailable)
{
port++;
isAvailable = true;
}
if (_usedPorts.Contains(port))
{
isAvailable = false;
continue;
}
foreach (IPEndPoint endPoint in endPoints)
{
if (endPoint.Port != port) continue;
isAvailable = false;
break;
}
} while (!isAvailable && port < IPEndPoint.MaxPort);
if (!isAvailable)
throw new ApplicationException("Not able to find a free TCP port.");
arrangedPorts[i] = port;
_usedPorts.Add(port);
}
return arrangedPorts;
}
finally
{
mutex.ReleaseMutex();
}
}
/// <summary>
/// Check if startPort is available, incrementing and
/// checking again if it's in use until a free port is found
/// </summary>
/// <param name="startPort">The first port to check</param>
/// <returns>The first available port</returns>
public static int FindNextAvailableUDPPort(int startPort)
{
int port = startPort;
bool isAvailable = true;
var mutex = new Mutex(false,
string.Concat("Global/", PortReleaseGuid));
mutex.WaitOne();
try
{
IPGlobalProperties ipGlobalProperties =
IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints =
ipGlobalProperties.GetActiveUdpListeners();
do
{
if (!isAvailable)
{
port++;
isAvailable = true;
}
foreach (IPEndPoint endPoint in endPoints)
{
if (endPoint.Port != port)
continue;
isAvailable = false;
break;
}
} while (!isAvailable && port < IPEndPoint.MaxPort);
if (!isAvailable)
throw new ApplicationException("Not able to find a free TCP port.");
return port;
}
finally
{
mutex.ReleaseMutex();
}
}
public static void SetKeepAlive(this TcpClient tcpClient, out string ErrorMsg)
{
ErrorMsg = "";
// not all platforms support IOControl
//try
//{
// var keepAliveValues = new KeepAliveValues
// {
// OnOff = 1,
// KeepAliveTime = 300000, // 300 seconds in milliseconds
// KeepAliveInterval = 10000 // 10 seconds in milliseconds
// };
// socket.IOControl(IOControlCode.KeepAliveValues, keepAliveValues.ToBytes(), null);
//}
//catch (PlatformNotSupportedException)
//{
// // most platforms should support this call to SetSocketOption, but just in case call it in a try/catch also
// try
// {
// socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
// }
// catch (PlatformNotSupportedException)
// {
// // ignore PlatformNotSupportedException
// }
//}
//return socket;
//try
//{
tcpClient.Client.IOControl(IOControlCode.KeepAliveValues, GetKeepAlivePkg(1, 1000, 20000), null);
//}
//catch (PlatformNotSupportedException)
//{
// // most platforms should support this call to SetSocketOption, but just in case call it in a try/catch also
// try
// {
// tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
// }
// catch (PlatformNotSupportedException ex)
// {
// //平台不支持
// ErrorMsg = ex.Message;
// }
//}
}
/// <summary>
/// 生成keepalive包用于Socket.IOControl
/// </summary>
/// <param name="onOff">是否開啟 Keep-Alive(開 1/ 關 0)</param>
/// <param name="keepAliveTime">當開啟keep-Alive後經過多久時間(ms)開啟偵測</param>
/// <param name="keepAliveInterval">多久偵測一次</param>
/// <returns></returns>
static byte[] GetKeepAlivePkg(int onOff, int keepAliveTime, int keepAliveInterval)
{
byte[] buffer = new byte[12];
BitConverter.GetBytes(onOff).CopyTo(buffer, 0);
BitConverter.GetBytes(keepAliveTime).CopyTo(buffer, 4);
BitConverter.GetBytes(keepAliveInterval).CopyTo(buffer, 8);
return buffer;
}
}
}