using System; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.Net.Sockets; using System.Reflection; using System.Runtime.CompilerServices; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading; using System.Threading.Tasks; using Emby.Model.ProcessRun; using Emby.Server.Implementations.Devices; using Emby.Server.Implementations.HttpServer; using Emby.Server.MediaEncoding.Codecs.Common.Interfaces; using Emby.Server.MediaEncoding.Unified.Ffmpeg; using Emby.Sqlite; using MediaBrowser.Common; using MediaBrowser.Common.Configuration; using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; using MediaBrowser.Controller; using MediaBrowser.Controller.Channels; using MediaBrowser.Controller.Codecs; using MediaBrowser.Controller.Collections; using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Connect; using MediaBrowser.Controller.Devices; using MediaBrowser.Controller.Drawing; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.IO; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.LiveTv; using MediaBrowser.Controller.MediaEncoding; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Notifications; using MediaBrowser.Controller.Persistence; using MediaBrowser.Controller.Playlists; using MediaBrowser.Controller.Plugins; using MediaBrowser.Controller.Providers; using MediaBrowser.Controller.Security; using MediaBrowser.Controller.Session; using MediaBrowser.Controller.Subtitles; using MediaBrowser.Controller.TV; using MediaBrowser.Model.Activity; using MediaBrowser.Model.Cryptography; using MediaBrowser.Model.Diagnostics; using MediaBrowser.Model.Events; using MediaBrowser.Model.Globalization; using MediaBrowser.Model.IO; using MediaBrowser.Model.Logging; using MediaBrowser.Model.Net; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.System; using MediaBrowser.Model.Tasks; using MediaBrowser.Model.Text; using MediaBrowser.Model.Updates; using SimpleInjector; using MediaBrowser.Controller.Entities; using System.Xml; using System.Linq; using System.Collections.Generic; using System.IO; namespace Emby.Server.Implementations { // Token: 0x0200015E RID: 350 public abstract partial class ApplicationHost : IServerApplicationHost, IApplicationHost, IDisposable { // Token: 0x06000AC7 RID: 2759 RVA: 0x0002EE97 File Offset: 0x0002D097 protected virtual HttpClientHandler CreateHttpClientHandler() { return new ProxyHttpClientHandler(); } } public class ProxyHttpClientHandler : HttpClientHandler { public ProxyHttpClientHandler() { ServerCertificateCustomValidationCallback = (message, certificate2, arg3, arg4) => true; UseProxy = true; } protected override Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (!(request.Headers.UserAgent?.Count() > 0)) { request.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"); } // 获取插件xml配置 string ProxyType = null; string JsProxy = null; List JsProxyBypass = null; string path = CollectionFolder.ConfigurationManager.ApplicationPaths.PluginConfigurationsPath + "\\EmbyJsProxy.xml"; if (File.Exists(path)) { XmlTextReader xtr = new XmlTextReader(path); while (xtr.Read()) { if (xtr.NodeType == XmlNodeType.Element) { string name = xtr.LocalName; if (name == "ProxyType") { ProxyType = xtr.ReadString(); } else if (name == "JsProxy") { JsProxy = xtr.ReadString(); } else if (name == "JsProxyBypass") { JsProxyBypass = xtr.ReadString()?.Split(" ,;,;".ToCharArray(), System.StringSplitOptions.RemoveEmptyEntries).Select(o => o.Trim()) .Distinct().ToList() ?? new List(); } } } xtr.Close(); } // 初始化 string url = request.RequestUri.ToString(); string org_url = url; // 如果禁用代理 或 域名不匹配 if (ProxyType == null || JsProxy == null || JsProxyBypass == null || ProxyType == "-1" || JsProxyBypass?.All(v => url.IndexOf(v, StringComparison.OrdinalIgnoreCase) == -1) == true) { if (request.Headers.Referrer == null) { request.Headers.Referrer = request.RequestUri; } return base.SendAsync(request, cancellationToken); } // 否则加上代理 int i = org_url.IndexOf("/http/", StringComparison.CurrentCultureIgnoreCase); if (i > 0) { org_url = org_url.Substring(i + 6); } Uri uri_org = new Uri(org_url); if (url.StartsWith(JsProxy, StringComparison.OrdinalIgnoreCase) != true) { url = $"{JsProxy.TrimEnd()}/http/{url}"; request.RequestUri = new Uri(url); } url = request.Headers.Referrer?.ToString(); if (string.IsNullOrWhiteSpace(url)) { request.Headers.Referrer = uri_org; } return base.SendAsync(request, cancellationToken); } } }