AliyunCore.cs 15 KB

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