AliyunCore.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. namespace molilian.core
  14. {
  15. public partial class AliyunCore
  16. {
  17. private AliyunPlus plus;
  18. public AliyunAccountDTO _account;
  19. public AliyunCore(int id)
  20. {
  21. _account = new DBContext.Table("aliyun_pool").Get<AliyunAccountDTO>(id);
  22. if (_account == null) throw new Exception("无效账号");
  23. plus = new AliyunPlus(_account.accessKeyId, _account.accessKeySecret);
  24. }
  25. public QueryAccountBalanceResponse QueryAccountBalance()
  26. {
  27. return plus.QueryAccountBalance();
  28. }
  29. /// <summary>
  30. /// 获取这个地区下的所有ecs
  31. /// </summary>
  32. /// <param name="region"></param>
  33. /// <returns></returns>
  34. /// <exception cref="Exception"></exception>
  35. public DescribeInstancesResponseBodyInstances GetInstances(string region)
  36. {
  37. try
  38. {
  39. //查询实例信息
  40. var response = plus.DescribeInstances(region);
  41. var Instances = response.Body.Instances;
  42. return Instances;
  43. }
  44. catch (Exception ex)
  45. {
  46. throw new Exception(ex.Message);
  47. }
  48. }
  49. public DescribeNetworkInterfacesResponseBodyNetworkInterfaceSets GetNetworkInterfaces(string region)
  50. {
  51. try
  52. {
  53. //查询实例信息
  54. var response = plus.DescribeNetworkInterfaces(region);
  55. var networkInterfaces = response.Body.NetworkInterfaceSets;
  56. return networkInterfaces;
  57. }
  58. catch (Exception ex)
  59. {
  60. throw new Exception(ex.Message);
  61. }
  62. }
  63. //public bool xxx(string region, string instanceId, string privateIp)
  64. //{
  65. // try
  66. // {
  67. // //查询实例信息
  68. // var instance = plus.DescribeInstanceAttribute(region, instanceId);
  69. // var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  70. // var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  71. // plus.DescribeNetworkInterfaces(region, instanceId)
  72. // //释放弹性网卡
  73. // ReleaseEipAddress("cn-beijing", allocationId);
  74. // //申请并绑定公网IP
  75. // EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  76. // return true;
  77. // }
  78. // catch (Exception ex)
  79. // {
  80. // throw new Exception(ex.Message);
  81. // }
  82. //}
  83. public (bool, string, string) ChangePublicIp(string privateIp)
  84. {
  85. try
  86. {
  87. var regions = _account.regions?.Split(',');
  88. foreach (var s in regions)
  89. {
  90. var region = s.Trim();
  91. if (string.IsNullOrEmpty(region)) continue;
  92. //获取这个地区下的所有网卡
  93. var list = GetNetworkInterfaces(region);
  94. if (list == null) continue;
  95. foreach (var networkInterface in list.NetworkInterfaceSet)
  96. {
  97. //找出网卡信息
  98. string networkInterfaceId = networkInterface.NetworkInterfaceId;
  99. var privateIps = networkInterface?.PrivateIpSets;
  100. var taraget = privateIps?.PrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  101. if (taraget == null) break;
  102. //找到当前公网IP
  103. string currentPublicIp = taraget?.AssociatedPublicIp?.PublicIpAddress;
  104. if (string.IsNullOrEmpty(currentPublicIp)) throw new Exception("没有找到公网IP");
  105. //第1步、获取当前的公网EIP
  106. var currentEip = DescribeEipAddresse(region, currentPublicIp) ?? throw new Exception("没有找到 EIP");
  107. var currentAllocationId = currentEip.AllocationId;
  108. //第2步、申请新的公网ip
  109. var eip = plus.AllocateEipAddress(region);
  110. var allocationId = eip.Body.AllocationId;
  111. string eipAddress = eip.Body.EipAddress;
  112. //第3步、解绑当前的公网ip
  113. plus.UnassociateEipAddress(region, currentAllocationId);
  114. //第4步、检查公网ip是否成功解绑
  115. string unPublicIpAddress = currentPublicIp;
  116. while (!string.IsNullOrEmpty(unPublicIpAddress))
  117. {
  118. var networkInterfaceState = plus.DescribeNetworkInterfaceAttribute(region, networkInterfaceId);
  119. var currentPrivateIpSet = networkInterfaceState?.Body?.PrivateIpSets?.PrivateIpSet;
  120. var currentPrivateIpAddress = currentPrivateIpSet.Where(e => e.PrivateIpAddress.Equals(privateIp)).FirstOrDefault();
  121. unPublicIpAddress = currentPrivateIpAddress?.AssociatedPublicIp?.PublicIpAddress;
  122. if (string.IsNullOrEmpty(unPublicIpAddress)) break;
  123. Thread.Sleep(500);
  124. }
  125. //第5步、绑定新的公网ip
  126. var AssociateEipAddress =
  127. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface, privateIp);
  128. //第5步、释放当前的公网IP
  129. plus.ReleaseEipAddress(region, currentAllocationId);
  130. return (true, currentPublicIp, eipAddress);
  131. }
  132. }
  133. throw new Exception("没有符合的数据");
  134. }
  135. catch (Exception ex)
  136. {
  137. throw new Exception(ex.Message);
  138. }
  139. }
  140. public bool EcsCreateNetworkInterface(string region, string instanceId)
  141. {
  142. try
  143. {
  144. //查询实例信息
  145. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  146. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  147. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  148. //申请弹性网卡
  149. var response = plus.CreateNetworkInterface(region, vswitchId, securityGroupId);
  150. string networkInterfaceId = response.Body.NetworkInterfaceId;
  151. string privateIpAddress = response.Body.PrivateIpAddress;
  152. if (string.IsNullOrEmpty(networkInterfaceId)) throw new Exception(response.Body.ToString());
  153. //绑定网卡
  154. plus.AttachNetworkInterface(region, instanceId, networkInterfaceId);
  155. //申请并绑定公网IP
  156. EcsAssociateEipAddress(region, networkInterfaceId, privateIpAddress);
  157. return true;
  158. }
  159. catch (Exception ex)
  160. {
  161. throw new Exception(ex.Message);
  162. }
  163. }
  164. public bool EcsAssignPrivateIpAddresses(string region, string instanceId, int count)
  165. {
  166. try
  167. {
  168. //查询实例信息
  169. var instance = plus.DescribeInstanceAttribute(region, instanceId);
  170. var vswitchId = instance.Body.VpcAttributes.VSwitchId;
  171. var securityGroupId = instance.Body.SecurityGroupIds.SecurityGroupId;
  172. var response = plus.DescribeNetworkInterfaces(region, instanceId);
  173. foreach (var e in response.Body.NetworkInterfaceSets.NetworkInterfaceSet)
  174. {
  175. if ("Secondary".Equals(e.Type))
  176. {
  177. //var response2 = plus.DescribeNetworkInterfaceAttribute(region, e.NetworkInterfaceId);
  178. //var PrivateIpAddressList = response2.Body.PrivateIpSets.PrivateIpSet;
  179. //foreach (var privateIp in PrivateIpAddressList)
  180. //{
  181. // if (privateIp.Primary == true) continue;
  182. // var privateIpAddress = privateIp.PrivateIpAddress;
  183. // //申请公网ip
  184. // var eip = plus.AllocateEipAddress(region);
  185. // var allocationId = eip.Body.AllocationId;
  186. // string eipAddress = eip.Body.EipAddress;
  187. // //绑定公网ip
  188. // var AssociateEipAddress =
  189. // plus.AssociateEipAddress(
  190. // region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  191. //}
  192. //添加辅助私网
  193. var response2 = plus.AssignPrivateIpAddresses(region, e.NetworkInterfaceId, count);
  194. var PrivateIpAddressList = response2.Body.AssignedPrivateIpAddressesSet.PrivateIpSet.PrivateIpAddress;
  195. foreach (var privateIpAddress in PrivateIpAddressList)
  196. {
  197. //申请公网ip
  198. var eip = plus.AllocateEipAddress(region);
  199. var allocationId = eip.Body.AllocationId;
  200. string eipAddress = eip.Body.EipAddress;
  201. //绑定公网ip
  202. var AssociateEipAddress =
  203. plus.AssociateEipAddress(region, allocationId, e.NetworkInterfaceId, EIPInstanceType.NetworkInterface, privateIpAddress);
  204. }
  205. return true;
  206. }
  207. }
  208. return false;
  209. }
  210. catch (Exception ex)
  211. {
  212. throw new Exception(ex.Message);
  213. }
  214. }
  215. /// <summary>
  216. /// 申请并绑定公网IP
  217. /// </summary>
  218. /// <param name="region"></param>
  219. /// <param name="instanceId"></param>
  220. /// <param name="privateIpAddress"></param>
  221. /// <returns></returns>
  222. /// <exception cref="Exception"></exception>
  223. public bool EcsAssociateEipAddress(string region, string networkInterfaceId, string privateIpAddress)
  224. {
  225. try
  226. {
  227. //申请公网ip
  228. var eip = plus.AllocateEipAddress(region);
  229. var allocationId = eip.Body.AllocationId;
  230. string eipAddress = eip.Body.EipAddress;
  231. //绑定公网ip
  232. var AssociateEipAddress =
  233. plus.AssociateEipAddress(region, allocationId, networkInterfaceId, EIPInstanceType.NetworkInterface);
  234. return true;
  235. }
  236. catch (Exception ex)
  237. {
  238. throw new Exception(ex.Message);
  239. }
  240. }
  241. public List<DescribeEipAddressesResponseBodyEipAddressesEipAddress> DescribeEipAddresses(string region, string eipAddress)
  242. {
  243. try
  244. {
  245. var response = plus.DescribeEipAddresses(region, eipAddress);
  246. var list = response?.Body?.EipAddresses?.EipAddress;
  247. return list;
  248. }
  249. catch (Exception ex)
  250. {
  251. throw new Exception(ex.Message);
  252. }
  253. }
  254. public DescribeEipAddressesResponseBodyEipAddressesEipAddress DescribeEipAddresse(string region, string eipAddress)
  255. {
  256. try
  257. {
  258. var response = plus.DescribeEipAddresses(region, eipAddress);
  259. var eip = response?.Body?.EipAddresses?.EipAddress?.FirstOrDefault();
  260. return eip;
  261. }
  262. catch (Exception ex)
  263. {
  264. throw new Exception(ex.Message);
  265. }
  266. }
  267. public bool ReleaseEipAddress(string region, string allocationId)
  268. {
  269. try
  270. {
  271. plus.ReleaseEipAddress(region, allocationId);
  272. return true;
  273. }
  274. catch (Exception ex)
  275. {
  276. throw new Exception(ex.Message);
  277. }
  278. }
  279. }
  280. }