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
{
///
/// 接收到websocket消息事件
///
public event EventHandler OnWebSocketData;
///
/// 客户端发送的WebSocket数据事件 (用于解析认证包等)
///
public event EventHandler OnWebSocketSentData;
///
/// 接收到http响应事件
///
public event EventHandler OnFetchResponse;
///
/// 代理切换时触发
///
public event EventHandler OnProxyStatus;
///
/// 直播伴侣操作事件
///
public event EventHandler OnLiveCompanAction;
///
/// 代理端口
///
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/ [礼物数据接口]
///
/// 检测是否是业务相关链接
///
///
///
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);
}
///
/// 触发客户端发送的websocket数据事件
///
///
protected void FireWsSentEvent(WsMessageEventArgs args)
{
OnWebSocketSentData?.Invoke(this, args);
}
///
/// 触发弹幕http弹幕事件
///
///
protected void FireOnFetchResponse(HttpResponseEventArgs args)
{
OnFetchResponse?.Invoke(this, args);
}
///
/// 触发直播伴侣操作事件
///
///
protected void FireLiveCompanEvent(LiveCompanEventArgs args)
{
OnLiveCompanAction?.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", $"127.0.0.1:{ProxyPort}");
OnProxyStatus?.Invoke(this, new SystemProxyChangeEventArgs()
{
Open = true
});
}
///
/// 关闭系统代理
///
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;
}
}
}