mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-19 23:03:51 +02:00
save cookies periodically
This commit is contained in:
49
WebApi.cs
49
WebApi.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Threading;
|
||||
|
||||
namespace VRCX
|
||||
{
|
||||
@@ -11,6 +12,8 @@ namespace VRCX
|
||||
{
|
||||
public static readonly WebApi Instance;
|
||||
private CookieContainer _cookieContainer;
|
||||
private bool _cookieDirty;
|
||||
private Timer _cookieSaveTimer;
|
||||
|
||||
static WebApi()
|
||||
{
|
||||
@@ -22,9 +25,33 @@ namespace VRCX
|
||||
public WebApi()
|
||||
{
|
||||
_cookieContainer = new CookieContainer();
|
||||
_cookieSaveTimer = new Timer(CookieSaveTimerCallback, this, -1, -1);
|
||||
}
|
||||
|
||||
private static void CookieSaveTimerCallback(object state)
|
||||
{
|
||||
var self = (WebApi)state;
|
||||
self.SaveCookies();
|
||||
}
|
||||
|
||||
internal void Init()
|
||||
{
|
||||
LoadCookies();
|
||||
_cookieSaveTimer.Change(1000, 1000);
|
||||
}
|
||||
|
||||
internal void Exit()
|
||||
{
|
||||
_cookieSaveTimer.Change(-1, -1);
|
||||
SaveCookies();
|
||||
}
|
||||
|
||||
public void ClearCookies()
|
||||
{
|
||||
_cookieContainer = new CookieContainer();
|
||||
}
|
||||
|
||||
internal void LoadCookies()
|
||||
{
|
||||
SQLite.Instance.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS `cookies` (`key` TEXT PRIMARY KEY, `value` TEXT)");
|
||||
SQLite.Instance.Execute((values) =>
|
||||
@@ -47,8 +74,12 @@ namespace VRCX
|
||||
);
|
||||
}
|
||||
|
||||
internal void Exit()
|
||||
internal void SaveCookies()
|
||||
{
|
||||
if (_cookieDirty == true)
|
||||
{
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
using (var memoryStream = new MemoryStream())
|
||||
@@ -62,17 +93,13 @@ namespace VRCX
|
||||
}
|
||||
);
|
||||
}
|
||||
_cookieDirty = false;
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public void ClearCookies()
|
||||
{
|
||||
_cookieContainer = new CookieContainer();
|
||||
}
|
||||
|
||||
#pragma warning disable CS4014
|
||||
public async void Execute(IDictionary<string, object> options, IJavascriptCallback callback)
|
||||
{
|
||||
@@ -123,6 +150,11 @@ namespace VRCX
|
||||
try
|
||||
{
|
||||
using (var response = await request.GetResponseAsync() as HttpWebResponse)
|
||||
{
|
||||
if (response.Headers["Set-Cookie"] != null)
|
||||
{
|
||||
_cookieDirty = true;
|
||||
}
|
||||
using (var stream = response.GetResponseStream())
|
||||
using (var streamReader = new StreamReader(stream))
|
||||
{
|
||||
@@ -136,10 +168,15 @@ namespace VRCX
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (WebException webException)
|
||||
{
|
||||
if (webException.Response is HttpWebResponse response)
|
||||
{
|
||||
if (response.Headers["Set-Cookie"] != null)
|
||||
{
|
||||
_cookieDirty = true;
|
||||
}
|
||||
using (var stream = response.GetResponseStream())
|
||||
using (var streamReader = new StreamReader(stream))
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user