Skip to content
This repository has been archived by the owner on Dec 20, 2023. It is now read-only.

Commit

Permalink
修复动态获取失败的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Richasy committed Jul 28, 2023
1 parent ce1e4a5 commit 2b251c1
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 12 deletions.
31 changes: 23 additions & 8 deletions src/Lib/Lib.Implementation/HttpProvider/HttpProvider.Extension.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
// Copyright (c) Richasy. All rights reserved.

using System;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
using Bili.Lib.Interfaces;
using Bili.Models.App.Constants;
using Bili.Models.App.Other;
using Bili.Models.BiliBili;
using Bili.Models.gRPC;
using Newtonsoft.Json;

namespace Bili.Lib
Expand All @@ -19,8 +22,8 @@ namespace Bili.Lib
/// </summary>
public partial class HttpProvider
{
private static string _buvid;
private readonly IAuthorizeProvider _authenticationProvider;
private HttpClient _httpClient;
private bool _disposedValue;
private CookieContainer _cookieContainer;

Expand All @@ -40,7 +43,7 @@ public void Dispose()
{
await Task.Run(async () =>
{
response = await _httpClient.SendAsync(request, cancellationToken);
response = await HttpClient.SendAsync(request, cancellationToken);
});
await ThrowIfHasExceptionAsync(response);
}
Expand Down Expand Up @@ -83,17 +86,29 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (this._httpClient != null)
{
this._httpClient.Dispose();
}
HttpClient?.Dispose();
}

this._httpClient = null;
HttpClient = null;
_disposedValue = true;
}
}

private static string GetBuvid()
{
if (string.IsNullOrEmpty(_buvid))
{
var macAddress = NetworkInterface.GetAllNetworkInterfaces()
.Where(nic => nic.OperationalStatus == OperationalStatus.Up && nic.NetworkInterfaceType != NetworkInterfaceType.Loopback)
.Select(nic => nic.GetPhysicalAddress().ToString())
.FirstOrDefault();
var buvidObj = new Buvid(macAddress);
_buvid = buvidObj.Generate();
}

return _buvid;
}

private void InitHttpClient()
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Expand All @@ -108,7 +123,7 @@ private void InitHttpClient()
var client = new HttpClient(handler);
client.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = false, NoStore = false };
client.DefaultRequestHeaders.Add("accept", ServiceConstants.DefaultAcceptString);
this._httpClient = client;
this.HttpClient = client;
}

private async Task ThrowIfHasExceptionAsync(HttpResponseMessage response)
Expand Down
9 changes: 5 additions & 4 deletions src/Lib/Lib.Implementation/HttpProvider/HttpProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public partial class HttpProvider : IHttpProvider, IDisposable
/// <param name="authProvider">授权验证模块.</param>
public HttpProvider(IAuthorizeProvider authProvider)
{
this._authenticationProvider = authProvider;
_authenticationProvider = authProvider;
InitHttpClient();
}

Expand All @@ -38,14 +38,14 @@ public TimeSpan OverallTimeout
{
get
{
return this._httpClient.Timeout;
return HttpClient.Timeout;
}

set
{
try
{
this._httpClient.Timeout = value;
HttpClient.Timeout = value;
}
catch (InvalidOperationException exception)
{
Expand All @@ -60,7 +60,7 @@ public TimeSpan OverallTimeout
}

/// <inheritdoc/>
public HttpClient HttpClient { get => _httpClient; }
public HttpClient HttpClient { get; private set; }

/// <inheritdoc/>
public async Task<HttpRequestMessage> GetRequestMessageAsync(
Expand Down Expand Up @@ -150,6 +150,7 @@ public async Task<HttpRequestMessage> GetRequestMessageAsync(string url, IMessag
requestMessage.Headers.Add(Headers.Envoriment, GRPCConfig.Envorienment);
requestMessage.Headers.Add(Headers.TransferEncodingKey, Headers.TransferEncodingValue);
requestMessage.Headers.Add(Headers.TEKey, Headers.TEValue);
requestMessage.Headers.Add(Headers.Buvid, GetBuvid());

var messageBytes = grpcMessage.ToByteArray();

Expand Down
1 change: 1 addition & 0 deletions src/Models/Models.App/Constants/ServiceConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public static class Headers
public const string TransferEncodingValue = "chunked";
public const string TEKey = "TE";
public const string TEValue = "trailers";
public const string Buvid = "buvid";
}

public static class Settings
Expand Down
57 changes: 57 additions & 0 deletions src/Models/Models.gRPC/Buvid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) Richasy. All rights reserved.

#pragma warning disable SA1300 // Element should begin with upper-case letter
using System.Security.Cryptography;
using System.Text;

namespace Bili.Models.gRPC
{
/// <summary>
/// BUVID.
/// </summary>
public class Buvid
{
private readonly string _mac;

/// <summary>
/// Initializes a new instance of the <see cref="Buvid"/> class.
/// </summary>
/// <param name="macAddress">MAC 地址.</param>
public Buvid(string macAddress) => _mac = macAddress;

/// <summary>
/// 生成 buvid.
/// </summary>
/// <returns>buvid.</returns>
public string Generate()
{
var buvidPrefix = "XY";
var inputStrMd5 = GetMd5Hash(_mac.Replace(":", string.Empty));

var buvidRaw = new StringBuilder();
buvidRaw.Append(buvidPrefix);
buvidRaw.Append(inputStrMd5[2]);
buvidRaw.Append(inputStrMd5[12]);
buvidRaw.Append(inputStrMd5[22]);
buvidRaw.Append(inputStrMd5);

return buvidRaw.ToString();
}

private static string GetMd5Hash(string input)
{
using (var md5 = MD5.Create())
{
var inputBytes = Encoding.UTF8.GetBytes(input);
var hashBytes = md5.ComputeHash(inputBytes);
var sb = new StringBuilder();
foreach (var b in hashBytes)
{
sb.Append(b.ToString("x2"));
}

return sb.ToString();
}
}
}
}

0 comments on commit 2b251c1

Please sign in to comment.