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