Proxy settings

This commit is contained in:
Natsumi
2024-07-17 10:31:18 +12:00
parent a4d03c7e00
commit e26aed5c6a
11 changed files with 114 additions and 26 deletions

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Net;
using CefSharp;
using CefSharp.SchemeHandler;
using CefSharp.WinForms;
@@ -9,6 +10,7 @@ namespace VRCX
public class CefService
{
public static readonly CefService Instance;
private static readonly NLog.Logger logger = NLog.LogManager.GetLogger("VRCX");
static CefService()
{
@@ -51,8 +53,14 @@ namespace VRCX
cefSettings.CefCommandLineArgs.Add("disable-web-security");
cefSettings.SetOffScreenRenderingBestPerformanceArgs(); // causes white screen sometimes?
if (WebApi.ProxySet)
{
cefSettings.CefCommandLineArgs["proxy-server"] = WebApi.ProxyUrl;
}
if (Program.LaunchDebug)
{
logger.Info("Debug mode enabled");
cefSettings.RemoteDebuggingPort = 8088;
cefSettings.CefCommandLineArgs["remote-allow-origins"] = "*";
}

View File

@@ -8,11 +8,10 @@ using System.Threading.Tasks;
namespace VRCX
{
class ImageCache
internal static class ImageCache
{
private static readonly string cacheLocation = Path.Combine(Program.AppDataDirectory, "ImageCache");
private static readonly HttpClientHandler httpClientHandler = new HttpClientHandler(){ Proxy = WebApi.Proxy };
private static readonly HttpClient httpClient = new HttpClient(httpClientHandler);
private static readonly string cacheLocation;
private static readonly HttpClient httpClient;
private static readonly List<string> _imageHosts =
[
"api.vrchat.cloud",
@@ -21,6 +20,16 @@ namespace VRCX
"assets.vrchat.com"
];
static ImageCache()
{
cacheLocation = Path.Combine(Program.AppDataDirectory, "ImageCache");
var httpClientHandler = new HttpClientHandler();
if (WebApi.ProxySet)
httpClientHandler.Proxy = WebApi.Proxy;
httpClient = new HttpClient(httpClientHandler);
}
public static async Task<string> GetImage(string url, string fileId, string version)
{
var directoryLocation = Path.Combine(cacheLocation, fileId);
@@ -74,9 +83,9 @@ namespace VRCX
private static void CleanImageCache()
{
DirectoryInfo dirInfo = new DirectoryInfo(cacheLocation);
var dirInfo = new DirectoryInfo(cacheLocation);
var folders = dirInfo.GetDirectories().OrderByDescending(p => p.LastWriteTime).Skip(1000);
foreach (DirectoryInfo folder in folders)
foreach (var folder in folders)
{
folder.Delete(true);
}

View File

@@ -58,12 +58,6 @@ namespace VRCX
Dock = DockStyle.Fill
};
string? proxyUrl = VRCXStorage.Instance.Get("VRCX_ProxyServer");
if (!string.IsNullOrEmpty(proxyUrl))
{
WebApi.Proxy = new WebProxy(proxyUrl);
}
Browser.IsBrowserInitializedChanged += (A, B) =>
{
if (Program.LaunchDebug)

View File

@@ -167,8 +167,7 @@ namespace VRCX
Application.SetCompatibleTextRenderingDefault(false);
logger.Info("{0} Starting...", Version);
ProcessMonitor.Instance.Init();
SQLiteLegacy.Instance.Init();
AppApi.Instance.Init();

View File

@@ -19,6 +19,7 @@ namespace VRCX
internal class StartupArgs
{
public static string LaunchCommand;
public static string ProxyUrl;
public static Process[] processList;
public static void ArgsCheck()
@@ -52,10 +53,7 @@ namespace VRCX
Program.LaunchDebug = true;
if (arg.Length >= 16 && arg.Substring(0, 14) == "--proxy-server")
{
string proxyUrl = arg.Substring(15).Replace("'", string.Empty).Replace("\"", string.Empty);
WebApi.Proxy = new WebProxy(proxyUrl);
}
ProxyUrl = arg.Substring(15).Replace("'", string.Empty).Replace("\"", string.Empty);
}
var type = CommandLineArgsParser.GetArgumentValue(args, CefSharpArguments.SubProcessTypeArgument);

View File

@@ -56,6 +56,7 @@ namespace VRCX
MessageBox.Show(e.ToString(), "Update failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public static void DownloadInstallRedist()
{
try
@@ -78,7 +79,6 @@ namespace VRCX
}
}
private static string DownloadFile(string fileUrl)
{
HttpClient client = new HttpClient();

View File

@@ -16,10 +16,14 @@ namespace VRCX
public class WebApi
{
public static readonly WebApi Instance;
public static bool ProxySet;
public static string ProxyUrl = "";
public static IWebProxy Proxy = WebRequest.DefaultWebProxy;
public CookieContainer _cookieContainer;
private bool _cookieDirty;
private Timer _timer;
public static IWebProxy? Proxy = WebRequest.DefaultWebProxy;
static WebApi()
{
@@ -47,10 +51,30 @@ namespace VRCX
internal void Init()
{
SetProxy();
LoadCookies();
_timer.Change(1000, 1000);
}
private void SetProxy()
{
if (!string.IsNullOrEmpty(StartupArgs.ProxyUrl))
ProxyUrl = StartupArgs.ProxyUrl;
if (string.IsNullOrEmpty(ProxyUrl))
{
var proxyUrl = VRCXStorage.Instance.Get("VRCX_ProxyServer");
if (!string.IsNullOrEmpty(proxyUrl))
ProxyUrl = proxyUrl;
}
if (string.IsNullOrEmpty(ProxyUrl))
return;
ProxySet = true;
Proxy = new WebProxy(ProxyUrl);
}
internal void Exit()
{
_timer.Change(-1, -1);
@@ -145,7 +169,9 @@ namespace VRCX
private static async Task LegacyImageUpload(HttpWebRequest request, IDictionary<string, object> options)
{
request.Proxy = Proxy;
if (ProxySet)
request.Proxy = Proxy;
request.Method = "POST";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
request.ContentType = "multipart/form-data; boundary=" + boundary;
@@ -190,7 +216,9 @@ namespace VRCX
private static async Task UploadFilePut(HttpWebRequest request, IDictionary<string, object> options)
{
request.Proxy = Proxy;
if (ProxySet)
request.Proxy = Proxy;
request.Method = "PUT";
request.ContentType = options["fileMIME"] as string;
var fileData = options["fileData"] as string;
@@ -205,7 +233,9 @@ namespace VRCX
private static async Task ImageUpload(HttpWebRequest request, IDictionary<string, object> options)
{
request.Proxy = Proxy;
if (ProxySet)
request.Proxy = Proxy;
request.Method = "POST";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
request.ContentType = "multipart/form-data; boundary=" + boundary;
@@ -259,7 +289,9 @@ namespace VRCX
try
{
var request = WebRequest.CreateHttp((string)options["url"]);
request.Proxy = Proxy;
if (ProxySet)
request.Proxy = Proxy;
request.CookieContainer = _cookieContainer;
request.KeepAlive = true;
request.UserAgent = Program.Version;