Example #1
0
        /// <summary>
        /// Connects to FreeSwitch and authenticates
        /// </summary>
        /// <param name="host">(Default: localhost) The hostname or ip to connect to.</param>
        /// <param name="port">(Default: 8021) The Tcp port to connect to.</param>
        /// <param name="password">(Default: ClueCon) The password to authenticate with.</param>
        /// <param name="timeout">(Optional) The auth request timeout.</param>
        /// <returns>A task of <see cref="InboundSocket"/>.</returns>
        public static async Task <InboundSocket> Connect(
            string host = "localhost", int port = 8021, string password = "******", TimeSpan?timeout = null)
        {
            var socket = new InboundSocket(host, port, timeout);

            await
            socket.Messages.Where(x => x.ContentType == ContentTypes.AuthRequest)
            .Take(1)
            .Timeout(
                socket.ResponseTimeOut,
                Observable.Throw <BasicMessage>(
                    new TimeoutException(
                        "No Auth Request received within the specified timeout of {0}.".Fmt(socket.ResponseTimeOut))))
            .Do(_ => Log.Trace(() => "Received Auth Request"), ex => Log.ErrorException("Error waiting for AuthRequest.", ex))
            .ToTask();

            var result = await socket.Auth(password);

            if (!result.Success)
            {
                Log.Error("InboundSocket authentication failed ({0}).".Fmt(result.ErrorMessage));
                throw new SecurityException("Invalid password");
            }

            Log.Trace(() => "InboundSocket authentication succeeded.");

            return(socket);
        }
Example #2
0
        /// <summary>
        /// Connects to FreeSwitch and authenticates
        /// </summary>
        /// <param name="host">(Default: localhost) The hostname or ip to connect to.</param>
        /// <param name="port">(Default: 8021) The Tcp port to connect to.</param>
        /// <param name="password">(Default: ClueCon) The password to authenticate with.</param>
        /// <param name="timeout">(Optional) The auth request timeout.</param>
        /// <returns>A task of <see cref="InboundSocket"/>.</returns>
        public static async Task<InboundSocket> Connect(
            string host = "localhost", int port = 8021, string password = "******", TimeSpan? timeout = null)
        {
            var socket = new InboundSocket(host, port, timeout);

            await
                socket.Messages.Where(x => x.ContentType == ContentTypes.AuthRequest)
                      .Take(1)
                      .Timeout(
                          socket.ResponseTimeOut,
                          Observable.Throw<BasicMessage>(
                              new TimeoutException(
                                  "No Auth Request received within the specified timeout of {0}.".Fmt(socket.ResponseTimeOut))))
                      .Do(_ => Log.Trace(() => "Received Auth Request"), ex => Log.ErrorException("Error waiting for AuthRequest.", ex))
                      .ToTask()
                      .ConfigureAwait(false);

            var result = await socket.Auth(password).ConfigureAwait(false);

            if (!result.Success)
            {
                Log.Error("InboundSocket authentication failed ({0}).".Fmt(result.ErrorMessage));
                throw new SecurityException("Invalid password");
            }

            Log.Trace(() => "InboundSocket authentication succeeded.");

            return socket;
        }
