/// <summary> /// 通过系统账户名查找系统账号全部信息 /// </summary> /// <param name="id">系统账号名</param> /// <returns>系统账号信息</returns> public ACCOUNTSYS FindAccountSYSByName(string name) { ACCOUNTSYS accountSYS = new ACCOUNTSYS(); using (var ctx = new NtripForwardDB()) { accountSYS = ctx.ACCOUNTSYS.FirstOrDefault(a => a.AccountSYS_Name == name && a.isDelete == false); } return(accountSYS); }
/// <summary> /// 通过ID号查找系统账号全部信息 /// </summary> /// <param name="id">系统账号ID号</param> /// <returns>系统账号信息</returns> public ACCOUNTSYS FindAccountSYSByID(Guid id) { ACCOUNTSYS accountSYS = new ACCOUNTSYS(); using (var ctx = new NtripProxyDB()) { accountSYS = ctx.ACCOUNTSYS.FirstOrDefault(a => a.ID == id && a.isDelete == false); } return(accountSYS); }
/// <summary> /// 添加单个系统账号 /// </summary> /// <param name="accountSYS">账号信息</param> /// <returns>是否添加成功</returns> public bool AddAccountSYS(ACCOUNTSYS accountSYS) { bool result = false; using (var ctx = new NtripProxyDB()) { ctx.ACCOUNTSYS.Add(accountSYS); result = ctx.SaveChanges() == 1; } return(result); }
/// <summary> /// 更新系统账号资料 /// </summary> /// <param name="accountSYS">需要更新的系统账号</param> /// <returns>是否更新成功</returns> public bool UpdateAccountSYS(ACCOUNTSYS accountSYS) { bool result = false; using (var ctx = new NtripForwardDB()) { if (accountSYS.ID != null) { ctx.ACCOUNTSYS.Attach(accountSYS); ctx.Entry(accountSYS).State = EntityState.Modified; result = ctx.SaveChanges() >= 1; } } return(result); }
/// <summary> /// 转换WebApi千寻账号账号实体为dal层千寻账号,只包括基础信息 /// </summary> /// <returns>dal层千寻账号</returns> public ACCOUNTSYS ToACCOUNTSYS() { ACCOUNTSYS accountSYS = new ACCOUNTSYS() { ID = ID, AccountSYS_Name = Name, AccountSYS_Password = Password, AccountSYS_Register = Register, AccountSYS_Expire = Expire, AccountSYS_LastLogin = LastLogin, AccountSYS_Age = Age, AccountSYS_IsOnline = IsOnline, AccountSYS_IsLocked = IsLocked, isDelete = IsDelete, createTime = CreateTime }; return(accountSYS); }
/// <summary> /// 模型ACCOUNTSYS类扩展方法,转换基础信息到AccountEntity /// </summary> /// <param name="accountSYS">DAL层系统账号</param> /// <returns>WebApi层系统账号实体</returns> public static AccountSYSEntity ToAccountSYSEntity(this ACCOUNTSYS accountSYS) { AccountSYSEntity accountSYSEntity = new AccountSYSEntity { ID = accountSYS.ID, Name = accountSYS.AccountSYS_Name, Password = accountSYS.AccountSYS_Password, Register = accountSYS.AccountSYS_Register, Expire = accountSYS.AccountSYS_Expire, LastLogin = accountSYS.AccountSYS_LastLogin, Age = (int)accountSYS.AccountSYS_Age, IsOnline = (bool)accountSYS.AccountSYS_IsOnline, IsLocked = (bool)accountSYS.AccountSYS_IsLocked, IsDelete = (bool)accountSYS.isDelete, CreateTime = (DateTime)accountSYS.createTime }; return(accountSYSEntity); }
public IHttpActionResult AddAccountSYS(AccountSYSEntity accountSYS) { bool isAddSuccess = false; ResultEntity result = new ResultEntity(); try { ACCOUNTSYS temp = accountSYS.ToACCOUNTSYS(); isAddSuccess = dal.AddAccountSYS(temp); result.Data = accountSYS; } catch (Exception e) { result.Message = e.Message; NtripProxyLogger.LogExceptionIntoFile("调用接口api/AccountSYS/AddAccountSYS异常,异常信息为:" + e.Message); } result.IsSuccess = isAddSuccess; return(Json <ResultEntity>(result)); }
public IHttpActionResult GetAccountSYSByID(string id) { AccountSYSEntity accountEntity = new AccountSYSEntity(); ResultEntity result = new ResultEntity(); try { ACCOUNTSYS temp = dal.FindAccountSYSByID(new Guid(id)); accountEntity = temp.ToAccountSYSEntity(); } catch (Exception e) { result.Message = e.Message; NtripProxyLogger.LogExceptionIntoFile("调用接口api/AccountSYS/GetAccountSYSByID/{id}异常,异常信息为:" + e.Message); } result.IsSuccess = result.Message == null; result.Data = accountEntity; return(Json <ResultEntity>(result)); }
/// <summary> /// 账号拨号以后查找适合使用的系统账号 /// </summary> /// <returns>返回的系统账号系统</returns> public ACCOUNTSYS FindSuitableAccountSYS() { ACCOUNTSYS accountSYS = new ACCOUNTSYS(); using (var ctx = new NtripForwardDB()) { List <ACCOUNTSYS> temp = ctx.ACCOUNTSYS.Where <ACCOUNTSYS>( a => a.isDelete == false && a.AccountSYS_IsOnline == false && a.AccountSYS_IsLocked == false && a.AccountSYS_Expire > DateTime.Now) .ToList <ACCOUNTSYS>(); int age = int.MaxValue; foreach (var item in temp) { if (item.AccountSYS_Age < age) { age = (int)item.AccountSYS_Age; accountSYS = item; } } } return(accountSYS); }
private void acceptCallback(IAsyncResult ar) { TcpListener lstn = (TcpListener)ar.AsyncState; TcpClient client = lstn.EndAcceptTcpClient(ar); NetworkStream stream = client.GetStream(); Task.Run(() => { TcpClient qianxunClient = new TcpClient(); qianxunClient.Connect(IPAddress.Parse(qianxunIP), ROMOTE_PORT); //中继缓存数据 string bufferMsg = ""; //主机地址 string host = client.Client.RemoteEndPoint.ToString(); //拨号的账户 ACCOUNT account = new ACCOUNT(); //使用的千寻账户 ACCOUNTSYS accountSYS = new ACCOUNTSYS(); //会话状态,并初始化 SessionHistory session = new SessionHistory(); session.ID = Guid.NewGuid(); session.GGACount = 0; session.FixedCount = 0; //GGA记录频率 int GGARecordRate = settings.GGARecordRate; //GGA初始计数 int GGACounter = 1; //是否继续维持连接 bool isLoop = true; //设置定时器,作为gga发送的中继 System.Timers.Timer timerRepeat = new System.Timers.Timer(); timerRepeat.Interval = settings.GGARepeatRate; timerRepeat.AutoReset = true; timerRepeat.Elapsed += delegate { if (MsgHelper.CheckRequestType(bufferMsg) == 3) { byte[] sendMsg = Connect(qianxunClient, bufferMsg); //将千寻返回数据转发给客户端 stream.Write(sendMsg, 0, sendMsg.Length); } }; timerRepeat.Start(); //设置定时器,如果长时间不发送数据,则关掉tcp连接 System.Timers.Timer timerTimeout = new System.Timers.Timer(); timerTimeout.Interval = settings.Timeout; timerTimeout.AutoReset = false; timerTimeout.Elapsed += delegate { //记录session session.ConnectionEnd = DateTime.Now; if (session.AccountType != null) { sessionDAL.UpdateSessionHistory(session); } //更新在线状态 if (session.AccountType == "Normal") { account.Account_IsOnline = false; accountSYS.AccountSYS_IsOnline = false; accountDAL.UpdateAccount(account); accountSYSDAL.UpdateAccountSYS(accountSYS); } //关闭计时器 timerTimeout.Stop(); timerRepeat.Stop(); //关闭所有连接 stream.Close(); client.Close(); qianxunClient.Close(); //跳出循环 isLoop = false; }; timerTimeout.Start(); #region 步读取 while (isLoop) { //解决cpu100%占用问题 Thread.Sleep(1); try { if (stream.DataAvailable) { //计时器重置 timerTimeout.Stop(); timerTimeout.Start(); //用来存储网络字节流数据 byte[] buffer = new byte[1024]; stream.Read(buffer, 0, buffer.Length); string recMsg = Encoding.UTF8.GetString(buffer); byte[] sendMsg = new byte[1024]; switch (MsgHelper.CheckRequestType(recMsg)) { //浏览器请求 case 1: sendMsg = Connect(qianxunClient, recMsg); break; //手部首次请求 case 2: string[] namePassword = MsgHelper.GetAuthorization(recMsg); account = accountDAL.FindAccountByName(namePassword[0]); //第三方账号 if (account == null) { session.AccountType = "Third"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = string.Join(":", MsgHelper.GetAuthorization(recMsg)); sessionDAL.AddSessionHistory(session); recMsg = recMsg + Environment.NewLine; sendMsg = Connect(qianxunClient, recMsg); } else { if (account.Account_Password == namePassword[1]) { //账户已过期 if (account.Account_Expire < DateTime.Now) { session.AccountType = "Expire"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已过期"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户已在线 if ((bool)account.Account_IsOnline) { session.AccountType = "Online"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已在线"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户被锁定 if ((bool)account.Account_IsLocked) { session.AccountType = "Locked"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "账户已锁定"; sessionDAL.AddSessionHistory(session); //sendMsg = Connect(qianxunClient, recMsg); } else { //账户正常 session.AccountType = "Normal"; //更新账户状态 account.Account_IsOnline = true; account.Account_LastLogin = DateTime.Now; accountDAL.UpdateAccount(account); //找到可替换的系统账号并更新状态 accountSYS = accountSYSDAL.FindSuitableAccountSYS(); accountSYS.AccountSYS_IsOnline = true; accountSYS.AccountSYS_LastLogin = DateTime.Now; accountSYS.AccountSYS_Age++; accountSYSDAL.UpdateAccountSYS(accountSYS); //更新session状态 session.AccountName = account.Account_Name; session.AccountSYSName = accountSYS.AccountSYS_Name; session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ClientAddress = host; sessionDAL.AddSessionHistory(session); //替换信息 string replaceMessage = MsgHelper.ReplaceAuthorization(recMsg, accountSYS.AccountSYS_Name, accountSYS.AccountSYS_Password) + Environment.NewLine; sendMsg = Connect(qianxunClient, replaceMessage); } } } } else { //用户名密码错误 session.AccountType = "PasswordError"; session.AccountName = namePassword[0]; session.MountPoint = MsgHelper.GetMountPoint(recMsg); session.ConnectionStart = DateTime.Now; session.Client = MsgHelper.GetUserAgent(recMsg); session.ClientAddress = host; session.ErrorInfo = "用户名密码错误"; account.Account_PasswordOvertime = DateTime.Now; account.Account_PasswordOvercount++; sessionDAL.AddSessionHistory(session); sendMsg = Connect(qianxunClient, recMsg); } } break; //GGA数据采集 case 3: //发送的GGA数据递增 session.GGACount++; //是否固定解 if (MsgHelper.FixModule(recMsg) == 4) { session.FixedCount++; } //更新session信息 sessionDAL.UpdateSessionHistory(session); //中继缓冲gga信息 bufferMsg = recMsg; if ( //是否记录GGA数据 settings.EnableLogGGA //到达指定评率记录GGA数据 && GGACounter % GGARecordRate == 0 //第三方账户不记录GGA数据 && account != null ) { GGAHistory gga = new GGAHistory(); gga.ID = Guid.NewGuid(); gga.Account = account.Account_Name; gga.AccountSYS = accountSYS.AccountSYS_Name; gga.AccountType = "Normal"; gga.FixedTime = DateTime.Now; gga.Lng = Convert.ToDecimal(MsgHelper.GetLng(recMsg)); gga.Lat = Convert.ToDecimal(MsgHelper.GetLat(recMsg)); gga.Status = MsgHelper.FixModule(recMsg); gga.GGAInfo = recMsg; gga.SessionID = session.ID; ggaDAL.AddGGAHistory(gga); } GGACounter++; sendMsg = Connect(qianxunClient, recMsg); break; default: break; } //将千寻返回数据转发给客户端 stream.Write(sendMsg, 0, sendMsg.Length); } } catch (Exception e) { //更新在线状态 if (session.AccountType == "Normal") { account.Account_IsOnline = false; accountSYS.AccountSYS_IsOnline = false; accountDAL.UpdateAccount(account); accountSYSDAL.UpdateAccountSYS(accountSYS); } //结束会话 session.ConnectionEnd = DateTime.Now; //session.ErrorInfo = e.Message; sessionDAL.UpdateSessionHistory(session); //关闭所有连接 stream.Close(); client.Close(); qianxunClient.Close(); timerTimeout.Stop(); timerRepeat.Stop(); break; } } #endregion }); lstn.BeginAcceptTcpClient(new AsyncCallback(acceptCallback), lstn); }