private static void ChangeMap(Map map, NatSession session) { try { 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; } LogHelper.Info($"Session映射{Enum.GetName(typeof(ChangeMapType), map.ChangeType)}成功:{JsonHelper.Instance.Serialize(map)}", false); LogHelper.Info($"【{map.name}】Session映射{Enum.GetName(typeof(ChangeMapType), map.ChangeType)}成功:{map.local_endpoint} --> {map.remote_endpoint}"); } catch (Exception ex) { throw ex; } }
public void ProcessData(NatSession session, NatRequestInfo requestInfo, TcpModel tcpModel) { try { switch (requestInfo.Body.Action) { case (int)TcpAction.TransferData: { //响应请求 int count = 0; mark: var tcpSession = GetSingleSession(c => c.SessionId == tcpModel.SessionId); if (tcpSession == null) { count++; Thread.Sleep(500); if (count < 5) { goto mark; } LogHelper.Warning($"tcpSession【{tcpModel.SessionId}】不存在"); return; } //先讲16进制字符串转为byte数组 再gzip解压 var response = DataHelper.Decompress(tcpModel.Content); tcpSession.Send(response); LogHelper.Info($"{tcpSession.Map.name} {tcpSession.Map.protocol} {tcpSession.Map.remote_endpoint} --> {tcpSession.Map.local_endpoint} 发送报文{response.Length}字节"); } break; case (int)TcpAction.Close: { //关闭请求 var tcpSession = GetSingleSession(c => c.SessionId == tcpModel.SessionId); if (tcpSession != null) { tcpSession.Close(); LogHelper.Info($"连接【{tcpSession.SessionId},{tcpSession.RemoteEndPoint}】关闭成功"); } } break; } } catch (Exception ex) { LogHelper.Error($"TcpServer穿透处理异常,{ex}"); } }
private static void Closed(NatSession session) { LogHelper.Info($"内网客户端【{session.RemoteEndPoint}】已下线"); if (session.Client != null) { Task.Run(() => { //更新在线状态 var bll = new ClientBll(); var updateRst = bll.UpdateOnlineStatus(new Client() { secret = session.Client.secret, is_online = false }); LogHelper.Info($"更新主机【{session.Client.name}】离线状态结果:{updateRst.Message}"); }); } }
private static void Received(NatSession session, NatRequestInfo requestInfo) { Task.Run(() => { try { switch (requestInfo.Body.Type) { case (byte)JsonType.NAT: { LogHelper.Info($"NAT收到数据:{requestInfo.Raw.ToHexWithSpace()},正文内容: {requestInfo.Body.ToJson()}", false); NATServer.ProcessData(session, requestInfo); break; } case (byte)JsonType.HTTP: { var httpModel = requestInfo.Body.Data.FromJson <HttpModel>(); var server = HttpServerList.Find(c => c.ServerId == httpModel.ServerId); server?.ProcessData(session, requestInfo, httpModel); break; } case (byte)JsonType.TCP: { var tcpModel = requestInfo.Body.Data.FromJson <TcpModel>(); var server = TcpServerList.Find(c => c.ServerId == tcpModel.ServerId); server?.ProcessData(session, requestInfo, tcpModel); break; } } } catch (Exception ex) { LogHelper.Error($"穿透传输连接【{session.RemoteEndPoint},{session.Client?.name}】响应请求异常:{ex}"); } }); }
private static void Connected(NatSession session) { LogHelper.Info($"内网客户端【{session.RemoteEndPoint}】已连接"); }
public void ProcessData(NatSession session, NatRequestInfo requestInfo, HttpModel httpModel) { try { switch (requestInfo.Body.Action) { case (int)HttpAction.Response: { var context = GetSingleSession(c => c.SessionId == httpModel.SessionId); if (context == null) { LogHelper.Error($"未找到上下文context,SessionId={httpModel.SessionId}"); return; } HttpResponse httpResponse = new HttpResponse() { HttpVersion = httpModel.HttpVersion, Headers = httpModel.Headers, Status = httpModel.StatusCode, StatusMessage = httpModel.StatusMessage }; if (httpModel.Content?.Length > 0) { //解压 var byteData = DataHelper.Decompress(httpModel.Content); httpResponse.ContentType = httpModel.ContentType; httpResponse.Body = byteData; } //把处理信息返回到客户端 context.Send(httpResponse.Write()); var response_time = DateTime.Now; var timeSpan = (response_time - httpModel.RequestTime); var totalSize = (httpResponse.Body?.Length ?? 0) * 1.00 / 1024; var map = session.MapList.Find(c => c.remote_endpoint == httpModel.Host); LogHelper.Info($"{session.Client.user_name} {session.Client.name} {map?.name} {httpModel.Method} {httpModel.Path} {httpModel.StatusCode} {httpModel.StatusMessage} {Math.Round(totalSize, 1)}KB {timeSpan.TotalMilliseconds}ms"); var request = new Request { request_url = $"{map.protocol}://{map.remote_endpoint}{httpModel.Path}", request_method = httpModel.Method, client_ip = session.RemoteEndPoint.ToString(), user_id = session.Client.user_id, request_time = httpModel.RequestTime, response_time = response_time, handle_time = Convert.ToInt64(timeSpan.TotalMilliseconds), //ms create_time = DateTime.Now, requet_content = context.RequestInfo.Body == null ? null : Encoding.UTF8.GetString(context.RequestInfo.Body), response_content = httpResponse.Body.Length <= 1024 ? Encoding.UTF8.GetString(httpResponse.Body) : "", status_code = httpModel.StatusCode, status_message = httpModel.StatusMessage, total_size = httpResponse.Body?.Length ?? 0, speed = Math.Round(totalSize / (timeSpan.TotalMilliseconds / 1000), 2),//KB/s map_id = map.id }; lock (RequestQueue) RequestQueue.Enqueue(request); } break; } } catch (Exception ex) { LogHelper.Error($"HttpsServer ProcessData穿透处理异常,{ex}"); } }