base.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using Dataoke;
  10. using System.Text.RegularExpressions;
  11. using Org.BouncyCastle.Ocsp;
  12. using System.Diagnostics;
  13. using TencentCloud.Fmu.V20191213.Models;
  14. using Microsoft.VisualBasic;
  15. using System.Text.Json;
  16. using static System.Runtime.InteropServices.JavaScript.JSType;
  17. using System.Security.Cryptography;
  18. using System.Security.Policy;
  19. using Google.Protobuf.WellKnownTypes;
  20. using Microsoft.AspNetCore.Components;
  21. using Org.BouncyCastle.Pqc.Crypto.Ntru;
  22. namespace molilian.core
  23. {
  24. public partial class JdUnionPlus
  25. {
  26. string _app_key;
  27. string _app_secret;
  28. static Random _random = new Random();
  29. public JdUnionPlus(string app_key, string app_secret)
  30. {
  31. _app_key = app_key;
  32. _app_secret = app_secret;
  33. }
  34. private string _sign(string method, string version, string param_json, string timestamp)
  35. {
  36. Dictionary<string, string> sysParameters = new()
  37. {
  38. { "360buy_param_json", param_json },
  39. { "access_token", ""},
  40. { "app_key", _app_key },
  41. { "method", method},
  42. { "timestamp", timestamp },
  43. { "v", version }
  44. };
  45. StringBuilder stringBuilder = new();
  46. foreach ((var key, var value) in sysParameters)
  47. {
  48. stringBuilder.Append(key);
  49. stringBuilder.Append(value);
  50. }
  51. string strParameters = _app_secret + stringBuilder.ToString() + _app_secret;
  52. string result = strParameters.MD5(false, false);
  53. return result;
  54. }
  55. public string Request(string method, string version, string param_json)
  56. {
  57. var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  58. var sign = _sign(method, version, param_json, timestamp);
  59. timestamp = timestamp.UrlEncode().Replace(" ", "+");
  60. string api = "https://api.jd.com/routerjson";
  61. string url = api;
  62. url += "?access_token=";
  63. url += $"&app_key={_app_key}";
  64. url += $"&method={method.UrlEncode()}";
  65. url += $"&v={version.UrlEncode()}";
  66. url += $"&sign={sign}";
  67. url += $"&360buy_param_json={param_json.UrlEncode()}";
  68. url += $"&timestamp={timestamp}";
  69. var log = new LoggerLibrary("jdunion", method).Info(url);
  70. var client = new WebClientUtility();
  71. var response = client.Request(url);
  72. var body = response.Body();
  73. _ = log.Info(body).SaveAsync();
  74. //var root = body.Convert2JsonElement();
  75. return body;
  76. }
  77. public async Task<string> RequestAsync(string method, string version, string param_json, CancellationToken cancellationToken = default)
  78. {
  79. var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  80. var sign = _sign(method, version, param_json, timestamp);
  81. timestamp = timestamp.UrlEncode().Replace(" ", "+");
  82. string api = "https://api.jd.com/routerjson";
  83. string url = api;
  84. url += "?access_token=";
  85. url += $"&app_key={_app_key}";
  86. url += $"&method={method.UrlEncode()}";
  87. url += $"&v={version.UrlEncode()}";
  88. url += $"&sign={sign}";
  89. url += $"&360buy_param_json={param_json.UrlEncode()}";
  90. url += $"&timestamp={timestamp}";
  91. var log = new LoggerLibrary("jdunion", method).Info(url);
  92. var client = new WebClientUtility();
  93. var response = await client.RequestAsync(url, "GET", cancellationToken);
  94. var body = response.Body();
  95. _ = log.Info(body).SaveAsync();
  96. return body;
  97. }
  98. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  99. {
  100. reason = string.Empty;
  101. try
  102. {
  103. // IP和流量控制
  104. var config = TkConfigCore.Get();
  105. string ignorePercentageCity = config.jdIgnorePercentageCity;
  106. string? ipInfo = IP2RegionPlus.Search(ip);
  107. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  108. {
  109. reason = $"地区控制:{regionInfo}";
  110. return true;
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. reason = "配置异常";
  116. }
  117. return false;
  118. }
  119. public static bool FlowControlIgnoreRequest(out string reason)
  120. {
  121. reason = string.Empty;
  122. try
  123. {
  124. // IP和流量控制
  125. var config = TkConfigCore.Get();
  126. int ignorePercentage = config.jdIgnorePercentage;
  127. if (ignorePercentage == 0) return false;
  128. var randomValue = (decimal)_random.Next(0, 100);
  129. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  130. if (result)
  131. {
  132. reason = "流量控制";
  133. return true;
  134. }
  135. }
  136. catch (Exception ex)
  137. {
  138. reason = $"配置异常";
  139. }
  140. return false;
  141. }
  142. }
  143. }