Skip to content

Commit

Permalink
Merge pull request #657 from My-Responsitories/master
Browse files Browse the repository at this point in the history
random user-agent
  • Loading branch information
nilaoda authored Jul 29, 2023
2 parents 5557870 + c2d5633 commit dece855
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 20 deletions.
4 changes: 2 additions & 2 deletions BBDown.Core/Util/BilibiliBvConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace BBDown.Core.Util
{
//code from: https://www.zhihu.com/question/381784377/answer/1099438784
internal class BilibiliBvConverter
public class BilibiliBvConverter
{
private static string table = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
private static Dictionary<char, long> tr = new Dictionary<char, long>();
Expand Down Expand Up @@ -41,7 +41,7 @@ public static string Encode(long x)
return new string(r);
}

private static int[] s = { 11, 10, 3, 8, 4, 6 };
private static byte[] s = { 9, 8, 1, 6, 2, 4 };
private static long xor = 177451812;
private static long add = 8728348608;
}
Expand Down
29 changes: 24 additions & 5 deletions BBDown.Core/Util/HTTPUtil.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System.Net;
using System;
using System.Net;
using System.Net.Http.Headers;
using static BBDown.Core.Logger;

namespace BBDown.Core.Util
{
public class HTTPUtil
{

public static readonly HttpClient AppHttpClient = new(new HttpClientHandler
{
AllowAutoRedirect = true,
Expand All @@ -16,18 +18,35 @@ public class HTTPUtil
Timeout = TimeSpan.FromMinutes(2)
};

private static readonly Random random = new Random();
private static readonly string[] platforms = { "Windows NT 10.0; Win64", "Macintosh; Intel Mac OS X 10_15", "X11; Linux x86_64" };

private static string RandomVersion(int min, int max)
{
double version = random.NextDouble() * (max - min) + min;
return version.ToString("F3");
}

public static string GetRandomUserAgent()
{
string[] browsers = { $"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/{RandomVersion(80, 110)} Safari/537.36", $"Gecko/20100101 Firefox/{RandomVersion(80, 110)}" };
return $"Mozilla/5.0 ({platforms[random.Next(platforms.Length)]}) {browsers[random.Next(browsers.Length)]}";
}

public static string UserAgent { get; set; } = GetRandomUserAgent();

public static async Task<string> GetWebSourceAsync(string url)
{
using var webRequest = new HttpRequestMessage(HttpMethod.Get, url);
webRequest.Headers.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15");
webRequest.Headers.TryAddWithoutValidation("User-Agent", UserAgent);
webRequest.Headers.TryAddWithoutValidation("Accept-Encoding", "gzip, deflate");
webRequest.Headers.TryAddWithoutValidation("Cookie", (url.Contains("/ep") || url.Contains("/ss")) ? Config.COOKIE + ";CURRENT_FNVAL=4048;" : Config.COOKIE);
if (url.Contains("api.bilibili.com/pgc/player/web/playurl") || url.Contains("api.bilibili.com/pugv/player/web/playurl"))
webRequest.Headers.TryAddWithoutValidation("Referer", "https://www.bilibili.com");
if (url.Contains("api.bilibili.com"))
webRequest.Headers.TryAddWithoutValidation("Referer", "https://www.bilibili.com/");
webRequest.Headers.CacheControl = CacheControlHeaderValue.Parse("no-cache");
webRequest.Headers.Connection.Clear();

LogDebug("获取网页内容Url: {0}, Headers: {1}", url, webRequest.Headers);
LogDebug("获取网页内容: Url: {0}, Headers: {1}", url, webRequest.Headers);
var webResponse = (await AppHttpClient.SendAsync(webRequest, HttpCompletionOption.ResponseHeadersRead)).EnsureSuccessStatusCode();

string htmlCode = await webResponse.Content.ReadAsStringAsync();
Expand Down
14 changes: 1 addition & 13 deletions BBDown/BBDownUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,7 @@ public static async Task<string> GetAidByBVAsync(string bv)
if (bv.Length == 10)
{
// 能在本地就在本地
string TABLE = "fZodR9XQDSUm21yCkr6zBqiveYah8bt4xsWpHnJE7jL5VG3guMTKNPAwcF";
var BVChange = new Dictionary<char, byte>();
byte[] S = { 9, 8, 1, 6, 2, 4 };
long XOR = 177451812;
long ADD = 8728348608;
for (byte i = 0; i < 58; i++) BVChange.Add(TABLE[i], i);

long T = 0;
for (byte i = 0; i < 6; i++)
{
T += (long)Math.Pow(58, i) * BVChange[bv[S[i]]];
}
return ((T - ADD) ^ XOR).ToString();
return Core.Util.BilibiliBvConverter.Decode(bv).ToString();
}
else
{
Expand Down
3 changes: 3 additions & 0 deletions BBDown/CommandLineInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal class CommandLineInvoker
private readonly static Option<bool> AudioAscending = new(new string[] { "--audio-ascending" }, "音频升序(最小体积优先)");
private readonly static Option<bool> AllowPcdn = new(new string[] { "--allow-pcdn" }, "不替换PCDN域名, 仅在正常情况与--upos-host均无法下载时使用");
private readonly static Option<string> Language = new(new string[] { "--language" }, "设置混流的音频语言(代码), 如chi, jpn等");
private readonly static Option<string> UserAgent = new(new string[] { "--user-agent", "-ua" }, "指定user-agent, 否则使用随机user-agent");
private readonly static Option<string> Cookie = new(new string[] { "--cookie", "-c" }, "设置字符串cookie用以下载网页接口的会员内容");
private readonly static Option<string> AccessToken = new(new string[] { "--access-token", "-token" }, "设置access_token用以下载TV/APP接口的会员内容");
private readonly static Option<string> WorkDir = new(new string[] { "--work-dir" }, "设置程序的工作目录");
Expand Down Expand Up @@ -106,6 +107,7 @@ protected override MyOption GetBoundValue(BindingContext bindingContext)
if (bindingContext.ParseResult.HasOption(MultiFilePattern)) option.MultiFilePattern = bindingContext.ParseResult.GetValueForOption(MultiFilePattern)!;
if (bindingContext.ParseResult.HasOption(SelectPage)) option.SelectPage = bindingContext.ParseResult.GetValueForOption(SelectPage)!;
if (bindingContext.ParseResult.HasOption(Language)) option.Language = bindingContext.ParseResult.GetValueForOption(Language)!;
if (bindingContext.ParseResult.HasOption(UserAgent)) option.UserAgent = bindingContext.ParseResult.GetValueForOption(UserAgent)!;
if (bindingContext.ParseResult.HasOption(Cookie)) option.Cookie = bindingContext.ParseResult.GetValueForOption(Cookie)!;
if (bindingContext.ParseResult.HasOption(AccessToken)) option.AccessToken = bindingContext.ParseResult.GetValueForOption(AccessToken)!;
if (bindingContext.ParseResult.HasOption(Aria2cArgs)) option.Aria2cArgs = bindingContext.ParseResult.GetValueForOption(Aria2cArgs)!;
Expand Down Expand Up @@ -167,6 +169,7 @@ public static RootCommand GetRootCommand(Func<MyOption, Task> action)
MultiFilePattern,
SelectPage,
Language,
UserAgent,
Cookie,
AccessToken,
Aria2cArgs,
Expand Down
1 change: 1 addition & 0 deletions BBDown/MyOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal class MyOption
public string MultiFilePattern { get; set; } = "";
public string SelectPage { get; set; } = "";
public string Language { get; set; } = "";
public string UserAgent { get; set; } = "";
public string Cookie { get; set; } = "";
public string AccessToken { get; set; } = "";
public string Aria2cArgs { get; set; } = "";
Expand Down
1 change: 1 addition & 0 deletions BBDown/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ private static async Task DoWorkAsync(MyOption myOption)
Config.HOST = myOption.Host;
Config.EPHOST = myOption.EpHost;
Config.AREA = myOption.Area;
HTTPUtil.UserAgent = myOption.UserAgent == "" ? HTTPUtil.GetRandomUserAgent() : myOption.UserAgent;
Config.COOKIE = myOption.Cookie;
Config.TOKEN = myOption.AccessToken.Replace("access_token=", "");

Expand Down

0 comments on commit dece855

Please sign in to comment.