/// <summary> /// Adds a specified client sink provider into the client sink chain after the formatter. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <param name="clientSinkProvider">Client sink provider to be added</param> /// <returns>Protocol setup</returns> public static IClientProtocolSetup AddClientSinkAfterFormatter(this IClientProtocolSetup protocolSetup, IClientChannelSinkProvider clientSinkProvider) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } if (clientSinkProvider == null) { throw new ArgumentNullException("clientSinkProvider"); } IClientFormatterSinkProvider formatter = GetClientFormatter(protocolSetup); if (formatter == null) { throw new ApplicationException(LanguageResource.ApplicationException_NoFormatterSpecified); } int index = protocolSetup.ClientSinkChain.IndexOf((IClientChannelSinkProvider)formatter); protocolSetup.ClientSinkChain.Insert(index + 1, clientSinkProvider); return(protocolSetup); }
/// <summary> /// Adds a specified server sink provider into the server sink chain before the formatter. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <param name="serverSinkProvider">Server sink provider to be added</param> /// <returns>Protocol setup</returns> public static IClientProtocolSetup AddServerSinkBeforeFormatter(this IClientProtocolSetup protocolSetup, IServerChannelSinkProvider serverSinkProvider) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } if (serverSinkProvider == null) { throw new ArgumentNullException("serverSinkProvider"); } IServerFormatterSinkProvider formatter = GetServerFormatter(protocolSetup); if (formatter == null) { throw new ApplicationException(LanguageResource.ApplicationException_NoFormatterSpecified); } int index = protocolSetup.ServerSinkChain.IndexOf((IServerChannelSinkProvider)formatter); protocolSetup.ServerSinkChain.Insert(index, serverSinkProvider); return(protocolSetup); }
/// <summary> /// Adds a single channel setting. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <param name="name">Name of channel setting (example: "port")</param> /// <param name="value">Value of channel setting (example: 8080)</param> /// <returns></returns> public static IClientProtocolSetup AddChannelSetting(this IClientProtocolSetup protocolSetup, string name, object value) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } protocolSetup.ChannelSettings.Add(name, value); return(protocolSetup); }
/// <summary> /// Returns the configured formatter from the server sink chain of a specified client protocol setup. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <returns>server formatter sink</returns> public static IServerFormatterSinkProvider GetServerFormatter(this IClientProtocolSetup protocolSetup) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } return((from sink in protocolSetup.ServerSinkChain where sink is IServerFormatterSinkProvider select sink as IServerFormatterSinkProvider).FirstOrDefault()); }
/// <summary> /// Adds a specified server sink provider into the server sink chain. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <param name="serverSinkProvider">Server sink provider to be added</param> /// <returns>Protocol setup</returns> public static IClientProtocolSetup AddServerSink(this IClientProtocolSetup protocolSetup, IServerChannelSinkProvider serverSinkProvider) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } if (serverSinkProvider == null) { throw new ArgumentNullException("serverSinkProvider"); } protocolSetup.ServerSinkChain.Add(serverSinkProvider); return(protocolSetup); }
/// <summary> /// Adds a specified client sink provider into the client sink chain. /// </summary> /// <param name="protocolSetup">Protocol setup</param> /// <param name="clientSinkProvider">Client sink provider to be added</param> /// <returns>Protocol setup</returns> public static IClientProtocolSetup AddClientSink(this IClientProtocolSetup protocolSetup, IClientChannelSinkProvider clientSinkProvider) { if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } if (clientSinkProvider == null) { throw new ArgumentNullException("clientSinkProvider"); } protocolSetup.ClientSinkChain.Add(clientSinkProvider); return(protocolSetup); }
private void TestVulnerability(string hostName, string serverUrl, IServerProtocolSetup serverSetup, IClientProtocolSetup clientSetup, object payload) { ZyanSettings.DisableUrlRandomization = true; using (var zyanHost = new ZyanComponentHost(hostName, serverSetup)) { zyanHost.RegisterComponent <ISampleServer, SampleServer>(ActivationType.Singleton); var credentials = new Hashtable { { "Login", "hacker" }, { "Password", "secret" }, { string.Empty, payload } }; using (var zyanConnection = new ZyanConnection(serverUrl, clientSetup)) { var proxy = zyanConnection.CreateProxy <ISampleServer>(); AssertEx.Throws <SecurityException>(() => proxy.TestMethod(payload)); } // should throw AssertEx.Throws <SecurityException>(() => { using (new ZyanConnection(serverUrl, clientSetup, credentials, true, true)) { } }); } }
/// <summary> /// Releases unmanaged and — optionally — managed resources. /// </summary> /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (!disposing) { return; } if (!_isDisposed) { _isDisposed = true; if (_pollingTimer != null) { _pollingTimer.Dispose(); _pollingTimer = null; } _pollingEnabled = false; if (_keepSessionAliveTimer != null) { _keepSessionAliveTimer.Dispose(); _keepSessionAliveTimer = null; } try { foreach (var zyanProxy in AliveProxies) { zyanProxy.RemoveAllRemoteEventHandlers(); } RemoteDispatcher.Logoff(_sessionID); } catch (RemotingException) { } catch (SocketException) { } catch (WebException) { } catch (MessageException) { } catch (Exception ex) { Trace.WriteLine("Unexpected exception of type {0} caught while disposing ZyanConnection: {1}", ex.GetType(), ex.Message); } finally { lock (_connections) { _connections.Remove(this); } } if (_remotingChannel != null) { // unsubscribe from connection notifications var connectionNotification = _remotingChannel as IConnectionNotification; if (connectionNotification != null) { connectionNotification.ConnectionEstablished -= Channel_ConnectionEstablished; } // unregister remoting channel var registeredChannel = ChannelServices.GetChannel(_remotingChannel.ChannelName); if (registeredChannel != null && registeredChannel == _remotingChannel) { ChannelServices.UnregisterChannel(_remotingChannel); } // dispose remoting channel, if it's disposable var disposableChannel = _remotingChannel as IDisposable; if (disposableChannel != null) { disposableChannel.Dispose(); } _remotingChannel = null; } _remoteDispatcher = null; _serverUrl = string.Empty; _sessionID = Guid.Empty; _protocolSetup = null; _serializationHandling = null; _componentHostName = string.Empty; if (_registeredComponents != null) { _registeredComponents.Clear(); _registeredComponents = null; } if (_callInterceptors != null) { _callInterceptors.Clear(); _callInterceptors = null; } if (_autoLoginCredentials != null) { _autoLoginCredentials = null; } GC.WaitForPendingFinalizers(); } }
/// <summary> /// Creates a new instance of the ZyanConnection class. /// </summary> /// <param name="serverUrl">URL of remote server (e.G. "tcp://server1:46123/myapp").</param> /// <param name="protocolSetup">Protocol and communication settings.</param> /// <param name="credentials">Login credentials.</param> /// <param name="autoLoginOnExpiredSession">Specifies whether the proxy should relogin automatically when the session expired.</param> /// <param name="keepSessionAlive">Specifies whether the session should be automaticly kept alive.</param> public ZyanConnection(string serverUrl, IClientProtocolSetup protocolSetup, AuthCredentials credentials, bool autoLoginOnExpiredSession, bool keepSessionAlive) { if (string.IsNullOrEmpty(serverUrl)) { throw new ArgumentException(LanguageResource.ArgumentException_ServerUrlMissing, "serverUrl"); } if (protocolSetup == null) { // try to select the protocol automatically protocolSetup = ClientProtocolSetup.GetClientProtocol(serverUrl); if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } } if (!protocolSetup.IsUrlValid(serverUrl)) { throw new ArgumentException(LanguageResource.ArgumentException_ServerUrlIsInvalid, "serverUrl"); } _proxies = new List <WeakReference>(); _protocolSetup = protocolSetup; _sessionID = Guid.NewGuid(); _serverUrl = serverUrl; _autoLoginOnExpiredSession = autoLoginOnExpiredSession; _keepSessionAlive = keepSessionAlive; if (_autoLoginOnExpiredSession) { _autoLoginCredentials = credentials; } _serializationHandling = new SerializationHandlerRepository(); CallInterceptionEnabled = false; _callInterceptors = new CallInterceptorCollection(); RegisterStandardSerializationHandlers(); string[] addressParts = _serverUrl.Split('/'); _componentHostName = addressParts[addressParts.Length - 1]; _remotingChannel = _protocolSetup.CreateChannel(); if (!ZyanSettings.DisableUrlRandomization) { _remotingChannel = ChannelWrapper.WrapChannel(_remotingChannel, _protocolSetup.ChannelName); } if (_remotingChannel != null) { var registeredChannel = ChannelServices.GetChannel(_remotingChannel.ChannelName); if (registeredChannel == null) { ChannelServices.RegisterChannel(_remotingChannel, false); } else { _remotingChannel = registeredChannel; } } else { throw new ApplicationException(LanguageResource.ApplicationException_NoChannelCreated); } var connectionNotification = _remotingChannel as IConnectionNotification; if (connectionNotification != null) { connectionNotification.ConnectionEstablished += Channel_ConnectionEstablished; } string channelName = _remotingChannel.ChannelName; if (credentials == null) { credentials = new AuthCredentials(); } try { credentials.Authenticate(_sessionID, RemoteDispatcher); _registeredComponents = new List <ComponentInfo>(RemoteDispatcher.GetRegisteredComponents()); _sessionAgeLimit = RemoteDispatcher.SessionAgeLimit; } catch (Exception ex) { // unregister remoting channel var registeredChannel = ChannelServices.GetChannel(channelName); if (registeredChannel != null) { ChannelServices.UnregisterChannel(registeredChannel); } // dispose channel if it's disposable var disposableChannel = registeredChannel as IDisposable; if (disposableChannel != null) { disposableChannel.Dispose(); } _remotingChannel = null; _remoteDispatcher = null; throw ex.PreserveStackTrace(); } var reconnectEvents = new Action(ReconnectRemoteEventsCore); var debounceInterval = ZyanSettings.ReconnectRemoteEventsDebounceInterval.TotalMilliseconds; ReconnectRemoteEvents = reconnectEvents.Debounce((int)debounceInterval); StartKeepSessionAliveTimer(); lock (_connections) { _connections.Add(this); } }
/// <summary> /// Creates a new instance of the ZyanConnection class. /// </summary> /// <param name="serverUrl">URL of remote server (e.G. "tcp://server1:46123/myapp")</param> /// <param name="protocolSetup">Protocol an communication settings</param> /// <param name="autoLoginOnExpiredSession">Specifies whether the proxy should relogin automatically when the session expired</param> public ZyanConnection(string serverUrl, IClientProtocolSetup protocolSetup, bool autoLoginOnExpiredSession) : this(serverUrl, protocolSetup, null, autoLoginOnExpiredSession, true) { }
/// <summary> /// Creates a new instance of the ZyanConnection class. /// </summary> /// <param name="serverUrl">URL of remote server (e.G. "tcp://server1:46123/myapp")</param> /// <param name="protocolSetup">Protocol an communication settings</param> public ZyanConnection(string serverUrl, IClientProtocolSetup protocolSetup) : this(serverUrl, protocolSetup, null, false, true) { }
/// <summary> /// Release managed resources. /// </summary> public void Dispose() { if (!_isDisposed) { _isDisposed = true; if (_pollingTimer != null) { _pollingTimer.Dispose(); _pollingTimer = null; } _pollingEnabled = false; if (_keepSessionAliveTimer != null) { _keepSessionAliveTimer.Dispose(); _keepSessionAliveTimer = null; } try { if (_proxies != null) { foreach (var proxyReference in _proxies) { if (proxyReference.IsAlive) { var proxy = proxyReference.Target as ZyanProxy; proxy.RemoveAllRemoteEventHandlers(); } } } //RemoteDispatcher.Logoff(_sessionID); _transportAdapter.SendRequest(new RequestMessage() { RequestType = RequestType.SystemOperation, MethodName = "Logoff", Address = _serverUrl, ParameterValues = new object[] { _sessionID } }); } //TODO: Get rid of .NET Remoting dependency. //catch (RemotingException) //{ } catch (SocketException) { } catch (WebException) { } catch (MessageException) { } catch (Exception ex) { Trace.WriteLine("Unexpected exception of type {0} caught while disposing ZyanConnection: {1}", ex.GetType(), ex.Message); } finally { _connections.Remove(this); } if (_transportAdapter != null) { // unregister remoting channel var registeredChannel = ClientTransportAdapterManager.Instance.GetTransportAdapter(_transportAdapter.UniqueName); if (registeredChannel != null && registeredChannel == _transportAdapter) { ClientTransportAdapterManager.Instance.Unregister(_transportAdapter); } // dispose remoting channel, if it's disposable var disposableChannel = _transportAdapter as IDisposable; if (disposableChannel != null) { disposableChannel.Dispose(); } _transportAdapter = null; } _serverUrl = string.Empty; _sessionID = Guid.Empty; _protocolSetup = null; _serializationHandling = null; _componentHostName = string.Empty; if (_registeredComponents != null) { _registeredComponents.Clear(); _registeredComponents = null; } if (_callInterceptors != null) { _callInterceptors.Clear(); _callInterceptors = null; } if (_autoLoginCredentials != null) { _autoLoginCredentials.Clear(); _autoLoginCredentials = null; } GC.WaitForPendingFinalizers(); } }
/// <summary> /// Creates a new instance of the ZyanConnection class. /// </summary> /// <param name="serverUrl">URL of remote server (e.G. "tcp://server1:46123/myapp").</param> /// <param name="protocolSetup">Protocol and communication settings.</param> /// <param name="credentials">Login credentials.</param> /// <param name="autoLoginOnExpiredSession">Specifies whether the proxy should relogin automatically when the session expired.</param> /// <param name="keepSessionAlive">Specifies whether the session should be automaticly kept alive.</param> public ZyanConnection(string serverUrl, IClientProtocolSetup protocolSetup, Hashtable credentials, bool autoLoginOnExpiredSession, bool keepSessionAlive) { if (string.IsNullOrEmpty(serverUrl)) { throw new ArgumentException(LanguageResource.ArgumentException_ServerUrlMissing, "serverUrl"); } if (protocolSetup == null) { // try to select the protocol automatically protocolSetup = ClientProtocolSetup.GetClientProtocol(serverUrl); if (protocolSetup == null) { throw new ArgumentNullException("protocolSetup"); } } _proxies = new List <WeakReference>(); _protocolSetup = protocolSetup; _sessionID = Guid.NewGuid(); _serverUrl = serverUrl; _autoLoginOnExpiredSession = autoLoginOnExpiredSession; _keepSessionAlive = keepSessionAlive; if (_autoLoginOnExpiredSession) { _autoLoginCredentials = credentials; } _serializationHandling = new SerializationHandlerRepository(); CallInterceptionEnabled = false; _callInterceptors = new CallInterceptorCollection(); RegisterStandardSerializationHandlers(); string[] addressParts = _serverUrl.Split('/'); _componentHostName = addressParts[addressParts.Length - 1]; _transportAdapter = _protocolSetup.CreateTransportAdapter(); //TODO: Implement URL randomization without .NET Remoting dependency //if (AllowUrlRandomization) //{ // _transportChannel = ChannelWrapper.WrapChannel(_transportChannel); //} if (_transportAdapter != null) { var registeredChannel = ClientTransportAdapterManager.Instance.GetTransportAdapter(_transportAdapter.UniqueName); if (registeredChannel == null) { ClientTransportAdapterManager.Instance.Register(_transportAdapter); } } else { throw new ApplicationException(LanguageResource.ApplicationException_NoChannelCreated); } string channelName = _transportAdapter.UniqueName; if (credentials != null && credentials.Count == 0) { credentials = null; } //try //{ //TODO: Generate logon request message and send it through transport channel //RemoteDispatcher.Logon(_sessionID, credentials); _sessionAgeLimit = SendLogonMessage(_sessionID, credentials); //TODO: Request registered components through transport channel //_registeredComponents = new List<ComponentInfo>(RemoteDispatcher.GetRegisteredComponents()); _registeredComponents = SendGetRegisteredComponentsMessage(); //TODO: Extract session age limit from logon response message. //_sessionAgeLimit = RemoteDispatcher.SessionAgeLimit; //} //catch (Exception ex) //{ // // unregister remoting channel // var registeredChannel = ClientTransportAdapterManager.Instance.GetTransportAdapter(channelName); // if (registeredChannel != null) // ClientTransportAdapterManager.Instance.Unregister(registeredChannel); // // dispose channel if it's disposable // var disposableChannel = registeredChannel as IDisposable; // if (disposableChannel != null) // disposableChannel.Dispose(); // throw ex.PreserveStackTrace(); //} StartKeepSessionAliveTimer(); _connections.Add(this); }
/// <summary> /// Konstruktor. /// </summary> /// <param name="serverUrl">Server-URL (z.B. "tcp://server1:46123/ebcserver")</param> /// <param name="protocolSetup">Protokoll-Einstellungen</param> /// <param name="credentials">Anmeldeinformationen</param> /// <param name="autoLoginOnExpiredSession">Gibt an, ob sich der Proxy automatisch neu anmelden soll, wenn die Sitzung abgelaufen ist</param> /// <param name="keepSessionAlive">Gib an, ob die Sitzung automatisch verlängert werden soll</param> public ZyanConnection(string serverUrl, IClientProtocolSetup protocolSetup, Hashtable credentials, bool autoLoginOnExpiredSession, bool keepSessionAlive) { // Wenn kein Server-URL angegeben wurde ... if (string.IsNullOrEmpty(serverUrl)) { // Ausnahme werfen throw new ArgumentException(LanguageResource.ArgumentException_ServerUrlMissing, "serverUrl"); } // Wenn keine Protokoll-Einstellungen angegeben wurde ... if (protocolSetup == null) { // Ausnahme werfen throw new ArgumentNullException("protocolSetup"); } // Protokoll-Einstellungen übernehmen _protocolSetup = protocolSetup; // Eindeutigen Sitzungsschlüssel generieren _sessionID = Guid.NewGuid(); // Server-URL übernehmen _serverUrl = serverUrl; // Einstellung für automatisches Anmelden bei abgelaufener Sitzung übernehmen _autoLoginOnExpiredSession = autoLoginOnExpiredSession; // Einstellung für automatische Sitzungsverlängung übernehmen _keepSessionAlive = keepSessionAlive; // Wenn automatisches Anmelden aktiv ist ... if (_autoLoginOnExpiredSession) { // Anmeldedaten speichern _autoLoginCredentials = credentials; } // Server-URL in Bestandteile zerlegen string[] addressParts = _serverUrl.Split('/'); // Name des Komponentenhots speichern _componentHostName = addressParts[addressParts.Length - 1]; // TCP-Kommunikationskanal öffnen IChannel channel = (IChannel)_protocolSetup.CreateChannel(); // Wenn der Kanal erzeugt wurde ... if (channel != null) { // Kanal registrieren ChannelServices.RegisterChannel(channel, false); } // Wörterbuch für Benachrichtigungs-Registrierungen erzeugen _subscriptions = new Dictionary <Guid, NotificationReceiver>(); // Wenn leere Anmeldeinformationen angegben wurden ... if (credentials != null && credentials.Count == 0) { // Auflistung löschen credentials = null; } // Am Server anmelden RemoteComponentFactory.Logon(_sessionID, credentials); // Registrierte Komponenten vom Server abrufen _registeredComponents = new List <ComponentInfo>(RemoteComponentFactory.GetRegisteredComponents()); // Sitzungslimit abrufen _sessionAgeLimit = RemoteComponentFactory.SessionAgeLimit; // Zeitgeber starten (Wenn die automatische Sitzungsverlängerung aktiv ist) StartKeepSessionAliveTimer(); // Verbindung der Auflistung zufügen _connections.Add(this); }