| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Dataoke;
- using System.Text.RegularExpressions;
- using Org.BouncyCastle.Ocsp;
- using System.Diagnostics;
- using TencentCloud.Fmu.V20191213.Models;
- using Microsoft.VisualBasic;
- using System.Text.Json;
- using static System.Runtime.InteropServices.JavaScript.JSType;
- using System.Security.Cryptography;
- using Google.Protobuf.WellKnownTypes;
- using System.Threading.Channels;
- using TencentCloud.Tione.V20211111.Models;
- using COSXML.Network;
- namespace molilian.core
- {
- public partial class UnionCpsCore
- {
- private static string _end_point;
- static UnionCpsCore()
- {
- _end_point = Environment.GetEnvironmentVariable("EndPoint");
- }
- public static UnionCpsDTO GetFormattedObject(string channel, string ip = "", string oaid = "")
- {
- return new()
- {
- channel = channel,
- end_point = _end_point,
- ip = ip,
- oaid = oaid
- };
- }
- #region CPS 相关
- public static async Task<APIResult> UnionCpsAsync(string channelStr, string style, string ip, string oaid)
- {
- var channel_list = channelStr.Split(',');
- List<dynamic> list = new List<dynamic>();
- foreach (var channel in channel_list)
- {
- var result = await CpsCouponAsync(channel, style, ip, oaid);
- list.Add(new
- {
- result.success,
- result.message,
- result.title,
- result.sub_title,
- result.deeplink_url,
- result.token,
- result.channel,
- });
- }
- return new APIResult(new
- {
- data = list,
- });
- }
- public static async Task<UnionCpsDTO> CpsCouponAsync(string channel, string style, string ip, string oaid)
- {
- UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
- if (string.IsNullOrEmpty(result.channel))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "无效渠道";
- _ = TkLogCore.CpsLogAsync(result);
- return result;
- }
- var config = TkConfigCore.Get();
- //============================== 放弃转链-地区过滤 ==============================
- if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
- {
- result.success = false;
- result.message = "流量控制";
- result.reason = reason;
- _ = TkLogCore.CpsLogAsync(result);
- return result;
- }
- var account = CpsPoolCore.GetOne(result.channel);
- if (account == null)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "没有匹配账号";
- _ = TkLogCore.CpsLogAsync(result);
- return result;
- }
- result.accountId = account.id;
- result.accountName = account.name;
- result.deeplink_url = account.deeplink;
- result.token = account.token;
- result.category = account.category;
- result.pid = account.pid;
- result.title = account.title;
- var style_record = CpsPoolCore.GetTitle(account, style);
- if (style_record != null)
- {
- result.title = style_record.title;
- result.sub_title = style_record.sub_title;
- }
- result.success = true;
- result.message = "OK";
- _ = TkLogCore.CpsLogAsync(result);
- return result;
- //return new APIResult(new
- //{
- // result.success,
- // result.message,
- // result.reason,
- // result.channel,
- //});
- //return new APIResult(new
- //{
- // result.success,
- // result.message,
- // result.title,
- // result.deeplink_url,
- // result.token,
- // result.channel,
- //});
- }
- public static async Task<APIResult> CpsCouponAsync22(string channel, string style, string ip, string oaid)
- {
- UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
- if (string.IsNullOrEmpty(result.channel))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "无效渠道";
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- channel,
- });
- }
- var config = TkConfigCore.Get();
- //============================== 放弃转链-地区过滤 ==============================
- if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
- {
- result.success = false;
- result.message = "流量控制";
- result.reason = reason;
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- result.channel,
- });
- }
- var account = CpsPoolCore.GetOne(result.channel);
- if (account == null)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "没有匹配账号";
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- result.channel,
- });
- }
- result.accountId = account.id;
- result.accountName = account.name;
- result.deeplink_url = account.deeplink;
- result.token = account.token;
- result.category = account.category;
- result.pid = account.pid;
- result.title = account.title;
- var style_record = CpsPoolCore.GetTitle(account, style);
- if (style_record != null)
- {
- result.title = style_record.title;
- result.sub_title = style_record.sub_title;
- }
- result.success = true;
- result.message = "OK";
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.title,
- result.sub_title,
- result.deeplink_url,
- result.token,
- result.channel,
- });
- }
- public static async Task<APIResult> TbtCouponAsync(string content, string channel, string ip, string oaid)
- {
- UnionCpsDTO result = GetFormattedObject(channel, ip, oaid);
- if (string.IsNullOrEmpty(result.channel))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "无效渠道";
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- channel,
- });
- }
- AlimamaPlus alimama = null;
- content = content.UrlDecode();
- content = AlimamaPlus.FakeItemUrl(content, string.Empty);
- result.rawContent = content;
- if (string.IsNullOrEmpty(content))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "无效的参数";
- result.rawContent = content;
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.reason,
- result.message,
- channel = result.channel.ToString(),
- });
- }
- try
- {
- string reason;
- if (AlimamaPlus.FilterRiskLink(ip, oaid, content, out reason) ||
- AlimamaPlus.ShouldIgnoreRequest(ip, oaid, string.Empty, 0, out reason))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = reason;
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- channel = result.channel.ToString(),
- });
- }
- var account = await TkPoolCore.GetOneAsync(TkPoolCore.TkAction.parse, false, string.Empty, 0);
- if (account == null)
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "没有匹配账号";
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.reason,
- channel = result.channel.ToString(),
- });
- }
- result.accountId = account.id;
- result.accountName = account.name;
- alimama = new AlimamaPlus(account);
- //result = await alimama.UnionCoupon2Async(content, result);
- TkDataDTO data = new();
- data = await alimama.alimamaParseAsync(content, data);
- result.success = data.success;
- result.reason = data.reason;
- result.message = data.success ? "OK" : data.message;
- if (result.success)
- {
- result.token = data.taoToken;
- //result.deeplink_url = data.deeplink_url;
- result.couponAmount = data.couponAmount;
- result.itemId = data.itemId;
- }
- }
- catch (Exception ex)
- {
- if (ex.Message.Contains("was canceled"))
- {
- result.success = false;
- result.message = "放弃转链";
- result.reason = "请求超时";
- }
- else
- {
- _ = new LoggerLibrary("unionCoupon", "tb_error")
- .Info(ip, oaid)
- .Info(content)
- .Info(ex.Message, ex.StackTrace)
- .SaveAsync();
- NotifyCore.Notify(new NifyMessage
- {
- message = $"【转链异常TB】\n{content}\n\n{ex.Message}\n{ex.StackTrace}",
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- result.success = false;
- result.message = "放弃转链";
- result.reason = "转链接口异常";
- }
- }
- _ = TkLogCore.CpsLogAsync(result);
- return new APIResult(new
- {
- result.success,
- result.message,
- result.title,
- result.sub_title,
- result.deeplink_url,
- result.token,
- result.channel,
- });
- }
- //public static async Task<APIResult> EleAsync(string ip, string oaid)
- //{
- // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.eleme, ip, oaid);
- // var config = TkConfigCore.Get();
- // //============================== 放弃转链-地区过滤 ==============================
- // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
- // {
- // result.success = false;
- // result.message = "流量控制";
- // result.reason = reason;
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.reason,
- // channel = result.channel.ToString(),
- // });
- // }
- // var account = ElePoolCore.GetOne();
- // if (account == null)
- // {
- // result.success = false;
- // result.message = "放弃转链";
- // result.reason = "没有匹配账号";
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.reason,
- // channel = result.channel.ToString(),
- // });
- // }
- // result.accountId = account.id;
- // result.accountName = account.name;
- // result.deeplink_url = account.deeplink;
- // result.success = true;
- // result.message = "OK";
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.deeplink_url,
- // channel = result.channel.ToString(),
- // });
- //}
- //public static async Task<APIResult> MeituanAsync(string ip, string oaid)
- //{
- // UnionCpsDTO result = GetFormattedObject(CpsChannelEnum.meituan, ip, oaid);
- // var config = TkConfigCore.Get();
- // //============================== 放弃转链-地区过滤 ==============================
- // if (CpsShouldIgnoreRequest(config, result.channel, ip, oaid, out string reason))
- // {
- // result.success = false;
- // result.message = "流量控制";
- // result.reason = reason;
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.reason,
- // channel = result.channel.ToString(),
- // });
- // }
- // var account = MeituanPoolCore.GetOne();
- // if (account == null)
- // {
- // result.success = false;
- // result.message = "放弃转链";
- // result.reason = "没有匹配账号";
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.reason,
- // channel = result.channel.ToString(),
- // });
- // }
- // result.accountId = account.id;
- // result.accountName = account.name;
- // result.deeplink_url = account.deeplink;
- // result.success = true;
- // result.message = "OK";
- // _ = TkLogCore.CpsLogAsync(result);
- // return new APIResult(new
- // {
- // result.success,
- // result.message,
- // result.deeplink_url,
- // channel = result.channel.ToString(),
- // });
- //}
- public static bool CpsShouldIgnoreRequest(TkConfigDTO config, string channel, string ip, string oaid, out string reason)
- {
- reason = string.Empty;
- try
- {
- // IP和流量控制
- string ignorePercentageCity = config.cpsIgnorePercentageCity;
- string? ipInfo = IP2RegionPlus.Search(ip);
- if (!TestParseCore.InWhitelist(ip, oaid) && AlimamaPlus.IgnoreRegionIncluded(ignorePercentageCity, ipInfo, out string regionInfo))
- {
- reason = $"地区控制:{regionInfo}";
- return true;
- }
- int limit_num = config.cps_limit_per_ip_24h;
- if (limit_num > 0)
- {
- int num = TkLogCore.getClientRequestTotalByIp(channel, ip);
- if (num > limit_num)
- {
- reason = "IP控制";
- return true;
- }
- }
- limit_num = config.cps_limit_per_oaid_24h;
- if (limit_num > 0)
- {
- int num = TkLogCore.getClientRequestTotalByOAID(channel, oaid);
- if (num > limit_num)
- {
- reason = "OAID控制";
- return true;
- }
- }
- }
- catch (Exception ex)
- {
- reason = "配置异常";
- }
- return false;
- }
- #endregion
- }
- }
|