Example #3
0
        /// <summary>
        /// Connects to FreeSwitch and authenticates
        /// </summary>
        /// <param name="host">(Default: localhost) The hostname or ip to connect to.</param>
        /// <param name="port">(Default: 8021) The Tcp port to connect to.</param>
        /// <param name="password">(Default: ClueCon) The password to authenticate with.</param>
        /// <param name="timeout">(Optional) The auth request timeout.</param>
        /// <returns>A task of <see cref="InboundSocket"/>.</returns>
        /// <exception cref="InboundSocketConnectionFailedException"></exception>
        public static async Task <InboundSocket> Connect(
            string host = "localhost", int port = 8021, string password = "******", TimeSpan?timeout = null)
        {
            try
            {
                var socket = new InboundSocket(host, port, timeout);

                var firstMessage =
                    await socket.Messages.Where(
                        x => x.ContentType == ContentTypes.AuthRequest ||
                        x.ContentType == ContentTypes.RudeRejection)
                    .Take(1)
                    .Timeout(
                        socket.ResponseTimeOut,
                        Observable.Throw <BasicMessage>(
                            new TimeoutException(
                                "No Auth Request received within the specified timeout of {0}.".Fmt(socket.ResponseTimeOut))))
                    .Do(_ => Log.LogDebug("Received Auth Request"), ex => Log.LogError(ex, "Error waiting for AuthRequest."))
                    .ToTask()
                    .ConfigureAwait(false);

                if (firstMessage.ContentType == ContentTypes.RudeRejection)
                {
                    Log.LogError("InboundSocket connection rejected ({0}).".Fmt(firstMessage.BodyText));
                    throw new InboundSocketConnectionFailedException("Connection Rejected - '{0}'. Check the acl in eventsocket.conf".Fmt(firstMessage.BodyText));
                }

                var result = await socket.Auth(password).ConfigureAwait(false);

                if (!result.Success)
                {
                    Log.LogError("InboundSocket authentication failed ({0}).".Fmt(result.ErrorMessage));
                    throw new InboundSocketConnectionFailedException("Invalid password when trying to connect to {0}:{1}".Fmt(host, port));
                }

                Log.LogDebug("InboundSocket authentication succeeded.");

                return(socket);
            }
            catch (SocketException ex)
            {
                throw new InboundSocketConnectionFailedException("Socket error when trying to connect to {0}:{1}".Fmt(host, port), ex);
            }
            catch (IOException ex)
            {
                throw new InboundSocketConnectionFailedException("IO error when trying to connect to {0}:{1}".Fmt(host, port), ex);
            }
            catch (TimeoutException ex)
            {
                throw new InboundSocketConnectionFailedException("Timeout when trying to connect to {0}:{1}.{2}".Fmt(host, port, ex.Message), ex);
            }
        }
Example #4
0
        /// <summary>
        /// Connects to FreeSwitch and authenticates
        /// </summary>
        /// <param name="host">(Default: localhost) The hostname or ip to connect to.</param>
        /// <param name="port">(Default: 8021) The Tcp port to connect to.</param>
        /// <param name="password">(Default: ClueCon) The password to authenticate with.</param>
        /// <param name="timeout">(Optional) The auth request timeout.</param>
        /// <returns>A task of <see cref="InboundSocket"/>.</returns>
        /// <exception cref="InboundSocketConnectionFailedException"></exception>
        public static async Task <InboundSocket> Connect(
            string host = "localhost", int port = 8021, string password = "******", TimeSpan?timeout = null)
        {
            try
            {
                var socket = new InboundSocket(host, port, timeout);

                await
                socket.Messages.FirstOrDefaultAsync(x => x.ContentType == ContentTypes.AuthRequest)
                .Timeout(
                    socket.ResponseTimeOut,
                    Observable.Throw <BasicMessage>(
                        new TimeoutException(
                            "No Auth Request received within the specified timeout of {0}.".Fmt(socket.ResponseTimeOut))))
                .Do(_ => Log.Trace(() => "Received Auth Request"), ex => Log.ErrorException("Error waiting for AuthRequest.", ex))
                .ToTask()
                .ConfigureAwait(false);

                var result = await socket.Auth(password).ConfigureAwait(false);

                if (!result.Success)
                {
                    Log.Error("InboundSocket authentication failed ({0}).".Fmt(result.ErrorMessage));
                    throw new InboundSocketConnectionFailedException("Invalid password when trying to connect to {0}:{1}".Fmt(host, port));
                }

                Log.Trace(() => "InboundSocket authentication succeeded.");

                return(socket);
            }
            catch (SocketException ex)
            {
                throw new InboundSocketConnectionFailedException("Socket error when trying to connect to {0}:{1}".Fmt(host, port), ex);
            }
            catch (IOException ex)
            {
                throw new InboundSocketConnectionFailedException("IO error when trying to connect to {0}:{1}".Fmt(host, port), ex);
            }
            catch (TimeoutException ex)
            {
                throw new InboundSocketConnectionFailedException("Timeout when trying to connect to {0}:{1}.{2}".Fmt(host, port, ex.Message), ex);
            }
        }