Files
DanMuRead/BarrageGrab/Proxy/SystemProxy.cs
T

114 lines
3.5 KiB
C#
Raw Normal View History

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
{
/// <summary>
/// 接收到websocket消息事件
/// </summary>
public event EventHandler<WsMessageEventArgs> OnWebSocketData;
/// <summary>
/// 接收到http响应事件
/// </summary>
public event EventHandler<HttpResponseEventArgs> OnFetchResponse;
/// <summary>
/// 代理端口
/// </summary>
2023-11-16 09:26:40 +08:00
public int ProxyPort { get { return Appsetting.Current.ProxyPort; } }
public abstract void Dispose();
public abstract void Start();
2023-03-09 00:33:03 +08:00
//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();
2023-03-09 00:33:03 +08:00
if (!Appsetting.Current.FilterHostName) return true;
if (host.StartsWith("webcast")) return true;
2023-03-09 00:33:03 +08:00
if (Appsetting.Current.HostNameFilter.Any(a => a.Trim().ToLower() == host)) return true;
return false;
2023-03-09 00:33:03 +08:00
}
/// <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);
}
2023-03-09 00:33:03 +08:00
/// <summary>
/// 获取进程名称
/// </summary>
/// <param name="processID"></param>
/// <returns></returns>
protected string GetProcessName(int processID)
{
2023-04-24 22:11:44 +08:00
try
{
2023-04-24 22:11:44 +08:00
var process = Process.GetProcessById(processID);
if (process != null)
{
return process.ProcessName;
}
}
2023-04-24 22:11:44 +08:00
catch (Exception ex){}
return $"<{processID}>";
}
2023-03-09 00:33:03 +08:00
/// <summary>
2023-11-14 20:29:18 +08:00
/// 注册为系统代理
2023-03-09 00:33:03 +08:00
/// </summary>
2023-11-14 20:29:18 +08:00
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}");
}
/// <summary>
/// 关闭系统代理
/// </summary>
2023-11-14 20:29:18 +08:00
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}");
}
}
}