Skip to content

Commit

Permalink
Merge pull request #19 from HaoDong108/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
ape-byte authored Sep 28, 2024
2 parents cf0c250 + e8d5df8 commit 144ef1a
Show file tree
Hide file tree
Showing 13 changed files with 1,039 additions and 189 deletions.
36 changes: 33 additions & 3 deletions BarrageGrab/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
<add key="listenAny" value="true" />
<!--系统代理端口-->
<add key="proxyPort" value="8827" />
<!--是否启用系统代理,若设置为false 则需要在程序手动指定代理地址 -->
<add key="usedProxy" value="true" />
<!--上游代理地址,例如开启了系统代理,但是需要将其他无关请求转发到VPN工具中,例如:127.0.0.1:11223,不要带http://-->
<add key="upstreamProxy" value="" />
<!--在控制台输出弹幕-->
Expand All @@ -22,8 +24,6 @@
<add key="logFilter" value="1,2,4,5,6,7,8" />
<!--要进行过滤的Web房间ID,多个使用','分隔,根据缓存来过滤的,直播伴侣不支持 -->
<add key="webRoomIds" value="" />
<!--是否启用系统代理,若设置为false 则需要在程序手动指定代理地址 -->
<add key="usedProxy" value="true" />
<!--开启内置的域名过滤,设置为false会解包所有https请求,cpu占用很高,建议在无法获取弹幕数据时调整 -->
<add key="filterHostName" value="true" />
<!--已知的弹幕域名列表 ','分隔 用作过滤规则中,凡是webcast开头的域名程序都会自动列入白名单-->
Expand All @@ -41,18 +41,48 @@
<!--弹幕轮询间隔,当 forcePolling 为 true 时生效 (毫秒,1000毫秒=1秒,不建议小于1000毫秒,太小可能会被封IP,值越小,弹幕流越丝滑,对于观众多的直播间可以改小)-->
<add key="pollingInterval" value="3000" />
<!--禁用直播页浏览器脚本缓存 (如果需要确保脚本每次能够正常匹配替换,则启用它,可能会损失一定的页面加载速度)-->
<add key="disableLivePageScriptCache" value="true" />
<add key="disableLivePageScriptCache" value="false" />
<!--指定要发送的COM串口 格式:'COM1:9600' 代表用COM1发送,波特率9600,若不需要使用串口抄送服务请留空 -->
<add key="comPort" value="" />
<!--启用自定义消息过滤,启用后可去程序根目录 Scripts/engine/comPortFilter.js 调整过滤规则 -->
<add key="useComPortFilter" value="true" />
<!--直播伴侣exe文件位置(除非程序找不到,否则请留空)-->
<add key="liveCompanPath" value="" />
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.IO.Compression" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.2.0" newVersion="4.1.2.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.Tracing" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Reflection" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.1.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Runtime.InteropServices" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Diagnostics.DiagnosticSource" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<startup>
Expand Down
6 changes: 6 additions & 0 deletions BarrageGrab/AppSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public AppSetting()
PollingInterval = int.Parse(AppSettings["pollingInterval"].Trim());
DisableLivePageScriptCache = bool.Parse(AppSettings["disableLivePageScriptCache"].Trim());
WebRoomIds = AppSettings["webRoomIds"].Trim().Split(',').Where(w => !string.IsNullOrWhiteSpace(w)).ToArray();
LiveCompanPath = AppSettings["liveCompanPath"].Trim();

ConfigComPort();
ConfigFilter();
Expand Down Expand Up @@ -243,5 +244,10 @@ public void Save()
/// 是否启用串口过滤器脚本
/// </summary>
public bool UseComPortFilter { get; set; }

/// <summary>
/// 直播伴侣文件位置
/// </summary>
public string LiveCompanPath { get; set; } = string.Empty;
}
}
4 changes: 4 additions & 0 deletions BarrageGrab/FodyWeavers.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
</Weavers>
173 changes: 173 additions & 0 deletions BarrageGrab/LiveCompanHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using IWshRuntimeLibrary;
using File = System.IO.File;

