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