예제 #1
0
        private static void Logout()
        {
            try
            {
                SDKEnvironment.Logout(new Uri(ServerName));
            }
            catch (Exception ex)
            {
                Program.Log.Error(null, ex);
            }

            try
            {
                if (scs != null)
                {
                    scs.Abort();
                    scs.Dispose();
                    scs = null;
                }

                SDKEnvironment.RemoveAllServers();
            }
            catch (Exception ex)
            {
                Program.Log.Error(null, ex);
            }
        }
        /// <summary>
        /// Constructor for the BasicConnection, which performs and sets some start-up routines
        /// </summary>
        /// <param name="isCServer">If the server is a C server</param>
        /// <param name="username">Username to use</param>
        /// <param name="password">Password to use</param>
        /// <param name="hostname">The hostname</param>
        /// <param name="port">Which port to use</param>
        public BasicConnection(bool isCServer, String username, String password, String hostname, int port)
        {
            //Precondition
            if (hostname.StartsWith("http://"))
            {
                hostname = hostname.Substring("http://".Length);
            }

            IsCServer  = isCServer;
            _serverUrl = hostname;
            _username  = username;
            _password  = password;
            _port      = port;

            if (IsCServer)
            {
                // SSL
                _uri = new Uri($"https://{_serverUrl}:{_port}/ManagementServer/ServerCommandService.svc");

                // Create Soap class from interface
                CServer = new ServerCommandServiceClient(GetBinding(),
                                                         new EndpointAddress(_uri, EndpointIdentity.CreateSpnIdentity(SpnFactory.GetSpn(_uri))));

                // Set basic credentials
                CServer.ClientCredentials.UserName.UserName = username;
                CServer.ClientCredentials.UserName.Password = password;
                // TODO Any certificate is accepted as OK !!
                CServer.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
                {
                    CertificateValidationMode = X509CertificateValidationMode.None,
                };

                // Open service (internally)
                CServer.Open();

                // Give OS a few milliseconds to get ready
                long tickStop = DateTime.Now.Ticks + 1000000; // 1.000.000 ticks == 100 ms
                while (DateTime.Now.Ticks < tickStop && CServer.State == CommunicationState.Opening)
                {
                    Thread.Sleep(5);
                }
            }
            else
            {
                _eServer = new ServerCommandService($"http://{_serverUrl}:{_port}/ServerCommandService/servercommandservice.asmx");

                String uri = $"http://{_serverUrl}";
                _uri = new Uri(uri);


                CredentialCache   credCache   = new CredentialCache();
                NetworkCredential credentials = new NetworkCredential(_username, _password);
                credCache.Add(new Uri(uri), "Basic", credentials);
                _eServer.Credentials = credCache;
            }
        }
        public void StatServerCommandTest()
        {
            var service = new ServerCommandService();
            var command = new StatServerCommand();

            service.RegisterCommand(command);
            var actor = new ServiceActorTest(service);

            TestLauncherActor.Test(() => actor.SendMessage(command.Key, new string[] { }));
        }
