2023-03-04 14:52:04 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Security;
|
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using System.Security.Cryptography.X509Certificates;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-03-28 23:06:09 +08:00
|
|
|
using BarrageGrab.Proxy.ProxyEventArgs;
|
2023-03-04 14:52:04 +08:00
|
|
|
using Microsoft.Win32;
|
|
|
|
|
using Titanium.Web.Proxy;
|
|
|
|
|
using Titanium.Web.Proxy.EventArguments;
|
|
|
|
|
using Titanium.Web.Proxy.Http;
|
|
|
|
|
using Titanium.Web.Proxy.Models;
|
|
|
|
|
using Titanium.Web.Proxy.StreamExtended.Network;
|
|
|
|
|
|
|
|
|
|
namespace BarrageGrab.Proxy
|
|
|
|
|
{
|
|
|
|
|
internal class TitaniumProxy : SystemProxy
|
|
|
|
|
{
|
|
|
|
|
ProxyServer proxyServer = null;
|
|
|
|
|
ExplicitProxyEndPoint explicitEndPoint = null;
|
|
|
|
|
|
|
|
|
|
public TitaniumProxy()
|
|
|
|
|
{
|
|
|
|
|
//注册系统代理
|
|
|
|
|
//RegisterSystemProxy();
|
|
|
|
|
proxyServer = new ProxyServer();
|
|
|
|
|
|
|
|
|
|
proxyServer.ReuseSocket = false;
|
|
|
|
|
proxyServer.EnableConnectionPool = true;
|
|
|
|
|
proxyServer.ForwardToUpstreamGateway = true;
|
|
|
|
|
proxyServer.CertificateManager.SaveFakeCertificates = true;
|
|
|
|
|
|
2023-03-04 14:52:43 +08:00
|
|
|
proxyServer.CertificateManager.RootCertificate = proxyServer.CertificateManager.LoadRootCertificate();
|
|
|
|
|
if (proxyServer.CertificateManager.RootCertificate == null)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("正在进行证书安装,需要安装证书才可进行https解密,若有提示请确定");
|
|
|
|
|
proxyServer.CertificateManager.CreateRootCertificate();
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 14:52:04 +08:00
|
|
|
proxyServer.ServerCertificateValidationCallback += ProxyServer_ServerCertificateValidationCallback; ;
|
|
|
|
|
proxyServer.BeforeResponse += ProxyServer_BeforeResponse;
|
2023-03-28 23:06:09 +08:00
|
|
|
proxyServer.AfterResponse += ProxyServer_AfterResponse;
|
2023-03-04 14:52:04 +08:00
|
|
|
|
2023-03-09 00:33:03 +08:00
|
|
|
explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, ProxyPort, true);
|
2023-03-04 14:52:04 +08:00
|
|
|
explicitEndPoint.BeforeTunnelConnectRequest += ExplicitEndPoint_BeforeTunnelConnectRequest;
|
|
|
|
|
proxyServer.AddEndPoint(explicitEndPoint);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-28 23:06:09 +08:00
|
|
|
private Task ProxyServer_AfterResponse(object sender, SessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string hostname = e.HttpClient.Request.RequestUri.Host;
|
|
|
|
|
var processid = e.HttpClient.ProcessId.Value;
|
|
|
|
|
|
|
|
|
|
this.FireOnResponse(new HttpResponseEventArgs()
|
|
|
|
|
{
|
|
|
|
|
ProcessID = processid,
|
|
|
|
|
HostName = hostname,
|
|
|
|
|
ProcessName = base.GetProcessName(processid),
|
|
|
|
|
HttpClient = e.HttpClient
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2023-03-04 14:52:04 +08:00
|
|
|
|
|
|
|
|
private Task ProxyServer_ServerCertificateValidationCallback(object sender, CertificateValidationEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
// set IsValid to true/false based on Certificate Errors
|
|
|
|
|
if (e.SslPolicyErrors == SslPolicyErrors.None)
|
|
|
|
|
{
|
|
|
|
|
e.IsValid = true;
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ProxyServer_BeforeResponse(object sender, SessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string hostname = e.HttpClient.Request.RequestUri.Host;
|
|
|
|
|
if (e.HttpClient.ConnectRequest?.TunnelType == TunnelType.Websocket)
|
|
|
|
|
{
|
|
|
|
|
e.DataReceived += WebSocket_DataReceived;
|
2023-04-09 13:08:17 +08:00
|
|
|
}
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task ExplicitEndPoint_BeforeTunnelConnectRequest(object sender, TunnelConnectSessionEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
string hostname = e.HttpClient.Request.RequestUri.Host;
|
2023-03-09 00:33:03 +08:00
|
|
|
if (!CheckHost(hostname))
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
|
|
|
|
e.DecryptSsl = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async void WebSocket_DataReceived(object sender, DataEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var args = (SessionEventArgs)sender;
|
|
|
|
|
|
|
|
|
|
string hostname = args.HttpClient.Request.RequestUri.Host;
|
|
|
|
|
|
|
|
|
|
var processid = args.HttpClient.ProcessId.Value;
|
|
|
|
|
|
2023-04-01 00:05:57 +08:00
|
|
|
List<byte> messageData = new List<byte>();
|
|
|
|
|
|
2023-04-09 22:25:09 +08:00
|
|
|
try
|
2023-04-01 00:05:57 +08:00
|
|
|
{
|
2023-04-09 22:25:09 +08:00
|
|
|
foreach (var frame in args.WebSocketDecoderReceive.Decode(e.Buffer, e.Offset, e.Count))
|
2023-03-04 14:52:04 +08:00
|
|
|
{
|
2023-04-09 22:25:09 +08:00
|
|
|
if (frame.OpCode == WebsocketOpCode.Continuation)
|
2023-04-01 00:05:57 +08:00
|
|
|
{
|
|
|
|
|
messageData.AddRange(frame.Data.ToArray());
|
2023-04-09 22:25:09 +08:00
|
|
|
continue;
|
2023-04-01 00:05:57 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-04-09 22:25:09 +08:00
|
|
|
byte[] payload;
|
|
|
|
|
if (messageData.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
messageData.AddRange(frame.Data.ToArray());
|
|
|
|
|
payload = messageData.ToArray();
|
|
|
|
|
messageData.Clear();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
payload = frame.Data.ToArray();
|
|
|
|
|
}
|
2023-04-01 00:05:57 +08:00
|
|
|
|
2023-04-09 22:25:09 +08:00
|
|
|
base.FireWsEvent(new WsMessageEventArgs()
|
|
|
|
|
{
|
|
|
|
|
ProcessID = processid,
|
|
|
|
|
HostName = hostname,
|
|
|
|
|
Payload = payload,
|
|
|
|
|
ProcessName = base.GetProcessName(processid)
|
|
|
|
|
});
|
|
|
|
|
}
|
2023-04-01 00:05:57 +08:00
|
|
|
}
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
2023-04-09 22:25:09 +08:00
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("解析某个WebSocket包出错:" + ex.Message);
|
|
|
|
|
}
|
2023-04-01 00:05:57 +08:00
|
|
|
|
|
|
|
|
if (messageData.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
// 没有收到 WebSocket 帧的结束帧,抛出异常或者进行处理
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-04 14:52:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 释放资源,关闭系统代理
|
|
|
|
|
/// </summary>
|
|
|
|
|
override public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
proxyServer.Stop();
|
|
|
|
|
proxyServer.Dispose();
|
|
|
|
|
CloseSystemProxy();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 启动监听
|
|
|
|
|
/// </summary>
|
|
|
|
|
override public void Start()
|
|
|
|
|
{
|
|
|
|
|
proxyServer.Start();
|
|
|
|
|
proxyServer.SetAsSystemHttpProxy(explicitEndPoint);
|
|
|
|
|
proxyServer.SetAsSystemHttpsProxy(explicitEndPoint);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|