Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit 6e3798a

Browse files
committed
Catch refused connection to "open.spotify.com"
The client itself is blocking the connection either through the firewall or by redirecting the host address in the "hosts" file.
1 parent dd11317 commit 6e3798a

File tree

6 files changed

+80
-21
lines changed

6 files changed

+80
-21
lines changed

Toastify/Properties/Resources.Designer.cs

+22-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Toastify/Properties/Resources.resx

+10-3
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,7 @@ Delete the "{0}" file and restart the application to recreate the settings file.
124124
The application will be started with default settings.</value>
125125
</data>
126126
<data name="ERROR_STARTUP_SPOTIFY" xml:space="preserve">
127-
<value>An error occurred when trying to start Spotify.
128-
Please, start it manually and then restart the application.</value>
127+
<value>An error occurred when trying to start Spotify.</value>
129128
</data>
130129
<data name="ERROR_STARTUP_PROCESS" xml:space="preserve">
131130
<value>Could not find Spotify process!
@@ -136,7 +135,7 @@ If Spotify is running, please open an issue at github.com/aleab/toastify.</value
136135
Please restart the application.</value>
137136
</data>
138137
<data name="ERROR_STARTUP_SPOTIFY_API_CONNECT" xml:space="preserve">
139-
<value>An error occurred when trying to start Spotify: Toastify was not able to connect with Spotify.</value>
138+
<value>Toastify was not able to connect with Spotify.</value>
140139
</data>
141140
<data name="ERROR_STARTUP_SPOTIFY_API_STATUS_NULL" xml:space="preserve">
142141
<value>An error occurred when trying to connect with Spotify: SpotifyAPI returned a null status.
@@ -225,4 +224,12 @@ Look for the blue icon in your system tray.</value>
225224
<data name="toastify_loading_spotify_9" type="System.Resources.ResXFileRef, System.Windows.Forms">
226225
<value>..\Resources\SystemTrayAnimation\toastify_loading_spotify_9.ico;System.Drawing.Icon, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
227226
</data>
227+
<data name="ERROR_STARTUP_SPOTIFY_API_CONNECTION_BLOCKED" xml:space="preserve">
228+
<value>An error occurred when trying to connect with Spotify.
229+
The firewall might have blocked the connection to "open.spotify.com".</value>
230+
</data>
231+
<data name="ERROR_STARTUP_SPOTIFY_API_CONNECTION_BLOCKED_HOSTS" xml:space="preserve">
232+
<value>An error occurred when trying to connect with Spotify.
233+
The host address "open.spotify.com" might have been redirected by the client. Check your "hosts" file (%WinDir%\System32\drivers\etc\hosts) and delete any entry containing "open.spotify.com".</value>
234+
</data>
228235
</root>

Toastify/Toastify.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
<Reference Include="System.Drawing" />
108108
<Reference Include="System.Management" />
109109
<Reference Include="System.Net" />
110+
<Reference Include="System.Net.Http" />
110111
<Reference Include="System.Windows.Forms" />
111112
<Reference Include="System.Xaml" />
112113
<Reference Include="System.Xml" />

Toastify/src/Core/ApplicationStartupException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public ApplicationStartupException(string message) : this(message, true)
1717
{
1818
}
1919

20-
public ApplicationStartupException(string message, bool spotifyStatus) : base($"{CreateMessage(spotifyStatus)}\n\nAdditional details\n{message}")
20+
public ApplicationStartupException(string message, bool spotifyStatus) : base($"{CreateMessage(spotifyStatus)}\nAdditional details:\n{message}")
2121
{
2222
}
2323

Toastify/src/Core/Spotify.cs

+35-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
using System.Diagnostics;
77
using System.Linq;
88
using System.Net;
9+
using System.Net.Http;
10+
using System.Net.Sockets;
11+
using System.Text.RegularExpressions;
912
using System.Threading;
1013
using System.Windows;
1114
using System.Windows.Threading;
@@ -174,8 +177,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
174177
logger.Error("Error while starting Spotify.", applicationStartupException);
175178

176179
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);
179181

180182
Analytics.TrackException(applicationStartupException, true);
181183
}
@@ -188,7 +190,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
188190
if (webException.Status == WebExceptionStatus.ProtocolError)
189191
status += $" ({(webException.Response as HttpWebResponse)?.StatusCode}, \"{(webException.Response as HttpWebResponse)?.StatusDescription}\")";
190192
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);
192194

193195
Analytics.TrackException(webException, true);
194196
}
@@ -198,7 +200,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
198200

199201
string errorMsg = Properties.Resources.ERROR_UNKNOWN;
200202
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);
202204

203205
Analytics.TrackException(e.Error, true);
204206
}
@@ -209,7 +211,7 @@ private void StartSpotify_WorkerTaskCompleted(object sender, RunWorkerCompletedE
209211

210212
string errorMsg = Properties.Resources.ERROR_STARTUP_SPOTIFY;
211213
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);
213215
}
214216

215217
// Terminate Toastify
@@ -288,6 +290,34 @@ private void ConnectWithSpotify(DoWorkEventArgs e)
288290
}
289291
catch (WebException ex)
290292
{
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+
291321
logger.Warn("WebException while connecting to Spotify.", ex);
292322
}
293323
} while (!connected && !signaled);

Toastify/src/Helpers/ScreenHelper.cs

+11-9
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,17 @@ public static Vector BringRectInsideWorkingArea(Rect rect)
4444
{
4545
Vector vector = new Vector();
4646
Rect totalRect = GetTotalWorkingArea();
47-
if (!totalRect.Contains(rect))
48-
{
49-
vector.X = rect.Right > totalRect.Right
50-
? totalRect.Right - rect.Right
51-
: rect.Left < totalRect.Left ? totalRect.Left - rect.Left : 0.0;
52-
vector.Y = rect.Bottom > totalRect.Bottom
53-
? totalRect.Bottom - rect.Bottom
54-
: rect.Top < totalRect.Top ? totalRect.Top - rect.Top : 0.0;
55-
}
47+
48+
if (totalRect.Contains(rect))
49+
return vector;
50+
51+
vector.X = rect.Right > totalRect.Right
52+
? totalRect.Right - rect.Right
53+
: rect.Left < totalRect.Left ? totalRect.Left - rect.Left : 0.0;
54+
vector.Y = rect.Bottom > totalRect.Bottom
55+
? totalRect.Bottom - rect.Bottom
56+
: rect.Top < totalRect.Top ? totalRect.Top - rect.Top : 0.0;
57+
5658
return vector;
5759
}
5860

0 commit comments

Comments
 (0)