SpotifyServiceImpl.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. package com.cyksj.service.register.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.util.RandomUtil;
  4. import cn.hutool.core.util.StrUtil;
  5. import cn.hutool.http.HttpRequest;
  6. import cn.hutool.http.HttpUtil;
  7. import cn.hutool.http.Method;
  8. import cn.hutool.json.JSONArray;
  9. import cn.hutool.json.JSONObject;
  10. import cn.hutool.json.JSONUtil;
  11. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  12. import com.cyksj.common.EnvCommonService;
  13. import com.cyksj.common.constant.TemplateEnum;
  14. import com.cyksj.common.util.Jsons;
  15. import com.cyksj.common.util.StringUtil;
  16. import com.cyksj.dto.RedisKey;
  17. import com.cyksj.mapper.ProxysMapper;
  18. import com.cyksj.mapper.RegisterOrderDonMapper;
  19. import com.cyksj.mapper.manage.coupon.CouponMapper;
  20. import com.cyksj.model.dto.WxMpTemplateData;
  21. import com.cyksj.model.dto.WxMpTemplateMessage;
  22. import com.cyksj.model.entity.Coupon;
  23. import com.cyksj.model.entity.CouponUser;
  24. import com.cyksj.model.entity.RegisterOrderDon;
  25. import com.cyksj.model.entity.SysConfig;
  26. import com.cyksj.model.request.SpotifyRegisterReq;
  27. import com.cyksj.model.response.ProxyInfo;
  28. import com.cyksj.model.views.WxAppTemplateView;
  29. import com.cyksj.redis.RedisService;
  30. import com.cyksj.service.mange.coupon.CouponCommonService;
  31. import com.cyksj.service.register.SpotifyService;
  32. import com.cyksj.service.sys.SysConfigService;
  33. import com.cyksj.service.template.TemplateCommonService;
  34. import com.cyksj.service.wechat.WeChatService;
  35. import lombok.RequiredArgsConstructor;
  36. import lombok.extern.slf4j.Slf4j;
  37. import org.apache.commons.lang3.StringUtils;
  38. import org.springframework.stereotype.Service;
  39. import java.net.InetSocketAddress;
  40. import java.net.Proxy;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Optional;
  45. import java.util.concurrent.CopyOnWriteArrayList;
  46. import java.util.concurrent.atomic.AtomicInteger;
  47. /**
  48. * @author chan
  49. * @date 2022/11/7 1:54 PM
  50. */
  51. @Service
  52. @Slf4j
  53. @RequiredArgsConstructor
  54. public class SpotifyServiceImpl implements SpotifyService {
  55. private final ProxysMapper proxysMapper;
  56. private final RegisterOrderDonMapper registerOrderDonMapper;
  57. private final RedisService redisService;
  58. private final EnvCommonService envCommonService;
  59. private final WeChatService weChatService;
  60. private final CouponMapper couponMapper;
  61. private final CouponCommonService couponCommonService;
  62. private final SysConfigService sysConfigService;
  63. private final TemplateCommonService templateCommonService;
  64. private static final List<String> User_Agent_LIST = List.of(
  65. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
  66. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
  67. "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
  68. "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
  69. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
  70. "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
  71. "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
  72. "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
  73. "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
  74. "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
  75. "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
  76. "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
  77. "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
  78. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
  79. "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
  80. "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52",
  81. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.11 TaoBrowser/2.0 Safari/536.11",
  82. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.71 Safari/537.1 LBBROWSER",
  83. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; LBBROWSER)",
  84. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E; LBBROWSER)",
  85. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.84 Safari/535.11 LBBROWSER",
  86. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
  87. "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; QQBrowser/7.0.3698.400)",
  88. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
  89. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SV1; QQDownload 732; .NET4.0C; .NET4.0E; 360SE)",
  90. "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 732; .NET4.0C; .NET4.0E)",
  91. "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)",
  92. "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
  93. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.89 Safari/537.1",
  94. "Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; zh-cn) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5",
  95. "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:2.0b13pre) Gecko/20110307 Firefox/4.0b13pre",
  96. "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:16.0) Gecko/20100101 Firefox/16.0",
  97. "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11",
  98. "Mozilla/5.0 (X11; U; Linux x86_64; zh-CN; rv:1.9.2.10) Gecko/20100922 Ubuntu/10.10 (maverick) Firefox/3.6.10");
  99. @Override
  100. public synchronized void register(Long registerOrderId){
  101. log.info("spotify 注册 orderId:{}",registerOrderId);
  102. RegisterOrderDon registerOrderDon = registerOrderDonMapper.selectById(registerOrderId);
  103. SpotifyRegisterReq registerReq = new SpotifyRegisterReq();
  104. SpotifyRegisterReq.AccountDetails accountDetails = new SpotifyRegisterReq.AccountDetails();
  105. SpotifyRegisterReq.AccountDetails.EmailAndPasswordIdentifier emailAndPasswordIdentifier = new SpotifyRegisterReq.AccountDetails.EmailAndPasswordIdentifier();
  106. emailAndPasswordIdentifier.setEmail(registerOrderDon.getEmail());
  107. emailAndPasswordIdentifier.setPassword(registerOrderDon.getPassword());
  108. accountDetails.setEmail_and_password_identifier(emailAndPasswordIdentifier);
  109. accountDetails.setBirthdate(Optional.ofNullable(registerOrderDon.getBirthYear()).orElse("1989") + "-" + Optional.ofNullable(registerOrderDon.getBirthMonth()).orElse("10") + "-" + Optional.ofNullable(registerOrderDon.getBirthDay()).orElse("06"));
  110. accountDetails.setDisplay_name(emailAndPasswordIdentifier.getEmail());
  111. accountDetails.setGender(1);
  112. registerReq.setAccount_details(accountDetails);
  113. Map<String,String> map = new HashMap<>();
  114. map.put("User-Agent", User_Agent_LIST.get(RandomUtil.randomInt(User_Agent_LIST.size())));
  115. map.put( "authority","spclient.wg.spotify.com");
  116. map.put( "method","POST");
  117. map.put("Host","spclient.wg.spotify.com");
  118. map.put("Accept", "*/*");
  119. map.put( "path","/signup/public/v2/account/create");
  120. map.put( "Referer", "https://www.spotify.com/");
  121. map.put("Content-Type", "application/json; charset=utf-8");
  122. map.put("Origin", "https://www.spotify.com");
  123. map.put("Connection", "keep-alive");
  124. map.put("sec-ch-ua", "\"Not?A_Brand\";v=\"8\", \"Chromium\";v=\"108\", \"Google Chrome\";v=\"108\"");
  125. map.put("sec-ch-ua-mobile", "?0");
  126. map.put("sec-ch-ua-platform", "Windows");
  127. map.put("Sec-Fetch-Dest", "empty");
  128. map.put("Sec-Fetch-Mode", "cors");
  129. map.put("Sec-Fetch-Site", "same-site");
  130. map.put("referrer-policy", "strict-origin-when-cross-origin");
  131. CopyOnWriteArrayList<ProxyInfo> proxyInfos = (CopyOnWriteArrayList<ProxyInfo>) redisService.get(RedisKey.IP_LIST);
  132. if (proxyInfos == null || proxyInfos.size() == 0) {
  133. //String res = HttpUtil.get("http://api.proxy.ipidea.io/getProxyIp?num=30&return_type=json&lb=1&sb=0&flow=1&regions=&protocol=http");
  134. //JSONObject jsonObject = JSONUtil.parseObj(res);
  135. //JSONArray data = jsonObject.getJSONArray("data");
  136. //proxyInfos = new CopyOnWriteArrayList<>(data.toList(ProxyInfo.class));
  137. String result = HttpUtil.get("https://dvapi.doveip.com/cmapi.php?rq=distribute&user=1031494735&token=ckc1MUFURnk3bi80MXVxeUFTZUh0Zz09&auth=0&geo=tr&agreement=0&timeout=10&num=1&type_ip=1&repeat=0");
  138. log.info("付费ip 请求返回 res:{}",result);
  139. /*
  140. * {
  141. * "errno": 200,
  142. * "msg": "Success",
  143. * "data": {
  144. * "geo": "mx", //国家
  145. * "ip": "47.253.12.97", //代理ip
  146. * "port": 40076, //代理端口
  147. * "d_ip": "201.162.169.86" //目标ip
  148. * "timeout": 600 //有效时间(秒)
  149. * }
  150. * }
  151. */
  152. JSONObject resObj = JSONUtil.parseObj(result);
  153. if (200 == resObj.getInt("errno")) {
  154. JSONObject data = resObj.getJSONObject("data");
  155. ProxyInfo proxyInfo = new ProxyInfo();
  156. proxyInfo.setPort(data.getInt("port"));
  157. proxyInfo.setIp(data.getStr("ip"));
  158. log.info("----------付费IP地址----------\n ip:{}",proxyInfo);
  159. proxyInfos = new CopyOnWriteArrayList<>();
  160. proxyInfos.add(proxyInfo);
  161. }else {
  162. log.error("----------获取 付费IP 错误 停止循环----------");
  163. return;
  164. }
  165. }
  166. AtomicInteger count = new AtomicInteger(1);
  167. while (count.get() <= 3) {
  168. boolean flag = false;
  169. count.addAndGet(1);
  170. for (ProxyInfo proxyInfo : proxyInfos) {
  171. try {
  172. log.info("执行:info:{}", proxyInfo);
  173. Proxy proxy = new Proxy(Proxy.Type.SOCKS,
  174. new InetSocketAddress(proxyInfo.getIp(), proxyInfo.getPort()));
  175. //OkHttpClient client = new OkHttpClient().newBuilder().proxy(proxy).build();
  176. //log.info("spotify request body :{}",JSONUtil.toJsonStr(registerReq));
  177. //RequestBody requestBody = FormBody.create(JSONUtil.toJsonStr(registerReq),MediaType.parse("application/json; charset=utf-8"));
  178. //
  179. //Request request = new Request.Builder()
  180. // .url("https://spclient.wg.spotify.com/signup/public/v2/account/create")
  181. // .post(requestBody)
  182. // .headers(Headers.of(map))
  183. // .build();
  184. //
  185. //okhttp3.Response response = client.newCall(request).execute();
  186. //ResponseBody body = response.body();
  187. HttpRequest request = new HttpRequest("https://spclient.wg.spotify.com/signup/public/v2/account/create");
  188. request.setProxy(proxy);
  189. request.body(JSONUtil.toJsonStr(registerReq));
  190. request.addHeaders(map);
  191. request.setMethod(Method.POST);
  192. request.setConnectionTimeout(15 * 1000);
  193. request.setReadTimeout(15 * 1000);
  194. String spotifyBody = request.execute().body();
  195. log.info("spotify 返回body:{}", spotifyBody);
  196. /*
  197. {
  198. "error":{
  199. "error_details":{
  200. "code":2,
  201. "title":"请检查你的详细信息,然后再试一次。",
  202. "body":"此电子邮件已关联至某个帐号。请登录你的帐号,或使用其他电子邮件注册。",
  203. "already_exists":{
  204. }
  205. }
  206. }
  207. }
  208. */
  209. /*
  210. {
  211. "success": {
  212. "username": "31naixo6v3uydhvcp63sya57ozae",
  213. "login_token": "N7N9gHZ3QeG74yHWAx8hAw"
  214. }
  215. }
  216. */
  217. JSONObject resObj = JSONUtil.parseObj(spotifyBody);
  218. log.info("spotify 返回:{}",resObj);
  219. JSONObject error = resObj.getJSONObject("error");
  220. if (error == null || error.isEmpty()) {
  221. JSONObject success = resObj.getJSONObject("success");
  222. if (StringUtils.isNotBlank(success.getStr("login_token")) || StringUtils.isNotBlank("username")) {
  223. log.info("注册成功!");
  224. registerOrderDon.setStatus(RegisterOrderDon.Status.complete);
  225. registerOrderDonMapper.updateById(registerOrderDon);
  226. //注册成功发放模板消息
  227. spotifyTemplateMsg(registerOrderDon, String.format("%s%s", envCommonService.getDomain(), "yinhe/web/sign/list"), true);
  228. //注册成功发放声破天优惠券
  229. sendSpotifyCoupon(registerOrderDon);
  230. flag = true;
  231. break;
  232. }else if(!resObj.getJSONObject("challenge").isEmpty()) {
  233. log.info("spotify 注册 被监测出代理服务 。 删除该代理 ip:{}", proxyInfo.getIp());
  234. proxyInfos.remove(proxyInfo);
  235. }
  236. }else {
  237. JSONObject error_details = error.getJSONObject("error_details");
  238. String errorMsg = error_details.getStr("body");
  239. Integer code = error_details.getInt("code");
  240. if (code.equals(4) || "你的设备似乎已连接至代理或 VPN 服务。请关闭此类服务,然后再试一次。".equals(errorMsg)) {
  241. log.info("spotify 注册 被监测出代理服务 。 删除该代理 ip:{}", proxyInfo.getIp());
  242. proxyInfos.remove(proxyInfo);
  243. continue;
  244. }
  245. //{"error":{"error_details":{"code":1,"invalid_argument":{"field_errors":[{"code":6,"message":"你的密码安全强度太低。设置安全强度较高的密码。","field":"FIELD_PASSWORD"}]},"title":"请检查你的详细信息,然后再试一次。"}}}
  246. //{"error":{"error_details":{"code":2,"title":"请检查你的详细信息,然后再试一次。","body":"此电子邮件已关联至某个帐号。请登录你的帐号,或使用其他电子邮件注册。","already_exists":{}}}}
  247. if(code == 1 || error_details.getJSONObject("invalid_argument") != null){
  248. JSONObject invalid_argument = error_details.getJSONObject("invalid_argument") ;
  249. JSONArray field_errors = invalid_argument.getJSONArray("field_errors");
  250. JSONObject errmsg = (JSONObject)field_errors.get(0);
  251. registerOrderDon.setErrorMsg(errmsg.getStr("message"));
  252. registerOrderDon.setStatus(RegisterOrderDon.Status.error);
  253. }else {
  254. registerOrderDon.setErrorMsg(error_details.getStr("body"));
  255. registerOrderDon.setStatus(RegisterOrderDon.Status.error);
  256. }
  257. registerOrderDonMapper.updateById(registerOrderDon);
  258. //注册失败发放模板消息
  259. spotifyTemplateMsg(registerOrderDon, String.format("%s%s", envCommonService.getDomain(), "yinhe/web/sign/list"), false);
  260. flag = true;
  261. break;
  262. }
  263. } catch (Exception e) {
  264. proxyInfos.remove(proxyInfo);
  265. log.error("错误 ip:{}", proxyInfo.getIp());
  266. log.error("spotify 注册 error:{}", StringUtil.getErrorText(e));
  267. break;
  268. }
  269. }
  270. if(flag){
  271. log.info("spotify 停止");
  272. break;
  273. }
  274. }
  275. redisService.set(RedisKey.IP_LIST,proxyInfos,360L);
  276. }
  277. private void sendSpotifyCoupon(RegisterOrderDon registerOrderDon) {
  278. //已经拥有声破天年费的不用发
  279. Integer exists = registerOrderDonMapper.isPaySpotifyYearTicket(registerOrderDon.getUserId());
  280. if (exists == null) {
  281. SysConfig spotify_register_success_cid = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, "spotify_register_success_cid").last("limit 1"));
  282. if (spotify_register_success_cid != null && StrUtil.isNotBlank(spotify_register_success_cid.getSysValue())) {
  283. Long spotifyRegisterSucCid = Long.parseLong(spotify_register_success_cid.getSysValue());
  284. Coupon coupon = couponMapper.selectById(spotifyRegisterSucCid);
  285. if (coupon != null) {
  286. couponCommonService.sendCouponToUser(registerOrderDon.getUserId(), spotifyRegisterSucCid, null, null, null, CouponUser.Channel.manual);
  287. }
  288. }
  289. }
  290. }
  291. private void spotifyTemplateMsg(RegisterOrderDon registerOrderDon, String url, Boolean isSuccess) throws Exception {
  292. Long userId = registerOrderDon.getUserId();
  293. WxAppTemplateView wxAppTemplateView = templateCommonService.getWxAppByUserId(userId, TemplateEnum.SPOTIFY_TEMPLATE.getDesc());
  294. if (wxAppTemplateView != null) {
  295. WxMpTemplateMessage templateMessage = new WxMpTemplateMessage()
  296. .setToUser(registerOrderDon.getOpenId())
  297. .setTemplateId(wxAppTemplateView.getTemplateId())
  298. .setUrl(url);
  299. WxMpTemplateMessage.TemplateData data = templateMessage.getData();
  300. data.setFirst(new WxMpTemplateData(isSuccess ? "Spotify账号已注册成功" : "Spotify账号注册失败,请修改后重新提交"));
  301. data.setKeyword1(new WxMpTemplateData(registerOrderDon.getPhone()));
  302. data.setKeyword2(new WxMpTemplateData(DateUtil.format(registerOrderDon.getCreatedTime(), "yyyy-MM-dd HH:mm:ss")));
  303. weChatService.sendTemplateMessage(weChatService.getAccessToken(), Jsons.toJson(templateMessage));
  304. }
  305. }
  306. }