Exemple #1
0
        internal static bool TryAuth(HttpRequest request, IServerAuthorization authorization)
        {
            Dictionary <string, string> headers = NameValuedCollectionToDictionary(request.Headers);

            string auth;

            if (!headers.TryGetValue("Authorization", out auth))
            {
                //m_logger.Trace("[" + request.UserHostAddress + "][false] No 'Authorization' header");
                return(false);
            }
            ;

            byte[] tempConverted = Convert.FromBase64String(auth.Replace("Basic ", "").Trim());
            string userInfo      = Encoding.UTF8.GetString(tempConverted);

            string[] usernamePassword = userInfo.Split(s_authSplitHelper, StringSplitOptions.RemoveEmptyEntries);
            if (usernamePassword.Length != 2)
            {
                //m_logger.Trace("[" + request.UserHostAddress + "][false] Can not split usernamePassword");
                return(false);
            }
            ;

            string username = usernamePassword[0];
            string password = usernamePassword[1];

            if (string.IsNullOrWhiteSpace(username) ||
                string.IsNullOrWhiteSpace(password))
            {
                //m_logger.Trace("[" + request.UserHostAddress + "][false] Username or password is empty");
                return(false);
            }
            ;

            bool result = authorization.CheckAccess(username, password);

            //m_logger.Trace("[" + request.UserHostAddress + "][" + result + "] User '" + username + "' auth");
            return(result);
        }
Exemple #2
0
        /// <summary>
        /// Constructs Server object.
        /// </summary>
        /// <param name="name">Server name - appears as this in JSON schema.</param>
        /// <param name="description">Server description - appears in JSON schema.</param>
        /// <param name="group">Server group - appears in JSON schema.</param>
        /// <param name="startPort">Minimum TCP port to bind to. Use 0 for default value (40234).</param>
        /// <param name="endPort">Maximum TCP port to bind to. Use 0 for default value (41234)</param>
        /// <param name="auth">Server uses HTTP Basic auth, if auth object is provided. Use null to disable auth check.</param>
        /// <param name="schemaPushUrl">Auto-push schema to specified URL. Null to disable.</param>
        public SjmpServer(
            string name,
            string description,
            string group,
            ushort startPort          = 0,
            ushort endPort            = 0,
            IServerAuthorization auth = null,
            string schemaPushUrl      = null
            )
        {
            m_logger.Info("Starting SjmpServer");

            if (startPort == 0)
            {
                startPort = PORT_MIN;
            }
            ;

            if (endPort == 0)
            {
                endPort = PORT_MAX;
            }
            ;

            m_name          = name;
            m_description   = description;
            m_group         = group;
            m_port          = startPort;
            m_auth          = auth;
            m_schemaPushUrl = null;

            m_server = new HttpServer();
            m_server.RequestReceived += RequestReceived;

            while (true)
            {
                RefreshSchema();

                try
                {
                    m_server.EndPoint = new IPEndPoint(IPAddress.Any, m_port);
                    m_server.Start();
                    break;
                }
                catch (NHttpException e)
                {
                    if (e.InnerException is SocketException)
                    {
                        m_logger.Info("Can not bind to port " + m_port + ", trying next");
                    }
                    else
                    {
                        throw new SjsmpServerException("Server start error", e);
                    };
                };

                if (++m_port > endPort)
                {
                    throw new SjsmpServerException("Can not find free TCP port in range " + startPort + " - " + endPort + " to bind to");
                }
                ;
            }
            ;
            m_logger.Info("Listening TCP port " + m_port);

            if (schemaPushUrl != null)
            {
                try
                {
                    m_schemaPushUrl = new Uri(schemaPushUrl);
                    m_logger.Info("Starting schema push thread for url " + m_schemaPushUrl);
                    m_timer = new Timer(ShemaPushJob, null, SHEMA_PUSH_INTERVAL_MS / 10, SHEMA_PUSH_INTERVAL_MS);
                }
                catch (Exception e)
                {
                    m_logger.Error(e, "Error starting schema push");
                }
            }
        }