将framework版本 从4.8 降低到4.6.2;

修复控制台初始化隐藏有几率无效的BUG
This commit is contained in:
梅浩东
2023-11-16 09:26:40 +08:00
parent ba46b2e533
commit 746f06f7dc
12 changed files with 81 additions and 50 deletions
+16 -16
View File
@@ -1,38 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--配置更改后重启才能生效-->
<appSettings>
<!--过滤Websocket数据源进程,可用','进行分隔,程序将会监听以下进程的弹幕信息-->
<add key="processFilter" value="直播伴侣,douyin,chrome,msedge,QQBrowser,360se,firefox,2345explorer,iexplore" />
<add key="processFilter" value="直播伴侣,douyin,chrome,msedge,QQBrowser,360se,firefox,2345explorer,iexplore"/>
<!--Websocket监听端口-->
<add key="wsListenPort" value="8888" />
<add key="wsListenPort" value="8888"/>
<!--true:监听在0.0.0.0,接受任意Ip连接,false:监听在127.0.0.1,仅接受本机连接-->
<add key="listenAny" value="true" />
<add key="listenAny" value="true"/>
<!--系统代理端口-->
<add key="proxyPort" value="8827" />
<add key="proxyPort" value="8827"/>
<!--上游代理地址,例如开启了系统代理,但是需要将其他无关请求转发到VPN工具中,例如:127.0.0.1:11223,不要带http://-->
<add key="upstreamProxy" value=""/>
<!--在控制台输出弹幕-->
<add key="printBarrage" value="true" />
<add key="printBarrage" value="true"/>
<!--要在控制台打印的弹幕类型,可以用','隔开 all[全部],1[普通弹幕],2[点赞消息],3[进入直播间],4[关注消息],5[礼物消息],6[统计消息],7[粉丝团消息]-->
<add key="printFilter" value="all" />
<add key="printFilter" value="all"/>
<!--是否启用系统代理,若设置为false 则需要在程序手动指定代理地址 -->
<add key="usedProxy" value="true" />
<add key="usedProxy" value="true"/>
<!--开启内置的域名过滤,设置为false会解包所有https请求,cpu占用很高,建议在无法获取弹幕数据时调整 -->
<add key="filterHostName" value="true" />
<add key="filterHostName" value="true"/>
<!--已知的弹幕域名列表 ','分隔 用作过滤规则中,凡是webcast开头的域名程序都会自动列入白名单-->
<add key="hostNameFilter" value="" />
<add key="hostNameFilter" value=""/>
<!--要进行过滤的房间ID,不填代表监听所有,多项使用','分隔,浏览器进入直播间 F12 控制台输入 'window.localStorage.playRoom' 即可快速看到房间ID(不是地址栏中的那个) -->
<add key="roomIds" value="" />
<!--隐藏控制台-->
<add key="hideConsole" value="false" />
<add key="roomIds" value=""/>
<!--隐藏控制台-->
<add key="hideConsole" value="false"/>
</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" />
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.1.2" newVersion="4.0.1.2"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/></startup></configuration>
+28 -2
View File
@@ -37,6 +37,30 @@ namespace BarrageGrab
}
/// <summary>
/// 隐藏/显示 控制台
/// </summary>
/// <param name="show"></param>
public static void DisplayConsole(bool show)
{
//var hWnd = WinApi.FindWindow(null, Console.Title);
var hWnd = WinApi.GetConsoleWindow();
if (hWnd != IntPtr.Zero)
{
WinApi.ShowWindow(hWnd, show? WinApi.CmdShow.SW_SHOW:WinApi.CmdShow.SW_HIDE);
}
}
public static void DisplayConsoleByTitle(bool show)
{
var hWnd = WinApi.FindWindow(null, Console.Title);
//var hWnd = WinApi.GetConsoleWindow();
if (hWnd != IntPtr.Zero)
{
WinApi.ShowWindow(hWnd, show ? WinApi.CmdShow.SW_SHOW : WinApi.CmdShow.SW_HIDE);
}
}
/// <summary>
/// 房间缓存管理器
/// </summary>
@@ -95,7 +119,8 @@ namespace BarrageGrab
/// <returns></returns>
public long GetCachedWebRoomid(string roomid)
{
if (RoomInfoCache.TryGetValue(roomid, out var value)) return long.Parse(value.WebRoomId??"-1");
RoomInfo value;
if (RoomInfoCache.TryGetValue(roomid,out value)) return long.Parse(value.WebRoomId??"-1");
return -1;
}
@@ -106,7 +131,8 @@ namespace BarrageGrab
/// <returns></returns>
public RoomInfo GetCachedWebRoomInfo(string roomid)
{
if (RoomInfoCache.TryGetValue(roomid, out var value)) return value;
RoomInfo value;
if (RoomInfoCache.TryGetValue(roomid, out value)) return value;
return null;
}
}
+1 -1
View File
@@ -12,7 +12,7 @@ namespace BarrageGrab
{
private static readonly Appsetting ins = new Appsetting();
public static Appsetting Current { get => ins; }
public static Appsetting Current { get { return ins; } }
public Appsetting()
{
+6 -6
View File
@@ -510,12 +510,12 @@ namespace BarrageGrab.Modles
/// <param name="html"></param>
/// <param name="result"></param>
/// <returns></returns>
public static (int, string) TryParseRoomPageHtml(string html, out RoomInfo result)
public static Tuple<int,string> TryParseRoomPageHtml(string html, out RoomInfo result)
{
result = null;
if (html.IsNullOrWhiteSpace())
{
return (-1, "无效的页面数据");
return Tuple.Create(-1, "无效的页面数据");
}
//var roomDataReg = new Regex(@"(?<=self\.__pace_f\.push\(\[1,""a:)\[.+?\](?=\\n?""\]\)[\s\n\r]*?<\/script>)");
//var roomDataReg = new Regex(@"(?<=self\.__pace_f\.push\(\[1,\s*""0:)\[.+?\](?=\\n""?\]\)[\s\n\r]*?<\/script>)");
@@ -526,7 +526,7 @@ namespace BarrageGrab.Modles
if (!match.Success)
{
//Logger.LogError("在通过正则匹配直播页房间信息时失败,可能是官方做了升级");
return (1, "未能匹配到房间信息");
return Tuple.Create(1, "未能匹配到房间信息");
}
var matchData = Regex.Unescape(match.Value);
@@ -537,7 +537,7 @@ namespace BarrageGrab.Modles
}
catch (Exception ex)
{
return (2, "匹配到的房间数据格式有错误");
return Tuple.Create(2, "匹配到的房间数据格式有错误");
}
//var root = jsonObject["children"][3];
//var roomInfo = root["initialState"]["roomStore"]["roomInfo"];
@@ -552,7 +552,7 @@ namespace BarrageGrab.Modles
if (room == null)
{
return (3, "房间信息为空,获取失败");
return Tuple.Create(3, "房间信息为空,获取失败");
}
result = new RoomInfo();
@@ -579,7 +579,7 @@ namespace BarrageGrab.Modles
FollowStatus = roomOwner["follow_info"]["follow_status"].Value<int>()
};
}
return (0, "succ");
return Tuple.Create(0, "succ");
}
}
}
+1 -1
View File
@@ -59,7 +59,7 @@ namespace BarrageGrab
{
return dic[key];
}
return default;
return default(TValue);
}
/// <summary>
+10 -10
View File
@@ -23,18 +23,17 @@ namespace BarrageGrab
WinApi.SetConsoleCtrlHandler(cancelHandler, true);//捕获控制台关闭
WinApi.DisableQuickEditMode();//禁用控制台快速编辑模式
Console.Title = "抖音弹幕监听推送";
if(Appsetting.Current.HideConsole)
{
var hWnd = WinApi.FindWindow(null, Console.Title);
if (hWnd != IntPtr.Zero)
{
WinApi.ShowWindow(hWnd,WinApi.CmdShow.SW_HIDE);
}
}
AppRuntime.DisplayConsole(!Appsetting.Current.HideConsole);
bool exited = false;
AppRuntime.WssService.StartListen();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"{AppRuntime.WssService.ServerLocation} 弹幕服务已启动...");
Console.ForegroundColor = ConsoleColor.Gray;
Console.Title = $"抖音弹幕监听推送 [{AppRuntime.WssService.ServerLocation}]";
AppRuntime.WssService.OnClose += (s, e) =>
{
//退出程序
@@ -73,7 +72,8 @@ namespace BarrageGrab
{
const string mutexName = "DyBarrageGrab";
// Try to create a new named mutex.
using (Mutex mutex = new Mutex(true, mutexName, out bool createdNew))
bool createdNew;
using (Mutex mutex = new Mutex(true, mutexName, out createdNew))
{
return !createdNew;
}
+1 -1
View File
@@ -27,7 +27,7 @@ namespace BarrageGrab.Proxy
/// <summary>
/// 代理端口
/// </summary>
public int ProxyPort { get => Appsetting.Current.ProxyPort; }
public int ProxyPort { get { return Appsetting.Current.ProxyPort; } }
public abstract void Dispose();
+4 -2
View File
@@ -173,8 +173,10 @@ namespace BarrageGrab.Proxy
{
//利用 HtmlAgilityPack 在尾部注入script 标签
var html = await e.GetResponseBodyAsString();
(int code, string msg) = RoomInfo.TryParseRoomPageHtml(html, out var roominfo);
RoomInfo roominfo;
var tup = RoomInfo.TryParseRoomPageHtml(html, out roominfo);
int code = tup.Item1;
string msg = tup.Item2;
if (code == 0)
{
+3
View File
@@ -37,6 +37,9 @@ namespace BarrageGrab
[DllImport("User32.dll", EntryPoint = "ShowWindow")]
public static extern bool ShowWindow(IntPtr hWnd, CmdShow nCmdShow);
[DllImport("kernel32.dll")]
public static extern IntPtr GetConsoleWindow();
/// <summary>
+6 -7
View File
@@ -40,6 +40,11 @@ namespace BarrageGrab
/// </summary>
public event EventHandler OnClose;
/// <summary>
/// WS服务器启动地址
/// </summary>
public string ServerLocation { get { return socketServer.Location; } }
public WsBarrageService()
{
#if DEBUG
@@ -495,11 +500,7 @@ namespace BarrageGrab
case CommandCode.DisplayConsole:
{
var display = (bool)cmdPack.Data;
var hand = WinApi.FindWindow(null, Console.Title);
if (hand != IntPtr.Zero)
{
WinApi.ShowWindow(hand, display ? WinApi.CmdShow.SW_SHOW : WinApi.CmdShow.SW_HIDE);
}
AppRuntime.DisplayConsole(display);
break;
}
}
@@ -551,8 +552,6 @@ namespace BarrageGrab
{
this.grab.Start(); //启动代理
this.socketServer.Start(Listen);//启动监听
console.WriteLine($"{this.socketServer.Location} 弹幕服务已启动...", ConsoleColor.Green);
Console.Title = $"抖音弹幕监听推送 [{this.socketServer.Location}]";
}
/// <summary>
+1 -1
View File
@@ -68,7 +68,7 @@ namespace BarrageGrab
/// <summary>
/// 代理
/// </summary>
public ISystemProxy Proxy { get => proxy; }
public ISystemProxy Proxy { get { return proxy; } }
public WssBarrageGrab()
{
+2 -1
View File
@@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>BarrageGrab</RootNamespace>
<AssemblyName>WssBarrageService</AssemblyName>
<TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<PublishUrl>publish\</PublishUrl>
@@ -28,6 +28,7 @@
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>