using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; using Microsoft.Win32; using System.Diagnostics; using BarrageGrab.Proxy.ProxyEventArgs; namespace BarrageGrab.Proxy { internal abstract class SystemProxy : ISystemProxy { /// /// 接收到websocket消息事件 /// public event EventHandler OnWebSocketData; /// /// 接收到http响应事件 /// public event EventHandler OnFetchResponse; /// /// 代理端口 /// public int ProxyPort { get => Appsetting.Current.ProxyPort; } public abstract void Dispose(); public abstract void Start(); //https://live.douyin.com/webcast/gift/list/ [礼物数据接口] /// /// 检测是否是业务相关链接 /// /// /// protected virtual bool CheckHost(string host) { host = host.Trim().ToLower(); if (!Appsetting.Current.FilterHostName) return true; if (host.StartsWith("webcast")) return true; if (Appsetting.Current.HostNameFilter.Any(a => a.Trim().ToLower() == host)) return true; return false; } /// /// 触发websocket消息事件 /// /// protected void FireWsEvent(WsMessageEventArgs args) { OnWebSocketData?.Invoke(this, args); } /// /// 触发弹幕http弹幕事件 /// /// protected void FireOnFetchResponse(HttpResponseEventArgs args) { OnFetchResponse?.Invoke(this, args); } /// /// 获取进程名称 /// /// /// protected string GetProcessName(int processID) { try { var process = Process.GetProcessById(processID); if (process != null) { return process.ProcessName; } } catch (Exception ex){} return $"<{processID}>"; } /// /// 注册为系统代理 /// public void RegisterSystemProxy() { RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); registry.SetValue("ProxyEnable", 1); registry.SetValue("ProxyServer", $"http=localhost:{ProxyPort};https=localhost:{ProxyPort}"); } /// /// 关闭系统代理 /// public void CloseSystemProxy() { RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true); registry.SetValue("ProxyEnable", 0); registry.SetValue("ProxyServer", $"http=localhost:{ProxyPort};https=localhost:{ProxyPort}"); } } }