AliyunCore.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. };
  80. if (newItem.h5_url.StartsWith("//")) newItem.h5_url = "https:" + newItem.h5_url;
  81. if (newItem.pic.StartsWith("//")) newItem.pic = "https:" + newItem.pic;
  82. arr.Add(newItem);
  83. }
  84. result.message = "OK";
  85. result.reason = string.Empty;
  86. result.PromotionImg = arr;
  87. return result;
  88. }
  89. public QueryAccountBalanceResponse QueryAccountBalance()
  90. {
  91. return plus.QueryAccountBalance();
  92. }
  93. /// <summary>
  94. /// 获取这个地区下的所有ecs
  95. /// </summary>
  96. /// <param name="region"></param>
  97. /// <returns></returns>
  98. /// <exception cref="Exception"></exception>
  99. public DescribeInstancesResponseBodyInstances GetInstances(string region)
  100. {
  101. try
  102. {
  103. //查询实例信息
  104. var response = plus.DescribeInstances(region);
  105. var Instances = response.Body.Instances;
  106. return Instances;
  107. }
  108. catch (Exception ex)
  109. {
  110. throw new Exception(ex.Message);
  111. }
  112. }
  113. public DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets GetNetworkInterfaces(string region)
  114. {
  115. try
  116. {
  117. //查询实例信息
  118. var response = plus.DescribeNetworkInterfaces(region);
  119. var networkInterfaces = response.Body.NetworkInterfaceSets;
  120. return networkInterfaces;
  121. }
  122. catch (Exception ex)
  123. {
  124. throw new Exception(ex.Message);
  125. }
  126. }
  127. //public bool xxx(string region, string instanceId, string privateIp)
  128. //{
  129. // try
  130. // {
  131. // //查询实例信息
  132. // var instance = plus.DescribeInstanceAttribute(region, instanceId);
  133. // var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  134. // var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  135. // plus.DescribeNetworkInterfaces(region, instanceId)
  136. // //释放弹性网卡
  137. // ReleaseEipAddress("cn-beijing", allocationId);
  138. // //申请并绑定公网IP
  139. // EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  140. // return true;
  141. // }
  142. // catch (Exception ex)
  143. // {
  144. // throw new Exception(ex.Message);
  145. // }
  146. //}
  147. public (bool, string, string) ChangePublicIp(string privateIp)
  148. {
  149. try
  150. {
  151. var regions = _account.regions?.Split(',');
  152. foreach (var s in regions)
  153. {
  154. var region = s.Trim();
  155. if (string.IsNullOrEmpty(region)) continue;
  156. //获取这个地区下的所有网卡
  157. var list = GetNetworkInterfaces(region);
  158. if (list == null) continue;
  159. foreach (var networkInterface in list.NetworkInterfaceSet)
  160. {
  161. //找出网卡信息
  162. string networkInterfaceId = networkInterface.NetworkInterfaceId;
  163. var privateIps = networkInterface?.PrivateIpSets;
  164. var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  165. if (taraget == null) continue;
  166. //找到当前公网IP
  167. string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
  168. DescribeEipAddressesResponseBodyEipAddressesEipAddress currentEip;
  169. if (string.IsNullOrEmpty(currentPublicIp))
  170. {
  171. var currentEips = DescribeEipsAddresse(region, string.Empty);
  172. currentEip = currentEips?.Where(e => privateIp.Equals(e.PrivateIpAddress))?.FirstOrDefault();
  173. currentPublicIp = currentEip?.IpAddress;
  174. if (string.IsNullOrEmpty(currentPublicIp))
  175. {
  176. var emptyPrivateEips = currentEips?.Where(e => string.IsNullOrEmpty(e.PrivateIpAddress));
  177. foreach (var emptyEip in emptyPrivateEips)
  178. {
  179. var body = DescribeNetworkInterfaceAttribute(region, emptyEip.InstanceId);
  180. if (body != null)
  181. {
  182. if (body.PrivateIpAddress == privateIp)
  183. {
  184. currentEip = emptyEip;
  185. currentPublicIp = emptyEip?.IpAddress;
  186. break;
  187. }
  188. }
  189. }
  190. }
  191. }
  192. else
  193. {
  194. currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
  195. }
  196. if (currentEip == null) throw new Exception("没有找到 EIP");
  197. //第1步、获取当前的公网EIP
  198. var currentAllocationId = currentEip.AllocationId;
  199. //第2步、申请新的公网ip
  200. var eip = plus.AllocateEipAddress(region);
  201. var allocationId = eip.Body.AllocationId;
  202. string eipAddress = eip.Body.EipAddress;
  203. //第3步、解绑当前的公网ip
  204. plus.UnassociateEipAddress(region, currentAllocationId);
  205. //第4步、检查公网ip是否成功解绑
  206. string unPublicIpAddress = currentPublicIp;
  207. while (!string.IsNullOrEmpty(unPublicIpAddress))
  208. {
  209. var networkInterfaceState = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  210. var currentPrivateIpSet = networkInterfaceState?.Body?.PrivateIpSets?.PrivateIpSet;
  211. var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  212. unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
  213. if (string.IsNullOrEmpty(unPublicIpAddress)) break;
  214. Thread.Sleep(500);
  215. }
  216. //第5步、绑定新的公网ip
  217. var AssociateEipAddress =
  218. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
  219. //第5步、释放当前的公网IP
  220. plus.ReleaseEipAddress(region, currentAllocationId);
  221. return (true, currentPublicIp, eipAddress);
  222. }
  223. }
  224. throw new Exception("没有符合的数据");
  225. }
  226. catch (Exception ex)
  227. {
  228. throw new Exception(ex.Message);
  229. }
  230. }
  231. public bool EcsCreateNetworkInterface(string region, string instanceId)
  232. {
  233. try
  234. {
  235. //查询实例信息
  236. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  237. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  238. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  239. //申请弹性网卡
  240. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  241. string networkInterfaceId = response.Body.NetworkInterfaceId;
  242. string privateIpAddress = response.Body.PrivateIpAddress;
  243. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  244. //绑定网卡
  245. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  246. //申请并绑定公网IP
  247. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  248. return true;
  249. }
  250. catch (Exception ex)
  251. {
  252. throw new Exception(ex.Message);
  253. }
  254. }
  255. public List<DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSet> DescribeNetworkInterfaces(string region, string instanceId)
  256. {
  257. try
  258. {
  259. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  260. var list = response.Body.NetworkInterfaceSets.NetworkInterfaceSet;
  261. return list;
  262. }
  263. catch (Exception ex)
  264. {
  265. throw new Exception(ex.Message);
  266. }
  267. }
  268. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count, bool isPrimaryNetworkInterface = false)
  269. {
  270. try
  271. {
  272. //查询实例信息
  273. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  274. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  275. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  276. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  277. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  278. {
  279. string type = isPrimaryNetworkInterface ? "Primary" : "Secondary";
  280. if (type.Equals(e.Type))
  281. {
  282. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  283. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  284. //foreach (var privateIp in PrivateIpAddressList)
  285. //{
  286. // if (privateIp.Primary == true) continue;
  287. // var privateIpAddress = privateIp.PrivateIpAddress;
  288. // //申请公网ip
  289. // var eip = plus.AllocateEipAddress(region);
  290. // var allocationId = eip.Body.AllocationId;
  291. // string eipAddress = eip.Body.EipAddress;
  292. // //绑定公网ip
  293. // var AssociateEipAddress =
  294. // plus.AssociateEipAddress(
  295. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  296. //}
  297. //添加辅助私网
  298. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  299. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  300. foreach (var privateIpAddress in PrivateIpAddressList)
  301. {
  302. //申请公网ip
  303. var eip = plus.AllocateEipAddress(region);
  304. var allocationId = eip.Body.AllocationId;
  305. string eipAddress = eip.Body.EipAddress;
  306. //绑定公网ip
  307. var AssociateEipAddress =
  308. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  309. }
  310. return true;
  311. }
  312. }
  313. return false;
  314. }
  315. catch (Exception ex)
  316. {
  317. throw new Exception(ex.Message);
  318. }
  319. }
  320. /// <summary>
  321. /// 申请并绑定公网IP
  322. /// </summary>
  323. /// <param name="region"></param>
  324. /// <param name="instanceId"></param>
  325. /// <param name="privateIpAddress"></param>
  326. /// <returns></returns>
  327. /// <exception cref="Exception"></exception>
  328. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  329. {
  330. try
  331. {
  332. //申请公网ip
  333. var eip = plus.AllocateEipAddress(region);
  334. var allocationId = eip.Body.AllocationId;
  335. string eipAddress = eip.Body.EipAddress;
  336. //绑定公网ip
  337. var AssociateEipAddress =
  338. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  339. return true;
  340. }
  341. catch (Exception ex)
  342. {
  343. throw new Exception(ex.Message);
  344. }
  345. }
  346. public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
  347. {
  348. try
  349. {
  350. var response = plus.DescribeEipAddresses(region, eipAddress);
  351. var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
  352. return eip;
  353. }
  354. catch (Exception ex)
  355. {
  356. throw new Exception(ex.Message);
  357. }
  358. }
  359. public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipsAddresse(string region, string eipAddress)
  360. {
  361. try
  362. {
  363. var response = plus.DescribeEipAddresses(region, eipAddress);
  364. var eips = response?.Body?.EipAddresses?.EipAddress;
  365. return eips;
  366. }
  367. catch (Exception ex)
  368. {
  369. throw new Exception(ex.Message);
  370. }
  371. }
  372. public DescribeNetworkInterfaceAttributeResponseBody DescribeNetworkInterfaceAttribute(string region, string networkInterfaceId)
  373. {
  374. try
  375. {
  376. var response = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  377. var body = response?.Body;
  378. return body;
  379. }
  380. catch (Exception ex)
  381. {
  382. throw new Exception(ex.Message);
  383. }
  384. }
  385. public bool ReleaseEipAddress(string region, string allocationId)
  386. {
  387. try
  388. {
  389. plus.ReleaseEipAddress(region, allocationId);
  390. return true;
  391. }
  392. catch (Exception ex)
  393. {
  394. throw new Exception(ex.Message);
  395. }
  396. }
  397. }
  398. }