network.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. const Ecs20140526 = require('@alicloud/ecs20140526').default
  2. const $Ecs20140526 = require('@alicloud/ecs20140526')
  3. const OpenApi = require('@alicloud/openapi-client').default;
  4. const $OpenApi = require('@alicloud/openapi-client');
  5. const $tea = require('@alicloud/tea-typescript')
  6. const STATUS = {
  7. /**
  8. * 等待状态
  9. */
  10. pending: "pending",
  11. /**
  12. * 完成状态
  13. */
  14. fulfilled: "fulfilled"
  15. }
  16. function getLocalTimeToUTC() {
  17. var date = new Date()
  18. date.setMilliseconds(0)
  19. date.setMinutes(date.getMinutes() + date.getTimezoneOffset() + 3)
  20. return date
  21. }
  22. function dateFormat(fmt, date) {
  23. let ret;
  24. const opt = {
  25. "Y+": date.getFullYear().toString(), // 年
  26. "m+": (date.getMonth() + 1).toString(), // 月
  27. "d+": date.getDate().toString(), // 日
  28. "H+": date.getHours().toString(), // 时
  29. "M+": date.getMinutes().toString(), // 分
  30. "S+": date.getSeconds().toString() // 秒
  31. // 有其他格式化字符需求可以继续添加,必须转化成字符串
  32. };
  33. for (let k in opt) {
  34. ret = new RegExp("(" + k + ")").exec(fmt);
  35. if (ret) {
  36. fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
  37. };
  38. };
  39. return fmt;
  40. }
  41. class Client {
  42. /**
  43. * 使用AK&SK初始化账号Client
  44. * @param accessKeyId
  45. * @param accessKeySecret
  46. * @return Client
  47. * @throws Exception
  48. */
  49. static createClient(accessKeyId, accessKeySecret) {
  50. let config = new $OpenApi.Config({
  51. // 您的AccessKey ID
  52. accessKeyId: accessKeyId,
  53. // 您的AccessKey Secret
  54. accessKeySecret: accessKeySecret,
  55. });
  56. // 访问的域名
  57. config.endpoint = "ecs-cn-hangzhou.aliyuncs.com";
  58. return new Ecs20140526(config);
  59. }
  60. constructor() {
  61. this.band = 0
  62. this.status = STATUS.fulfilled;
  63. this.client = Client.createClient("LTAIUrvuHqj8pvry", "JLOVl0k8Ke0aaM8nLMMiUAZ3EiiqI4")
  64. // 看房服务器
  65. this.instanceId = 'i-wz9g1c3fleilk1n9q5uu'
  66. this.describeInstances()
  67. setInterval(() => {
  68. this.describeInstances()
  69. }, 15000);
  70. }
  71. /**
  72. * 查看实例详细信息
  73. */
  74. async describeInstances() {
  75. //https://help.aliyun.com/document_detail/25506.html#h2-url-4
  76. try {
  77. let describeInstancesRequest = new $Ecs20140526.DescribeInstancesRequest({
  78. regionId: "cn-shenzhen",
  79. instanceIds: "[\"" + this.instanceId + "\"]",
  80. });
  81. let response = await this.client.describeInstances(describeInstancesRequest);
  82. if (response.body.totalCount > 0) {
  83. if (response.body.instances.instance[0]) {
  84. let band = response.body.instances.instance[0].internetMaxBandwidthOut
  85. if (band) {
  86. if (this.band) {
  87. if (this.band != band) {
  88. if (this.band < band) {
  89. console.log("带宽已升级:" + band + "MB")
  90. } else if (this.band > band) {
  91. console.log("带宽已变化:" + band + "MB")
  92. }
  93. } else {
  94. console.log('当前带宽:'+ band + "MB",'连接数:'+(this.conns || 0))
  95. }
  96. // 当带宽有变化时重置为初始状态
  97. this.status = STATUS.fulfilled
  98. }
  99. if(!this.band){
  100. console.log("带宽初始值:" + band + "MB")
  101. }
  102. this.band = band
  103. }
  104. }
  105. }
  106. } catch (error) {
  107. console.log("获取带宽出错:", error)
  108. }
  109. }
  110. async modifyInstanceNetwork(band) {
  111. //https://help.aliyun.com/document_detail/25545.htm?spm=a2c4g.11186623.0.0.1ec05d7aOys0wB#t9937.html
  112. if (!band || band > 100 || this.band >= 100 || band<=this.band) {
  113. return
  114. }
  115. console.log("开始升级带宽:"+band)
  116. this.status = STATUS.pending
  117. try {
  118. var utc = getLocalTimeToUTC()
  119. var startTime = dateFormat('YYYY-mm-ddTHH:MMZ', utc)
  120. utc.setHours(utc.getHours() + 4)
  121. var endTime = dateFormat('YYYY-mm-ddTHHZ', utc)
  122. console.log(startTime, endTime)
  123. let modifyInstanceNetworkSpecRequest = new $Ecs20140526.ModifyInstanceNetworkSpecRequest({
  124. instanceId: this.instanceId,
  125. internetMaxBandwidthOut: band,
  126. startTime: startTime,
  127. endTime: endTime,
  128. autoPay: true,
  129. });
  130. let response = await this.client.modifyInstanceNetworkSpec(modifyInstanceNetworkSpecRequest);
  131. console.log(response)
  132. } catch (error) {
  133. this.status = STATUS.fulfilled
  134. console.log("升级带宽出错:", error)
  135. }
  136. }
  137. async autoUpdate(conns) {
  138. this.conns = conns
  139. if (this.status == STATUS.pending) {
  140. return
  141. }
  142. if (conns >= 300 && conns < 800) {
  143. await this.modifyInstanceNetwork(50)
  144. } else if (conns >= 800 && conns < 1500) {
  145. await this.modifyInstanceNetwork(75)
  146. } else if (conns > 1500) {
  147. await this.modifyInstanceNetwork(100)
  148. }
  149. }
  150. }
  151. const client = new Client()
  152. module.exports = client