AliyunCore.cs 16 KB

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