Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
Fixed memory leak
Browse files Browse the repository at this point in the history
  • Loading branch information
MatrixDJ96 committed Mar 21, 2018
1 parent 3b39f30 commit e671b84
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 55 deletions.
21 changes: 13 additions & 8 deletions EZBlocker2/Custom/CustomWebClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,8 +25,7 @@ public int Timeout

private void Timer_Timeout(object sender, ElapsedEventArgs e)
{
timer = null;
handler = null;
timer.Stop();
CancelAsync();
}

Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions EZBlocker2/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
98 changes: 52 additions & 46 deletions EZBlocker2/Spotilocal/Spotilocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,44 @@ 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
{
if (e.Error == null)
{
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<CsrfToken>(result).Token;
else if (type == typeof(OAuthToken))
oauth = JsonConvert.DeserializeObject<OAuthToken>(result).T;
csrf = JsonConvert.DeserializeObject<CsrfToken>(result).Token;
GetStatus();
}
else if (type == typeof(OAuthToken))
{
oauth = JsonConvert.DeserializeObject<OAuthToken>(result).T;
GetStatus();
}
else if (type == typeof(SpotilocalStatus))
Expand All @@ -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;
Expand All @@ -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++;
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion EZBlocker2/UpdateForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e671b84

Please sign in to comment.