UnionCpsPlus.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Google.Protobuf.WellKnownTypes;
  19. using System.Threading.Channels;
  20. using TencentCloud.Tione.V20211111.Models;
  21. namespace molilian.core
  22. {
  23. public partial class UnionCpsPlus
  24. {
  25. private static string _end_point;
  26. static UnionCpsPlus()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. public static UnionCpsDTO GetFormattedObject(CpsChannelEnum channel, string ip = "", string oaid = "")
  31. {
  32. return new()
  33. {
  34. channel = channel,
  35. end_point = _end_point,
  36. ip = ip,
  37. oaid = oaid
  38. };
  39. }
  40. #region CPS 相关
  41. public static async Task<APIResult> EleAsync(string ip, string oaid)
  42. {
  43. UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
  44. var config = TkConfigCore.Get();
  45. //============================== 放弃转链-地区过滤 ==============================
  46. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  47. {
  48. result.success = false;
  49. result.message = "流量控制";
  50. result.reason = reason;
  51. _ = TkLogCore.CpsLogAsync(result);
  52. return new APIResult(new
  53. {
  54. result.success,
  55. result.message,
  56. result.reason,
  57. channel = result.channel.ToString(),
  58. });
  59. }
  60. result.deeplink_url = "eleme://web?action=ali.open.nav&module=h5&packageName=me.ele&bc_fl_src=locallife_wtzt_0-0-ADGROUPID-__REQID__-2&url=https%3A%2F%2Ffc.ele.me%2Fa%2FMWJhZDM1OTY5ZTg5MTFlYzllYWEwMDE2M2UxM2FkMTE%3D%3Fscene%3D8d6c64c7f62c48ea83e88383530edd2b%26o2i_1st_clk%3D__CLICK_ID__&fastmode=1";
  61. result.success = true;
  62. result.message = "OK";
  63. _ = TkLogCore.CpsLogAsync(result);
  64. return new APIResult(new
  65. {
  66. result.success,
  67. result.message,
  68. result.deeplink_url,
  69. channel = result.channel.ToString(),
  70. });
  71. }
  72. public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  73. {
  74. UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
  75. var config = TkConfigCore.Get();
  76. //============================== 放弃转链-地区过滤 ==============================
  77. if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  78. {
  79. result.success = false;
  80. result.message = "流量控制";
  81. result.reason = reason;
  82. _ = TkLogCore.CpsLogAsync(result);
  83. return new APIResult(new
  84. {
  85. result.success,
  86. result.message,
  87. result.reason,
  88. channel = result.channel.ToString(),
  89. });
  90. }
  91. result.deeplink_url = "imeituan://www.meituan.com/web?lch=cps:waimai:3:0f860c7cc90569fb14bc1fc4a4d8c057:001:33:445029&url=https%3A%2F%2Fclick.meituan.com%2Ft%3Ft%3D1%26c%3D2%26p%3DnvaI7r5zjmJB";
  92. result.success = true;
  93. result.message = "OK";
  94. _ = TkLogCore.CpsLogAsync(result);
  95. return new APIResult(new
  96. {
  97. result.success,
  98. result.message,
  99. result.deeplink_url,
  100. channel = result.channel.ToString(),
  101. });
  102. }
  103. public static bool CpsShouldIgnoreRequest(TkConfigDTO config, CpsChannelEnum channel, string ip, string oaid, out string reason)
  104. {
  105. reason = string.Empty;
  106. try
  107. {
  108. // IP和流量控制
  109. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  110. string? ipInfo = IP2RegionPlus.Search(ip);
  111. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  112. {
  113. reason = $"地区控制:{regionInfo}";
  114. return true;
  115. }
  116. int limit_num = config.cps_limit_per_ip_24h;
  117. if (limit_num > 0)
  118. {
  119. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  120. if (num > limit_num)
  121. {
  122. reason = "IP控制";
  123. return true;
  124. }
  125. num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  126. if (num > limit_num)
  127. {
  128. reason = "OAID控制";
  129. return true;
  130. }
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. reason = "配置异常";
  136. }
  137. return false;
  138. }
  139. #endregion
  140. }
  141. }