6
6
using Plugin . NINA . AstroAppHTTPAPI . Equipment ;
7
7
using System ;
8
8
using System . Linq ;
9
- using System . Threading ;
10
- using System . Timers ;
11
9
using System . Net . NetworkInformation ;
12
10
using System . Security . Cryptography . X509Certificates ;
13
- using System . Configuration ;
14
- using EmbedIO . Files ;
15
11
using System . Text ;
12
+ using System . Threading ;
13
+ using System . Timers ;
16
14
17
15
namespace Plugin . NINA . AstroAppHTTPAPI . Web {
18
16
@@ -29,7 +27,7 @@ public class WebServerManager {
29
27
private string certificatePath ;
30
28
private string certificatePassword ;
31
29
32
- public WebServerManager ( int port , bool useHttps , string apiKey , EquipmentManager equipmentManager ,
30
+ public WebServerManager ( int port , bool useHttps , string apiKey , EquipmentManager equipmentManager ,
33
31
string certificatePath , string certificatePassword ) {
34
32
this . port = port ;
35
33
this . equipmentManager = equipmentManager ;
@@ -41,9 +39,9 @@ public WebServerManager(int port, bool useHttps, string apiKey, EquipmentManager
41
39
42
40
private void CreateServer ( ) {
43
41
webSocketHandler = new WebSocketHandler ( "/events/v1" , equipmentManager , apiKey ) ;
44
-
42
+
45
43
var urlPrefix = useHttps ? $ "https://*:{ port } " : $ "http://*:{ port } ";
46
-
44
+
47
45
var options = new WebServerOptions ( )
48
46
. WithUrlPrefix ( urlPrefix )
49
47
. WithMode ( HttpListenerMode . EmbedIO ) ;
@@ -64,10 +62,6 @@ private void CreateServer() {
64
62
. WithModule ( new CorsModule ( "/" , "*" , "*" , "*" ) )
65
63
. WithModule ( webSocketHandler )
66
64
. WithModule ( new BasicAuthenticationModule ( "/api" ) . WithAccount ( "user" , apiKey ) )
67
- . WithAction ( "/" , HttpVerbs . Get , ctx => {
68
- ctx . Response . ContentType = "text/html" ;
69
- return ctx . SendStringAsync ( StaticContent . GetIndexHtml ( ) , "text/html" , Encoding . UTF8 ) ;
70
- } )
71
65
. WithWebApi ( "/api/v1/camera" , m => m . WithController ( ( ) => new CameraRouteController ( null , equipmentManager ) ) )
72
66
. WithWebApi ( "/api/v1/dome" , m => m . WithController ( ( ) => new DomeRouteController ( null , equipmentManager ) ) )
73
67
. WithWebApi ( "/api/v1/mount" , m => m . WithController ( ( ) => new MountRouteController ( null , equipmentManager ) ) )
@@ -77,8 +71,11 @@ private void CreateServer() {
77
71
. WithWebApi ( "/api/v1/filterwheel" , m => m . WithController ( ( ) => new FilterWheelRouteController ( null , equipmentManager ) ) )
78
72
. WithWebApi ( "/api/v1/flatdevice" , m => m . WithController ( ( ) => new FlatDeviceRouteController ( null , equipmentManager ) ) )
79
73
. WithWebApi ( "/api/v1/safetymonitor" , m => m . WithController ( ( ) => new SafetyMonitorController ( null , equipmentManager ) ) )
80
- . WithWebApi ( "/api/v1/weather" , m => m . WithController ( ( ) => new WeatherRouteController ( null , equipmentManager ) ) ) ;
81
-
74
+ . WithWebApi ( "/api/v1/weather" , m => m . WithController ( ( ) => new WeatherRouteController ( null , equipmentManager ) ) )
75
+ . WithAction ( "/" , HttpVerbs . Any , ctx => {
76
+ ctx . Response . ContentType = "text/html" ;
77
+ return ctx . SendStringAsync ( StaticContent . GetIndexHtml ( ) , "text/html" , Encoding . UTF8 ) ;
78
+ } ) ;
82
79
UpdateServerUrls ( ) ;
83
80
}
84
81
@@ -91,9 +88,9 @@ private void UpdateServerUrls() {
91
88
. Where ( x => x . Address . AddressFamily == System . Net . Sockets . AddressFamily . InterNetwork )
92
89
. Select ( x => $ "{ protocol } ://{ x . Address } :{ port } ")
93
90
. ToList ( ) ;
94
-
91
+
95
92
ServerUrls = string . Join ( "\n " , addresses ) ;
96
-
93
+
97
94
// Add localhost if not already included
98
95
if ( ! addresses . Any ( x => x . Contains ( "127.0.0.1" ) ) ) {
99
96
ServerUrls = $ "{ protocol } ://127.0.0.1:{ port } \n " + ServerUrls ;
@@ -172,19 +169,17 @@ public string ApiKey {
172
169
173
170
}
174
171
175
- private X509Certificate2 GetCertificate ( )
176
- {
172
+ private X509Certificate2 GetCertificate ( ) {
177
173
try {
178
174
if ( string . IsNullOrEmpty ( certificatePath ) ) {
179
175
throw new Exception ( "No certificate path specified" ) ;
180
176
}
181
177
182
178
return new X509Certificate2 (
183
- certificatePath ,
179
+ certificatePath ,
184
180
certificatePassword ?? ""
185
181
) ;
186
- }
187
- catch ( Exception ex ) {
182
+ } catch ( Exception ex ) {
188
183
throw new Exception ( $ "Failed to load certificate: { ex . Message } ") ;
189
184
}
190
185
}
0 commit comments