/// <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); }
/// <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; }
/// <summary> /// Issues an authentication response to an authentication challenge from FreeSwitch. /// </summary> /// <param name="eventSocket">The <seealso cref="InboundSocket"/> instance to operate on.</param> /// <param name="password">The password to pass to FreeSwitch.</param> /// <returns>A Task of <seealso cref="CommandReply"/>.</returns> public static Task <CommandReply> Auth(this InboundSocket eventSocket, string password) { if (password == null) { throw new ArgumentNullException("password"); } return(eventSocket.SendCommand("auth {0}".Fmt(password))); }
/// <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); } }
/// <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); } }
/// <summary> /// The 'myevents' subscription allows your inbound socket connection to behave like an outbound socket connection. /// </summary> /// <remarks> /// It will "lock on" to the events for a particular uuid and will ignore all other events, closing the socket when /// the channel goes away or closing the channel when the socket disconnects and all applications have finished executing. /// https://freeswitch.org/confluence/display/FREESWITCH/mod_event_socket#mod_event_socket-SpecialCase-'myevents' /// Once the socket connection has locked on to the events for this particular uuid it will NEVER see any events that are /// not related to the channel, even if subsequent event commands are sent. If you need to monitor a specific channel/uuid /// and you need watch for other events as well then it is best to use a filter. /// </remarks> /// <param name="eventSocket">The <seealso cref="InboundSocket"/> instance to operate on.</param> /// <param name="uuid">The UUID of the Channel to operate on.</param> /// <returns>A Task of <seealso cref="CommandReply"/>.</returns> public static Task <CommandReply> MyEvents(this InboundSocket eventSocket, string uuid) { return(eventSocket.SendCommand("myevents {0} plain".Fmt(uuid))); }