Skip to content

Commit

Permalink
1.软件启动时添加网络连接状态检查;
Browse files Browse the repository at this point in the history
2.添加扫码登录方式;
3.已知bug,Release模式有时无法完全退出软件(状态栏不可见,任务管理器可见)。
  • Loading branch information
shanxing2 committed Mar 5, 2020
1 parent 445b1e9 commit b0322f3
Show file tree
Hide file tree
Showing 16 changed files with 1,692 additions and 102 deletions.
9 changes: 0 additions & 9 deletions source/Class/ApplicationEvents.vb
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,5 @@ Namespace My
Logger.WriteLine(e.Exception)
MessageBox.Show("程序遇到未处理异常,请关闭程序后重启程序", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Sub

Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
#If Not DEBUG Then
' 检测到被附加到其他进程(比如od?)调试时,强制退出
If Win32API.IsDebuggerPresent Then
Environment.Exit(0)
End If
#End If
End Sub
End Class
End Namespace
60 changes: 46 additions & 14 deletions source/Class/BilibiliApi.vb
Original file line number Diff line number Diff line change
Expand Up @@ -586,23 +586,34 @@ Public NotInheritable Class BilibiliApi
Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """code"":0", 3)
End Function

''' <summary>
''' 获取当前登录用户信息
''' </summary>
''' <returns></returns>
Public Shared Async Function GetCurrentUserAsync() As Task(Of HttpResponse)
''' <summary>
''' 获取当前登录用户信息
''' </summary>
''' <returns></returns>
Public Shared Async Function GetCurrentUserInfoAsync() As Task(Of HttpResponse)
Dim url = "https://api.live.bilibili.com/live_user/v1/UserInfo/live_info"
m_HttpHeadersParam("Referer") = "http://link.bilibili.com/p/center/index"

Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """code"":0", 3)
End Function
Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """code"":0", 3)
End Function

''' <summary>
''' 获取真正的直播间Id
''' </summary>
''' <param name="shortRoomId">直播间短Id</param>
''' <returns></returns>
Public Shared Async Function GetRoomRealIdFromNetworkAsync(ByVal shortRoomId As String) As Task(Of (Success As Boolean, RoomShortId As Integer, RoomRealId As Integer))
''' <summary>
''' 获取当前登录用户资产信息
''' </summary>
''' <returns></returns>
Public Shared Async Function GetCurrentUserNavAsync() As Task(Of HttpResponse)
Dim url = "https://api.bilibili.com/x/web-interface/nav"
m_HttpHeadersParam("Referer") = "https://www.bilibili.com/"

Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """isLogin"":true", 3)
End Function

''' <summary>
''' 获取真正的直播间Id
''' </summary>
''' <param name="shortRoomId">直播间短Id</param>
''' <returns></returns>
Public Shared Async Function GetRoomRealIdFromNetworkAsync(ByVal shortRoomId As String) As Task(Of (Success As Boolean, RoomShortId As Integer, RoomRealId As Integer))
Dim funcRst As Boolean
Dim shortId As Integer
Dim realId As Integer
Expand Down Expand Up @@ -739,7 +750,7 @@ Public NotInheritable Class BilibiliApi
''' </summary>
''' <param name="userId"></param>
''' <returns></returns>
Public Shared Async Function GetUserInfoAsync(ByVal userId As String) As Task(Of HttpResponse)
Public Shared Async Function GetMemberInfoAsync(ByVal userId As String) As Task(Of HttpResponse)
Dim url = "https://space.bilibili.com/ajax/member/GetInfo"
Dim postData = $"mid={userId}&csrf={m_User.Token}"
Dim referer = "https://space.bilibili.com/" & userId
Expand All @@ -756,5 +767,26 @@ Public NotInheritable Class BilibiliApi
Dim url = $"https://api.live.bilibili.com/fans_medal/v1/fans_medal/get_fans_medal_info?source=1&uid={viewerUid}&target_id={upUid}"
Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """code"":0", 3)
End Function

''' <summary>
''' 获取登录Url(暂时是用于生成二维码)
''' </summary>
''' <returns></returns>
Public Shared Async Function GetLoginUrlAsync() As Task(Of HttpResponse)
Dim url = "https://passport.bilibili.com/qrcode/getLoginUrl"
Return Await HttpAsync.TryGetAsync(url, m_HttpHeadersParam, """code"":0", 3)
End Function

''' <summary>
''' 获取登录(扫码)结果,data值含义:-2 二维码过期或者已确认授权,登录成功、 -4 未扫码、-5 已扫码未确认、。
''' </summary>
''' <param name="oauthKey"></param>
''' <returns></returns>
Public Shared Async Function GetLoginInfoAsync(ByVal oauthKey As String) As Task(Of HttpResponse)
Dim url = "https://passport.bilibili.com/qrcode/getLoginInfo"
Dim postData = $"oauthKey={oauthKey}&gourl=https://www.bilibili.com/"
Dim referer = "https://passport.bilibili.com/login"
Return Await DoPostAsync(url, postData, referer)
End Function
#End Region
End Class
7 changes: 3 additions & 4 deletions source/Class/DanmuEntry/DanmuEntry.vb
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ Public NotInheritable Class DanmuEntry
''' </summary>
Private Shared Async Function ConfigureHimesByUserAsync() As Task(Of Boolean)
' 获取当前登录用户信息
Dim getRst = Await BilibiliApi.GetCurrentUserAsync
Dim getRst = Await BilibiliApi.GetCurrentUserInfoAsync
Dim json = getRst.Message
Dim funcRst = getRst.Success

Expand Down Expand Up @@ -1124,12 +1124,11 @@ Public NotInheritable Class DanmuEntry
Return status
End Function


Protected Friend Shared Async Function GetFansNickAsync(ByVal userId As String) As Task(Of String)
Dim funcRst = String.Empty

Try
Dim postRst = Await BilibiliApi.GetUserInfoAsync(userId)
Dim postRst = Await BilibiliApi.GetMemberInfoAsync(userId)
If Not postRst.Success Then Exit Try
Dim json = postRst.Message

Expand Down Expand Up @@ -1236,7 +1235,7 @@ Public NotInheritable Class DanmuEntry
End Function

Private Shared Async Function InternalGetUserIfnoAsync(ByVal viewerId As String) As Task(Of (Uid As String, Uname As String))
Dim getRst = Await BilibiliApi.GetUserInfoAsync(viewerId)
Dim getRst = Await BilibiliApi.GetMemberInfoAsync(viewerId)
If getRst.Success Then
Try
Dim root = MSJsSerializer.Deserialize(Of UserInfoEntity.Root)(getRst.Message)
Expand Down
41 changes: 41 additions & 0 deletions source/Class/Entity/LoginUrlEntity.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Namespace LoginUrlEntity
Public Class Data

''' <summary>
'''
''' </summary>
''' <returns></returns>
Public Property url As String

''' <summary>
'''
''' </summary>
''' <returns></returns>
Public Property oauthKey As String

End Class



Public Class Root
Inherits APIPostResponseBaseEntity.Root
''' <summary>
'''
''' </summary>
''' <returns></returns>
Public Property status As String

''' <summary>
'''
''' </summary>
''' <returns></returns>
Public Property ts As Integer

''' <summary>
'''
''' </summary>
''' <returns></returns>
Public Property data As Data
End Class
End Namespace

62 changes: 31 additions & 31 deletions source/Class/Tcp/TcpListener.vb
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,29 @@ Public Class TcpListener
m_Initialized = True
End Sub

Private Sub EnsureInitCancellationToken()
m_Cts = New CancellationTokenSource
m_Ct = m_Cts.Token
End Sub
''' <summary>
''' 连接到弹幕服务器
''' </summary>
Public Async Function ConnectAsync(ByVal roomId As Integer, ByVal userId As String, ByVal upId As String, Optional ByVal serverHost As String = "broadcastlv.chat.bilibili.com", Optional ByVal serverPort As Integer = 2243) As Task
If 0 = roomId Then
Throw New ArgumentException(String.Format(My.Resources.ArgumentOutOfRange & " 这个房间不存在" & RandomEmoji.Helpless, NameOf(roomId), ">0"))
End If
If Not Net2.NetHelper.IsConnectedToInternet() Then
Throw New ArgumentException("网络异常")
End If

Private Async Function EnsureNetConnectedAsync() As Task
While Not Net2.NetHelper.IsConnected()
If m_Ct.IsCancellationRequested Then Exit While
Await Task.Delay(6180)
End While
_UserId = userId
_UpId = upId
_RoomId = roomId
_ServerHost = serverHost
_ServerPort = serverPort

Await EnsurePreInitAsync()
EnsureInitCancellationToken()

' 确保网络可用再连接
Await EnsureNetConnectedAsync()
Await InternalConnectAsync(Me.RoomId, Me.UserId, Me.ServerHost, Me.ServerPort)
End Function

''' <summary>
Expand All @@ -254,29 +267,16 @@ Public Class TcpListener
m_Initialized = True
End Function

''' <summary>
''' 连接到弹幕服务器
''' </summary>
Public Async Function ConnectAsync(ByVal roomId As Integer, ByVal userId As String, ByVal upId As String, Optional ByVal serverHost As String = "broadcastlv.chat.bilibili.com", Optional ByVal serverPort As Integer = 2243) As Task
If 0 = roomId Then
Throw New ArgumentException(String.Format(My.Resources.ArgumentOutOfRange & " 这个房间不存在" & RandomEmoji.Helpless, NameOf(roomId), ">0"))
End If
If Not Net2.NetHelper.IsConnected() Then
Throw New ArgumentException("网络异常")
End If

_UserId = userId
_UpId = upId
_RoomId = roomId
_ServerHost = serverHost
_ServerPort = serverPort

Await EnsurePreInitAsync()
EnsureInitCancellationToken()
Private Sub EnsureInitCancellationToken()
m_Cts = New CancellationTokenSource
m_Ct = m_Cts.Token
End Sub

' 确保网络可用再连接
Await EnsureNetConnectedAsync()
Await InternalConnectAsync(Me.RoomId, Me.UserId, Me.ServerHost, Me.ServerPort)
Private Async Function EnsureNetConnectedAsync() As Task
While Not Net2.NetHelper.IsConnectedToInternet()
If m_Ct.IsCancellationRequested Then Exit While
Await Task.Delay(6180)
End While
End Function

''' <summary>
Expand Down
7 changes: 6 additions & 1 deletion source/Form/FrmMain.vb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Public Class FrmMain

Private Async Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
#If Not DEBUG Then
' 检测到被附加到其他进程(比如od?)调试时,强制退出
' 检测到被附加到其他进程(比如od?vs release调试)调试时,强制退出
If Win32API.IsDebuggerPresent Then
Me.Visible = False
MessageBox.Show(ShanXingTechQ2287190283, "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Stop)
Expand Down Expand Up @@ -212,6 +212,11 @@ Public Class FrmMain
''' </summary>
Private Async Function TryLoginAsync() As Task
Try
If Not Net2.NetHelper.IsConnectedToInternet() Then
Windows2.DrawTipsTask(Me, "网络异常 " & RandomEmoji.Helpless, 3000, False, False)
Return
End If

AddHandler DanmuEntry.UserEnsured, AddressOf UserEnsuredTask
AddHandler DanmuEntry.RoomRealIdEnsured, AddressOf RoomRealIdEnsuredTask

Expand Down
6 changes: 0 additions & 6 deletions source/Form/Login/FrmLoginFromBrowser.vb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ Imports ShanXingTech.Net2
''' </summary>
Public Class FrmLoginFromBrowser
#Region "属性区"
Public ReadOnly Property IsLogined() As Boolean?
Get
Return m_LoginedCookies?.Count > 0
End Get
End Property

Private m_LoginedUrl As String

Private m_LoginedCookies As CookieContainer
Expand Down
20 changes: 13 additions & 7 deletions source/Form/Login/FrmLoginFromCookies.vb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Imports ShanXingTech
Imports System.Net
Imports ShanXingTech

Public Class FrmLoginFromCookies

#Region "属性区"
Private Const CS_NOCLOSE = &H200

Expand All @@ -13,12 +15,6 @@ Public Class FrmLoginFromCookies
End Get
End Property

Public ReadOnly Property IsLogined() As Boolean?
Get
Return m_LoginedCookies?.Count > 0
End Get
End Property

Private m_LoginedCookies As Net.CookieContainer
Public ReadOnly Property LoginedCookies As Net.CookieContainer
Get
Expand All @@ -27,6 +23,16 @@ Public Class FrmLoginFromCookies
End Property
#End Region

Sub New()

' 此调用是设计器所必需的。
InitializeComponent()

' 在 InitializeComponent() 调用之后添加任何初始化。
Me.TopMost = True
Me.StartPosition = FormStartPosition.CenterParent
End Sub

Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim domain = txtDomain.Text.Trim
If domain.IsNullOrEmpty Then
Expand Down
Loading

0 comments on commit b0322f3

Please sign in to comment.