diff --git a/EZBlocker2/Custom/CustomWebClient.cs b/EZBlocker2/Custom/CustomWebClient.cs index 905c7d6..b25ad06 100644 --- a/EZBlocker2/Custom/CustomWebClient.cs +++ b/EZBlocker2/Custom/CustomWebClient.cs @@ -6,10 +6,17 @@ namespace EZBlocker2 { class CustomWebClient : WebClient { - private int timeout = 0; + private int timeout = 60000; private Timer timer = null; private ElapsedEventHandler handler = null; + public CustomWebClient() + { + timer = new Timer(timeout); + handler = Timer_Timeout; + timer.Elapsed += handler; + } + public int Timeout { get => timeout; @@ -18,8 +25,7 @@ public int Timeout private void Timer_Timeout(object sender, ElapsedEventArgs e) { - timer = null; - handler = null; + timer.Stop(); CancelAsync(); } @@ -33,11 +39,10 @@ protected override WebRequest GetWebRequest(Uri address) request.Timeout = timeout; // Async timeout - timer = new Timer(timeout); - handler = Timer_Timeout; - - timer.Elapsed += handler; - timer.Enabled = true; + if (timer.Interval != timeout) + timer.Interval = timeout; + + timer.Start(); } return request; diff --git a/EZBlocker2/Program.cs b/EZBlocker2/Program.cs index 080eedc..26645a5 100644 --- a/EZBlocker2/Program.cs +++ b/EZBlocker2/Program.cs @@ -23,6 +23,9 @@ static class Program public static readonly string ezBlockerExe = Path.GetFileName(ezBlockerFullExe); public static readonly string ezBlockerLog = Path.GetFileNameWithoutExtension(ezBlockerFullExe) + ".log"; + // Timeout + public static int timeout = 3000; + // StringComparison public static StringComparison comp = StringComparison.OrdinalIgnoreCase; diff --git a/EZBlocker2/Spotilocal/Spotilocal.cs b/EZBlocker2/Spotilocal/Spotilocal.cs index 84bfbb6..6dfc474 100644 --- a/EZBlocker2/Spotilocal/Spotilocal.cs +++ b/EZBlocker2/Spotilocal/Spotilocal.cs @@ -10,21 +10,29 @@ namespace EZBlocker2 { internal class Spotilocal { - private static readonly string user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"; + private static readonly string user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"; private static readonly string open_spotify = "https://open.spotify.com"; private static readonly string localhost = "http://127.0.0.1"; + + private static int port = 0; - private static readonly int timeout = 3000; - public static int Timeout => timeout; + private static string oauth = null; + private static string csrf = null; - private static int port = 0; + private static WebClient clientSecondary = null; + private static WebClient clientPrimary = null; + + public static CustomEmitter emitter = null; - private static string oauth; - private static string csrf; + static Spotilocal() + { + InitializeClient(ref clientPrimary, typeof(SpotilocalStatus)); + ((CustomWebClient)clientPrimary).Timeout = timeout; - public static CustomEmitter emitter = new CustomEmitter(); + emitter = new CustomEmitter(); + } - private static void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e, Type type = null) + private static void Client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e, Type type) { try { @@ -32,12 +40,14 @@ private static void Client_DownloadDataCompleted(object sender, DownloadDataComp { string result = Encoding.UTF8.GetString(e.Result); - if (type == typeof(CsrfToken) || type == typeof(OAuthToken)) + if (type == typeof(CsrfToken)) { - if (type == typeof(CsrfToken)) - csrf = JsonConvert.DeserializeObject(result).Token; - else if (type == typeof(OAuthToken)) - oauth = JsonConvert.DeserializeObject(result).T; + csrf = JsonConvert.DeserializeObject(result).Token; + GetStatus(); + } + else if (type == typeof(OAuthToken)) + { + oauth = JsonConvert.DeserializeObject(result).T; GetStatus(); } else if (type == typeof(SpotilocalStatus)) @@ -54,10 +64,20 @@ private static void Client_DownloadDataCompleted(object sender, DownloadDataComp } } + private static void InitializeClient(ref WebClient client, Type type = null) + { + client = new CustomWebClient(); + client.Headers.Add("Origin", open_spotify); + client.Headers.Add("User-Agent", user_agent); + + if (type != null) + client.DownloadDataCompleted += (sender, e) => Client_DownloadDataCompleted(sender, e, type); + } + private static async void GetPort() { - WebClient client = new CustomWebClient(); - ((CustomWebClient)client).Timeout = 50; + InitializeClient(ref clientSecondary); + ((CustomWebClient)clientSecondary).Timeout = 50; int i_port = 4370; int f_port = 4390; @@ -66,12 +86,11 @@ private static async void GetPort() { try { - var page = await client.DownloadDataTaskAsync(new Uri(localhost + ":" + i_port.ToString())); + var page = await clientSecondary.DownloadDataTaskAsync(new Uri(localhost + ":" + i_port.ToString())); } - catch (Exception ex) + catch (WebException ex) { - WebException webException = (WebException)ex; - if (webException.Status == WebExceptionStatus.ProtocolError) + if (ex.Status == WebExceptionStatus.ProtocolError) break; else i_port++; @@ -89,43 +108,30 @@ private static async void GetPort() private static void GetCSRF() { - WebClient client = new CustomWebClient(); - ((CustomWebClient)client).Timeout = timeout; - client.Headers.Add("Origin", open_spotify); + InitializeClient(ref clientSecondary, typeof(CsrfToken)); + ((CustomWebClient)clientSecondary).Timeout = timeout; - client.DownloadDataCompleted += (sender, e) => Client_DownloadDataCompleted(sender, e, typeof(CsrfToken)); - client.DownloadDataAsync(new Uri(localhost + ":" + port.ToString() + "/simplecsrf/token.json")); + clientSecondary.DownloadDataAsync(new Uri(localhost + ":" + port.ToString() + "/simplecsrf/token.json")); } private static void GetOAuth() { - WebClient client = new CustomWebClient(); - ((CustomWebClient)client).Timeout = timeout; - client.Headers.Add("User-Agent", user_agent); + InitializeClient(ref clientSecondary, typeof(OAuthToken)); + ((CustomWebClient)clientSecondary).Timeout = timeout; - client.DownloadDataCompleted += (sender, e) => Client_DownloadDataCompleted(sender, e, typeof(OAuthToken)); - client.DownloadDataAsync(new Uri(open_spotify + "/token")); + clientSecondary.DownloadDataAsync(new Uri(open_spotify + "/token")); } public static void GetStatus() { - if (port == 0 || csrf == null || oauth == null) - { - if (port == 0) - GetPort(); - else if (csrf == null) - GetCSRF(); - else if (oauth == null) - GetOAuth(); - return; - } - - WebClient client = new CustomWebClient(); - ((CustomWebClient)client).Timeout = timeout; - client.Headers.Add("Origin", open_spotify); - - client.DownloadDataCompleted += (sender, e) => Client_DownloadDataCompleted(sender, e, typeof(SpotilocalStatus)); - client.DownloadDataAsync(new Uri(localhost + ":" + port.ToString() + "/remote/status.json" + "?csrf=" + csrf + "&oauth=" + oauth)); + if (port == 0) + GetPort(); + else if (csrf == null) + GetCSRF(); + else if (oauth == null) + GetOAuth(); + else + clientPrimary.DownloadDataAsync(new Uri(localhost + ":" + port.ToString() + "/remote/status.json" + "?csrf=" + csrf + "&oauth=" + oauth)); } public static void WriteLog(Exception ex) diff --git a/EZBlocker2/UpdateForm.cs b/EZBlocker2/UpdateForm.cs index 4e015af..c57cb68 100644 --- a/EZBlocker2/UpdateForm.cs +++ b/EZBlocker2/UpdateForm.cs @@ -105,7 +105,7 @@ private void UpdateForm_Load(object sender, EventArgs e) DeleteFile(ezBlockerFullExeOld); WebRequest request = WebRequest.Create(website); - request.Timeout = Spotilocal.Timeout; + request.Timeout = timeout; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream);