11using iGotify_Notification_Assist . Models ;
22using iGotify_Notification_Assist . Services ;
33using Microsoft . AspNetCore . Mvc ;
4+ using Newtonsoft . Json ;
45using SecNtfyNuGet ;
56using Environments = iGotify_Notification_Assist . Services . Environments ;
67
@@ -20,29 +21,32 @@ public async Task<IActionResult> PostDeviceModel(DeviceModel deviceModel)
2021 {
2122 string result ;
2223 bool resultBool ;
23-
24+
2425 Console . WriteLine ( $ "ClientToken: { deviceModel . ClientToken } ") ;
2526 Console . WriteLine ( $ "DeviceToken: { deviceModel . DeviceToken } ") ;
2627 Console . WriteLine ( $ "GotifyUrl: { deviceModel . GotifyUrl } ") ;
2728
2829 if (
2930 deviceModel . ClientToken . Length == 0 || deviceModel . ClientToken == "string" ||
30- deviceModel . DeviceToken . Length == 0 || deviceModel . DeviceToken . Length < 60 || deviceModel . DeviceToken == "string" ||
31+ deviceModel . DeviceToken . Length == 0 || deviceModel . DeviceToken . Length < 60 ||
32+ deviceModel . DeviceToken == "string" ||
3133 deviceModel . GotifyUrl . Length == 0 || deviceModel . GotifyUrl == "string"
32- )
34+ )
3335 {
3436 result = "Fehler beim hinzugefügen des Gerätes!" ;
3537 resultBool = false ;
3638 return Ok ( new { Message = result , Successful = resultBool } ) ;
3739 }
38-
40+
3941 if ( await deviceModel . Insert ( ) )
4042 {
4143 GotifySocketService . getInstance ( ) ;
4244 GotifySocketService . StartWsThread ( deviceModel . GotifyUrl , deviceModel . ClientToken ) ;
4345 result = "Gerät erfolgreich hinzugefügt" ;
4446 resultBool = true ;
45- } else {
47+ }
48+ else
49+ {
4650 result = "Fehler beim hinzugefügen des Gerätes!" ;
4751 resultBool = false ;
4852 }
@@ -53,18 +57,18 @@ public async Task<IActionResult> PostDeviceModel(DeviceModel deviceModel)
5357 /// <summary>
5458 /// Delete device from TXT when loggin out from iGotify
5559 /// </summary>
56- /// <param name="token"></param>
60+ /// <param name="token">Clienttoken for verify the correct entry </param>
5761 /// <returns></returns>
5862 [ HttpDelete ]
5963 public async Task < IActionResult > DeleteDevcice ( string token )
6064 {
6165 string result ;
6266 bool resultBool ;
63-
67+
6468 Console . WriteLine ( $ "Delete Token: { token } ") ;
6569 if ( token . Length == 0 || token == "string" )
6670 {
67- result = "Fehler beim löschen des Gerätes !" ;
71+ result = "Error deleting device !" ;
6872 resultBool = false ;
6973 return Ok ( new { Message = result , Successful = resultBool } ) ;
7074 }
@@ -79,22 +83,70 @@ public async Task<IActionResult> DeleteDevcice(string token)
7983 GotifySocketService . KillWsThread ( usr . ClientToken ) ;
8084 }
8185
82- result = "Gerät erfolgreich gelöscht " ;
86+ result = "Device deleted successfully! " ;
8387 resultBool = true ;
84- } else {
85- result = "Fehler beim löschen des Gerätes!" ;
88+ }
89+ else
90+ {
91+ result = "Error deleting device!" ;
8692 resultBool = false ;
8793 }
88-
94+
8995 return Ok ( new { Message = result , Successful = resultBool } ) ;
9096 }
91-
97+
98+ /// <summary>
99+ /// Add Custom Headers e.g. Cloudflare, Pangolin authentication
100+ /// </summary>
101+ /// <param name="customHeaders">Custome Header items</param>
102+ /// <param name="token">Clienttoken for verify the correct entry and instance</param>
103+ /// <returns></returns>
104+ [ HttpPost ( "CustomHeaders/{token}" ) ]
105+ public async Task < IActionResult > CustomHeaders ( [ FromBody ] List < CustomHeaders > customHeaders , string token )
106+ {
107+ string result = "" ;
108+ bool resultBool = false ;
109+
110+ if ( token == null || token . Length == 0 )
111+ {
112+ resultBool = false ;
113+ result = "Token not set!" ;
114+ return BadRequest ( new { Message = result , Successful = resultBool } ) ;
115+ }
116+
117+ if ( customHeaders . Count == 0 )
118+ {
119+ resultBool = false ;
120+ result = "CustomHeaders were not set!" ;
121+ return BadRequest ( new { Message = result , Successful = resultBool } ) ;
122+ }
123+
124+ var usr = await DatabaseService . GetUser ( token ) ;
125+ usr . Headers = JsonConvert . SerializeObject ( customHeaders ) ;
126+ resultBool = await usr . Update ( ) ;
127+
128+ if ( resultBool )
129+ {
130+ var gss = GotifySocketService . getInstance ( ) ;
131+ GotifySocketService . KillAllWsThread ( ) ;
132+ gss . Start ( ) ;
133+ result = "CustomHeaders successfully added!" ;
134+ }
135+
136+ return Ok ( new { Message = result , Successful = resultBool } ) ;
137+ }
138+
139+ /// <summary>
140+ /// Send a Test message if remote notification work
141+ /// </summary>
142+ /// <param name="deviceToken">SecNtfy Token</param>
143+ /// <returns></returns>
92144 [ HttpGet ( "Test/{deviceToken}" ) ]
93145 public async Task < IActionResult > Test ( string deviceToken )
94146 {
95147 var ntfy = new SecNtfy ( Environments . secNtfyUrl ) ;
96148 if ( deviceToken . Length > 0 )
97- _ = await ntfy . SendNotification ( deviceToken , "Test" , "Test Nachricht " ) ;
149+ _ = await ntfy . SendNotification ( deviceToken , "Test" , "Test Notification " ) ;
98150 if ( Environments . isLogEnabled )
99151 Console . WriteLine ( ntfy . encTitle ) ;
100152
0 commit comments