AliyunCore.cs 15 KB

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