From 8532b484690481d19e254a057b4db8d3f5dd3c28 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=B0=8F=E7=99=BD=E7=8C=BF?= <1083092844@qq.com>
Date: Thu, 9 Mar 2023 01:23:46 +0800
Subject: [PATCH] =?UTF-8?q?=E5=9F=9F=E5=90=8D=E8=A7=84=E5=88=99=E6=B7=BB?=
=?UTF-8?q?=E5=8A=A0=E8=BF=9B=E9=85=8D=E7=BD=AE=E9=A1=B9?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
BarrageGrab/App.config | 13 +++++++--
BarrageGrab/Appsetting.cs | 18 ++++++++++--
BarrageGrab/WssBarrageGrab.cs | 52 ++++++++++++++++++++++++++++-------
3 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/BarrageGrab/App.config b/BarrageGrab/App.config
index de8fa47..83b86f9 100644
--- a/BarrageGrab/App.config
+++ b/BarrageGrab/App.config
@@ -3,7 +3,7 @@
-
+
@@ -11,6 +11,15 @@
-
+
+
+
+
+
\ No newline at end of file
diff --git a/BarrageGrab/Appsetting.cs b/BarrageGrab/Appsetting.cs
index 583a51a..82add9c 100644
--- a/BarrageGrab/Appsetting.cs
+++ b/BarrageGrab/Appsetting.cs
@@ -18,11 +18,13 @@ namespace BarrageGrab
{
try
{
- FilterProcess = AppSettings["filterProcess"].Trim().Split(',');
+ ProcessFilter = AppSettings["processFilter"].Trim().Split(',');
WsProt = int.Parse(AppSettings["wsListenPort"]);
PrintBarrage = AppSettings["printBarrage"].ToLower() == "true";
- ProxyPort = int.Parse(AppSettings["proxPort"]);
+ ProxyPort = int.Parse(AppSettings["proxyPort"]);
PrintFilter = Enum.GetValues(typeof(BarrageMsgType)).Cast().ToArray();
+ FilterHostName = bool.Parse(AppSettings["filterHostName"].Trim());
+ HostNameFilter = AppSettings["hostNameFilter"].Trim().Split(',');
//RoomIds = AppSettings["roomIds"].Trim().Split(',').Where(w=>!string.IsNullOrWhiteSpace(w)).Select(s => long.Parse(s)).ToArray();
var printFilter = AppSettings["printFilter"].Trim().ToLower();
@@ -42,7 +44,7 @@ namespace BarrageGrab
///
/// 过滤的进程
///
- public string[] FilterProcess { get; private set; }
+ public string[] ProcessFilter { get; private set; }
///
/// 端口号
@@ -68,5 +70,15 @@ namespace BarrageGrab
/// 监听的房间号
///
public long[] RoomIds { get; private set; } = new long[0];
+
+ ///
+ /// 使用域名过滤
+ ///
+ public bool FilterHostName { get; private set; } = true;
+
+ ///
+ /// 域名白名单列表
+ ///
+ public string[] HostNameFilter { get; private set; } = new string[0];
}
}
diff --git a/BarrageGrab/WssBarrageGrab.cs b/BarrageGrab/WssBarrageGrab.cs
index 951436f..9ade026 100644
--- a/BarrageGrab/WssBarrageGrab.cs
+++ b/BarrageGrab/WssBarrageGrab.cs
@@ -21,15 +21,11 @@ namespace BarrageGrab
ISystemProxy proxy = new TitaniumProxy();
Appsetting appsetting = Appsetting.Current;
ConsoleWriter console = new ConsoleWriter();
+ //解包成功的域名缓存
+ List succPackHostNames = new List();
//已知的弹幕域名服务器
- string[] wsHostNames = new string[]
- {
- "webcast3-ws-web-hl.douyin.com",
- "webcast3-ws-web-lf.douyin.com",
- "frontier-im.douyin.com",
- "webcast100-ws-web-lq.amemv.com"
- };
+ string[] wsHostNames = Appsetting.Current.HostNameFilter;
///
/// 进入直播间
@@ -87,9 +83,11 @@ namespace BarrageGrab
proxy.Dispose();
}
+
//域名过滤器
private bool HostNameChecker(string hostName)
{
+ if (!Appsetting.Current.FilterHostName) return true;
if (hostName.StartsWith("webcast")) return true;
if (wsHostNames.Any(a => a.ToLower() == hostName.ToLower())) return true;
@@ -115,11 +113,11 @@ namespace BarrageGrab
compressedzipStream.Close();
return outBuffer.ToArray();
}
-
+
//ws数据处理
private void Proxy_OnWebSocketData(object sender, WsMessageEventArgs e)
{
- if (!appsetting.FilterProcess.Contains(e.ProcessName)) return;
+ if (!appsetting.ProcessFilter.Contains(e.ProcessName)) return;
var buff = e.Payload;
if (buff.Length == 0) return;
if (buff[0] != 0x08) return;
@@ -133,12 +131,17 @@ namespace BarrageGrab
//检测包格式
if (!enty.Headers.Any(a => a.Key == "compress_type" && a.Value == "gzip")) return;
-
//解压gzip
var odata = enty.Payload;
var decomp = Decompress(odata);
var response = Serializer.Deserialize(new ReadOnlyMemory(decomp));
+
+ if (!succPackHostNames.Contains(e.HostName))
+ {
+ succPackHostNames.Add(e.HostName);
+ SaveHostNameCache();
+ }
response.Messages.ForEach(f => DoMessage(f));
}
catch (Exception) { }
@@ -253,5 +256,34 @@ namespace BarrageGrab
return;
}
}
+
+ //将成功解包的域名缓存到文件
+ private void SaveHostNameCache()
+ {
+ //获取程序运行目录
+ var baseDir = Directory.GetCurrentDirectory();
+ var fullPath = Path.Combine(baseDir, "成功解包域名缓存.txt");
+
+ try
+ {
+ //文件不存在则创建
+ if (!File.Exists(fullPath))
+ {
+ File.Create(fullPath);
+ }
+ var text = File.ReadAllText(fullPath);
+ var currentHosts = text.Split('\n').Where(w => !string.IsNullOrWhiteSpace(w)).Select(s=>s.Trim().Trim('\r')).ToList();
+ var newHosts = succPackHostNames.Except(currentHosts).ToList();
+ //保存
+ if (newHosts.Count > 0)
+ {
+ File.AppendAllLines(fullPath, newHosts);
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine("写入成功解包域名缓存失败:" + ex.Message, ConsoleColor.Red);
+ }
+ }
}
}