namespace BarrageGrab
{
public static class LiveCompanHelper
{
/// <summary>
/// 获取抖音直播伴侣的exe路径
/// </summary>
/// <returns></returns>
public static string GetExePath()
{
string exePath = "";
//获取exe所在目录
//1.先在开始菜单找
var shortcutPath = @"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\直播伴侣.lnk";
if (File.Exists(shortcutPath))
{
//获取快捷方式的目标路径
exePath = GetInkTargetPath(shortcutPath);
if (!File.Exists(exePath))
{
exePath = "";
Logger.LogWarn($"直播伴侣.lnk 所指向的exe文件位置 {exePath} 无效");
}
}
else
{
Logger.LogInfo("未找到直播伴侣的桌面快捷方式");
}

//再从配置文件读取
if (exePath.IsNullOrWhiteSpace())
{
exePath = AppSetting.Current.LiveCompanPath;
if(!exePath.IsNullOrWhiteSpace() && !File.Exists(exePath))
{
Logger.LogWarn($"所配置的 {exePath} 不存在");
exePath = "";
}
}
return exePath;
}

/// <summary>
/// 初始化直播伴侣环境
/// </summary>
public static void SwitchSetup()
{
var exePath = GetExePath();
if (string.IsNullOrEmpty(exePath))
{
Logger.LogWarn("未找到直播伴侣.exe文件,跳过环境设置");
return;
}

//设置index.js
var indexJsPath = Path.Combine(Path.GetDirectoryName(exePath), "resources", "app", "index.js");
Logger.LogInfo($"正在配置 " + indexJsPath);

if (!File.Exists(indexJsPath))
{
throw new Exception("未找到直播伴侣的index.js文件");
}
var indexJs = File.ReadAllText(indexJsPath, Encoding.UTF8);

CheckBackFile(indexJsPath);
var newjs = SetIndexJsContent(indexJs);
if (newjs != indexJs && !newjs.IsNullOrWhiteSpace())
{
File.WriteAllText(indexJsPath, newjs);
}

Logger.LogInfo("直播伴侣环境初始化完成");
}

//获取Ink快捷方式的目标路径
private static string GetInkTargetPath(string shortcutPath)
{
// 创建 Windows Script Host 对象
WshShell shell = new WshShell();

// 使用 IWshShortcut 接口加载 .ink 文件
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutPath);

// 获取目标路径
string targetPath = shortcut.TargetPath;

return targetPath;
}

//检测备份文件
private static void CheckBackFile(string filePath)
{
if (!File.Exists(filePath)) return;

var bakPath = filePath + ".bak";
if (!File.Exists(bakPath))
{
//拷贝一份备份
File.Copy(filePath, bakPath);
Logger.LogInfo($"已备份 {filePath} -> {bakPath}");
}
}

//设置index.js内容
private static string SetIndexJsContent(string content)
{
//检测注入代理
var mineProxyHost = $"127.0.0.1:{AppSetting.Current.ProxyPort},direct://";
SetSwitch("proxy-server", mineProxyHost, ref content);

//移除文件损坏检测校验
var checkReg = new Regex(@"if\(\(0,\w.integrityCheckReport\)\(\w\),!\w\.ok\)");
if (checkReg.IsMatch(content))
{
content = checkReg.Replace(content, "if(false)");
Logger.LogInfo($"直播伴侣文件改动检测已拦截");
}

return content;
}

//添加 electron 启动参数
private static void SetSwitch(string name, string value, ref string content)
{
if (name.IsNullOrWhiteSpace()) return;

//检测注入
var proxyReg = new Regex($@"(?<varname>\w+)\.commandLine\.appendSwitch\(""{name}"",""(?<value>[^""]*)""\)");
var proxyMatch = proxyReg.Match(content);
//检测到已经存在配置,则更新参数值
if (proxyMatch.Success)
{
var matchValue = proxyMatch.Groups["value"].Value;
if (value != matchValue)
{
content = proxyReg.Replace(content, $@"${{varname}}.commandLine.appendSwitch(""{name}"",""{value}"")");
Logger.LogInfo($"直播伴侣成功覆盖启动参数 [{name}] = [{value}]");
}
}
//否则添加新的配置
else
{
var nosandboxReg = new Regex(@"(?<varname>\w+)\.commandLine\.appendSwitch\(""no-sandbox""\)");
var match = nosandboxReg.Match(content);
if (match.Success)
{
var newvalue = $@"{match.Groups["varname"].ToString()}.commandLine.appendSwitch(""{name}"",""{value}""),";
content = content.Insert(match.Index, newvalue);
Logger.LogInfo($"直播伴侣成功添加启动参数 [{name}] = [{value}]");
}
}
}

private static void SetInnerText(HtmlNode node, string text)
{
node.InnerHtml = "";
var textNode = node.OwnerDocument.CreateTextNode(text);
node.AppendChild(textNode);
}
}
}
Loading

0 comments on commit 144ef1a

Please sign in to comment.