6
6
using System . Diagnostics ;
7
7
using System . Linq ;
8
8
using System . Net ;
9
+ using System . Net . Http ;
10
+ using System . Net . Sockets ;
11
+ using System . Text . RegularExpressions ;
9
12
using System . Threading ;
10
13
using System . Windows ;
11
14
using System . Windows . Threading ;
@@ -174,8 +177,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
174
177
logger . Error ( "Error while starting Spotify." , applicationStartupException ) ;
175
178
176
179
string errorMsg = Properties . Resources . ERROR_STARTUP_SPOTIFY ;
177
- string techDetails = $ "Technical details\n { applicationStartupException . Message } ";
178
- MessageBox . Show ( $ "{ errorMsg } \n \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
180
+ MessageBox . Show ( $ "{ errorMsg } \n { applicationStartupException . Message } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
179
181
180
182
Analytics . TrackException ( applicationStartupException , true ) ;
181
183
}
@@ -188,7 +190,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
188
190
if ( webException . Status == WebExceptionStatus . ProtocolError )
189
191
status += $ " ({ ( webException . Response as HttpWebResponse ) ? . StatusCode } , \" { ( webException . Response as HttpWebResponse ) ? . StatusDescription } \" )";
190
192
string techDetails = $ "Technical details: { webException . Message } \n { webException . HResult } , { status } ";
191
- MessageBox . Show ( $ "{ errorMsg } \n \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
193
+ MessageBox . Show ( $ "{ errorMsg } \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
192
194
193
195
Analytics . TrackException ( webException , true ) ;
194
196
}
@@ -198,7 +200,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
198
200
199
201
string errorMsg = Properties . Resources . ERROR_UNKNOWN ;
200
202
string techDetails = $ "Technical Details: { e . Error . Message } \n { e . Error . StackTrace } ";
201
- MessageBox . Show ( $ "{ errorMsg } \n \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
203
+ MessageBox . Show ( $ "{ errorMsg } \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
202
204
203
205
Analytics . TrackException ( e . Error , true ) ;
204
206
}
@@ -209,7 +211,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
209
211
210
212
string errorMsg = Properties . Resources . ERROR_STARTUP_SPOTIFY ;
211
213
const string techDetails = "Technical Details: timeout" ;
212
- MessageBox . Show ( $ "{ errorMsg } \n \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
214
+ MessageBox . Show ( $ "{ errorMsg } \n { techDetails } ", "Toastify" , MessageBoxButton . OK , MessageBoxImage . Error ) ;
213
215
}
214
216
215
217
// Terminate Toastify
@@ -288,6 +290,34 @@ private void ConnectWithSpotify(DoWorkEventArgs e)
288
290
}
289
291
catch ( WebException ex )
290
292
{
293
+ if ( ex . InnerException is SocketException socketException && socketException . SocketErrorCode == SocketError . ConnectionRefused )
294
+ {
295
+ bool hostRedirected = Regex . IsMatch ( socketException . Message , @"(127\.0\.0\.1|localhost|0\.0\.0\.0):80(?![0-9]+)" , RegexOptions . IgnoreCase | RegexOptions . Compiled ) ;
296
+
297
+ // Check if "open.spotify.com" is blocked by the firewall or redirected in the hosts file
298
+ using ( HttpClient http = new HttpClient ( ) )
299
+ {
300
+ using ( HttpRequestMessage request = new HttpRequestMessage ( ) )
301
+ {
302
+ request . Method = HttpMethod . Head ;
303
+ request . RequestUri = new Uri ( "http://open.spotify.com" ) ;
304
+ request . Headers . Add ( "User-Agent" , "Spotify (1.0.50.41368.gbd68dbef)" ) ;
305
+
306
+ try
307
+ {
308
+ using ( http . SendAsync ( request ) . Result ) { }
309
+ }
310
+ catch
311
+ {
312
+ logger . Error ( "Couldn't access \" open.spotify.com\" : the client blocked the connection to the host." ) ;
313
+ throw new ApplicationStartupException ( hostRedirected
314
+ ? Properties . Resources . ERROR_STARTUP_SPOTIFY_API_CONNECTION_BLOCKED_HOSTS
315
+ : Properties . Resources . ERROR_STARTUP_SPOTIFY_API_CONNECTION_BLOCKED , false ) ;
316
+ }
317
+ }
318
+ }
319
+ }
320
+
291
321
logger . Warn ( "WebException while connecting to Spotify." , ex ) ;
292
322
}
293
323
} while ( ! connected && ! signaled ) ;
0 commit comments