예제 #4
0
        private static void PrepareToLogin(string serverName, string userName, string password)
        {
            ServerName        = serverName;
            milestoneUserName = userName;
            milestonePassword = password;

            if (string.IsNullOrEmpty(ServerName))
            {
                string msg = "Server address cannot be emnpty";
                Program.Log.Warn(msg);
                Console.WriteLine(msg);
                return;
            }

            Uri               uri    = null;
            CredentialCache   cc     = null;
            NetworkCredential nc     = null;
            bool              logged = false;

            if (!string.IsNullOrEmpty(milestoneUserName))
            {
                // You need this to apply Enterprise "basic" credentials.
                uri = new UriBuilder(ServerName).Uri;
                cc  = Util.BuildCredentialCache(uri, milestoneUserName, milestonePassword, "Negotiate");//"Basic" : "Negotiate"
                SDKEnvironment.AddServer(uri, cc);
                // use the same credentiel as when logge on to the Management Server
                logged = Login(uri);
            }

            if (!logged)
            {
                try
                {
                    uri = new UriBuilder(ServerName).Uri;
                }
                catch (Exception ex)
                {
                    Program.Log.Error(ServerName, ex);
                    Program.Log.Error(ServerName, ex.InnerException);
                    return;
                }

                // This will reuse the Windows credentials you are logged in with
                nc = CredentialCache.DefaultNetworkCredentials;
                SDKEnvironment.AddServer(uri, nc);

                //Pick up the login settings from the management server to be used when logging into the Recording Server.
                loginSettings = LoginSettingsCache.GetLoginSettings(ServerName);

                logged = Login(uri);
            }

            if (!logged)
            {
                uri = new Uri($"http://{Configuration.Instance.ServerFQID.ServerId.ServerHostname}:{Configuration.Instance.ServerFQID.ServerId.Serverport}/");
                SDKEnvironment.AddServer(uri, nc);
                logged = Login(uri);
            }

            if (!logged && loginSettings != null)
            {
                SDKEnvironment.AddServer(uri, nc);
                logged = Login(loginSettings.Uri);
            }

            if (!logged)
            {
                Program.Log.Error("Doesn't logged in.");
                return;
            }

            try
            {
                if (!SDKEnvironment.IsServerConnected(uri))
                {
                    string msg = $"Failed to connect to {uri}";
                    Program.Log.Warn(msg);
                    return;
                }

                scs          = new ServerCommandService();
                scs.Timeout *= 3;                                       // The default is 100,000 milliseconds.

                // use the same credentiel as when logge on to the Management Server
                if (cc != null)
                {
                    scs.Credentials = cc;
                }
                else if (nc != null)
                {
                    scs.Credentials = nc;
                }

                connectedToMilestone = true;
                List <Item> items = Configuration.Instance.GetItems();
                EnumerateElementChildren(items, AllCameras);
                ProductCode = EnvironmentManager.Instance.SystemLicense.ProductCode;
                ServerId    = Configuration.Instance.ServerFQID.ServerId.Id;
            }
            catch (Exception ex)
            {
                Program.Log.Error($"Internal error connecting to: {uri.DnsSafeHost}", ex);
            }

            if (loginSettings != null)
            {
                Token = loginSettings.Token;
            }

            if (connectedToMilestone)
            {
                try
                {
                    string           path             = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
                    MilestoneVersion milestoneVersion = new MilestoneVersion();
                    milestoneVersion.Connection = connectedToMilestone;

                    File.WriteAllText($"{path}\\Milestone.json", JsonHelper.FromClass(milestoneVersion));
                }
                catch (Exception ex)
                {
                    Program.Log.Error(null, ex);
                }
                scs.Url = $"{ServerName}ServerAPI/ServerCommandService.asmx";
                string msg = $"Connected to Milestone server: {ServerName}";

                MilestoneInfo();

                Console.WriteLine(msg);
                Program.Log.Info(msg);
            }
            else
            {
                Console.WriteLine($"Failed to connect to {ServerName}");
            }
        }
        /// <summary>
        /// Constructor of the NtlmConnection class. Will perform and set the needed start-up routines
        /// </summary>
        /// <param name="domain">The domain (may be empty)</param>
        /// <param name="authType">Authentication type</param>
        /// <param name="username">The username</param>
        /// <param name="password">The password related to the username</param>
        /// <param name="isCCode">If the server is a C server</param>
        /// <param name="hostname">The hostname</param>
        /// <param name="port">The used port</param>
        public NtlmConnection(String domain, AuthenticationType authType, String username, String password,
                              bool isCCode, String hostname, int port = 80)
        {
            //Precondition
            if (hostname.StartsWith("http://"))
            {
                hostname = hostname.Substring("http://".Length);
            }

            _serverUrl = hostname;
            _port      = port;
            _authType  = authType;
            _username  = username;
            _password  = password;
            _domain    = domain;
            IsCCode    = isCCode;

            String url;
            String prefix = "http";

            if (_port == 443) //Note: E servers doesn't support SSL
            {
                prefix += "s";
            }


            if (IsCCode)
            {
                url = $"{prefix}://{hostname}:{_port}/ManagementServer/ServerCommandService.svc";
                WSHttpBinding binding = new WSHttpBinding()
                {
                    MaxReceivedMessageSize = 1000000
                };
                EndpointAddress remoteAddress = new EndpointAddress(url);

                CServer = new ServerCommandServiceClient(binding, remoteAddress);
                CServer.ClientCredentials.Windows.ClientCredential.UserName = username;
                CServer.ClientCredentials.Windows.ClientCredential.Password = password;
                if (!String.IsNullOrEmpty(_domain))
                {
                    CServer.ClientCredentials.Windows.ClientCredential.Domain = _domain;
                }

                // TODO Any certificate is accepted as OK !!
                CServer.ClientCredentials.ServiceCertificate.SslCertificateAuthentication = new X509ServiceCertificateAuthentication()
                {
                    CertificateValidationMode = X509CertificateValidationMode.None,
                };
            }
            else
            {
                url = $"{prefix}://{hostname}";

                _eServer = new ServerCommandService($"http://{_serverUrl}/ServerCommandService/servercommandservice.asmx");

                CredentialCache   credCache = new CredentialCache();
                NetworkCredential credentials;
                if (String.IsNullOrEmpty(_domain))
                {
                    credentials = new NetworkCredential(_username, _password);
                }
                else
                {
                    credentials = new NetworkCredential(_username, _password, _domain);
                }

                credCache.Add(new Uri(url), "NTLM", credentials);
                _eServer.Credentials = credCache;
            }
        }
 public ServiceActorTest(ServerCommandService service) : base()
 {
     Become(service);
 }