public bool CheckPermission(string ip)
        {
            var ips = ConfigHelper.AppConfig(_ipsConfig);

            if (string.IsNullOrEmpty(ips))
            {
                return(true);
            }

            foreach (var subip in ips.Split(','))
            {
                var subip2 = subip.Split('-');
                if (subip2.Length == 1)
                {
                    if (ip.Equals(subip2[0]))
                    {
                        return(true);
                    }
                }
                else if (subip2.Length == 2)
                {
                    var iplong = IP2Long(ip);
                    if (iplong >= IP2Long(subip2[0]) && iplong <= IP2Long(subip2[1]))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Пример #2
0
        internal static void Debug(string info)
        {
            bool boo;

            if (bool.TryParse(ConfigHelper.AppConfig("Debug"), out boo) && boo)
            {
                Console.WriteLine(info);
            }
        }
Пример #3
0
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            try
            {
                var methed = url.Substring(url.LastIndexOf('/') + 1).ToLower();

                if ("json".Equals(methed))
                {
                    var urlnodes = url.Split('/');

                    APIHandler fun;
                    methed = urlnodes[urlnodes.Length - 2].ToLower();
                    if (apiFunMapper.TryGetValue(methed, out fun))
                    {
                        if (!fun.ApiMethodProp.IsVisible)
                        {
                            throw new NotSupportedException();
                        }
                        var jsonfun = new APIJsonHandler(methed, fun);
                        jsonfun._ipLimit = ConfigHelper.AppConfig(fun.ApiMethodProp.IpLimitConfig);
                        return(jsonfun);
                    }
                    else
                    {
                        throw new NotSupportedException(string.Format("找不到api方法:{0}", methed));
                    }
                }
                else
                {
                    APIHandler fun;
                    if (!apiFunMapper.TryGetValue(methed, out fun))
                    {
                        throw new NotSupportedException(string.Format("找不到api方法:{0}", methed));
                    }

                    if (!string.IsNullOrEmpty(fun.ApiMethodProp.IpLimitConfig))
                    {
                        APIPermission permission = new APIPermission(fun.ApiMethodProp.IpLimitConfig);
                        if (!permission.CheckPermission(context.Request.UserHostAddress))
                        {
                            throw new Exception(string.Format("ip[{0}]没有调用权限!", context.Request.UserHostAddress));
                        }
                    }

                    return(fun);
                }
            }
            catch (Exception ex)
            {
                return(new ErrorHandler(ex));
            }
        }
        private void App_Login(Message message, Session session)
        {
            Exception           ex      = null;
            LoginRequestMessage request = message.GetMessageBody <LoginRequestMessage>();
            //string uid = message.Get<string>(FieldEnum.LoginID);
            //string pwd = message.Get<string>(FieldEnum.LoginPwd);

            Message LoginSuccessMessage      = new Message(MessageType.LOGIN);
            LoginResponseMessage responsemsg = new LoginResponseMessage();

            string loginFailMsg = string.Empty;
            bool   canLogin     = false;

            try
            {
                canLogin = OnUserLogin(request.LoginID, request.LoginPwd, out loginFailMsg);
            }
            catch (Exception e)
            {
                ex           = e;
                loginFailMsg = "服务器出错";
            }
            if (canLogin)
            {
                responsemsg.LoginResult = true;
                session.IsLogin         = true;
                session.UserName        = request.LoginID;

                //session.Socket = s;
                //session.IPAddress = ((System.Net.IPEndPoint)s.RemoteEndPoint).Address.ToString();
                lock (appLoginSockets)
                {
                    if (appLoginSockets.ContainsKey(session.SessionID))
                    {
                        appLoginSockets.Remove(session.SessionID);
                    }
                    appLoginSockets.Add(session.SessionID, session);
                }
                Console.WriteLine("{0}成功登陆", request.LoginID);
            }
            else
            {
                responsemsg.LoginResult = false;
            }

            string heartBeatConfig = ConfigHelper.AppConfig("HeartBeat");
            //int headBeatInt = int.Parse(ConfigurationManager.AppSettings["HeartBeat"]);
            int headBeatInt;

            if (!int.TryParse(heartBeatConfig, out headBeatInt))
            {
                headBeatInt = 5000;
            }

            responsemsg.SessionID        = session.SessionID;
            responsemsg.LoginID          = request.LoginID;
            responsemsg.HeadBeatInterVal = headBeatInt;
            responsemsg.SessionTimeOut   = headBeatInt * 3;
            responsemsg.LoginFailReson   = loginFailMsg;
            LoginSuccessMessage.SetMessageBody(responsemsg);
            session.SendMessage(LoginSuccessMessage);

            if (!canLogin)
            {
                session.Close();
                Console.WriteLine("{0}登录失败", request.LoginID);
            }

            if (ex != null)
            {
                throw ex;
            }
        }