private Elite(Uri CovenantURI, string CovenantUsername, string CovenantPassword, string CovenantHash) { HttpClientHandler clientHandler = new HttpClientHandler(); clientHandler.ServerCertificateCustomValidationCallback = (sender, cert, chain, errors) => { // Cert Pinning - Trusts only the Covenant API certificate if (CovenantHash == "" || cert.GetCertHashString() == CovenantHash) { return(true); } else { return(false); } }; CovenantAPI LoginCovenantClient = new CovenantAPI(CovenantURI, new BasicAuthenticationCredentials { UserName = "", Password = "" }, clientHandler); CovenantUserLoginResult result = LoginCovenantClient.ApiUsersLoginPost(new CovenantUserLogin { UserName = CovenantUsername, Password = CovenantPassword }); if (result.Success ?? default) { TokenCredentials creds = new TokenCredentials(result.Token); CovenantAPI CovenantClient = new CovenantAPI(CovenantURI, creds, clientHandler); this.EliteMenu = new EliteMenu(CovenantClient); EventPoller poller = new EventPoller(); poller.EventOccurred += this.EliteMenu.onEventOccured; Task.Run(() => poller.Poll(CovenantClient)); ReadLine.AutoCompletionHandler = this.EliteMenu.GetCurrentMenuItem().TabCompletionHandler; } else { EliteConsole.PrintFormattedErrorLine("Covenant login failed. Check your username and password again."); } }
public bool PrintEvent(EventModel theEvent, string Context = "*") { lock (_EventLock) { if (ContextMatches(theEvent, Context)) { EliteConsole.PrintInfoLine(); switch (theEvent.Level) { case EventLevel.Highlight: EliteConsole.PrintFormattedHighlightLine(theEvent.MessageHeader); break; case EventLevel.Info: EliteConsole.PrintFormattedInfoLine(theEvent.MessageHeader); break; case EventLevel.Warning: EliteConsole.PrintFormattedWarningLine(theEvent.MessageHeader); break; case EventLevel.Error: EliteConsole.PrintFormattedErrorLine(theEvent.MessageHeader); break; default: EliteConsole.PrintFormattedInfoLine(theEvent.MessageHeader); break; } if (!string.IsNullOrWhiteSpace(theEvent.MessageBody)) { EliteConsole.PrintInfoLine(theEvent.MessageBody); } return(true); } return(false); } }
static void Main(string[] args) { CommandLineApplication app = new CommandLineApplication(); app.HelpOption("-? | -h | --help"); var UserNameOption = app.Option( "-u | --username <USERNAME>", "The UserName to login to the Covenant API.", CommandOptionType.SingleValue ); var PasswordOption = app.Option( "-p | --password <PASSWORD>", "The Password to login to the Covenant API.", CommandOptionType.SingleValue ); var HashOption = app.Option( "-h | --hash <HASH>", "The Covenant API certificate hash to trust.", CommandOptionType.SingleValue ); var ComputerNameOption = app.Option( "-c | --computername <COMPUTERNAME>", "The ComputerName (IPAddress or Hostname) to bind the Covenant API to.", CommandOptionType.SingleValue ); app.OnExecute(() => { string username = UserNameOption.Value(); string password = PasswordOption.Value(); string hash = HashOption.Value(); if (!UserNameOption.HasValue()) { EliteConsole.PrintHighlight("Username: "******"Password: "******"Covenant CertHash (Empty to trust all): "); hash = EliteConsole.Read(); } var CovenantComputerName = ComputerNameOption.HasValue() ? ComputerNameOption.Value() : "localhost"; Elite elite = null; try { elite = new Elite(new Uri("https://" + CovenantComputerName + ":7443"), username, password, hash); } catch (HttpRequestException e) { if (e.InnerException.GetType().Name == "SocketException") { EliteConsole.PrintFormattedErrorLine("Could not connect to Covenant at: " + CovenantComputerName); return(-1); } else if (e.InnerException.GetType().Name == "AuthenticationException") { EliteConsole.PrintFormattedErrorLine("Covenant certificate does not match: " + hash); } return(-2); } catch (HttpOperationException e) { EliteConsole.PrintFormattedErrorLine("error: " + e.Message + e.StackTrace); EliteConsole.PrintFormattedErrorLine("Incorrect password for user: " + username); return(-3); } elite.Launch(); return(0); }); app.Execute(args); }
static void Main(string[] args) { CommandLineApplication app = new CommandLineApplication(); app.HelpOption("-? | -h | --help"); var UserNameOption = app.Option( "-u | --username <USERNAME>", "The UserName to login to the Covenant API.", CommandOptionType.SingleValue ); var PasswordOption = app.Option( "-p | --password <PASSWORD>", "The Password to login to the Covenant API.", CommandOptionType.SingleValue ); var HashOption = app.Option( "-h | --hash <HASH>", "The Covenant API certificate hash to trust.", CommandOptionType.SingleValue ); var ComputerNameOption = app.Option( "-c | --computername <COMPUTERNAME>", "The ComputerName (IPAddress or Hostname) to bind the Covenant API to.", CommandOptionType.SingleValue ); app.OnExecute(() => { string username = UserNameOption.Value(); string password = PasswordOption.Value(); string computername = ComputerNameOption.Value(); string hash = HashOption.Value(); try { if (!ComputerNameOption.HasValue()) { EliteConsole.PrintHighlight("Covenant ComputerName: "); computername = EliteConsole.Read(); } EliteConsole.PrintFormattedHighlightLine("Connecting to Covenant..."); Elite elite = new Elite(new Uri("https://" + computername + ":7443")); bool connected = elite.Connect(); if (!connected) { EliteConsole.PrintFormattedErrorLine("Could not connect to Covenant at: " + computername); if (computername.ToLower() == "localhost" || computername == "127.0.0.1") { EliteConsole.PrintFormattedErrorLine("Are you using Docker? Elite cannot connect over the loopback address while using Docker, because Covenant is not running within the Elite docker container."); } return(-1); } if (!UserNameOption.HasValue()) { EliteConsole.PrintHighlight("Username: "******"Password: "******"Covenant CertHash (Empty to trust all): "); hash = EliteConsole.Read(); } EliteConsole.PrintFormattedHighlightLine("Logging in to Covenant..."); bool login = elite.Login(username, password, hash); if (login) { elite.Launch(); elite.CancelEventPoller.Cancel(); } else { EliteConsole.PrintFormattedErrorLine("Covenant login failed. Check your username and password again."); EliteConsole.PrintFormattedErrorLine("Incorrect password for user: "******"SocketException") { EliteConsole.PrintFormattedErrorLine("Could not connect to Covenant at: " + computername); if (computername.ToLower() == "localhost" || computername == "127.0.0.1") { EliteConsole.PrintFormattedErrorLine("Are you using Docker? Elite cannot connect over the loopback address while using Docker, because Covenant is not running within the Elite docker container."); } return(-1); } catch (HttpRequestException e) when(e.InnerException.GetType().Name == "AuthenticationException") { EliteConsole.PrintFormattedErrorLine("Covenant certificate does not match: " + hash); return(-2); } catch (HttpRequestException) { EliteConsole.PrintFormattedErrorLine("Covenant login failed. Check your username and password again."); EliteConsole.PrintFormattedErrorLine("Incorrect password for user: "******"Unknown Exception Occured: " + e.Message + Environment.NewLine + e.StackTrace); return(-4); } return(0); }); app.Execute(args); }