提交Unity 联机Pro

This commit is contained in:
PC-20230316NUNE\Administrator
2024-08-17 14:27:18 +08:00
parent f00193b000
commit 894100ae37
7448 changed files with 854473 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
#if !BESTHTTP_DISABLE_PROXY && UNITY_ANDROID && !UNITY_EDITOR
using System;
using UnityEngine;
namespace BestHTTP.Proxies.Autodetect
{
public sealed class AndroidProxyDetector : IProxyDetector
{
private const string ClassPath = "com.besthttp.proxy.ProxyFinder";
Proxy IProxyDetector.GetProxy(HTTPRequest request)
{
try
{
var proxyUrl = FindFor(request.CurrentUri.ToString());
HTTPManager.Logger.Information(nameof(AndroidProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - FindFor returned with proxyUrl: '{proxyUrl}'", request.Context);
if (proxyUrl == null)
return null;
if (proxyUrl.StartsWith("socks://", StringComparison.OrdinalIgnoreCase))
{
return new SOCKSProxy(new Uri(proxyUrl), null);
}
else if (proxyUrl.StartsWith("http://", StringComparison.OrdinalIgnoreCase))
{
return new HTTPProxy(new Uri(proxyUrl));
}
else
{
HTTPManager.Logger.Warning(nameof(AndroidProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - FindFor returned with unknown format. proxyUrl: '{proxyUrl}'", request.Context);
}
}
catch (Exception ex)
{
HTTPManager.Logger.Exception(nameof(AndroidProxyDetector), nameof(IProxyDetector.GetProxy), ex, request.Context);
}
return null;
}
private string FindFor(string uriStr) => Call<string>("FindFor", uriStr);
private static T Call<T>(string methodName, params object[] args)
{
bool isMainThread = HTTPUpdateDelegator.Instance.IsMainThread();
try
{
if (!isMainThread)
AndroidJNI.AttachCurrentThread();
using (var javaClass = new AndroidJavaClass(ClassPath))
return javaClass.CallStatic<T>(methodName, args);
}
finally
{
if (!isMainThread)
AndroidJNI.DetachCurrentThread();
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ac7faed5a1e9336479173f34a38a8965
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,80 @@
#if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using System.Linq;
using BestHTTP.Connections;
// Examples on proxy strings:
// https://gist.github.com/yougg/5d2b3353fc5e197a0917aae0b3287d64
namespace BestHTTP.Proxies.Autodetect
{
/// <summary>
/// Based on https://curl.se/docs/manual.html "Environment Variables" section.
/// </summary>
public sealed class EnvironmentProxyDetector : IProxyDetector
{
private Proxy _cachedProxy;
Proxy IProxyDetector.GetProxy(HTTPRequest request)
{
if (this._cachedProxy != null)
return this._cachedProxy;
string proxyUrl = null;
if (HTTPProtocolFactory.IsSecureProtocol(request.CurrentUri))
{
proxyUrl = GetEnv("HTTPS_PROXY");
HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - HTTPS_PROXY: '{proxyUrl}'", request.Context);
}
else
{
proxyUrl = GetEnv("HTTP_PROXY");
HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - HTTP_PROXY: '{proxyUrl}'", request.Context);
}
if (proxyUrl == null)
{
proxyUrl = GetEnv("ALL_PROXY");
}
else
HTTPManager.Logger.Information(nameof(EnvironmentProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - ALL_PROXY: '{proxyUrl}'", request.Context);
if (string.IsNullOrEmpty(proxyUrl))
return null;
// if the url is just a host[:port], add the http:// part too. Checking for :// should keep and treat the socks:// scheme too.
if (proxyUrl.IndexOf("://") == -1 && !proxyUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
proxyUrl = "http://" + proxyUrl;
string exceptionList = null;
try
{
var proxyUri = new Uri(proxyUrl);
Proxy proxy = null;
if (proxyUri.Scheme.StartsWith("socks", StringComparison.OrdinalIgnoreCase))
proxy = new SOCKSProxy(proxyUri, null);
else
proxy = new HTTPProxy(proxyUri);
// A comma-separated list of host names that should not go through any proxy is set in (only an asterisk, * matches all hosts)
exceptionList = GetEnv("NO_PROXY");
if (!string.IsNullOrEmpty(exceptionList))
proxy.Exceptions = exceptionList.Split(';').ToList<string>();
return this._cachedProxy = proxy;
}
catch (Exception ex)
{
HTTPManager.Logger.Exception(nameof(EnvironmentProxyDetector), $"GetProxy - proxyUrl: '{proxyUrl}', exceptionList: '{exceptionList}'", ex, request.Context);
}
return null;
}
string GetEnv(string key) => System.Environment.GetEnvironmentVariable(key) ?? System.Environment.GetEnvironmentVariable(key.ToLowerInvariant());
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 152166acb3a569d45a58d0fa2b33eec3
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,62 @@
#if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
namespace BestHTTP.Proxies.Autodetect
{
/// <summary>
/// This is a detector using the .net framework's implementation. It might work not just under Windows but MacOS and Linux too.
/// </summary>
/// <see cref="https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient.defaultproxy?view=net-6.0"/>
public sealed class FrameworkProxyDetector : IProxyDetector
{
Proxy IProxyDetector.GetProxy(HTTPRequest request)
{
var detectedProxy = System.Net.WebRequest.GetSystemWebProxy() as System.Net.WebProxy;
if (detectedProxy != null && detectedProxy.Address != null)
{
var proxyUri = detectedProxy.GetProxy(request.CurrentUri);
if (proxyUri != null && !proxyUri.Equals(request.CurrentUri))
{
if (proxyUri.Scheme.StartsWith("socks", StringComparison.OrdinalIgnoreCase))
{
return SetExceptionList(new SOCKSProxy(proxyUri, null), detectedProxy);
}
else if (proxyUri.Scheme.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{
return SetExceptionList(new HTTPProxy(proxyUri), detectedProxy);
}
else
{
HTTPManager.Logger.Warning(nameof(FrameworkProxyDetector), $"{nameof(IProxyDetector.GetProxy)} - FindFor returned with unknown format. proxyUri: '{proxyUri}'", request.Context);
}
}
}
return null;
}
private Proxy SetExceptionList(Proxy proxy, System.Net.WebProxy detectedProxy)
{
if (detectedProxy.BypassProxyOnLocal)
{
proxy.Exceptions = proxy.Exceptions ?? new System.Collections.Generic.List<string>();
proxy.Exceptions.Add("localhost");
proxy.Exceptions.Add("127.0.0.1");
}
// TODO: use BypassList to put more entries to the Exceptions list.
// But because BypassList contains regex strings, we either
// 1.) store and use regex strings in the Exception list (not backward compatible)
// 2.) store non-regex strings but create a new list for regex
// 3.) detect if the stored entry in the Exceptions list is regex or not and use it accordingly
// "^.*\\.httpbin\\.org$"
// https://github.com/Benedicht/BestHTTP-Issues/issues/141
return proxy;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 58ef74f62e307544fa7e434332a6d3ac
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
#if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
namespace BestHTTP.Proxies.Autodetect
{
/// <summary>
/// This one just returns with HTTPManager.Proxy,
/// so when ProgrammaticallyAddedProxyDetector is used in the first place for the ProxyDetector,
/// HTTPManager.Proxy gets the highest priority.
/// </summary>
public sealed class ProgrammaticallyAddedProxyDetector : IProxyDetector
{
Proxy IProxyDetector.GetProxy(HTTPRequest request) => HTTPManager.Proxy;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ad0e404074491274e83de0ce83db8a97
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,145 @@
#if !BESTHTTP_DISABLE_PROXY && (!UNITY_WEBGL || UNITY_EDITOR)
using System;
using BestHTTP.Core;
namespace BestHTTP.Proxies.Autodetect
{
public interface IProxyDetector
{
Proxy GetProxy(HTTPRequest request);
}
public enum ProxyDetectionMode
{
/// <summary>
/// In Continouos mode the ProxyDetector will check for a proxy for every request.
/// </summary>
Continouos,
/// <summary>
/// This mode will cache the first Proxy found and use it for consecutive requests.
/// </summary>
CacheFirstFound
}
public sealed class ProxyDetector
{
public static IProxyDetector[] GetDefaultDetectors() => new IProxyDetector[] {
// HTTPManager.Proxy has the highest priority
new ProgrammaticallyAddedProxyDetector(),
// then comes the environment set
new EnvironmentProxyDetector(),
// .net framework's detector
new FrameworkProxyDetector(),
#if UNITY_ANDROID && !UNITY_EDITOR
new AndroidProxyDetector(),
#endif
};
private IProxyDetector[] _proxyDetectors;
private ProxyDetectionMode _detectionMode;
private bool _attached;
public ProxyDetector()
: this(ProxyDetectionMode.CacheFirstFound, GetDefaultDetectors())
{ }
public ProxyDetector(ProxyDetectionMode detectionMode)
:this(detectionMode, GetDefaultDetectors())
{ }
public ProxyDetector(ProxyDetectionMode detectionMode, IProxyDetector[] proxyDetectors)
{
this._detectionMode = detectionMode;
this._proxyDetectors = proxyDetectors;
if (this._proxyDetectors != null)
Reattach();
}
public void Reattach()
{
HTTPManager.Logger.Information(nameof(ProxyDetector), $"{nameof(Reattach)}({this._attached})");
if (!this._attached)
{
RequestEventHelper.OnEvent += OnRequestEvent;
this._attached = true;
}
}
/// <summary>
/// Call Detach() to disable ProxyDetector's logic to find and set a proxy.
/// </summary>
public void Detach()
{
HTTPManager.Logger.Information(nameof(ProxyDetector), $"{nameof(Detach)}({this._attached})");
if (this._attached)
{
RequestEventHelper.OnEvent -= OnRequestEvent;
this._attached = false;
}
}
private void OnRequestEvent(RequestEventInfo @event)
{
// The Resend event is raised for every request when it's queued up (sent or redirected).
if (@event.Event == RequestEvents.Resend && @event.SourceRequest.Proxy == null)
{
Uri uri = @event.SourceRequest.CurrentUri;
HTTPManager.Logger.Information(nameof(ProxyDetector), $"OnRequestEvent(RequestEvents.Resend) uri: '{uri}'", @event.SourceRequest.Context);
try
{
foreach (var detector in this._proxyDetectors)
{
if (detector == null)
continue;
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"Calling {detector.GetType().Name}'s GetProxy", @event.SourceRequest.Context);
var proxy = detector.GetProxy(@event.SourceRequest);
if (proxy != null && proxy.UseProxyForAddress(uri))
{
if (HTTPManager.Logger.Level == Logger.Loglevels.All)
HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"[{detector.GetType().Name}] Proxy found: {proxy.Address} ", @event.SourceRequest.Context);
switch (this._detectionMode)
{
case ProxyDetectionMode.Continouos:
@event.SourceRequest.Proxy = proxy;
break;
case ProxyDetectionMode.CacheFirstFound:
HTTPManager.Proxy = @event.SourceRequest.Proxy = proxy;
HTTPManager.Logger.Verbose(nameof(ProxyDetector), $"Proxy cached in HTTPManager.Proxy!", @event.SourceRequest.Context);
Detach();
break;
}
return;
}
}
HTTPManager.Logger.Information(nameof(ProxyDetector), $"No Proxy for '{uri}'.", @event.SourceRequest.Context);
}
catch (Exception ex)
{
if (HTTPManager.Logger.Level == BestHTTP.Logger.Loglevels.All)
HTTPManager.Logger.Exception(nameof(ProxyDetector), $"GetProxyFor({@event.SourceRequest.CurrentUri})", ex, @event.SourceRequest.Context);
}
}
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f11e35f15ae944141a7fe661e2783ae4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: