域名规则添加进配置项
This commit is contained in:
+11
-2
@@ -3,7 +3,7 @@
|
||||
<!--配置更改后重启才能生效-->
|
||||
<appSettings>
|
||||
<!--过滤Websocket数据源进程,可用','进行分隔,程序将会监听以下进程的弹幕信息-->
|
||||
<add key="filterProcess" value="直播伴侣,chrome,msedge"/>
|
||||
<add key="processFilter" value="直播伴侣,chrome,msedge"/>
|
||||
<!--Websocket监听端口-->
|
||||
<add key="wsListenPort" value="8888"/>
|
||||
<!--在控制台输出弹幕-->
|
||||
@@ -11,6 +11,15 @@
|
||||
<!--要在控制台打印的弹幕类型,可以用','隔开 all[全部],1[普通弹幕],2[点赞消息],3[进入直播间],4[关注消息],5[礼物消息],6[统计消息],7[粉丝团消息]-->
|
||||
<add key="printFilter" value="all"/>
|
||||
<!--系统代理端口-->
|
||||
<add key="proxPort" value="8827"/>
|
||||
<add key="proxyPort" value="8827"/>
|
||||
<!--开启内置的域名过滤,设置为false会解包所有https请求,cpu占用很高,建议在无法获取弹幕数据时调整 -->
|
||||
<add key="filterHostName" value="true"/>
|
||||
<!--已知的弹幕域名列表,用作过滤规则中,凡是webcast开头的域名程序都会列入白名单-->
|
||||
<add key="hostNameFilter" value="
|
||||
webcast3-ws-web-hl.douyin.com,
|
||||
webcast3-ws-web-lf.douyin.com,
|
||||
webcast100-ws-web-lq.amemv.com
|
||||
frontier-im.douyin.com,
|
||||
"/>
|
||||
</appSettings>
|
||||
</configuration>
|
||||
@@ -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<int>().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
|
||||
/// <summary>
|
||||
/// 过滤的进程
|
||||
/// </summary>
|
||||
public string[] FilterProcess { get; private set; }
|
||||
public string[] ProcessFilter { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// 端口号
|
||||
@@ -68,5 +70,15 @@ namespace BarrageGrab
|
||||
/// 监听的房间号
|
||||
/// </summary>
|
||||
public long[] RoomIds { get; private set; } = new long[0];
|
||||
|
||||
/// <summary>
|
||||
/// 使用域名过滤
|
||||
/// </summary>
|
||||
public bool FilterHostName { get; private set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// 域名白名单列表
|
||||
/// </summary>
|
||||
public string[] HostNameFilter { get; private set; } = new string[0];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,15 +21,11 @@ namespace BarrageGrab
|
||||
ISystemProxy proxy = new TitaniumProxy();
|
||||
Appsetting appsetting = Appsetting.Current;
|
||||
ConsoleWriter console = new ConsoleWriter();
|
||||
//解包成功的域名缓存
|
||||
List<string> succPackHostNames = new List<string>();
|
||||
|
||||
//已知的弹幕域名服务器
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// 进入直播间
|
||||
@@ -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;
|
||||
@@ -119,7 +117,7 @@ namespace BarrageGrab
|
||||
//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<Response>(new ReadOnlyMemory<byte>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user