예제 #1
0
        /// <summary>
        /// 执行函数
        /// </summary>
        private APIResault DoMethods(HttpListenerRequest request)
        {
            ServiceRequest req = new ServiceRequest();

            req.Load(request);
            string arg    = req.GetArgString();
            string method = req.MethodName;
            string url    = request.Url.AbsolutePath;

            if (string.IsNullOrWhiteSpace(method))
            {
                return(ApiCommon.GetFault("函数MethodName不能为空"));
            }
            try
            {
                APIResault con = RunMethod(url, method, arg, request);
                if (_message != null && _message.ShowLog)
                {
                    string mess = con.Message;

                    _message.Log(mess);
                }
                return(con);
            }
            catch (Exception ex)
            {
                if (OnException != null)
                {
                    OnException(ex);
                }
                return(ApiCommon.GetException(ex));
            }
        }
예제 #2
0
        /// <summary>
        /// 运行函数
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public APIResault RunMethod(string url, string methodName, string args, HttpListenerRequest request)
        {
            INetHandle handle = null;

            if (!_dicUrlMap.TryGetValue(url.Trim(' ', '\\', '/'), out handle))
            {
                return(ApiCommon.GetFault("找不到位置:" + url));
            }
            return(handle.InvokeMethod(methodName, args, request));
        }
예제 #3
0
        public APIResault InvokeMethod(string methodName, string arg, HttpListenerRequest request)
        {
            Buffalo.Kernel.FastReflection.FastInvokeHandler han = _handle.GetMethod(methodName);
            if (han == null)
            {
                return(ApiCommon.GetFault("找不到函数:" + methodName));
            }

            APIResault con = han.Invoke(this, new object[] { arg, request }) as APIResault;

            return(con);
        }
예제 #4
0
        /// <summary>
        /// 加载信息
        /// </summary>
        public APIResault LoadInfo()
        {
            _fwHandle = LoadAdapter();

            if (_fwHandle == null)
            {
                return(ApiCommon.GetFault("没有可用的防火墙进程"));
            }
            _fwHandle.AllUser = FWUser.LoadConfig();

            return(ApiCommon.GetSuccess());
        }
예제 #5
0
        /// <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());
        }
예제 #6
0
        /// <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());
        }
예제 #7
0
        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());
        }
예제 #8
0
        /// <summary>
        /// 执行函数
        /// </summary>
        private APIResault DoMethods(HttpListenerRequest request)
        {
            ServiceRequest req = new ServiceRequest();

            req.Load(request);
            string arg    = req.GetArgString();
            string method = req.MethodName;
            string url    = request.Url.AbsolutePath;
            string ip     = LanUnit.GetHostAddress(request);


            if (_lanUnit != null && _lanUnit.IsAllowIP(ip))
            {
                return(ApiCommon.GetException(new System.Net.WebException("调用IP:" + ip + "不在白名单内")));
            }

            if (string.IsNullOrWhiteSpace(method))
            {
                return(ApiCommon.GetFault("函数MethodName不能为空"));
            }
            try
            {
                APIResault con = RunMethod(url, method, arg, request);
                if (_message != null && _message.ShowLog)
                {
                    string mess = con.Message;

                    _message.Log(mess);
                }
                return(con);
            }
            catch (Exception ex)
            {
                if (OnException != null)
                {
                    OnException(ex);
                }
                return(ApiCommon.GetException(ex));
            }
        }
예제 #9
0
        /// <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());
        }
예제 #10
0
        private void DoRequest(object objhttpListenerContext)
        {
            try
            {
                HttpListenerContext context = objhttpListenerContext as HttpListenerContext;
                if (context == null)
                {
                    return;
                }
                HttpListenerRequest request = context.Request;
                APIResault          res     = DoMethods(request);

                //取得响应对象
                HttpListenerResponse response = context.Response;
                string responseBody           = null;
                if (res == null)
                {
                    res = ApiCommon.GetFault("请求错误");
                }
                responseBody = res.ToJson();
                //设置响应头部内容,长度及编码
                response.ContentLength64 = System.Text.Encoding.UTF8.GetByteCount(responseBody);
                response.ContentType     = "application/json; Charset=UTF-8";
                using (StreamWriter sw = new StreamWriter(response.OutputStream))
                {
                    sw.Write(responseBody);
                }
            }
            catch (Exception ex)
            {
                if (OnException != null)
                {
                    OnException(ex);
                }
            }
        }
예제 #11
0
        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());
        }