Skip to content

Commit

Permalink
优化登录逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
withsalt committed Apr 22, 2021
1 parent b958833 commit 5ddde62
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 40 deletions.
89 changes: 49 additions & 40 deletions src/BilibiliLiver/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,31 @@ public App(ILogger<App> logger

public async Task Run(params string[] args)
{
//登录
await LoginToBilibili();
_logger.LogInformation($"登录成功,登录账号为:{_config.UserSetting.Account}");
//获取直播间房间信息
LiveRoomStreamDataInfo liveRoomInfo = await _liveApi.GetRoomInfo(this._account);
if (liveRoomInfo == null)
try
{
_logger.LogError($"开启直播失败,无法获取直播间信息!");
return;
//登录
await LoginToBilibili();
_logger.LogInformation($"登录成功,登录账号为:{_config.UserSetting.Account}");
//获取直播间房间信息
LiveRoomStreamDataInfo liveRoomInfo = await _liveApi.GetRoomInfo(this._account);
if (liveRoomInfo == null)
{
_logger.LogError($"开启直播失败,无法获取直播间信息!");
return;
}
//测试FFmpeg
if (!await FFmpegTest())
{
_logger.LogError($"开启推流失败,未找到FFmpeg,请确认已经安装FFmpeg!");
return;
}
//开始执行ffmpeg推流
await UseFFmpegLive(liveRoomInfo.Rtmp.Addr + liveRoomInfo.Rtmp.Code);
}
//测试FFmpeg
if (!await FFmpegTest())
catch (Exception ex)
{
_logger.LogError($"开启推流失败,未找到FFmpeg,请确认已经安装FFmpeg!");
return;
_logger.LogError(ex.Message, ex);
}
//开始执行ffmpeg推流
await UseFFmpegLive(liveRoomInfo.Rtmp.Addr + liveRoomInfo.Rtmp.Code);
}

#region Private
Expand Down Expand Up @@ -134,7 +141,7 @@ private async Task<bool> UseFFmpegLive(string url)
{
throw new Exception("命令参数不能为空!");
}

var psi = new ProcessStartInfo
{
FileName = cmdName,
Expand Down Expand Up @@ -252,6 +259,7 @@ async Task LoginByPassword()
await LoginByPassword();
return;
}
Account account = null;
try
{
string decodeStr = AES128.AESDecrypt(fileStr, _config.AppSetting.Key, "40863a4f-7cbe-4be2-bb54-765233c83d25");
Expand All @@ -260,37 +268,38 @@ async Task LoginByPassword()
await LoginByPassword();
return;
}
Account account = JsonUtil.DeserializeJsonToObject<Account>(decodeStr);
//判断AccessToken是否有效
if (!ByPassword.IsTokenAvailable(account.AccessToken))
account = JsonUtil.DeserializeJsonToObject<Account>(decodeStr);
}
catch
{
await LoginByPassword();
return;
}
//判断AccessToken是否有效
if (account == null || !ByPassword.IsTokenAvailable(account.AccessToken) || account.UserName != _config.UserSetting.Account)
{
await LoginByPassword();
return;
}
//判断AccessToken是否需要续期
if (account.Expires_AccessToken != DateTime.MinValue
&& account.Expires_AccessToken.AddDays(-7) < DateTime.Now)
{
DateTime? dt = ByPassword.RefreshToken(account.AccessToken, account.RefreshToken);
if (dt == null)
{
await LoginByPassword();
return;
}
//判断AccessToken是否需要续期
if (account.Expires_AccessToken != DateTime.MinValue
&& account.Expires_AccessToken.AddDays(-7) < DateTime.Now)
{
DateTime? dt = ByPassword.RefreshToken(account.AccessToken, account.RefreshToken);
if (dt == null)
{
await LoginByPassword();
return;
}
account.Expires_AccessToken = dt.Value;
//更新
await WriteLoginDataToFile(account);
_account = account;
}
else
{
_account = account;
return;
}
account.Expires_AccessToken = dt.Value;
//更新
await WriteLoginDataToFile(account);
_account = account;
}
catch
else
{
await LoginByPassword();
_account = account;
return;
}
}
catch (Exception ex)
Expand Down
3 changes: 3 additions & 0 deletions src/BilibiliLiver/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -14,6 +15,8 @@ class Program
{
static async Task Main(string[] args)
{
Console.Title = "BilibiliLiver " + Assembly.GetExecutingAssembly().GetName().Version.ToString();

//编码注册
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
//DI
Expand Down

0 comments on commit 5ddde62

Please sign in to comment.