diff --git a/BarrageGrab/LiveCompanHelper.cs b/BarrageGrab/LiveCompanHelper.cs new file mode 100644 index 0000000..740e19c --- /dev/null +++ b/BarrageGrab/LiveCompanHelper.cs @@ -0,0 +1,154 @@ +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 + { + /// + /// 获取抖音直播伴侣的exe路径 + /// + /// + 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); + } + + return exePath; + } + + /// + /// 初始化直播伴侣环境 + /// + 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($@"(?\w+)\.commandLine\.appendSwitch\(""{name}"",""(?[^""]*)""\)"); + 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(@"(?\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); + } + } +} diff --git a/BarrageGrab/Program.cs b/BarrageGrab/Program.cs index 0920b34..8752188 100644 --- a/BarrageGrab/Program.cs +++ b/BarrageGrab/Program.cs @@ -58,6 +58,7 @@ namespace BarrageGrab private static void Init() { AppRuntime.Init(); + LiveCompanHelper.SwitchSetup(); WinApi.SetConsoleCtrlHandler(controlCtr, true);//捕获控制台关闭 WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式 AppRuntime.DisplayConsole(!AppSetting.Current.HideConsole);//控制控制台可见 @@ -90,7 +91,8 @@ namespace BarrageGrab } AppRuntime.WsServer.StartListen();//启动WS以及代理服务 - Logger.PrintColor($"{AppRuntime.WsServer.ServerLocation} 弹幕服务已启动...",ConsoleColor.Green); + Logger.PrintColor($"{AppRuntime.WsServer.ServerLocation} 弹幕服务已启动,其他端可通过此地址获取到弹幕流信息", ConsoleColor.Green); + Version version = System.Reflection.Assembly.GetAssembly(typeof(Program)).GetName().Version; SetTitle($"抖音弹幕监听推送 v{version} [{AppRuntime.WsServer.ServerLocation}]"); }