AliyunCore.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. namespace molilian.core
  16. {
  17. public partial class AliyunCore
  18. {
  19. private AliyunPlus plus;
  20. public AliyunPoolDTO _account;
  21. public AliyunCore(int id)
  22. {
  23. _account = new DBContext.Table("aliyun_pool").Get<AliyunPoolDTO>(id);
  24. if (_account == null) throw new Exception("无效账号");
  25. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  26. }
  27. public AliyunCore(AliyunPoolDTO item)
  28. {
  29. _account = item;
  30. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  31. }
  32. public AliyunCore(string accessKeyId, string accessKeySecret)
  33. {
  34. plus = new AliyunPlus(accessKeyId, accessKeySecret);
  35. }
  36. public async Task<SearchByPicResponse> SearchByPicAsync(string base64String, string pid)
  37. {
  38. return await plus.SearchByPicAsync(base64String, pid);
  39. }
  40. public async Task<PromotionQueryDTO> picSimilaritemAsync(string pic, string pid, PromotionQueryDTO result)
  41. {
  42. var response = await plus.SearchByPicAsync(pic, pid);
  43. if (response == null)
  44. {
  45. result.message = "调用失败";
  46. result.reason = "内部错误";
  47. return result;
  48. }
  49. result.success = (bool)response.Body.Success;
  50. if (!result.success)
  51. {
  52. result.message = "调用失败";
  53. result.reason = $"{response.Body.Code}:{response.Body.Message}";
  54. return result;
  55. }
  56. List<PromotionQueryItemDTO> arr = [];
  57. foreach (var e in response.Body.Data.Auctions)
  58. {
  59. var item = e.Result;
  60. PromotionQueryItemDTO newItem = new()
  61. {
  62. itemId = item.ItemId,
  63. h5_url = string.IsNullOrEmpty(item.CouponShareUrl) ? item.Url : item.CouponShareUrl,
  64. deeplink_url = string.IsNullOrEmpty(item.DeeplinkCouponShareUrl) ? item.DeeplinkUrl : item.DeeplinkCouponShareUrl,
  65. userType = $"{item.UserType}",
  66. shopTitle = item.ShopTitle,
  67. itemName = item.Title,
  68. pic = item.PicUrl,
  69. price = item.ReservePrice,
  70. promotionPrice = item.PriceAfterCoupon,
  71. couponAmount = $"{item.CouponAmount}",
  72. couponEffectiveStartTime = item.CouponStartTime,
  73. couponEffectiveEndTime = item.CouponEndTime,
  74. sellCountStr = $"{item.Volume}",
  75. provcity = item.Provcity
  76. };
  77. if (newItem.h5_url.StartsWith("//")) newItem.h5_url = "https:" + newItem.h5_url;
  78. if (newItem.pic.StartsWith("//")) newItem.pic = "https:" + newItem.pic;
  79. arr.Add(newItem);
  80. }
  81. result.message = "OK";
  82. result.reason = string.Empty;
  83. result.PromotionImg = arr;
  84. return result;
  85. }
  86. public QueryAccountBalanceResponse QueryAccountBalance()
  87. {
  88. return plus.QueryAccountBalance();
  89. }
  90. /// <summary>
  91. /// 获取这个地区下的所有ecs
  92. /// </summary>
  93. /// <param name="region"></param>
  94. /// <returns></returns>
  95. /// <exception cref="Exception"></exception>
  96. public DescribeInstancesResponseBodyInstances GetInstances(string region)
  97. {
  98. try
  99. {
  100. //查询实例信息
  101. var response = plus.DescribeInstances(region);
  102. var Instances = response.Body.Instances;
  103. return Instances;
  104. }
  105. catch (Exception ex)
  106. {
  107. throw new Exception(ex.Message);
  108. }
  109. }
  110. public DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets GetNetworkInterfaces(string region)
  111. {
  112. try
  113. {
  114. //查询实例信息
  115. var response = plus.DescribeNetworkInterfaces(region);
  116. var networkInterfaces = response.Body.NetworkInterfaceSets;
  117. return networkInterfaces;
  118. }
  119. catch (Exception ex)
  120. {
  121. throw new Exception(ex.Message);
  122. }
  123. }
  124. //public bool xxx(string region, string instanceId, string privateIp)
  125. //{
  126. // try
  127. // {
  128. // //查询实例信息
  129. // var instance = plus.DescribeInstanceAttribute(region, instanceId);
  130. // var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  131. // var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  132. // plus.DescribeNetworkInterfaces(region, instanceId)
  133. // //释放弹性网卡
  134. // ReleaseEipAddress("cn-beijing", allocationId);
  135. // //申请并绑定公网IP
  136. // EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  137. // return true;
  138. // }
  139. // catch (Exception ex)
  140. // {
  141. // throw new Exception(ex.Message);
  142. // }
  143. //}
  144. public (bool, string, string) ChangePublicIp(string privateIp)
  145. {
  146. try
  147. {
  148. var regions = _account.regions?.Split(',');
  149. foreach (var s in regions)
  150. {
  151. var region = s.Trim();
  152. if (string.IsNullOrEmpty(region)) continue;
  153. //获取这个地区下的所有网卡
  154. var list = GetNetworkInterfaces(region);
  155. if (list == null) continue;
  156. foreach (var networkInterface in list.NetworkInterfaceSet)
  157. {
  158. //找出网卡信息
  159. string networkInterfaceId = networkInterface.NetworkInterfaceId;
  160. var privateIps = networkInterface?.PrivateIpSets;
  161. var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  162. if (taraget == null) break;
  163. //找到当前公网IP
  164. string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
  165. if (string.IsNullOrEmpty(currentPublicIp)) throw new Exception("没有找到公网IP");
  166. //第1步、获取当前的公网EIP
  167. var currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
  168. var currentAllocationId = currentEip.AllocationId;
  169. //第2步、申请新的公网ip
  170. var eip = plus.AllocateEipAddress(region);
  171. var allocationId = eip.Body.AllocationId;
  172. string eipAddress = eip.Body.EipAddress;
  173. //第3步、解绑当前的公网ip
  174. plus.UnassociateEipAddress(region, currentAllocationId);
  175. //第4步、检查公网ip是否成功解绑
  176. string unPublicIpAddress = currentPublicIp;
  177. while (!string.IsNullOrEmpty(unPublicIpAddress))
  178. {
  179. var networkInterfaceState = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  180. var currentPrivateIpSet = networkInterfaceState?.Body?.PrivateIpSets?.PrivateIpSet;
  181. var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  182. unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
  183. if (string.IsNullOrEmpty(unPublicIpAddress)) break;
  184. Thread.Sleep(500);
  185. }
  186. //第5步、绑定新的公网ip
  187. var AssociateEipAddress =
  188. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
  189. //第5步、释放当前的公网IP
  190. plus.ReleaseEipAddress(region, currentAllocationId);
  191. return (true, currentPublicIp, eipAddress);
  192. }
  193. }
  194. throw new Exception("没有符合的数据");
  195. }
  196. catch (Exception ex)
  197. {
  198. throw new Exception(ex.Message);
  199. }
  200. }
  201. public bool EcsCreateNetworkInterface(string region, string instanceId)
  202. {
  203. try
  204. {
  205. //查询实例信息
  206. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  207. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  208. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  209. //申请弹性网卡
  210. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  211. string networkInterfaceId = response.Body.NetworkInterfaceId;
  212. string privateIpAddress = response.Body.PrivateIpAddress;
  213. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  214. //绑定网卡
  215. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  216. //申请并绑定公网IP
  217. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  218. return true;
  219. }
  220. catch (Exception ex)
  221. {
  222. throw new Exception(ex.Message);
  223. }
  224. }
  225. public List<DescribeNetworkInterfacesResponseBodyNetworkInterfaceSetsNetworkInterfaceSet> DescribeNetworkInterfaces(string region, string instanceId)
  226. {
  227. try
  228. {
  229. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  230. var list = response.Body.NetworkInterfaceSets.NetworkInterfaceSet;
  231. return list;
  232. }
  233. catch (Exception ex)
  234. {
  235. throw new Exception(ex.Message);
  236. }
  237. }
  238. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count, bool isPrimaryNetworkInterface = false)
  239. {
  240. try
  241. {
  242. //查询实例信息
  243. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  244. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  245. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  246. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  247. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  248. {
  249. string type = isPrimaryNetworkInterface ? "Primary" : "Secondary";
  250. if (type.Equals(e.Type))
  251. {
  252. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  253. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  254. //foreach (var privateIp in PrivateIpAddressList)
  255. //{
  256. // if (privateIp.Primary == true) continue;
  257. // var privateIpAddress = privateIp.PrivateIpAddress;
  258. // //申请公网ip
  259. // var eip = plus.AllocateEipAddress(region);
  260. // var allocationId = eip.Body.AllocationId;
  261. // string eipAddress = eip.Body.EipAddress;
  262. // //绑定公网ip
  263. // var AssociateEipAddress =
  264. // plus.AssociateEipAddress(
  265. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  266. //}
  267. //添加辅助私网
  268. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  269. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  270. foreach (var privateIpAddress in PrivateIpAddressList)
  271. {
  272. //申请公网ip
  273. var eip = plus.AllocateEipAddress(region);
  274. var allocationId = eip.Body.AllocationId;
  275. string eipAddress = eip.Body.EipAddress;
  276. //绑定公网ip
  277. var AssociateEipAddress =
  278. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  279. }
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. catch (Exception ex)
  286. {
  287. throw new Exception(ex.Message);
  288. }
  289. }
  290. /// <summary>
  291. /// 申请并绑定公网IP
  292. /// </summary>
  293. /// <param name="region"></param>
  294. /// <param name="instanceId"></param>
  295. /// <param name="privateIpAddress"></param>
  296. /// <returns></returns>
  297. /// <exception cref="Exception"></exception>
  298. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  299. {
  300. try
  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, networkInterfaceId, EIPInstanceType.NetworkInterface);
  309. return true;
  310. }
  311. catch (Exception ex)
  312. {
  313. throw new Exception(ex.Message);
  314. }
  315. }
  316. public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipAddresses(string region, string eipAddress)
  317. {
  318. try
  319. {
  320. var response = plus.DescribeEipAddresses(region, eipAddress);
  321. var list = response?.Body?.EipAddresses?.EipAddress;
  322. return list;
  323. }
  324. catch (Exception ex)
  325. {
  326. throw new Exception(ex.Message);
  327. }
  328. }
  329. public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
  330. {
  331. try
  332. {
  333. var response = plus.DescribeEipAddresses(region, eipAddress);
  334. var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
  335. return eip;
  336. }
  337. catch (Exception ex)
  338. {
  339. throw new Exception(ex.Message);
  340. }
  341. }
  342. public bool ReleaseEipAddress(string region, string allocationId)
  343. {
  344. try
  345. {
  346. plus.ReleaseEipAddress(region, allocationId);
  347. return true;
  348. }
  349. catch (Exception ex)
  350. {
  351. throw new Exception(ex.Message);
  352. }
  353. }
  354. }
  355. }