mirror of
https://github.com/MrUnknownDE/VRCX.git
synced 2026-04-19 14:53:50 +02:00
save cookies periodically
This commit is contained in:
81
WebApi.cs
81
WebApi.cs
@@ -4,6 +4,7 @@ using System.Collections.Generic;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Runtime.Serialization.Formatters.Binary;
|
||||||
|
using System.Threading;
|
||||||
|
|
||||||
namespace VRCX
|
namespace VRCX
|
||||||
{
|
{
|
||||||
@@ -11,6 +12,8 @@ namespace VRCX
|
|||||||
{
|
{
|
||||||
public static readonly WebApi Instance;
|
public static readonly WebApi Instance;
|
||||||
private CookieContainer _cookieContainer;
|
private CookieContainer _cookieContainer;
|
||||||
|
private bool _cookieDirty;
|
||||||
|
private Timer _cookieSaveTimer;
|
||||||
|
|
||||||
static WebApi()
|
static WebApi()
|
||||||
{
|
{
|
||||||
@@ -22,24 +25,48 @@ namespace VRCX
|
|||||||
public WebApi()
|
public WebApi()
|
||||||
{
|
{
|
||||||
_cookieContainer = new CookieContainer();
|
_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()
|
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.ExecuteNonQuery("CREATE TABLE IF NOT EXISTS `cookies` (`key` TEXT PRIMARY KEY, `value` TEXT)");
|
||||||
SQLite.Instance.Execute((values) =>
|
SQLite.Instance.Execute((values) =>
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
try
|
using (var stream = new MemoryStream(Convert.FromBase64String((string)values[0])))
|
||||||
{
|
{
|
||||||
using (var stream = new MemoryStream(Convert.FromBase64String((string)values[0])))
|
_cookieContainer = (CookieContainer)new BinaryFormatter().Deserialize(stream);
|
||||||
{
|
|
||||||
_cookieContainer = (CookieContainer)new BinaryFormatter().Deserialize(stream);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch
|
}
|
||||||
{
|
catch
|
||||||
}
|
{
|
||||||
},
|
}
|
||||||
|
},
|
||||||
"SELECT `value` FROM `cookies` WHERE `key` = @key",
|
"SELECT `value` FROM `cookies` WHERE `key` = @key",
|
||||||
new Dictionary<string, object>() {
|
new Dictionary<string, object>() {
|
||||||
{"@key", "default"}
|
{"@key", "default"}
|
||||||
@@ -47,8 +74,12 @@ namespace VRCX
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void Exit()
|
internal void SaveCookies()
|
||||||
{
|
{
|
||||||
|
if (_cookieDirty == true)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var memoryStream = new MemoryStream())
|
using (var memoryStream = new MemoryStream())
|
||||||
@@ -62,17 +93,13 @@ namespace VRCX
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
_cookieDirty = false;
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ClearCookies()
|
|
||||||
{
|
|
||||||
_cookieContainer = new CookieContainer();
|
|
||||||
}
|
|
||||||
|
|
||||||
#pragma warning disable CS4014
|
#pragma warning disable CS4014
|
||||||
public async void Execute(IDictionary<string, object> options, IJavascriptCallback callback)
|
public async void Execute(IDictionary<string, object> options, IJavascriptCallback callback)
|
||||||
{
|
{
|
||||||
@@ -123,16 +150,22 @@ namespace VRCX
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
using (var response = await request.GetResponseAsync() as HttpWebResponse)
|
using (var response = await request.GetResponseAsync() as HttpWebResponse)
|
||||||
using (var stream = response.GetResponseStream())
|
|
||||||
using (var streamReader = new StreamReader(stream))
|
|
||||||
{
|
{
|
||||||
if (callback.CanExecute == true)
|
if (response.Headers["Set-Cookie"] != null)
|
||||||
{
|
{
|
||||||
callback.ExecuteAsync(null, new
|
_cookieDirty = true;
|
||||||
|
}
|
||||||
|
using (var stream = response.GetResponseStream())
|
||||||
|
using (var streamReader = new StreamReader(stream))
|
||||||
|
{
|
||||||
|
if (callback.CanExecute == true)
|
||||||
{
|
{
|
||||||
data = await streamReader.ReadToEndAsync(),
|
callback.ExecuteAsync(null, new
|
||||||
status = response.StatusCode
|
{
|
||||||
});
|
data = await streamReader.ReadToEndAsync(),
|
||||||
|
status = response.StatusCode
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -140,6 +173,10 @@ namespace VRCX
|
|||||||
{
|
{
|
||||||
if (webException.Response is HttpWebResponse response)
|
if (webException.Response is HttpWebResponse response)
|
||||||
{
|
{
|
||||||
|
if (response.Headers["Set-Cookie"] != null)
|
||||||
|
{
|
||||||
|
_cookieDirty = true;
|
||||||
|
}
|
||||||
using (var stream = response.GetResponseStream())
|
using (var stream = response.GetResponseStream())
|
||||||
using (var streamReader = new StreamReader(stream))
|
using (var streamReader = new StreamReader(stream))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user