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