Пример #1
0
        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}");
                }
            });
        }
Пример #2
0
        private static void NATServer_NewRequestReceived(NatAppSession session, NatRequestInfo requestInfo)
        {
            try
            {
                //HandleLog.WriteLine($"NAT服务收到数据:{requestInfo.Hex}");
                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 checkSession = NATServer.GetSessions(c => c.Client?.secret == secret).FirstOrDefault();
                    if (checkSession != null)
                    {
                        session.SendMsg($"密钥{secret}已被主机:{checkSession.Client.name},{checkSession.RemoteEndPoint}使用!");
                        return;
                    }
                    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;

                case 0x3:
                {
                    //响应请求
                    var packJson = JsonHelper.Instance.Deserialize <PackJson>(requestInfo.BodyRaw);
                    int count    = 0;
mark:
                    var webSession = HttpServer.GetSessions(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 rawResponse = Encoding.UTF8.GetString(response);
                    var res         = webSession.TrySend(response, 0, response.Length);
                    HandleLog.WriteLine($"{packJson.ResponseInfo} {Math.Ceiling((DateTime.Now - webSession.RequestTime).Value.TotalMilliseconds)}ms");
                    //webSession?.Close();
                }
                break;
                }
            }
            catch (Exception ex)
            {
                HandleLog.WriteLine($"webSession响应请求异常:{ex}");
            }
        }