diff --git a/TGCommandLine/AdminCommands.cs b/TGCommandLine/AdminCommands.cs index e9336b509e..90c50f0b6e 100644 --- a/TGCommandLine/AdminCommands.cs +++ b/TGCommandLine/AdminCommands.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using TGServiceInterface; using TGServiceInterface.Components; @@ -14,7 +13,7 @@ namespace TGCommandLine } public override string GetHelpText() { - return "Manage server service authentication"; + return "Manage instance authentication"; } } diff --git a/TGCommandLine/Program.cs b/TGCommandLine/Program.cs index 494cbd8bd6..d3f46d17cd 100644 --- a/TGCommandLine/Program.cs +++ b/TGCommandLine/Program.cs @@ -64,21 +64,21 @@ namespace TGCommandLine return Command.ExitCode.BadCommand; } - var res = currentInterface.VerifyConnection(); - if (res != null) + var res = currentInterface.ConnectionStatus(out string error); + if (!res.HasFlag(ConnectivityLevel.Connected)) { - Console.WriteLine("Unable to connect to service: " + res); + Console.WriteLine("Unable to connect to service: " + error); Console.WriteLine("Remote connection usage: <-c/--connect> username:password@address:port"); return Command.ExitCode.ConnectionError; } - if (!currentInterface.Authenticate()) + if (!res.HasFlag(ConnectivityLevel.Authenticated)) { Console.WriteLine("Authentication error: Username/password/windows identity is not authorized!"); return Command.ExitCode.ConnectionError; } - if (!SentVMMWarning && currentInterface.VersionMismatch(out string error)) + if (!SentVMMWarning && currentInterface.VersionMismatch(out error)) { SentVMMWarning = true; Console.WriteLine(error); @@ -190,13 +190,13 @@ namespace TGCommandLine Console.Write("Enter password: "); var password = ReadLineSecure(); ReplaceInterface(new Interface(address, port, username, password)); - var res = currentInterface.VerifyConnection(); - if (res != null) + var res = currentInterface.ConnectionStatus(out string error); + if (!res.HasFlag(ConnectivityLevel.Connected)) { - Console.WriteLine("Unable to connect: " + res); + Console.WriteLine("Unable to connect: " + error); ReplaceInterface(new Interface()); } - else if (!currentInterface.Authenticate()) + else if (!res.HasFlag(ConnectivityLevel.Authenticated)) { Console.WriteLine("Authentication error: Username/password/windows identity is not authorized! Returning to local mode..."); ReplaceInterface(new Interface()); @@ -204,10 +204,10 @@ namespace TGCommandLine else { Console.WriteLine("Connected remotely"); - if (currentInterface.VersionMismatch(out res)) + if (currentInterface.VersionMismatch(out error)) { SentVMMWarning = true; - Console.WriteLine(res); + Console.WriteLine(error); } Console.WriteLine("Type 'disconnect' to return to local mode"); } diff --git a/TGCommandLine/RootCommands.cs b/TGCommandLine/RootCommands.cs index 37c6cb00db..3f35d95518 100644 --- a/TGCommandLine/RootCommands.cs +++ b/TGCommandLine/RootCommands.cs @@ -10,7 +10,7 @@ namespace TGCommandLine public CLICommand(Interface I) { var tmp = new List { new UpdateCommand(), new TestmergeCommand(), new RepoCommand(), new BYONDCommand(), new DMCommand(), new DDCommand(), new ConfigCommand(), new IRCCommand(), new DiscordCommand(), new AutoUpdateCommand(), new SetAutoUpdateCommand() }; - if (I.VerifyConnection() == null && I.Authenticate() && I.AuthenticateAdmin()) + if (I.ConnectToInstance().HasFlag(ConnectivityLevel.Administrator)) tmp.Add(new AdminCommand()); Children = tmp.ToArray(); } diff --git a/TGControlPanel/ControlPanel.cs b/TGControlPanel/ControlPanel.cs index e4ce2968b6..4e37861ab7 100644 --- a/TGControlPanel/ControlPanel.cs +++ b/TGControlPanel/ControlPanel.cs @@ -66,7 +66,7 @@ namespace TGControlPanel bool CheckAdminWithWarning() { - if (!Interface.AuthenticateAdmin()) + if (!Interface.ConnectToInstance().HasFlag(ConnectivityLevel.Administrator)) { MessageBox.Show("Only system administrators may use this command!"); return false; diff --git a/TGControlPanel/Login.cs b/TGControlPanel/Login.cs index e75498e0d0..2199840999 100644 --- a/TGControlPanel/Login.cs +++ b/TGControlPanel/Login.cs @@ -62,13 +62,13 @@ namespace TGControlPanel void VerifyAndConnect(Interface I) { - var res = I.VerifyConnection(); - if (res != null) + var res = I.ConnectionStatus(out string error); + if (!res.HasFlag(ConnectivityLevel.Connected)) { - MessageBox.Show("Unable to connect to service! Error: " + res); + MessageBox.Show("Unable to connect to service! Error: " + error); return; } - if (!I.Authenticate()) + if (!res.HasFlag(ConnectivityLevel.Authenticated)) { MessageBox.Show("Authentication error: Username/password/windows identity is not authorized! Ensure you are a system administrator or in the correct Windows group on the service machine."); return; diff --git a/TGControlPanel/StaticPage.cs b/TGControlPanel/StaticPage.cs index 61a3ca7155..1f17132820 100644 --- a/TGControlPanel/StaticPage.cs +++ b/TGControlPanel/StaticPage.cs @@ -23,7 +23,7 @@ namespace TGControlPanel void InitStaticPage() { - if(!Interface.AuthenticateAdmin()) + if(!Interface.ConnectToInstance().HasFlag(ConnectivityLevel.Administrator)) RecreateStaticButton.Visible = false; BuildFileList(); } diff --git a/TGServiceInterface/DreamDaemonBridge.cs b/TGServiceInterface/DreamDaemonBridge.cs index 77de577130..1b09192bb2 100644 --- a/TGServiceInterface/DreamDaemonBridge.cs +++ b/TGServiceInterface/DreamDaemonBridge.cs @@ -26,7 +26,7 @@ namespace TGServiceInterface parsedArgs.AddRange(args); parsedArgs.RemoveAt(0); using (var I = new Interface()) - if(I.ConnectToInstanceImpl(parsedArgs[0], true).HasFlag(InstanceConnectivity.Connected)) + if(I.ConnectToInstanceImpl(parsedArgs[0], true).HasFlag(ConnectivityLevel.Connected)) I.GetComponent().InteropMessage(String.Join(" ", parsedArgs)); } catch { } diff --git a/TGServiceInterface/Enumerations.cs b/TGServiceInterface/Enumerations.cs index 5ce44bc951..e342014b8b 100644 --- a/TGServiceInterface/Enumerations.cs +++ b/TGServiceInterface/Enumerations.cs @@ -3,10 +3,10 @@ namespace TGServiceInterface { /// - /// Description of the connectivity level to an + /// Description of the connectivity level to an or the /// [Flags] - public enum InstanceConnectivity + public enum ConnectivityLevel { /// /// The connection could not be made, either a communication error occurred or the specified does not exist diff --git a/TGServiceInterface/Interface.cs b/TGServiceInterface/Interface.cs index 976fd567ca..bda7c09afd 100644 --- a/TGServiceInterface/Interface.cs +++ b/TGServiceInterface/Interface.cs @@ -116,26 +116,30 @@ namespace TGServiceInterface /// Targets as the instance to use with . Closes all connections to any previous instance /// /// The name of the instance to connect to - /// The apporopriate value - public InstanceConnectivity ConnectToInstance(string instanceName) + /// The apporopriate + public ConnectivityLevel ConnectToInstance(string instanceName = null) { + if (instanceName == null) + instanceName = InstanceName; return ConnectToInstanceImpl(instanceName, false); } /// - /// Targets as the instance to use with . Closes all connections to any previous instance + /// Targets as the instance to use with . Closes all connections to any previous instance. Sets /// /// The name of the instance to connect to - /// If set to , skips the connectivity and authentication checks and returns - /// The apporopriate value - internal InstanceConnectivity ConnectToInstanceImpl(string instanceName, bool skipAuthChecks) { - if (VerifyConnection() != null) - return InstanceConnectivity.None; + /// If set to , skips the connectivity and authentication checks and returns + /// The apporopriate + internal ConnectivityLevel ConnectToInstanceImpl(string instanceName, bool skipAuthChecks) + { + if (!ConnectionStatus().HasFlag(ConnectivityLevel.Connected)) + return ConnectivityLevel.None; var prevInstance = InstanceName; - CloseAllChannels(); + if (prevInstance != instanceName) + CloseAllChannels(); InstanceName = instanceName; if (skipAuthChecks) - return InstanceConnectivity.Connected; + return ConnectivityLevel.Connected; try { GetComponent().VerifyConnection(); @@ -143,14 +147,24 @@ namespace TGServiceInterface catch { InstanceName = prevInstance; - return InstanceConnectivity.None; + return ConnectivityLevel.None; } try { GetComponent().ServerDirectory(); - return InstanceConnectivity.Authenticated; - } catch { - return InstanceConnectivity.Connected; + } + catch + { + return ConnectivityLevel.Connected; + } + try + { + GetComponent().GetCurrentAuthorizedGroup(); + return ConnectivityLevel.Administrator; + } + catch + { + return ConnectivityLevel.Authenticated; } } @@ -301,7 +315,7 @@ namespace TGServiceInterface /// The component of the channel to be created /// The correct /// Thrown if isn't a valid component - public ChannelFactory CreateChannel(string instanceName) + ChannelFactory CreateChannel(string instanceName) { var accessPath = instanceName == null ? ServiceInterfaceName : String.Format("{0}/{1}", InstanceInterfaceName, instanceName); @@ -335,53 +349,51 @@ namespace TGServiceInterface } /// - /// Used to test if the service is avaiable on the machine. Note that state can technically change at any time and any call to the service may throw an exception because it failed + /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors /// /// on successful connection, error message on failure - public string VerifyConnection() + public ConnectivityLevel ConnectionStatus() + { + return ConnectionStatus(out string unused); + } + + /// + /// Used to test if the is avaiable on the target machine. Note that state can change at any time and any call into the may throw an exception because of communcation errors + /// + /// String of the error that prevented an elevated connectivity level + /// The apporopriate + public ConnectivityLevel ConnectionStatus(out string error) { try { GetComponentImpl(false).VerifyConnection(); - return null; } - catch (Exception e) + catch (CommunicationException e) { - return e.ToString(); + error = e.ToString(); + return ConnectivityLevel.None; } - } - - /// - /// Checks if the supplied user's credentials have permission to use the service. Requires a successful prior call to - /// - /// if credentials are valid, otherwise - public bool Authenticate() - { + var service = GetService(); try { - GetService().Version(); - return true; + service.Version(); } - catch + catch(Exception e) { - return false; + error = e.ToString(); + return ConnectivityLevel.Connected; } - } - - /// - /// Checks if the current login can use . Requires a successful prior call to - /// - /// if the connection may use , otherwise - public bool AuthenticateAdmin() - { try { - GetComponent().GetCurrentAuthorizedGroup(); - return true; + // TODO + + error = null; + return ConnectivityLevel.Administrator; } - catch + catch(Exception e) { - return false; + error = e.ToString(); + return ConnectivityLevel.Authenticated; } }