private static bool validNewAuthToken(string host, string token) { if (host.Length <= 0) { GUI.user.RaiseError("Host field is required."); return(false); } if (token.Length <= 0) { GUI.user.RaiseError("Token field is required."); return(false); } if (Uri.CheckHostName(host) == UriHostNameType.Unknown) { GUI.user.RaiseError("{0} is not a valid host name.", host); return(false); } string oldToken; if (Win32Registry.TryGetAuthToken(host, out oldToken)) { GUI.user.RaiseError("{0} already has an authentication token.", host); return(false); } return(true); }
public void LoadInstancesFromRegistry() { log.Info("Loading KSP instances from registry"); instances.Clear(); foreach (Tuple <string, string> instance in Win32Registry.GetInstances()) { var name = instance.Item1; var path = instance.Item2; log.DebugFormat("Loading {0} from {1}", name, path); // Add unconditionally, sort out invalid instances downstream instances.Add(name, new KSP(path, name, User)); } try { AutoStartInstance = Win32Registry.AutoStartInstance; } catch (InvalidKSPInstanceKraken e) { log.WarnFormat("Auto-start instance was invalid: {0}", e.Message); AutoStartInstance = null; } }
public void LoadInstancesFromRegistry() { log.Debug("Loading KSP instances from registry"); instances.Clear(); foreach (Tuple <string, string> instance in Win32Registry.GetInstances()) { var name = instance.Item1; var path = instance.Item2; log.DebugFormat("Loading {0} from {1}", name, path); if (KSP.IsKspDir(path)) { instances.Add(name, new KSP(path, User)); log.DebugFormat("Added {0} at {1}", name, path); } else { log.WarnFormat("{0} at {1} is not a vaild install", name, path); } //var ksp = new KSP(path, User); //instances.Add(name, ksp); } try { AutoStartInstance = Win32Registry.AutoStartInstance; } catch (InvalidKSPInstanceKraken e) { log.WarnFormat("Auto-start instance was invalid: {0}", e.Message); AutoStartInstance = null; } }
public NetAsyncDownloaderDownloadPart(Net.DownloadTarget target, string path = null) { this.url = target.url; this.fallbackUrl = target.fallbackUrl; this.triedFallback = false; this.path = path ?? Path.GetTempFileName(); size = bytesLeft = target.size; lastProgressUpdateTime = DateTime.Now; agent.Headers.Add("User-Agent", Net.UserAgentString); // Tell the server what kind of files we want if (!string.IsNullOrEmpty(target.mimeType)) { log.InfoFormat("Setting MIME type {0}", target.mimeType); agent.Headers.Add("Accept", target.mimeType); } // Check whether to use an auth token for this host string token; if (Win32Registry.TryGetAuthToken(this.url.Host, out token) && !string.IsNullOrEmpty(token)) { log.InfoFormat("Using auth token for {0}", this.url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {token}"); } }
/// <summary> /// Renames an instance in the registry and saves. /// </summary> /// // TODO: What should we do if our target name already exists? public void RenameInstance(string from, string to) { var ksp = instances[from]; instances.Remove(from); instances.Add(to, ksp); Win32Registry.SetRegistryToInstances(instances, AutoStartInstance); }
/// <summary> /// Initialize a settings window /// </summary> public SettingsDialog() { InitializeComponent(); if (Platform.IsMono) { this.ClearCacheMenu.Renderer = new FlatToolStripRenderer(); } winReg = new Win32Registry(); }
/// <summary> /// Renames an instance in the registry and saves. /// </summary> public void RenameInstance(string from, string to) { // TODO: What should we do if our target name already exists? KSP ksp = instances[from]; instances.Remove(from); ksp.Name = to; instances.Add(to, ksp); Win32Registry.SetRegistryToInstances(instances); }
public static string DownloadText(Uri url, string authToken = "") { log.DebugFormat("About to download {0}", url.OriginalString); try { WebClient agent = MakeDefaultHttpClient(); // Check whether to use an auth token for this host if (!string.IsNullOrEmpty(authToken) || (Win32Registry.TryGetAuthToken(url.Host, out authToken) && !string.IsNullOrEmpty(authToken))) { log.InfoFormat("Using auth token for {0}", url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {authToken}"); } return(agent.DownloadString(url.OriginalString)); } catch (Exception) { try { log.InfoFormat("Download failed, trying with curlsharp..."); var content = string.Empty; var client = Curl.CreateEasy(url.OriginalString, delegate(byte[] buf, int size, int nmemb, object extraData) { content += Encoding.UTF8.GetString(buf); return(size * nmemb); }); using (client) { var result = client.Perform(); if (result != CurlCode.Ok) { throw new Exception("Curl download failed with error " + result); } log.DebugFormat("Download from {0}:\r\n\r\n{1}", url, content); return(content); } } catch (Exception e) { throw new Kraken("Downloading using cURL failed", e); } } }
private void RefreshAuthTokensListBox() { AuthTokensListBox.Items.Clear(); foreach (string host in Win32Registry.GetAuthTokenHosts()) { string token; if (Win32Registry.TryGetAuthToken(host, out token)) { AuthTokensListBox.Items.Add(string.Format("{0} | {1}", host, token)); } } }
private void DeleteAuthTokenButton_Click(object sender, EventArgs e) { if (AuthTokensListBox.SelectedItem != null) { string item = AuthTokensListBox.SelectedItem as string; string host = item?.Split('|')[0].Trim(); Win32Registry.SetAuthToken(host, null); RefreshAuthTokensListBox(); DeleteRepoButton.Enabled = false; } }
public void UpdateRefreshTimer() { refreshTimer.Stop(); Win32Registry winReg = new Win32Registry(); // Interval is set to 1 minute * RefreshRate if (winReg.RefreshRate > 0) { refreshTimer.Interval = 1000 * 60 * winReg.RefreshRate; refreshTimer.Start(); } }
/// <summary> /// Adds a KSP instance to registry. /// Returns the resulting KSP object. /// </summary> public KSP AddInstance(string name, KSP ksp_instance) { if (ksp_instance.Valid) { instances.Add(name, ksp_instance); Win32Registry.SetRegistryToInstances(instances, AutoStartInstance); } else { throw new NotKSPDirKraken(ksp_instance.GameDir()); } return(ksp_instance); }
private void ResetAgent() { agent = new WebClient(); agent.Headers.Add("User-Agent", Net.UserAgentString); // Tell the server what kind of files we want if (!string.IsNullOrEmpty(mimeType)) { log.InfoFormat("Setting MIME type {0}", mimeType); agent.Headers.Add("Accept", mimeType); } // Check whether to use an auth token for this host string token; if (Win32Registry.TryGetAuthToken(this.url.Host, out token) && !string.IsNullOrEmpty(token)) { log.InfoFormat("Using auth token for {0}", this.url.Host); // Send our auth token to the GitHub API (or whoever else needs one) agent.Headers.Add("Authorization", $"token {token}"); } // Forward progress and completion events to our listeners agent.DownloadProgressChanged += (sender, args) => { if (Progress != null) { Progress(sender, args); } }; agent.DownloadFileCompleted += (sender, args) => { if (Done != null) { Done(sender, args); } }; }
public void LoadInstancesFromRegistry() { log.Info("Loading KSP instances from registry"); instances.Clear(); foreach (Tuple <string, string> instance in Win32Registry.GetInstances()) { var name = instance.Item1; var path = instance.Item2; log.DebugFormat("Loading {0} from {1}", name, path); // Add unconditionally, sort out invalid instances downstream instances.Add(name, new KSP(path, name, User)); } if (!Directory.Exists(Win32Registry.DownloadCacheDir)) { Directory.CreateDirectory(Win32Registry.DownloadCacheDir); } string failReason; TrySetupCache(Win32Registry.DownloadCacheDir, out failReason); }
public SettingsDialog() { InitializeComponent(); StartPosition = FormStartPosition.CenterScreen; winReg = new Win32Registry(); }
private void NewAuthTokenButton_Click(object sender, EventArgs e) { // Inspired by https://stackoverflow.com/a/17546909/2422988 Form newAuthTokenPopup = new Form() { FormBorderStyle = FormBorderStyle.FixedToolWindow, StartPosition = FormStartPosition.CenterParent, ClientSize = new Size(300, 100), Text = "Add Authentication Token" }; Label hostLabel = new Label() { AutoSize = true, Location = new Point(3, 6), Size = new Size(271, 13), Text = "Host:" }; TextBox hostTextBox = new TextBox() { Location = new Point(45, 6), Size = new Size(newAuthTokenPopup.ClientSize.Width - 40 - 10, 23), Text = "" }; Label tokenLabel = new Label() { AutoSize = true, Location = new Point(3, 35), Size = new Size(271, 13), Text = "Token:" }; TextBox tokenTextBox = new TextBox() { Location = new Point(45, 35), Size = new Size(newAuthTokenPopup.ClientSize.Width - 40 - 10, 23), Text = "" }; Button acceptButton = new Button() { DialogResult = DialogResult.OK, Name = "okButton", Size = new Size(75, 23), Text = "&Accept", Location = new Point((newAuthTokenPopup.ClientSize.Width - 80 - 80) / 2, 64) }; acceptButton.Click += (origin, evt) => { newAuthTokenPopup.DialogResult = validNewAuthToken(hostTextBox.Text, tokenTextBox.Text) ? DialogResult.OK : DialogResult.None; }; Button cancelButton = new Button() { DialogResult = DialogResult.Cancel, Name = "cancelButton", Size = new Size(75, 23), Text = "&Cancel", Location = new Point(acceptButton.Location.X + acceptButton.Size.Width + 5, 64) }; newAuthTokenPopup.Controls.Add(hostLabel); newAuthTokenPopup.Controls.Add(hostTextBox); newAuthTokenPopup.Controls.Add(tokenLabel); newAuthTokenPopup.Controls.Add(tokenTextBox); newAuthTokenPopup.Controls.Add(acceptButton); newAuthTokenPopup.Controls.Add(cancelButton); newAuthTokenPopup.AcceptButton = acceptButton; newAuthTokenPopup.CancelButton = cancelButton; switch (newAuthTokenPopup.ShowDialog(this)) { case DialogResult.Abort: case DialogResult.Cancel: case DialogResult.Ignore: case DialogResult.No: // User cancelled out, so do nothing break; case DialogResult.OK: case DialogResult.Yes: Win32Registry.SetAuthToken(hostTextBox.Text, tokenTextBox.Text); RefreshAuthTokensListBox(); break; } }
/// <summary> /// Adds a KSP instance to registry. /// Returns the resulting KSP object. /// </summary> public KSP AddInstance(string name, KSP ksp_instance) { instances.Add(name, ksp_instance); Win32Registry.SetRegistryToInstances(instances, AutoStartInstance); return(ksp_instance); }
/// <summary> /// Removes the instance from the registry and saves. /// </summary> public void RemoveInstance(string name) { instances.Remove(name); Win32Registry.SetRegistryToInstances(instances, AutoStartInstance); }