-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
115 lines (103 loc) · 4.24 KB
/
Program.cs
File metadata and controls
115 lines (103 loc) · 4.24 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
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Hosting;
namespace IphoneRemoteCamera
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Determine certificate presence
string baseDir = AppContext.BaseDirectory;
var certPath = Path.Combine(baseDir, "cert.pfx");
bool hasCert = File.Exists(certPath);
if (!hasCert)
{
MessageBox.Show(
$"Certificate not found at:\n{certPath}\nRunning HTTP mode on port 8080.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
// Build and start web host
var builder = Host.CreateDefaultBuilder()
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseContentRoot(baseDir);
webBuilder.UseWebRoot(Path.Combine(baseDir, "wwwroot"));
webBuilder.ConfigureKestrel(options =>
{
// HTTP on port 8080
options.Listen(IPAddress.Any, 8080, listenOptions =>
{
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
});
// HTTPS on port 8443 if certificate exists
if (hasCert)
{
options.Listen(IPAddress.Any, 8443, listenOptions =>
{
listenOptions.UseHttps(certPath, "yourpassword");
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
});
}
});
webBuilder.Configure(app =>
{
app.UseWebSockets();
// Default to result.html
var defaults = new DefaultFilesOptions();
defaults.DefaultFileNames.Clear();
defaults.DefaultFileNames.Add("result.html");
app.UseDefaultFiles(defaults);
app.UseStaticFiles();
// WebSocket endpoint
app.Use(async (ctx, next) =>
{
if (ctx.Request.Path == "/ws")
{
if (ctx.WebSockets.IsWebSocketRequest)
{
var ws = await ctx.WebSockets.AcceptWebSocketAsync();
bool isPublisher = ctx.Request.Query.ContainsKey("pub");
await WebSocketHandler.HandleAsync(isPublisher, ws);
return;
}
ctx.Response.StatusCode = 400;
return;
}
await next();
});
});
});
var host = builder.Build();
host.Start();
// Start OBS headless in background
Task.Run(async () =>
{
try
{
ObsHeadlessLauncher.LaunchObsHeadless();
var controller = new ObsHeadlessLauncher.ObsController("127.0.0.1", 4455, "");
await controller.InitializeAsync();
}
catch (Exception ex)
{
MessageBox.Show($"OBS launch error: {ex.Message}", "OBS Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
});
// Run WinForms UI
Application.Run(new Form1(hasCert));
host.StopAsync().Wait();
}
}
}