UnionCPSCore.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 Spire.Pdf.Exporting.XPS.Schema;
  10. using System.Xml.Linq;
  11. using System.Net;
  12. using TencentCloud.Mrs.V20200910.Models;
  13. using Microsoft.AspNetCore.Mvc;
  14. using Microsoft.AspNetCore.Authentication;
  15. using Spire.Pdf.Graphics;
  16. using Google.Protobuf.WellKnownTypes;
  17. using System.Threading.Channels;
  18. using TencentCloud.Weilingwith.V20230427.Models;
  19. using System.Threading;
  20. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  21. namespace molilian.core
  22. {
  23. public partial class UnionCPSCore
  24. {
  25. private static string _end_point;
  26. static UnionCPSCore()
  27. {
  28. _end_point = Environment.GetEnvironmentVariable("EndPoint");
  29. }
  30. public static async Task<ActionResult> GetLinkAsync(string channel, string ip, string oaid)
  31. {
  32. return channel switch
  33. {
  34. "eleme" => await EleAsync(ip, oaid),
  35. "meituan" => await MeituanAsync(ip, oaid),
  36. _ => await InvalidChannelAsync(ip, oaid),
  37. };
  38. }
  39. private static async Task<APIResult> InvalidChannelAsync(string ip, string oaid)
  40. {
  41. UnionCpsDTO result = new()
  42. {
  43. channel = CpsChannelEnum.invalid,
  44. end_point = _end_point,
  45. ip = ip,
  46. oaid = oaid
  47. };
  48. result.success = false;
  49. result.message = "无效渠道";
  50. _ = TkLogCore.CpsLogAsync(result);
  51. return new APIResult(new
  52. {
  53. result.success,
  54. result.message,
  55. channel = result.channel.ToString(),
  56. });
  57. }
  58. public static bool ShouldIgnoreRequest(TkConfigDTO config, CpsChannelEnum channel, string ip, string oaid, out string reason)
  59. {
  60. reason = string.Empty;
  61. try
  62. {
  63. // IP和流量控制
  64. string ignorePercentageCity = config.cpsIgnorePercentageCity;
  65. string? ipInfo = IP2RegionPlus.Search(ip);
  66. if (AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
  67. {
  68. reason = $"地区控制:{regionInfo}";
  69. return true;
  70. }
  71. int limit_num = config.cps_limit_per_ip_24h;
  72. if (limit_num > 0)
  73. {
  74. int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
  75. if (num > limit_num)
  76. {
  77. reason = "IP控制";
  78. return true;
  79. }
  80. num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
  81. if (num > limit_num)
  82. {
  83. reason = "OAID控制";
  84. return true;
  85. }
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. reason = "配置异常";
  91. }
  92. return false;
  93. }
  94. public static async Task<APIResult> EleAsync(string ip, string oaid)
  95. {
  96. UnionCpsDTO result = new()
  97. {
  98. channel = CpsChannelEnum.eleme,
  99. end_point = _end_point,
  100. ip = ip,
  101. oaid = oaid
  102. };
  103. var config = TkConfigCore.Get();
  104. //============================== 放弃转链-地区过滤 ==============================
  105. if (ShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  106. {
  107. result.success = false;
  108. result.message = "流量控制";
  109. result.reason = reason;
  110. _ = TkLogCore.CpsLogAsync(result);
  111. return new APIResult(new
  112. {
  113. result.success,
  114. result.message,
  115. result.reason,
  116. channel = result.channel.ToString(),
  117. });
  118. }
  119. 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";
  120. result.success = true;
  121. result.message = "OK";
  122. _ = TkLogCore.CpsLogAsync(result);
  123. return new APIResult(new
  124. {
  125. result.success,
  126. result.message,
  127. result.deeplink_url,
  128. channel = result.channel.ToString(),
  129. });
  130. }
  131. public static async Task<APIResult> MeituanAsync(string ip, string oaid)
  132. {
  133. UnionCpsDTO result = new()
  134. {
  135. channel = CpsChannelEnum.meituan,
  136. end_point = _end_point,
  137. ip = ip,
  138. oaid = oaid
  139. };
  140. var config = TkConfigCore.Get();
  141. //============================== 放弃转链-地区过滤 ==============================
  142. if (ShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
  143. {
  144. result.success = false;
  145. result.message = "流量控制";
  146. result.reason = reason;
  147. _ = TkLogCore.CpsLogAsync(result);
  148. return new APIResult(new
  149. {
  150. result.success,
  151. result.message,
  152. result.reason,
  153. channel = result.channel.ToString(),
  154. });
  155. }
  156. 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";
  157. result.success = true;
  158. result.message = "OK";
  159. _ = TkLogCore.CpsLogAsync(result);
  160. return new APIResult(new
  161. {
  162. result.success,
  163. result.message,
  164. result.deeplink_url,
  165. channel = result.channel.ToString(),
  166. });
  167. }
  168. }
  169. }