private static void TcpServer_NewSessionConnected(TcpAppSession session) { try { //转发请求 var natSession = NATServer.GetSessions(c => c.MapList?.Any(m => m.remote_port == session.LocalEndPoint.Port) ?? false).FirstOrDefault(); if (natSession == null) { session?.Close(); HandleLog.WriteLine($"请求:{session.LocalEndPoint}失败,Nat客户端连接不在线!"); return; } var map = natSession.MapList?.Find(c => c.remote_port == session.LocalEndPoint.Port); if (map == null) { session?.Close(); HandleLog.WriteLine($"请求:{session.LocalEndPoint}失败,映射{session.LocalEndPoint}不存在!"); return; } session.Map = map; var pack = new PackJson() { Host = map?.remote_endpoint, Local = map?.local_endpoint, UserId = session.UserId, Method = "TCP" }; session.PackJson = pack; var json = JsonHelper.Instance.Serialize(pack); var jsonBytes = Encoding.UTF8.GetBytes(json); //03 01 数据长度(4) 正文数据(n) ---tcp连接注册包 var sendBytes = new List <byte>() { 0x3, 0x1 }; sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse()); sendBytes.AddRange(jsonBytes); natSession.Send(sendBytes.ToArray(), 0, sendBytes.Count); session.NatSession = natSession; HandleLog.WriteLine($"客户端【{session.PackJson.UserId},{session.RemoteEndPoint}】已连接【{session.LocalEndPoint}】"); } catch (Exception ex) { HandleLog.WriteLine($"连接【{session.PackJson.UserId},{session.LocalEndPoint}】发生异常:{ex}"); } }
private static void WebServer_NewRequestReceived(WebAppSession session, HttpRequestInfo requestInfo) { Task.Run(() => { try { if (session.RequestTime == null) { session.RequestTime = DateTime.Now; } //转发请求 var host = requestInfo.HeaderDict["Host"]; var natSession = NATServer.GetSessions(c => c.MapList?.Any(m => m.remote_endpoint == host) ?? false).FirstOrDefault(); if (natSession == null) { session?.Close(); HandleLog.WriteLine($"请求:{host}失败,Nat客户端连接不在线!"); return; } var pack = new PackJson() { Host = host, UserId = session.UserId, Method = requestInfo.Method, Route = requestInfo.Route, Headers = requestInfo.HeaderDict, Content = requestInfo.Content }; var json = JsonHelper.Instance.Serialize(pack); var jsonBytes = Encoding.UTF8.GetBytes(json); //02 01 数据长度(4) 正文数据(n) ---http响应包 var sendBytes = new List <byte>() { 0x2, 0x1 }; sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse()); sendBytes.AddRange(jsonBytes); natSession.Send(sendBytes.ToArray(), 0, sendBytes.Count); } catch (Exception ex) { HandleLog.WriteLine($"【{session.RemoteEndPoint}】请求参数:{Encoding.UTF8.GetString(requestInfo.Data)},处理发生异常:{ex}"); } }); }
public static void ChangeMap(int type, Map map) { try { map.ChangeType = type; ChangeMap(map); //请求头 01 04 长度(4) var sendBytes = new List <byte>() { 0x1, 0x4 }; var jsonBytes = Encoding.UTF8.GetBytes(JsonHelper.Instance.Serialize(map)); sendBytes.AddRange(BitConverter.GetBytes(jsonBytes.Length).Reverse()); sendBytes.AddRange(jsonBytes); var natClient = NATServer.GetSessions(c => c.Client?.id == map.client_id).FirstOrDefault(); natClient?.Send(sendBytes.ToArray()); } catch (Exception ex) { HandleLog.WriteLine($"映射更新异常:{ex},参数为:{JsonHelper.Instance.Serialize(map)}"); } }
static void ChangeMap(Map map) { try { var session = NATServer.GetSessions(c => c.Client.id == map.client_id).FirstOrDefault(); if (session == null) { return; } if (session.MapList == null) { session.MapList = new List <Map>(); } switch (map.ChangeType) { case (int)ChangeMapType.新增: session.MapList.Add(map); break; case (int)ChangeMapType.修改: session.MapList.RemoveAll(c => c.id == map.id); session.MapList.Add(map); break; case (int)ChangeMapType.除: session.MapList.RemoveAll(c => c.id == map.id); break; } HandleLog.WriteLine($"映射{Enum.GetName(typeof(ChangeMapType), map.ChangeType)}成功:{JsonHelper.Instance.Serialize(map)}", false); HandleLog.WriteLine($"【{map.name}】映射{Enum.GetName(typeof(ChangeMapType), map.ChangeType)}成功:{map.local_endpoint} --> {map.remote_endpoint}"); } catch (Exception ex) { throw ex; } }
private static void NATServer_NewRequestReceived(NatAppSession session, NatRequestInfo requestInfo) { Task.Run(() => { try { //HandleLog.WriteLine($"NAT服务收到数据:{requestInfo.Hex}"); if (requestInfo.Mode == 0x1)//nat { switch (requestInfo.FunCode) { case 0x1: { //注册包 var secret = requestInfo.BodyRaw; using var bll = new ClientBll(); var client = bll.GetOne(secret).Data; if (client == null) { HandleLog.WriteLine($"主机【{session.RemoteEndPoint}】密钥不正确!!"); session.SendMsg("主机密钥不正确,请确认是否填写正确!"); return; } var checkSessions = NATServer.GetSessions(c => c.Client?.secret == secret).ToList(); if (checkSessions.Any()) { checkSessions.ForEach(c => { session.SendMsg($"您的密钥已被主机{client.name},{session.RemoteEndPoint}使用,已强制下线!!"); Thread.Sleep(500); c.Close(); }); } session.Client = client; using var mapBll = new MapBll(); session.MapList = mapBll.GetMapList(secret).Data ?? new List <Map>(); //原样返回回复客户端注册成功 session.Send(requestInfo.Data); Task.Run(() => { //更新在线状态 using var bll = new ClientBll(); var updateRst = bll.UpdateOnlineStatus(new Client() { secret = session.Client.secret, is_online = true, last_heart_time = DateTime.Now }); HandleLog.WriteLine($"更新主机【{session.Client.name}】在线状态结果:{updateRst.Message}", false); }); } break; case 0x2: { //心跳包 var secret = requestInfo.BodyRaw; HandleLog.WriteLine($"收到连接{session.RemoteEndPoint}的心跳包,密钥为:{secret},当前映射个数:{session.MapList.Count}", false); Task.Run(() => { //更新在线状态 using var bll = new ClientBll(); var updateRst = bll.UpdateOnlineStatus(new Client() { secret = session.Client.secret, is_online = true, last_heart_time = DateTime.Now }); HandleLog.WriteLine($"更新主机【{session.Client.name}】在线状态结果:{updateRst.Message}", false); }); } break; } } else if (requestInfo.Mode == 0x2)//http { var packJson = JsonHelper.Instance.Deserialize <PackJson>(requestInfo.BodyRaw); switch (requestInfo.FunCode) { case 0x1: { //02 01 数据长度(4) 正文数据(n) ---http响应包 int count = 0; mark: var webSession = HttpServerList.SelectMany(c => c.GetAllSessions()).Where(c => c.UserId.ToLower() == packJson.UserId.ToLower()).FirstOrDefault(); if (webSession == null) { count++; Thread.Sleep(500); if (count < 5) { goto mark; } HandleLog.WriteLine($"webSession【{packJson.UserId}】不存在"); return; } //先讲16进制字符串转为byte数组 再gzip解压 var response = DataHelper.Decompress(packJson.Content); var res = webSession.TrySend(response, 0, response.Length); HandleLog.WriteLine($"{packJson.ResponseInfo} {Math.Ceiling((DateTime.Now - webSession.RequestTime).Value.TotalMilliseconds)}ms"); } break; } } else if (requestInfo.Mode == 0x3)//tcp { var packJson = JsonHelper.Instance.Deserialize <PackJson>(requestInfo.BodyRaw); switch (requestInfo.FunCode) { case 0x2: { //响应请求 int count = 0; mark: var tcpSession = TcpServerList.SelectMany(c => c.GetAllSessions()).Where(c => c.UserId.ToLower() == packJson.UserId.ToLower()).FirstOrDefault(); if (tcpSession == null) { count++; Thread.Sleep(500); if (count < 5) { goto mark; } HandleLog.WriteLine($"tcpSession【{packJson.UserId}】不存在"); return; } //先讲16进制字符串转为byte数组 再gzip解压 var response = DataHelper.Decompress(packJson.Content); tcpSession.Send(response, 0, response.Length); HandleLog.WriteLine($"----> {tcpSession.PackJson.UserId} 发送报文{response.Length}字节"); } break; case 0x3: { //响应请求 var tcpSession = TcpServerList.SelectMany(c => c.GetAllSessions()).Where(c => c.UserId.ToLower() == packJson.UserId.ToLower()).FirstOrDefault(); if (tcpSession != null) { tcpSession.Close(); HandleLog.WriteLine($"连接【{tcpSession.PackJson.UserId},{tcpSession.RemoteEndPoint}】关闭成功"); } } break; } } } catch (Exception ex) { HandleLog.WriteLine($"穿透传输连接【{session.RemoteEndPoint},{session.Client?.name}】响应请求异常:{ex}"); } }); }