AliyunCore.cs 23 KB

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