/// <summary> /// 加载信息 /// </summary> public APIResault LoadInfo() { _fwHandle = LoadAdapter(); if (_fwHandle == null) { return(ApiCommon.GetFault("没有可用的防火墙进程")); } _fwHandle.AllUser = FWUser.LoadConfig(); return(ApiCommon.GetSuccess()); }
/// <summary> /// 添加用户 /// </summary> /// <param name="user"></param> /// <returns></returns> public APIResault AddUser(FWUser user) { foreach (FWUser u in _lstUser) { if (u.UserName == user.UserName) { return(ApiCommon.GetFault("已存在用户:" + user.UserName)); } } _lstUser.Add(user); return(ApiCommon.GetSuccess()); }
/// <summary> /// 检查是否被屏蔽 /// </summary> /// <param name="remoteIP"></param> /// <returns></returns> private APIResault CheckBlockIP(string blockkey, string remoteIP, long curTick) { long bTick = _cache.GetValue <long>(blockkey); if (curTick - bTick < BlockSecond) { return(ApiCommon.GetFault(remoteIP + ",被写入黑名单")); } if (bTick > 0) { _cache.DeleteValue(blockkey); } return(ApiCommon.GetSuccess()); }
public APIResault StartServer() { if (_lisAddress == null || _lisAddress.Length <= 0) { return(ApiCommon.GetFault("请先设置监听地址")); } _server = new HttpListener(); _server.AuthenticationSchemes = AuthenticationSchemes.Anonymous; foreach (string strUrl in _lisAddress) { _server.Prefixes.Add(strUrl); } _server.Start(); _thd = new Thread(new ThreadStart(DoListen)); _thd.Start(); return(ApiCommon.GetSuccess()); }
/// <summary> /// 检查数据包ID /// </summary> /// <param name="curTick"></param> /// <param name="tick"></param> /// <returns></returns> private APIResault CheckPacket(long curTick, string name, long tick) { long left = Math.Abs(curTick - tick); if (left > PacketTimeout) { return(ApiCommon.GetFault("请求已过期")); } StringBuilder sbKey = new StringBuilder(); sbKey.Append(PackIDHead); sbKey.Append(name); sbKey.Append("."); sbKey.Append(curTick.ToString("X")); string key = sbKey.ToString(); bool isSet = _cache.SetValue <int>(key, 1, SetValueType.AddNew, 30); if (!isSet) { return(ApiCommon.GetFault("重复请求")); } return(ApiCommon.GetSuccess()); }
public APIResault UpdateAddress(string args, HttpListenerRequest request) { string remoteIP = GetIP(request); string blockkey = KeyHead + remoteIP; long curTick = (long)CommonMethods.ConvertDateTimeInt(DateTime.Now, true, true); APIResault res = CheckBlockIP(blockkey, remoteIP, curTick); if (!res.IsSuccess) { return(res); } ArgValues arg = ApiCommon.GetArgs(args); long tick = arg.GetDataValue <long>("Tick"); string name = arg.GetDataValue <string>("Name"); string sign = arg.GetDataValue <string>("Sign"); res = CheckPacket(curTick, name, tick); if (!res.IsSuccess) { return(res); } FWUser user = _userMan.GetUser(name); if (user == null) { return(ApiCommon.GetFault("找不到用户:" + name)); } string cntkey = KeyCntHead + remoteIP; string cursign = user.GetSign(tick); if (!string.Equals(cursign, sign, StringComparison.CurrentCultureIgnoreCase)) { int times = _cache.GetValue <int>(cntkey); times++; string err = null; if (times >= BlockTimes) { _cache.SetValue <long>(blockkey, curTick, SetValueType.Set, BlockSecond); _cache.DeleteValue(cntkey); err = "效验错误,IP被屏蔽:" + remoteIP; } else { _cache.SetValue <int>(cntkey, times, SetValueType.Set, BlockSecond); err = "效验错误,错误次数:" + times; } return(ApiCommon.GetFault(err)); } _cache.DeleteValue(cntkey); if (!user.UpdateIP(remoteIP)) { return(ApiCommon.GetSuccess()); } _userMan.RefreashFirewall(); _userMan.SaveConfig(); _form.OnUserUpdate(); if (_message != null && _message.ShowLog) { _message.Log("用户:" + name + ",的IP更新为:" + remoteIP); } return(ApiCommon.GetSuccess()); }