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
{
///
/// 域名过滤器
///
public Func HostNameFilter { get; set; }
///
/// 接收到websocket消息事件
///
public event EventHandler OnWebSocketData;
///
/// 接收到http响应事件
///
public event EventHandler OnResponse;
///
/// 代理端口
///
public int ProxyPort { get => Appsetting.Current.ProxyPort; }
public abstract void Dispose();
public abstract void Start();
//https://live.douyin.com/webcast/gift/list/ [礼物数据接口]
///
/// 检测是否是业务相关链接
///
///
///
protected bool CheckHost(string host)
{
var result = true;
if (HostNameFilter != null) return HostNameFilter(host);
return result;
}
///
/// 触发websocket消息事件
///
///
protected void FireWsEvent(WsMessageEventArgs args)
{
OnWebSocketData?.Invoke(this, args);
}
///
/// 触发http响应事件
///
///
protected void FireOnResponse(HttpResponseEventArgs args)
{
OnResponse?.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}>";
}
///
/// 注册系统代理
///
protected 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}");
}
///
/// 关闭系统代理
///
protected 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}");
}
}
}