2023-03-04 14:52:04 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2025-04-14 15:16:55 +08:00
|
|
|
using System.Diagnostics;
|
2023-03-04 14:52:04 +08:00
|
|
|
using System.Linq;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Cryptography;
|
2025-04-14 15:16:55 +08:00
|
|
|
using System.Security.Cryptography.X509Certificates;
|
2023-03-04 14:52:04 +08:00
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-03-28 23:06:09 +08:00
|
|
|
using BarrageGrab.Proxy.ProxyEventArgs;
|
2025-04-14 15:16:55 +08:00
|
|
|
using Microsoft.Win32;
|
2023-03-04 14:52:04 +08:00
|
|
|
|
|
|
|
|
namespace BarrageGrab.Proxy
|
|
|
|
|
{
|
|
|
|
|
internal abstract class SystemProxy : ISystemProxy
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 接收到websocket消息事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<WsMessageEventArgs> OnWebSocketData;
|
|
|
|
|
|
2023-03-28 23:06:09 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 接收到http响应事件
|
|
|
|
|
/// </summary>
|
2023-08-17 20:36:57 +08:00
|
|
|
public event EventHandler<HttpResponseEventArgs> OnFetchResponse;
|
2023-03-28 23:06:09 +08:00
|
|
|
|
2023-11-23 16:43:21 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 代理切换时触�?
|
|
|
|
|
/// </summary>
|
|
|
|
|
public event EventHandler<SystemProxyChangeEventArgs> OnProxyStatus;
|
|
|
|
|
|
2023-03-04 14:52:04 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 代理端口
|
|
|
|
|
/// </summary>
|
2024-03-29 17:46:24 +08:00
|
|
|
public int ProxyPort { get { return AppSetting.Current.ProxyPort; } }
|
2023-11-23 16:43:21 +08:00
|
|
|
|
|
|
|
|
public abstract string HttpUpstreamProxy { get; }
|
|
|
|
|
|
|
|
|
|
public abstract string HttpsUpstreamProxy { get; }
|
2023-03-04 14:52:04 +08:00
|
|
|
|
|
|
|
|
public abstract void Dispose();
|
|
|
|
|
|
|
|
|
|
public abstract void Start();
|
|
|
|
|
|
2025-04-14 15:16:55 +08:00
|
|
|
public abstract void SetUpstreamProxy(string addr);
|
2023-11-23 16:43:21 +08:00
|
|
|
|
2023-03-09 00:33:03 +08:00
|
|
|
//https://live.douyin.com/webcast/gift/list/ [礼物数据接口]
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 检测是否是业务相关链接
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="host"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-08-17 20:36:57 +08:00
|
|
|
protected virtual bool CheckHost(string host)
|
2023-11-23 16:43:21 +08:00
|
|
|
{
|
2023-08-17 20:36:57 +08:00
|
|
|
host = host.Trim().ToLower();
|
2023-03-09 00:33:03 +08:00
|
|
|
|
2024-03-29 17:46:24 +08:00
|
|
|
if (!AppSetting.Current.FilterHostName) return true;
|
2023-11-23 16:43:21 +08:00
|
|
|
|
2023-08-17 20:36:57 +08:00
|
|
|
if (host.StartsWith("webcast")) return true;
|
2023-03-09 00:33:03 +08:00
|
|
|
|
2024-03-29 17:46:24 +08:00
|
|
|
if (AppSetting.Current.HostNameFilter.Any(a => a.Trim().ToLower() == host)) return true;
|
2023-08-17 20:36:57 +08:00
|
|
|
|
2023-11-23 16:43:21 +08:00
|
|
|
return false;
|
2023-03-09 00:33:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 触发websocket消息事件
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"></param>
|
|
|
|
|
protected void FireWsEvent(WsMessageEventArgs args)
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
|
|
|
|
OnWebSocketData?.Invoke(this, args);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 23:06:09 +08:00
|
|
|
/// <summary>
|
2023-08-17 20:36:57 +08:00
|
|
|
/// 触发弹幕http弹幕事件
|
2023-03-28 23:06:09 +08:00
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="args"></param>
|
2023-08-17 20:36:57 +08:00
|
|
|
protected void FireOnFetchResponse(HttpResponseEventArgs args)
|
2023-03-28 23:06:09 +08:00
|
|
|
{
|
2023-08-17 20:36:57 +08:00
|
|
|
OnFetchResponse?.Invoke(this, args);
|
2023-03-28 23:06:09 +08:00
|
|
|
}
|
|
|
|
|
|
2023-03-09 00:33:03 +08:00
|
|
|
/// <summary>
|
|
|
|
|
/// 获取进程名称
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="processID"></param>
|
|
|
|
|
/// <returns></returns>
|
2023-03-04 14:52:04 +08:00
|
|
|
protected string GetProcessName(int processID)
|
|
|
|
|
{
|
2023-04-24 22:11:44 +08:00
|
|
|
try
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
2023-04-24 22:11:44 +08:00
|
|
|
var process = Process.GetProcessById(processID);
|
|
|
|
|
if (process != null)
|
|
|
|
|
{
|
|
|
|
|
return process.ProcessName;
|
|
|
|
|
}
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
2023-11-23 16:43:21 +08:00
|
|
|
catch (Exception ex) { }
|
2023-03-04 14:52:04 +08:00
|
|
|
return $"<{processID}>";
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 00:33:03 +08:00
|
|
|
/// <summary>
|
2023-11-23 16:43:21 +08:00
|
|
|
/// 注册为系统代�?
|
2023-03-09 00:33:03 +08:00
|
|
|
/// </summary>
|
2023-11-14 20:29:18 +08:00
|
|
|
public void RegisterSystemProxy()
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
|
|
|
|
RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
|
|
|
|
|
registry.SetValue("ProxyEnable", 1);
|
2024-05-12 15:43:10 +08:00
|
|
|
registry.SetValue("ProxyServer", $"127.0.0.1:{ProxyPort}");
|
2025-04-14 15:16:55 +08:00
|
|
|
|
2023-11-23 16:43:21 +08:00
|
|
|
OnProxyStatus?.Invoke(this, new SystemProxyChangeEventArgs()
|
|
|
|
|
{
|
|
|
|
|
Open = true
|
|
|
|
|
});
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 关闭系统代理
|
|
|
|
|
/// </summary>
|
2023-11-14 20:29:18 +08:00
|
|
|
public void CloseSystemProxy()
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
|
|
|
|
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}");
|
2023-11-23 16:43:21 +08:00
|
|
|
OnProxyStatus?.Invoke(this, new SystemProxyChangeEventArgs()
|
|
|
|
|
{
|
|
|
|
|
Open = false
|
|
|
|
|
});
|
2025-04-14 15:16:55 +08:00
|
|
|
}
|
2023-11-23 16:43:21 +08:00
|
|
|
|
|
|
|
|
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;
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|