Files
DanMuRead/BarrageGrab/Proxy/SystemProxy.cs
T
Muihd ce23bde71e 直播伴侣开播/下播事件支持与配置优化
新增对直播伴侣开播/下播事件的检测与弹幕推送,增加相关数据结构和事件机制。移除脚本缓存配置,简化直播页自动暂停逻辑。同步优化消息类型、颜色映射及项目结构,版本升级至2.8.0。
2026-04-09 22:47:04 +08:00

159 lines
4.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using BarrageGrab.Proxy.ProxyEventArgs;
using Microsoft.Win32;
namespace BarrageGrab.Proxy
{
internal abstract class SystemProxy : ISystemProxy
{
/// <summary>
/// 接收到websocket消息事件
/// </summary>
public event EventHandler<WsMessageEventArgs> OnWebSocketData;
/// <summary>
/// 接收到http响应事件
/// </summary>
public event EventHandler<HttpResponseEventArgs> OnFetchResponse;
/// <summary>
/// 代理切换时触发
/// </summary>
public event EventHandler<SystemProxyChangeEventArgs> OnProxyStatus;
/// <summary>
/// 直播伴侣操作事件
/// </summary>
public event EventHandler<LiveCompanEventArgs> OnLiveCompanAction;
/// <summary>
/// 代理端口
/// </summary>
public int ProxyPort { get { return AppSetting.Current.ProxyPort; } }
public abstract string HttpUpstreamProxy { get; }
public abstract string HttpsUpstreamProxy { get; }
public abstract void Dispose();
public abstract void Start();
public abstract void SetUpstreamProxy(string addr);
//https://live.douyin.com/webcast/gift/list/ [礼物数据接口]
/// <summary>
/// 检测是否是业务相关链接
/// </summary>
/// <param name="host"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 触发websocket消息事件
/// </summary>
/// <param name="args"></param>
protected void FireWsEvent(WsMessageEventArgs args)
{
OnWebSocketData?.Invoke(this, args);
}
/// <summary>
/// 触发弹幕http弹幕事件
/// </summary>
/// <param name="args"></param>
protected void FireOnFetchResponse(HttpResponseEventArgs args)
{
OnFetchResponse?.Invoke(this, args);
}
/// <summary>
/// 触发直播伴侣操作事件
/// </summary>
/// <param name="args"></param>
protected void FireLiveCompanEvent(LiveCompanEventArgs args)
{
OnLiveCompanAction?.Invoke(this, args);
}
/// <summary>
/// 获取进程名称
/// </summary>
/// <param name="processID"></param>
/// <returns></returns>
protected string GetProcessName(int processID)
{
try
{
var process = Process.GetProcessById(processID);
if (process != null)
{
return process.ProcessName;
}
}
catch (Exception ex) { }
return $"<{processID}>";
}
/// <summary>
/// 注册为系统代�?
/// </summary>
public void RegisterSystemProxy()
{
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
registry.SetValue("ProxyEnable", 1);
registry.SetValue("ProxyServer", $"127.0.0.1:{ProxyPort}");
OnProxyStatus?.Invoke(this, new SystemProxyChangeEventArgs()
{
Open = true
});
}
/// <summary>
/// 关闭系统代理
/// </summary>
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}");
OnProxyStatus?.Invoke(this, new SystemProxyChangeEventArgs()
{
Open = false
});
}
public static bool ProxyIsOpen()
{
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
var proxyEnable = registry.GetValue("ProxyEnable");
if (proxyEnable != null && proxyEnable.ToString() == "1")
{
return true;
}
return false;
}
}
}