AliyunCore.cs 15 KB

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