1 | //#define USE_MSG_BINDING
|
---|
2 | using System;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.ServiceModel.Channels;
|
---|
7 | using System.ServiceModel;
|
---|
8 | using System.Security.Cryptography.X509Certificates;
|
---|
9 | using System.Net;
|
---|
10 | using System.ServiceModel.Description;
|
---|
11 | using HeuristicLab.Tracing;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Contracts {
|
---|
14 | public static class WcfSettings {
|
---|
15 | public const string SlaveServiceName = "SlaveService";
|
---|
16 | public const string ServerConsoleServiceName = "ServerConsoleService";
|
---|
17 | public const string ClientServiceName = "ClientService";
|
---|
18 | public const string ClientStreamedServiceName = "ClientServiceStreamed";
|
---|
19 |
|
---|
20 | public const int DefaultPort = 9000;
|
---|
21 |
|
---|
22 | /// <summary>
|
---|
23 | /// Gets a pre-defined binding using TCP for secure transport.
|
---|
24 | /// </summary>
|
---|
25 | /// <returns>A binding type of <see cref="NetTcpBinding"/></returns>
|
---|
26 | public static Binding GetBinding() {
|
---|
27 | #if USE_MSG_BINDING
|
---|
28 | NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
|
---|
29 | #else
|
---|
30 | NetTcpBinding binding = new NetTcpBinding();
|
---|
31 | #endif
|
---|
32 | binding.MaxBufferSize = int.MaxValue;
|
---|
33 | binding.MaxReceivedMessageSize = int.MaxValue;
|
---|
34 | binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
---|
35 | binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
---|
36 | binding.CloseTimeout = new TimeSpan(0, 5, 0);
|
---|
37 | binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
|
---|
38 | binding.SendTimeout = new TimeSpan(0, 5, 0);
|
---|
39 | return binding;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public static Binding GetStreamedBinding() {
|
---|
43 | #if USE_MSG_BINDING
|
---|
44 | NetTcpBinding binding = new NetTcpBinding(SecurityMode.Message);
|
---|
45 | #else
|
---|
46 | NetTcpBinding binding = new NetTcpBinding();
|
---|
47 | #endif
|
---|
48 | binding.TransferMode = TransferMode.Streamed;
|
---|
49 | binding.MaxReceivedMessageSize = int.MaxValue;
|
---|
50 | binding.ReaderQuotas.MaxArrayLength = int.MaxValue;
|
---|
51 | binding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
|
---|
52 | binding.CloseTimeout = new TimeSpan(0, 5, 0);
|
---|
53 | binding.ReceiveTimeout = new TimeSpan(0, 5, 0);
|
---|
54 | binding.SendTimeout = new TimeSpan(0, 5, 0);
|
---|
55 | //Disabling security mode, for the moment
|
---|
56 | binding.Security.Mode = SecurityMode.None;
|
---|
57 |
|
---|
58 | return binding;
|
---|
59 | }
|
---|
60 |
|
---|
61 | // /// <summary>
|
---|
62 | // /// Defines the used certificate for authentification located in a certification store.
|
---|
63 | // /// </summary>
|
---|
64 | // /// <param name="svchost">A service for which this certificate is applicable.</param>
|
---|
65 | // public static void SetServiceCertificate(ServiceHost svchost) {
|
---|
66 | //#if USE_MSG_BINDING
|
---|
67 | // svchost.Credentials.ServiceCertificate.SetCertificate(
|
---|
68 | // StoreLocation.LocalMachine,
|
---|
69 | // StoreName.My,
|
---|
70 | // X509FindType.FindBySubjectName,
|
---|
71 | // SERVERCERT);
|
---|
72 | //#endif
|
---|
73 | // }
|
---|
74 |
|
---|
75 | /// <summary>
|
---|
76 | /// Gets the currently active IP address.
|
---|
77 | /// <remarks>If more than one IP connections is active, the first one will be used.</remarks>
|
---|
78 | /// </summary>
|
---|
79 | /// <returns></returns>
|
---|
80 | public static IPAddress GetActiveIP() {
|
---|
81 | return System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties().GetActiveTcpConnections()[0].LocalEndPoint.Address;
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Gets the default port used for HIVE services.
|
---|
86 | /// </summary>
|
---|
87 | /// <returns></returns>
|
---|
88 | public static int GetDefaultPort() {
|
---|
89 | return DefaultPort;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /// <summary>
|
---|
93 | /// This method changes the endpoint-address while preserving the identity-certificate defined in the config file
|
---|
94 | /// </summary>
|
---|
95 | public static void SetEndpointAddress(ServiceEndpoint endpoint, string hostAddress) {
|
---|
96 | EndpointAddressBuilder builder = new EndpointAddressBuilder(endpoint.Address);
|
---|
97 | UriBuilder uriBuilder = new UriBuilder(builder.Uri);
|
---|
98 | uriBuilder.Host = hostAddress;
|
---|
99 | builder.Uri = uriBuilder.Uri;
|
---|
100 | endpoint.Address = builder.ToEndpointAddress();
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// Securely disposes a WCF client proxy object
|
---|
105 | /// </summary>
|
---|
106 | /// <param name="obj"></param>
|
---|
107 | public static void DisposeWcfClient(ICommunicationObject obj) {
|
---|
108 | if (obj != null) {
|
---|
109 | if (obj.State != CommunicationState.Faulted &&
|
---|
110 | obj.State != CommunicationState.Closed) {
|
---|
111 | try { obj.Close(); }
|
---|
112 | catch (CommunicationObjectFaultedException) { obj.Abort(); }
|
---|
113 | catch (TimeoutException) { obj.Abort(); }
|
---|
114 | catch (Exception e) {
|
---|
115 | obj.Abort();
|
---|
116 | Logger.Error(e);
|
---|
117 | }
|
---|
118 | } else
|
---|
119 | obj.Abort();
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | }
|
---|
124 |
|
---|
125 | /// <summary>
|
---|
126 | /// This class verifies the certificate defined by <see cref="SetServerCertificate"></see> method. Normally,
|
---|
127 | /// the verification process is managed by the underlying operating system.
|
---|
128 | /// </summary>
|
---|
129 | /// <remarks>
|
---|
130 | /// WARNUNG: Dieser Code wird nur für Testzertifikate benötigt, wie sie beispielsweise von makecert erstellt werden.
|
---|
131 | /// Sie sollten diesen Code nicht in einer Produktionsumgebung verwenden.
|
---|
132 | /// </remarks>
|
---|
133 | /*
|
---|
134 | public class PermissiveCertificatePolicy {
|
---|
135 | string subjectName;
|
---|
136 | static PermissiveCertificatePolicy currentPolicy;
|
---|
137 | PermissiveCertificatePolicy(string subjectName) {
|
---|
138 | this.subjectName = subjectName;
|
---|
139 | ServicePointManager.ServerCertificateValidationCallback +=
|
---|
140 | new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
|
---|
141 | }
|
---|
142 |
|
---|
143 | public static void Enact(string subjectName) {
|
---|
144 | currentPolicy = new PermissiveCertificatePolicy(subjectName);
|
---|
145 | }
|
---|
146 |
|
---|
147 | bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) {
|
---|
148 | if (cert.Subject == subjectName) {
|
---|
149 | return true;
|
---|
150 | }
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 | } */
|
---|
154 | }
|
---|