forked from tmoonlight/NSmartProxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientConnectionManager.cs
More file actions
362 lines (328 loc) · 14.5 KB
/
ClientConnectionManager.cs
File metadata and controls
362 lines (328 loc) · 14.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
using NSmartProxy.Data;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using NSmartProxy.Authorize;
using NSmartProxy.Database;
using NSmartProxy.Infrastructure;
using NSmartProxy.Shared;
using static NSmartProxy.Server;
namespace NSmartProxy
{
/// <summary>
/// 反向连接处理类
/// </summary>
public class ClientConnectionManager
{
/// <summary>
/// 当app增加时触发
/// </summary>
// public event EventHandler<AppChangedEventArgs> AppTcpClientMapReverseConnected = delegate { };
public event EventHandler<AppChangedEventArgs> AppTcpClientMapConfigApplied = delegate { };
private NSPServerContext ServerContext;
private ClientConnectionManager()
{
Server.Logger.Debug("ClientManager initialized");
// Task.Run(ListenServiceClient);
}
private readonly object _lockObject = new Object();
private readonly object _lockObject2 = new Object();
private readonly Random _rand = new Random();
public async Task ListenServiceClient(IDbOperator dbOp)
{
//侦听,并且构造连接池
Server.Logger.Debug("Listening client on port " + ServerContext.ServerConfig.ReversePort + "...");
TcpListener listener = new TcpListener(IPAddress.Any, ServerContext.ServerConfig.ReversePort);
listener.Start(1000);
while (true)
{
SecurityTcpClient incomeClient = await listener.AcceptSecureTcpClientAsync(dbOp);
Server.Logger.Debug("已建立一个空连接" +
incomeClient.Client.Client.LocalEndPoint.ToString() + "-" +
incomeClient.Client.Client.RemoteEndPoint.ToString());
incomeClient.Client.SetKeepAlive(out _);
_ = ProcessReverseRequest(incomeClient);
}
}
public static ClientConnectionManager GetInstance()
{
return Instance;
}
public ClientConnectionManager SetServerContext(NSPServerContext serverContext)
{
ServerContext = serverContext;
return this;
}
/// <summary>
/// 处理反向连接请求(服务端)
/// </summary>
/// <param name="incomeClient"></param>
/// <returns></returns>
private async Task ProcessReverseRequest(SecurityTcpClient incomeClient)
{
var iClient = incomeClient.Client;
try
{
var result = await incomeClient.AuthorizeAsync();
if (result == null) return;//主动关闭
if (!result.IsSuccess)
{
Server.Logger.Debug("SecurityTcpClient校验失败:" + incomeClient.ErrorMessage);
await iClient.GetStream().WriteAsync(new byte[] { (byte)result.ResultState });
iClient.Close();//如果校验失败则发送一个字节的直接关闭连接
}
else
{
Server.Logger.Debug("SecurityTcpClient校验成功!");
await iClient.GetStream().WriteAsync(new byte[] { (byte)result.ResultState });
}
//读取头四个字节
byte[] bytes = new byte[4];
if (await iClient.GetStream().ReadAsync(bytes, 0, bytes.Length, Global.DefaultConnectTimeout) < 1)
{
Server.Logger.Debug("服务端read出错,关闭连接");
incomeClient.Client.Close();
return;
}
var clientIdAppId = GetAppFromBytes(bytes);
Server.Logger.Debug("已获取到消息ClientID:" + clientIdAppId.ClientID
+ "AppID:" + clientIdAppId.AppID
);
//分配
lock (_lockObject)
{
ServerContext.Clients[clientIdAppId.ClientID].GetApp(clientIdAppId.AppID)
.PushInComeClient(iClient);
}
//var arg = new AppChangedEventArgs();
//arg.App = clientIdAppId;
//AppTcpClientMapReverseConnected(this, arg);
}
catch (Exception e)
{
Logger.Debug(e);
}
}
private static readonly ClientConnectionManager Instance = new Lazy<ClientConnectionManager>(() => new ClientConnectionManager()).Value;
public TcpClient GetClientForUdp(int consumerPort, string host = null)
{
NSPApp nspApp = null;
nspApp = ServerContext.UDPPortAppMap[consumerPort].ActivateApp;
TcpClient client = nspApp.TcpClientBlocks.Peek();
if (client == null)
{
throw new TimeoutException($"UDP获取{consumerPort}失败");
}
return client;
}
/// <summary>
/// 从当前app的连接池取出一个socket
/// </summary>
/// <param name="consumerPort"></param>
/// <param name="host"></param>
/// <param name="onlySeek">仅仅获取这个client 而不消费它(开启新连接)</param>
/// <returns></returns>
public async Task<TcpClient> GetClientForTcp(int consumerPort, string host = null)
{
NSPApp nspApp = null;
if (host == null || string.IsNullOrEmpty(host.Trim())) //host为空则随便匹配一个
{ nspApp = ServerContext.PortAppMap[consumerPort].ActivateApp; }
else
{
if (!ServerContext.PortAppMap[consumerPort].TryGetValue(host, out nspApp))
{
//throw new KeyNotFoundException($"无法找到{consumerPort}的host:{host}");
Server.Logger.Debug($"无法找到{consumerPort}的host:{host}");
nspApp = ServerContext.PortAppMap[consumerPort].ActivateApp;
}
}
if (nspApp == null) throw new KeyNotFoundException($"无法找到{consumerPort}下的任何一个客户端app");
//TODO ***需要处理服务端长时间不来请求的情况(无法建立隧道)
//TODO 2这里的弹出比较麻烦了
TcpClient client = await nspApp.PopClientAsync();
if (client == null)
{
throw new TimeoutException($"弹出{consumerPort}超时");
}
//TODO 2 反向链接还写在这里??
//ServerContext.PortAppMap[consumerPort].ActivateApp.ReverseClients.Add(client);
nspApp.ReverseClients.Add(client);
return client;
}
/// <summary>
/// 分配端口方法
/// arrange ConfigId from top 4 bytes which received from client.
/// response:
/// 2 1 1 1 1 ...N
/// clientid appid port appid2 port2
/// request:
/// 2 2
/// clientid count
/// methodType value = 0
/// </summary>
public byte[] ArrangeConfigIds(byte[] appRequestBytes, byte[] consumerPortBytes, int highPriorityClientId)
{
// byte[] arrangedBytes = new byte[256];
ClientModel clientModel = new ClientModel();
int clientId;
//apprequestbytes里本身有clientId,但是如果传了highPriorityClientId,那就用这个clientId
if (highPriorityClientId != 0)
{
clientId = highPriorityClientId;
}
else
{
clientId = StringUtil.DoubleBytesToInt(appRequestBytes[0], appRequestBytes[1]);
}
//2.分配clientid //TODO 这一段可能不会再用到了
int appCount = (int)appRequestBytes[2];
if (clientId == 0)
{
lock (_lockObject)
{
byte[] tempClientIdBytes = new byte[2];
//分配clientid
for (int i = 0; i < 10000; i++)
{
_rand.NextBytes(tempClientIdBytes);
int tempClientId = StringUtil.DoubleBytesToInt(tempClientIdBytes);
if (!ServerContext.Clients.ContainsKey(tempClientId))
{
clientModel.ClientId = tempClientId;
clientId = tempClientId;
break;
}
}
}
}
else
{
clientModel.ClientId = clientId;
}
//注册客户端
ServerContext.Clients.RegisterNewClient(clientModel.ClientId);
//port proto option(iscompress) host description
//2 1 1 1024 96
int oneEndpointLength = 2 + 1 + 1 + 1024 + 96;
lock (_lockObject2)
{
//注册app
clientModel.AppList = new List<App>(appCount);
for (int i = 0; i < appCount; i++)
{
int offset = oneEndpointLength * i;
int startPort = StringUtil.DoubleBytesToInt(consumerPortBytes[offset], consumerPortBytes[offset + 1]);
int arrangedAppid = ServerContext.Clients[clientId].RegisterNewApp();
Protocol protocol = (Protocol)consumerPortBytes[offset + 2];
bool isCompress = consumerPortBytes[offset + 3] == 1 ? true : false;
string host = Encoding.ASCII.GetString(consumerPortBytes, offset + 4, 1024).TrimEnd('\0');
string description = Encoding.UTF8.GetString(consumerPortBytes, offset + 4 + 1024, 96).TrimEnd('\0');
//查找port的起始端口如果未指定,则设置为20000
if (startPort == 0) startPort = Global.StartArrangedPort;
int port = 0;
//如果端口是指定的并且是绑定的,不加任何检测
bool hasListened = false;
if (IsBoundedByUser(clientId, startPort))
{
port = startPort;
}
else
{
if (protocol == Protocol.TCP)
{
int relocatedPort = NetworkUtil.FindOneAvailableTCPPort(startPort);
port = relocatedPort;
}
else if (protocol == Protocol.UDP)
{
int relocatedPort = NetworkUtil.FindOneAvailableUDPPort(startPort);
port = relocatedPort;
}
else if (protocol == Protocol.HTTP)
{//因为端口复用,允许公用端口
int relocatedPort = NetworkUtil.FindOneAvailableTCPPort(startPort);
//兼容http侦听端口公用
if (port != relocatedPort)
{
//http协议下如果portappmap已经有值,说明已经发起过侦听,接下来不必再侦听
if (ServerContext.PortAppMap.ContainsKey(startPort))
{
port = startPort;
hasListened = true;
}
else
{
port = relocatedPort;
}
}
}
}
NSPApp app = ServerContext.Clients[clientId].AppMap[arrangedAppid];
app.ClientId = clientId;
app.AppId = arrangedAppid;
app.ConsumePort = port;
app.AppProtocol = protocol;
app.Host = host;
app.Description = description;
app.Tunnels = new List<TcpTunnel>();
app.ReverseClients = new List<TcpClient>();
app.IsCompress = isCompress;
if (protocol == Protocol.UDP)
{
if (!ServerContext.UDPPortAppMap.ContainsKey(port))
{
ServerContext.UDPPortAppMap[port] = new NSPAppGroup();
}
ServerContext.UDPPortAppMap[port][host] = app;
}
else
{
//TODO 设置app的host和protocoltype(TCP Only)
if (!ServerContext.PortAppMap.ContainsKey(port))
{
ServerContext.PortAppMap[port] = new NSPAppGroup();
}
ServerContext.PortAppMap[port][host] = app;
}
clientModel.AppList.Add(new App
{
AppId = arrangedAppid,
Port = port
});
Logger.Info(port);
//配置时触发
if (!hasListened)
{
AppTcpClientMapConfigApplied(this, new AppChangedEventArgs() { App = app });//触发listener侦听
}
}
Logger.Debug(" <=端口已分配。");
}
return clientModel.ToBytes();
}
private bool IsBoundedByUser(int clientId, int port)
{
var boundHash = ServerContext.ServerConfig.BoundConfig.UserPortBounds;
if (boundHash.TryGetValue(clientId.ToString(), out UserPortBound userBound))
{
return userBound.Bound.Contains(port);
}
return false;
}
private ClientIDAppID GetAppFromBytes(byte[] bytes)
{
return new ClientIDAppID()
{
ClientID = (bytes[0] << 8) + bytes[1],
AppID = bytes[2]
};
}
}
}