-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
185 lines (163 loc) · 6.58 KB
/
Form1.cs
File metadata and controls
185 lines (163 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Core;
namespace IphoneRemoteCamera
{
public partial class Form1 : Form
{
private readonly WebView2 webView;
private readonly bool useHttps;
private const string CertFileName = "cert.pfx";
private const string CertPassword = "changeit";
public Form1(bool useHttps)
{
this.useHttps = useHttps;
InitializeComponent();
// Initialize WebView2 and add to panel
webView = new WebView2 { Dock = DockStyle.Fill };
pnlBrowser.Controls.Add(webView);
Log("Form initialized.");
}
private async void Form1_Load(object sender, EventArgs e)
{
await InitializeBrowserAsync();
}
private async Task InitializeBrowserAsync()
{
try
{
Log("Initializing WebView2 environment...");
var env = await CoreWebView2Environment.CreateAsync();
await webView.EnsureCoreWebView2Async(env);
webView.CoreWebView2.Settings.IsStatusBarEnabled = false;
string host = Dns.GetHostName();
string url = useHttps
? $"https://{host}:8443/result.html"
: $"http://{host}:8080/result.html";
Log($"Navigating to URL: {url}");
webView.Source = new Uri(url);
webView.CoreWebView2.NavigationCompleted += (s, args) =>
Log(args.IsSuccess ? "Page loaded successfully." : $"Navigation error: {args.WebErrorStatus}");
}
catch (Exception ex)
{
Log($"Error initializing browser: {ex.Message}");
MessageBox.Show("Error initializing browser: " + ex.Message);
}
}
private void btnGenCert_Click(object sender, EventArgs e)
{
try
{
string path = System.IO.Path.Combine(AppContext.BaseDirectory, CertFileName);
Log($"Generating self-signed cert at '{path}'...");
using var rsa = RSA.Create(2048);
var req = new CertificateRequest(
"CN=localhost", rsa, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
req.CertificateExtensions.Add(
new X509KeyUsageExtension(X509KeyUsageFlags.DigitalSignature, false));
req.CertificateExtensions.Add(
new X509EnhancedKeyUsageExtension(
new OidCollection { new Oid("1.3.6.1.5.5.7.3.1") }, false));
var cert = req.CreateSelfSigned(
DateTimeOffset.Now.AddDays(-1), DateTimeOffset.Now.AddYears(1));
var pfxBytes = cert.Export(X509ContentType.Pfx, CertPassword);
System.IO.File.WriteAllBytes(path, pfxBytes);
Log("Certificate generation succeeded.");
MessageBox.Show($"Generated cert.pfx with password '{CertPassword}'", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
Log($"Cert generation failed: {ex.Message}");
MessageBox.Show("Certificate generation failed: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void btnRunInTray_Click(object sender, EventArgs e)
{
Log("Minimizing to tray.");
notifyIcon.Icon = Icon;
notifyIcon.Visible = true;
Hide();
}
private void btnCleanup_Click(object sender, EventArgs e)
{
CleanupAndExit();
}
private void btnReload_Click(object sender, EventArgs e)
{
Log("Reload: restarting OBS and WebSocket listener...");
Task.Run(async () =>
{
CleanupObsProcesses();
// restart OBS headless
ObsHeadlessLauncher.LaunchObsHeadless();
await ObsHeadlessLauncher.WaitForPortAsync("127.0.0.1", 4455, TimeSpan.FromSeconds(30));
var ctrl = new ObsHeadlessLauncher.ObsController("127.0.0.1", 4455, null);
await ctrl.InitializeAsync();
Log("Reload complete.");
});
}
private void CleanupAndExit()
{
Log("Cleanup: terminating OBS processes...");
CleanupObsProcesses();
Log("Cleanup complete. Exiting application.");
Process.GetCurrentProcess().Kill();
}
private void CleanupObsProcesses()
{
var obsProcesses = Process.GetProcesses()
.Where(p => p.ProcessName.IndexOf("obs", StringComparison.OrdinalIgnoreCase) >= 0)
.ToList();
foreach (var proc in obsProcesses)
{
try
{
Log($"Closing process {proc.ProcessName} (ID {proc.Id})");
if (!proc.CloseMainWindow() || proc.WaitForExit(2000))
{
proc.Kill();
Log($"Killed process ID {proc.Id}");
}
}
catch (Exception ex)
{
Log($"Error terminating {proc.ProcessName}: {ex.Message}");
}
}
}
private void notifyIcon_DoubleClick(object sender, EventArgs e)
{
Log("Restoring from tray.");
Show();
WindowState = FormWindowState.Normal;
notifyIcon.Visible = false;
}
private void trayMenuOpen_Click(object sender, EventArgs e)
{
Log("Tray menu: Open.");
notifyIcon_DoubleClick(sender, e);
}
private void trayMenuExit_Click(object sender, EventArgs e)
{
Log("Tray menu: Exit.");
Application.Exit();
}
private void Log(string message)
{
string timestamp = DateTime.Now.ToString("HH:mm:ss");
if (txtDebugLog.InvokeRequired)
txtDebugLog.Invoke(new Action(() => txtDebugLog.AppendText($"[{timestamp}] {message}{Environment.NewLine}")));
else
txtDebugLog.AppendText($"[{timestamp}] {message}{Environment.NewLine}");
}
}
}