SettingsForm.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. namespace qdr.app.tools.claudecodebalancewidget
  2. {
  3. public partial class SettingsForm : Form
  4. {
  5. private TextBox sessionTokenTextBox;
  6. private Button okButton;
  7. private Button cancelButton;
  8. private Button showHideButton;
  9. private Label instructionsLabel;
  10. private bool isTokenVisible = false;
  11. public string SessionToken { get; private set; }
  12. public SettingsForm(string currentToken)
  13. {
  14. SessionToken = currentToken;
  15. InitializeComponent();
  16. sessionTokenTextBox.Text = currentToken;
  17. }
  18. //private void InitializeComponent()
  19. //{
  20. // this.SuspendLayout();
  21. // // Form
  22. // this.ClientSize = new Size(500, 250);
  23. // this.Text = "Settings - Claude Balance Monitor";
  24. // this.StartPosition = FormStartPosition.CenterParent;
  25. // this.FormBorderStyle = FormBorderStyle.FixedDialog;
  26. // this.MaximizeBox = false;
  27. // this.MinimizeBox = false;
  28. // // Instructions Label
  29. // instructionsLabel = new Label();
  30. // instructionsLabel.Text = "To get your session token:\n" +
  31. // "1. Go to https://console.anthropic.com/settings/billing\n" +
  32. // "2. Open Developer Tools (F12)\n" +
  33. // "3. Go to Application → Cookies\n" +
  34. // "4. Find and copy the 'sessionKey' cookie value";
  35. // instructionsLabel.Location = new Point(10, 10);
  36. // instructionsLabel.Size = new Size(470, 80);
  37. // instructionsLabel.Font = new Font("Segoe UI", 9);
  38. // this.Controls.Add(instructionsLabel);
  39. // // Session Token Label
  40. // var tokenLabel = new Label();
  41. // tokenLabel.Text = "Session Token:";
  42. // tokenLabel.Location = new Point(10, 100);
  43. // tokenLabel.Size = new Size(100, 20);
  44. // tokenLabel.Font = new Font("Segoe UI", 9, FontStyle.Bold);
  45. // this.Controls.Add(tokenLabel);
  46. // // Session Token TextBox
  47. // sessionTokenTextBox = new TextBox();
  48. // sessionTokenTextBox.Location = new Point(10, 125);
  49. // sessionTokenTextBox.Size = new Size(370, 23);
  50. // sessionTokenTextBox.UseSystemPasswordChar = true;
  51. // sessionTokenTextBox.Font = new Font("Consolas", 9);
  52. // this.Controls.Add(sessionTokenTextBox);
  53. // // Show/Hide Button
  54. // showHideButton = new Button();
  55. // showHideButton.Text = "Show";
  56. // showHideButton.Location = new Point(390, 125);
  57. // showHideButton.Size = new Size(60, 23);
  58. // showHideButton.Click += ShowHideButton_Click;
  59. // this.Controls.Add(showHideButton);
  60. // // Test Connection Button
  61. // var testButton = new Button();
  62. // testButton.Text = "Test Connection";
  63. // testButton.Location = new Point(10, 160);
  64. // testButton.Size = new Size(120, 30);
  65. // testButton.Click += TestButton_Click;
  66. // this.Controls.Add(testButton);
  67. // // Status Label
  68. // var statusLabel = new Label();
  69. // statusLabel.Name = "statusLabel";
  70. // statusLabel.Text = "Enter your session token and click 'Test Connection'";
  71. // statusLabel.Location = new Point(140, 165);
  72. // statusLabel.Size = new Size(350, 20);
  73. // statusLabel.ForeColor = Color.Gray;
  74. // this.Controls.Add(statusLabel);
  75. // // OK Button
  76. // okButton = new Button();
  77. // okButton.Text = "OK";
  78. // okButton.Location = new Point(295, 200);
  79. // okButton.Size = new Size(90, 30);
  80. // okButton.Click += OkButton_Click;
  81. // this.Controls.Add(okButton);
  82. // // Cancel Button
  83. // cancelButton = new Button();
  84. // cancelButton.Text = "Cancel";
  85. // cancelButton.Location = new Point(395, 200);
  86. // cancelButton.Size = new Size(90, 30);
  87. // cancelButton.Click += CancelButton_Click;
  88. // this.Controls.Add(cancelButton);
  89. // this.ResumeLayout(false);
  90. //}
  91. private void ShowHideButton_Click(object sender, EventArgs e)
  92. {
  93. isTokenVisible = !isTokenVisible;
  94. sessionTokenTextBox.UseSystemPasswordChar = !isTokenVisible;
  95. showHideButton.Text = isTokenVisible ? "Hide" : "Show";
  96. }
  97. private async void TestButton_Click(object sender, EventArgs e)
  98. {
  99. var statusLabel = this.Controls.Find("statusLabel", false)[0] as Label;
  100. var testToken = sessionTokenTextBox.Text.Trim();
  101. if (string.IsNullOrEmpty(testToken))
  102. {
  103. statusLabel.Text = "Please enter a session token";
  104. statusLabel.ForeColor = Color.Red;
  105. return;
  106. }
  107. statusLabel.Text = "Testing connection...";
  108. statusLabel.ForeColor = Color.Blue;
  109. try
  110. {
  111. using (var testClient = new HttpClient())
  112. {
  113. testClient.DefaultRequestHeaders.Add("User-Agent",
  114. "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
  115. testClient.DefaultRequestHeaders.Add("Cookie", $"sessionKey={testToken}");
  116. var response = await testClient.GetAsync("https://console.anthropic.com/settings/billing");
  117. if (response.IsSuccessStatusCode)
  118. {
  119. statusLabel.Text = "✓ Connection successful!";
  120. statusLabel.ForeColor = Color.Green;
  121. }
  122. else
  123. {
  124. statusLabel.Text = $"✗ Connection failed (Status: {response.StatusCode})";
  125. statusLabel.ForeColor = Color.Red;
  126. }
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. statusLabel.Text = $"✗ Connection error: {ex.Message}";
  132. statusLabel.ForeColor = Color.Red;
  133. }
  134. }
  135. private void OkButton_Click(object sender, EventArgs e)
  136. {
  137. SessionToken = sessionTokenTextBox.Text.Trim();
  138. DialogResult = DialogResult.OK;
  139. Close();
  140. }
  141. private void CancelButton_Click(object sender, EventArgs e)
  142. {
  143. DialogResult = DialogResult.Cancel;
  144. Close();
  145. }
  146. }
  147. }