base.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 DyUnionPlus
  25. {
  26. string _app_key;
  27. string _app_secret;
  28. const string _base_url = "https://ecom.pangolin-sdk-toutiao.com";
  29. public DyUnionPlus(string app_key, string app_secret)
  30. {
  31. _app_key = app_key;
  32. _app_secret = app_secret;
  33. }
  34. public string _sign(string req_id, int timestamp, string data)
  35. {
  36. string signStr = $"app_id={_app_key}&data={data}&req_id={req_id}&timestamp={timestamp}{_app_secret}";
  37. return signStr.MD5();
  38. }
  39. public async Task<DyDataDTO> ParseAsync(string content, DyDataDTO result, CancellationToken cancellationToken = default)
  40. {
  41. string path = "/command_parse";
  42. string post = new { command = content, share_type = new int[] { 1, 3, 5 } }.Convert2Json();
  43. var body = await RequestAsync(path, post, cancellationToken);
  44. //{"code":500000,"desc":"command parse error:商品没有加入精选联盟"}
  45. //{"code":500000,"desc":"command parse error:商家已设置该商品仅指定作者可推广,请您更换其他商品"}
  46. //{"code":0,"desc":"","data":{"command_type":1,"product_info":{"product_id":3660263780149019445,"share_info":{"deeplink":"snssdk1128://lynxview?surl=https%3A%2F%2Flf-dy-sourcecdn-tos.bytegecko.com%2Fobj%2Fbyte-gurd-source%2F1325%2Fgecko%2Fresource%2Ffe_lynx_distribution_landing%2Fapp%2Ftemplate.js&hide_nav_bar=1&hide_status_bar=1&token=ijRcTEAk&distribution_ab=1&alliance_distribution=1","share_command":"","zlink":"","share_link":""}}}}
  47. var root = body.Convert2JsonElement();
  48. int code = root.Read<int>("code");
  49. if (code == 0)
  50. {
  51. var share_info = result.link_type switch
  52. {
  53. LinkTypeEnum.live => root.ElementRead("data").ElementRead("live_info").ElementRead("share_info"),
  54. _ => root.ElementRead("data").ElementRead("product_info").ElementRead("share_info"),
  55. };
  56. result.deeplink_url = share_info.Read<string>("deeplink");
  57. result.taoToken = share_info.Read<string>("share_command");
  58. result.shortLinkurl = share_info.Read<string>("share_link");
  59. result.success = true;
  60. result.message = "OK";
  61. }
  62. else
  63. {
  64. //{"code":500000,"desc":"command parse error:商品没有加入精选联盟"}
  65. //{"code":500000,"desc":"command parse error:商家已设置该商品仅指定作者可推广,请您更换其他商品"}
  66. string desc = root.Read<string>("desc", string.Empty);
  67. desc = desc.Replace("command parse error:", "");
  68. result.reason = desc;
  69. result.success = false;
  70. result.message = "转链失败";
  71. result.reason = "";
  72. result.success = false;
  73. }
  74. return result;
  75. }
  76. public async Task<string> AggregateH5Async(string material_id, CancellationToken cancellationToken = default)
  77. {
  78. //string path = "/aggregate/h5";
  79. //string post = new { material_id }.Convert2Json();
  80. //material_id = "1796636629603354";
  81. //string path = "/life/product/detail";
  82. //string post = new
  83. //{
  84. // product_id_list = new string[] { material_id },
  85. //}.Convert2Json();
  86. string path = "/life/product/search";
  87. string post = new
  88. {
  89. keyword = material_id,
  90. }.Convert2Json();
  91. var body = await RequestAsync(path, post, cancellationToken);
  92. return body;
  93. }
  94. public async Task<string> RequestAsync(string path, string data, CancellationToken cancellationToken = default)
  95. {
  96. int timestamp = DateTime.Now.Convert2UnixTimestamp();
  97. string req_id = Guid.NewGuid().ToString();
  98. var sign = _sign(req_id, timestamp, data);
  99. string url = $"{_base_url}{path}";
  100. var postData = new
  101. {
  102. app_id = _app_key,
  103. timestamp,
  104. sign,
  105. req_id,
  106. data
  107. }.Convert2Json();
  108. var log = new LoggerLibrary("pangolin", path.Replace("/", "_")).Info(postData);
  109. var client = new WebClientUtility();
  110. client.SetContentType("application/json");
  111. client.Post(postData);
  112. var response = await client.RequestAsync(url, "POST", cancellationToken);
  113. var body = response.Body();
  114. _ = log.Info(body).SaveAsync();
  115. //var root = body.Convert2JsonElement();
  116. return body;
  117. }
  118. public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  119. {
  120. reason = string.Empty;
  121. try
  122. {
  123. // IP和流量控制
  124. var config = TkConfigCore.Get();
  125. int ignorePercentage = config.dyIgnorePercentage;
  126. if (ignorePercentage == 0) return false;
  127. Random random = new Random();
  128. var randomValue = (decimal)random.Next(0, 100);
  129. bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  130. if (result) reason = "流量控制";
  131. return result;
  132. }
  133. catch (Exception ex)
  134. {
  135. reason = $"配置异常";
  136. return true;
  137. }
  138. }
  139. //public static bool ShouldIgnoreRequest(string ip, string oaid, out string reason)
  140. //{
  141. // reason = string.Empty;
  142. // try
  143. // {
  144. // // IP和流量控制
  145. // var config = TkConfigCore.Get();
  146. // string ignorePercentageCity = config.dyIgnorePercentageCity;
  147. // string? ipInfo = IP2RegionPlus.Search(ip);
  148. // if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  149. // {
  150. // reason = $"地区控制:{regionInfo}";
  151. // return true;
  152. // }
  153. // int ignorePercentage = config.dyIgnorePercentage;
  154. // if (ignorePercentage == 0) return false;
  155. // Random random = new Random();
  156. // var randomValue = (decimal)random.Next(0, 100);
  157. // bool result = randomValue <= ignorePercentage; // 如果生成的随机数小于忽略百分比,则返回 true,表示需要忽略请求
  158. // if (result) reason = "流量控制";
  159. // int limit_num = config.dy_limit_per_ip_24h;
  160. // if (limit_num > 0)
  161. // {
  162. // int num = TkLogCore.getClientRequestTotalByIp(TkChannelEnum.dy, ip);
  163. // if (num > limit_num)
  164. // {
  165. // reason = "IP控制";
  166. // return true;
  167. // }
  168. // }
  169. // limit_num = config.dy_limit_per_oaid_24h;
  170. // if (limit_num > 0)
  171. // {
  172. // int num = TkLogCore.getClientRequestTotalByOAID(TkChannelEnum.dy, oaid);
  173. // if (num > limit_num)
  174. // {
  175. // reason = "OAID控制";
  176. // return true;
  177. // }
  178. // }
  179. // return result;
  180. // }
  181. // catch (Exception ex)
  182. // {
  183. // reason = $"配置异常";
  184. // return true;
  185. // }
  186. //}
  187. }
  188. }