AliyunCore.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. using dodohold.core;
  2. using Dataoke;
  3. using Tea;
  4. using AlibabaCloud.SDK.BssOpenApi20171214.Models;
  5. using AlibabaCloud.SDK.Ecs20140526.Models;
  6. using AlibabaCloud.SDK.Vpc20160428.Models;
  7. using TencentCloud.Dbbrain.V20210527.Models;
  8. using Org.BouncyCastle.Bcpg.OpenPgp;
  9. using TencentCloud.Tke.V20180525.Models;
  10. using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeInstancesResponseBody;
  11. using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeNetworkInterfacesResponseBody;
  12. using static AlibabaCloud.SDK.Vpc20160428.Models.DescribeEipAddressesResponseBody.DescribeEipAddressesResponseBodyEipAddresses;
  13. using AlibabaCloud.SDK.ImageSearch20210501.Models;
  14. using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeNetworkInterfacesResponseBody.DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets;
  15. using TencentCloud.Cam.V20190116.Models;
  16. using static AlibabaCloud.SDK.Vpc20160428.Models.DescribeNetworkAclAttributesResponseBody;
  17. using static AlibabaCloud.SDK.Ecs20140526.Models.DescribeNetworkInterfaceAttributeResponseBody;
  18. namespace molilian.core
  19. {
  20. public partial class AliyunCore
  21. {
  22. private AliyunPlus plus;
  23. public AliyunPoolDTO _account;
  24. public AliyunCore(int id)
  25. {
  26. _account = new DBContext.Table("aliyun_pool").Get<AliyunPoolDTO>(id);
  27. if (_account == null) throw new Exception("无效账号");
  28. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  29. }
  30. public AliyunCore(AliyunPoolDTO item)
  31. {
  32. _account = item;
  33. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  34. }
  35. public AliyunCore(string accessKeyId, string accessKeySecret)
  36. {
  37. plus = new AliyunPlus(accessKeyId, accessKeySecret);
  38. }
  39. public async Task<SearchByPicResponse> SearchByPicAsync(string base64String, string pid)
  40. {
  41. return await plus.SearchByPicAsync(base64String, pid);
  42. }
  43. public async Task<PromotionQueryDTO> picSimilaritemAsync(string pic, string pid, PromotionQueryDTO result)
  44. {
  45. var response = await plus.SearchByPicAsync(pic, pid);
  46. if (response == null)
  47. {
  48. result.message = "调用失败";
  49. result.reason = "内部错误";
  50. return result;
  51. }
  52. result.success = (bool)response.Body.Success;
  53. if (!result.success)
  54. {
  55. result.message = "调用失败";
  56. result.reason = $"{response.Body.Code}:{response.Body.Message}";
  57. return result;
  58. }
  59. List<PromotionQueryItemDTO> arr = [];
  60. foreach (var e in response.Body.Data.Auctions)
  61. {
  62. var item = e.Result;
  63. PromotionQueryItemDTO newItem = new()
  64. {
  65. itemId = item.ItemId,
  66. h5_url = string.IsNullOrEmpty(item.CouponShareUrl) ? item.Url : item.CouponShareUrl,
  67. deeplink_url = string.IsNullOrEmpty(item.DeeplinkCouponShareUrl) ? item.DeeplinkUrl : item.DeeplinkCouponShareUrl,
  68. userType = $"{item.UserType}",
  69. shopTitle = item.ShopTitle,
  70. itemName = item.Title,
  71. pic = item.PicUrl,
  72. price = item.ReservePrice,
  73. promotionPrice = item.PriceAfterCoupon,
  74. couponAmount = $"{item.CouponAmount}",
  75. couponEffectiveStartTime = item.CouponStartTime,
  76. couponEffectiveEndTime = item.CouponEndTime,
  77. sellCountStr = $"{item.Volume}",
  78. provcity = item.Provcity,
  79. exposeTracks = $"https://api.molilian.com/tracks/track?track_id=7&unique_id={item.ItemId}",
  80. clickTracks = $"https://api.molilian.com/tracks/track?track_id=8&unique_id={item.ItemId}"
  81. };
  82. if (newItem.h5_url.StartsWith("//")) newItem.h5_url = "https:" + newItem.h5_url;
  83. if (newItem.pic.StartsWith("//")) newItem.pic = "https:" + newItem.pic;
  84. arr.Add(newItem);
  85. }
  86. result.message = "OK";
  87. result.reason = string.Empty;
  88. result.PromotionImg = arr;
  89. return result;
  90. }
  91. public async Task<PromotionQueryDTO> itemSimilaritemAsync(string itemids, string pid, PromotionQueryDTO result)
  92. {
  93. var response = await plus.GetProductInfoByIds(itemids, pid);
  94. if (response == null)
  95. {
  96. result.message = "调用失败";
  97. result.reason = "内部错误";
  98. return result;
  99. }
  100. result.success = (bool)response.Body.Success;
  101. if (!result.success)
  102. {
  103. result.message = "调用失败";
  104. result.reason = $"{response.Body.Code}:{response.Body.Message}";
  105. return result;
  106. }
  107. List<PromotionQueryItemDTO> arr = [];
  108. foreach (var e in response.Body.Data.Auctions)
  109. {
  110. var item = e.Result;
  111. PromotionQueryItemDTO newItem = new()
  112. {
  113. itemId = item.ItemId,
  114. h5_url = string.IsNullOrEmpty(item.CouponShareUrl) ? item.Url : item.CouponShareUrl,
  115. deeplink_url = string.IsNullOrEmpty(item.DeeplinkCouponShareUrl) ? item.DeeplinkUrl : item.DeeplinkCouponShareUrl,
  116. userType = $"{item.UserType}",
  117. shopTitle = item.ShopTitle,
  118. itemName = item.Title,
  119. pic = item.PicUrl,
  120. price = item.ReservePrice,
  121. promotionPrice = item.PriceAfterCoupon,
  122. couponAmount = $"{item.CouponAmount}",
  123. couponEffectiveStartTime = item.CouponStartTime,
  124. couponEffectiveEndTime = item.CouponEndTime,
  125. sellCountStr = $"{item.Volume}",
  126. provcity = item.Provcity,
  127. exposeTracks = $"https://api.molilian.com/tracks/track?track_id=7&unique_id={item.ItemId}",
  128. clickTracks = $"https://api.molilian.com/tracks/track?track_id=8&unique_id={item.ItemId}"
  129. };
  130. if (newItem.h5_url.StartsWith("//")) newItem.h5_url = "https:" + newItem.h5_url;
  131. if (newItem.pic.StartsWith("//")) newItem.pic = "https:" + newItem.pic;
  132. arr.Add(newItem);
  133. }
  134. result.message = "OK";
  135. result.reason = string.Empty;
  136. result.PromotionImg = arr;
  137. return result;
  138. }
  139. public QueryAccountBalanceResponse QueryAccountBalance()
  140. {
  141. return plus.QueryAccountBalance();
  142. }
  143. /// <summary>
  144. /// 获取这个地区下的所有ecs
  145. /// </summary>
  146. /// <param name="region"></param>
  147. /// <returns></returns>
  148. /// <exception cref="Exception"></exception>
  149. public DescribeInstancesResponseBodyInstances GetInstances(string region)
  150. {
  151. try
  152. {
  153. //查询实例信息
  154. var response = plus.DescribeInstances(region);
  155. var Instances = response.Body.Instances;
  156. return Instances;
  157. }
  158. catch (Exception ex)
  159. {
  160. throw new Exception(ex.Message);
  161. }
  162. }
  163. public DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets GetNetworkInterfaces(string region)
  164. {
  165. try
  166. {
  167. //查询实例信息
  168. var response = plus.DescribeNetworkInterfaces(region);
  169. var networkInterfaces = response.Body.NetworkInterfaceSets;
  170. return networkInterfaces;
  171. }
  172. catch (Exception ex)
  173. {
  174. throw new Exception(ex.Message);
  175. }
  176. }
  177. //public bool xxx(string region, string instanceId, string privateIp)
  178. //{
  179. // try
  180. // {
  181. // //查询实例信息
  182. // var instance = plus.DescribeInstanceAttribute(region, instanceId);
  183. // var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  184. // var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  185. // plus.DescribeNetworkInterfaces(region, instanceId)
  186. // //释放弹性网卡
  187. // ReleaseEipAddress("cn-beijing", allocationId);
  188. // //申请并绑定公网IP
  189. // EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  190. // return true;
  191. // }
  192. // catch (Exception ex)
  193. // {
  194. // throw new Exception(ex.Message);
  195. // }
  196. //}
  197. public async Task<(bool, string, string)> ChangePublicIpAsync(string privateIp)
  198. {
  199. LoggerLibrary log = new LoggerLibrary("ChangePublicIp");
  200. log.Info($"=====更改公网IP:{privateIp}=====");
  201. try
  202. {
  203. var regions = _account.regions?.Split(',');
  204. foreach (var s in regions)
  205. {
  206. var region = s.Trim();
  207. if (string.IsNullOrEmpty(region)) continue;
  208. //获取这个地区下的所有网卡
  209. var list = GetNetworkInterfaces(region);
  210. if (list == null) continue;
  211. foreach (var networkInterface in list.NetworkInterfaceSet)
  212. {
  213. //找出网卡信息
  214. string networkInterfaceId = networkInterface.NetworkInterfaceId;
  215. var privateIps = networkInterface?.PrivateIpSets;
  216. var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  217. if (taraget == null) continue;
  218. //找到当前公网IP
  219. string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
  220. DescribeEipAddressesResponseBodyEipAddressesEipAddress currentEip;
  221. if (string.IsNullOrEmpty(currentPublicIp))
  222. {
  223. await Task.Delay(500);
  224. var currentEips = DescribeEipsAddresse(region, string.Empty);
  225. currentEip = currentEips?.Where(e => privateIp.Equals(e.PrivateIpAddress))?.FirstOrDefault();
  226. currentPublicIp = currentEip?.IpAddress;
  227. if (string.IsNullOrEmpty(currentPublicIp))
  228. {
  229. var emptyPrivateEips = currentEips?.Where(e => string.IsNullOrEmpty(e.PrivateIpAddress));
  230. foreach (var emptyEip in emptyPrivateEips)
  231. {
  232. await Task.Delay(500);
  233. var body = DescribeNetworkInterfaceAttribute(region, emptyEip.InstanceId);
  234. if (body != null)
  235. {
  236. if (body.PrivateIpAddress == privateIp)
  237. {
  238. currentEip = emptyEip;
  239. currentPublicIp = emptyEip?.IpAddress;
  240. break;
  241. }
  242. }
  243. }
  244. }
  245. }
  246. else
  247. {
  248. await Task.Delay(500);
  249. currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
  250. }
  251. log.Info($"找到当前公网IP:{currentPublicIp}");
  252. if (currentEip == null) throw new Exception("没有找到 EIP");
  253. //第1步、获取当前的公网EIP
  254. var currentAllocationId = currentEip.AllocationId;
  255. log.Info($"获取当前的公网EIP:{currentAllocationId}");
  256. //第2步、申请新的公网ip
  257. await Task.Delay(500);
  258. var eip = plus.AllocateEipAddress(region);
  259. var allocationId = eip.Body.AllocationId;
  260. string eipAddress = eip.Body.EipAddress;
  261. if (string.IsNullOrEmpty(eipAddress))
  262. {
  263. log.Info("申请新的公网ip 可能出现异常", eip.Convert2Json());
  264. }
  265. else
  266. {
  267. log.Info($"申请新的公网ip:{eipAddress}");
  268. }
  269. //第3步、解绑当前的公网ip
  270. await Task.Delay(500);
  271. plus.UnassociateEipAddress(region, currentAllocationId);
  272. log.Info("解绑当前的公网ip");
  273. //第4步、检查公网ip是否成功解绑
  274. string unPublicIpAddress = currentPublicIp;
  275. log.Info("检查公网ip是否成功解绑");
  276. while (!string.IsNullOrEmpty(unPublicIpAddress))
  277. {
  278. await Task.Delay(500);
  279. var networkInterfaceState = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  280. var currentPrivateIpSet = networkInterfaceState?.Body?.PrivateIpSets?.PrivateIpSet;
  281. var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  282. unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
  283. log.Info($"{unPublicIpAddress} 还没解绑");
  284. if (string.IsNullOrEmpty(unPublicIpAddress)) break;
  285. await Task.Delay(500);
  286. }
  287. log.Info("公网ip成功解绑");
  288. await Task.Delay(1500);
  289. //第5步、绑定新的公网ip
  290. var AssociateEipAddress =
  291. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
  292. log.Info($"绑定新的公网ip", AssociateEipAddress.Convert2Json());
  293. await Task.Delay(500);
  294. //第5步、释放当前的公网IP
  295. plus.ReleaseEipAddress(region, currentAllocationId);
  296. log.Info($"释放当前的公网IP");
  297. return (true, currentPublicIp, eipAddress);
  298. }
  299. }
  300. throw new Exception("没有符合的数据");
  301. }
  302. catch (Exception ex)
  303. {
  304. log.Info($"发生异常:{ex.Message}");
  305. throw;
  306. }
  307. finally
  308. {
  309. log.Info($"=====结束更改公网IP:{privateIp}=====");
  310. log.Save();
  311. }
  312. }
  313. public bool EcsCreateNetworkInterface(string region, string instanceId)
  314. {
  315. try
  316. {
  317. //查询实例信息
  318. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  319. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  320. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  321. //申请弹性网卡
  322. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  323. string networkInterfaceId = response.Body.NetworkInterfaceId;
  324. string privateIpAddress = response.Body.PrivateIpAddress;
  325. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  326. //绑定网卡
  327. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  328. //申请并绑定公网IP
  329. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  330. return true;
  331. }
  332. catch (Exception ex)
  333. {
  334. throw new Exception(ex.Message);
  335. }
  336. }
  337. public List<DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSet> DescribeNetworkInterfaces(string region, string instanceId)
  338. {
  339. try
  340. {
  341. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  342. var list = response.Body.NetworkInterfaceSets.NetworkInterfaceSet;
  343. return list;
  344. }
  345. catch (Exception ex)
  346. {
  347. throw new Exception(ex.Message);
  348. }
  349. }
  350. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count, bool isPrimaryNetworkInterface = false)
  351. {
  352. try
  353. {
  354. //查询实例信息
  355. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  356. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  357. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  358. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  359. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  360. {
  361. string type = isPrimaryNetworkInterface ? "Primary" : "Secondary";
  362. if (type.Equals(e.Type))
  363. {
  364. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  365. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  366. //foreach (var privateIp in PrivateIpAddressList)
  367. //{
  368. // if (privateIp.Primary == true) continue;
  369. // var privateIpAddress = privateIp.PrivateIpAddress;
  370. // //申请公网ip
  371. // var eip = plus.AllocateEipAddress(region);
  372. // var allocationId = eip.Body.AllocationId;
  373. // string eipAddress = eip.Body.EipAddress;
  374. // //绑定公网ip
  375. // var AssociateEipAddress =
  376. // plus.AssociateEipAddress(
  377. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  378. //}
  379. //添加辅助私网
  380. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  381. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  382. foreach (var privateIpAddress in PrivateIpAddressList)
  383. {
  384. //申请公网ip
  385. var eip = plus.AllocateEipAddress(region);
  386. var allocationId = eip.Body.AllocationId;
  387. string eipAddress = eip.Body.EipAddress;
  388. //绑定公网ip
  389. var AssociateEipAddress =
  390. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  391. }
  392. return true;
  393. }
  394. }
  395. return false;
  396. }
  397. catch (Exception ex)
  398. {
  399. throw new Exception(ex.Message);
  400. }
  401. }
  402. /// <summary>
  403. /// 申请并绑定公网IP
  404. /// </summary>
  405. /// <param name="region"></param>
  406. /// <param name="instanceId"></param>
  407. /// <param name="privateIpAddress"></param>
  408. /// <returns></returns>
  409. /// <exception cref="Exception"></exception>
  410. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  411. {
  412. try
  413. {
  414. //申请公网ip
  415. var eip = plus.AllocateEipAddress(region);
  416. var allocationId = eip.Body.AllocationId;
  417. string eipAddress = eip.Body.EipAddress;
  418. //绑定公网ip
  419. var AssociateEipAddress =
  420. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  421. return true;
  422. }
  423. catch (Exception ex)
  424. {
  425. throw new Exception(ex.Message);
  426. }
  427. }
  428. public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
  429. {
  430. try
  431. {
  432. var response = plus.DescribeEipAddresses(region, eipAddress);
  433. var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
  434. return eip;
  435. }
  436. catch (Exception ex)
  437. {
  438. throw new Exception(ex.Message);
  439. }
  440. }
  441. public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipsAddresse(string region, string eipAddress)
  442. {
  443. try
  444. {
  445. var response = plus.DescribeEipAddresses(region, eipAddress);
  446. var eips = response?.Body?.EipAddresses?.EipAddress;
  447. return eips;
  448. }
  449. catch (Exception ex)
  450. {
  451. throw new Exception(ex.Message);
  452. }
  453. }
  454. public DescribeNetworkInterfaceAttributeResponseBody DescribeNetworkInterfaceAttribute(string region, string networkInterfaceId)
  455. {
  456. try
  457. {
  458. var response = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  459. var body = response?.Body;
  460. return body;
  461. }
  462. catch (Exception ex)
  463. {
  464. throw new Exception(ex.Message);
  465. }
  466. }
  467. public bool ReleaseEipAddress(string region, string allocationId)
  468. {
  469. try
  470. {
  471. plus.ReleaseEipAddress(region, allocationId);
  472. return true;
  473. }
  474. catch (Exception ex)
  475. {
  476. throw new Exception(ex.Message);
  477. }
  478. }
  479. }
  480. }