├── .gitignore
├── LICENSE
├── README.md
└── vultr-helper.js
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | vultr-helper-dev.js
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Spencer Woo
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 | # :cloud: Vultr Helper
4 |
5 | [](https://github.com/spencerwoo98/jsbox-vultr-helper/blob/master/LICENSE)
6 | 
7 | 
8 |
9 | 一款运行在 JSBox (iOS) 上的 Vultr 服务器账户查询小工具。
10 |
11 | Vultr Helper 利用 [Vultr](https://vultr.com) 提供的 API 查询您的账户余额、账户信息以及本月花销。
12 |
13 | 同时以小组件的形式完美的展现在 Today 栏目里面。
14 |
15 | [Vultr-Helper 下载地址](https://xteko.com/redir?name=Vultr-Helper&url=https://raw.githubusercontent.com/spencerwoo98/jsbox-vultr-helper/master/vultr-helper.js)
16 |
17 | 
18 |
19 |
20 |
21 | ## 1. 申请您自己的 API Key
22 |
23 | 访问链接 :link: [Vultr API Key](https://my.vultr.com/settings/#settingsapi) 来生成您自己的 API Key,此脚本依赖于 API Key 与 Vultr 账户进行沟通。
24 |
25 | 
26 |
27 |
28 |
29 | ## 2. 更改 API 的 IP 使用权限
30 |
31 | Vultr 的 API 权限管理很合理。
32 | 请在刚刚 API Key 申请页面下面的 Access Control 部分将您自己的 IP 地址添加至白名单(或者允许全部 IPv4 地址访问)。
33 |
34 |
35 |
36 | ## 3. 将代码第一行 `apiKey` 更换成您自己的 API Key
37 |
38 | 这样运行,就可以显示您 Vultr 云账户中包括账户邮箱、账户姓名、余额、本月使用金额等等信息。
39 |
40 |
41 |
42 | ## 4. 利用 `curl` 请求你的 Server 的唯一识别码 `serverSubId`
43 |
44 | > v1.1.0 新版本功能:根据服务器唯一识别码更新服务器信息。
45 |
46 | 在终端中运行代码:
47 |
48 | ```shell
49 | curl -H 'API-Key: YOUR_OWN_API_KEY' "https://api.vultr.com/v1/server/list"
50 | ```
51 |
52 | 同样也要将上面命令中的 `API-Key` 后面换成你自己的 `API_Key`,得到返回数据的每个级别就是服务器唯一识别码,即 Server `SUBID`.
53 |
54 | 
55 |
56 | 这样找到服务器唯一识别码之后,将此项数据在代码中更新,这样也就得到了服务器的具体信息显示。
57 |
58 | 推荐如果将本小组件添加至 Today Widget,那么推荐将视图高度设置为 300.
59 |
60 |
61 |
62 | 欢迎来 Star 与 Fork,项目地址位于 --> https://github.com/spencerwoo98/jsbox-vultr-helper
63 |
64 | 欢迎来我的博客看看,我的博客在 --> https://spencerwoo.com
65 |
66 |
67 |
68 | # License
69 |
70 | This project is published via the [MIT License](https://github.com/spencerwoo98/jsbox-vultr-helper/blob/master/LICENSE)
71 |
72 | © Spencer Woo
--------------------------------------------------------------------------------
/vultr-helper.js:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | vultr-helper.js
4 |
5 | Author: Spencer Woo
6 |
7 | https://spencerwoo.com
8 |
9 | A tool written according to vultr cloud api to check account/balance etc.
10 |
11 | */
12 |
13 | // 将下面 apiKey 更改至你自己的 apiKey
14 | var apiKey = 'YOUR_OWN_APIKEY'
15 | // 将下面的 serverSubId 更改至你自己想要显示的 serverSubId
16 | var serverSubId = YOUR_OWN_SERVER_SUBID
17 |
18 | function getAccountInfo(apiKey) {
19 | $http.request({
20 | method: "GET",
21 | url: "https://api.vultr.com/v1/account/info",
22 | header: {
23 | "API-key": apiKey
24 | },
25 | body: {},
26 | handler: function (resp) {
27 | var data = resp.data;
28 | // console.log(data);
29 | var balance = Math.abs(data.pending_charges / data.balance);
30 | percentBalance = parseFloat(balance * 100).toFixed(2) + "%";
31 | remainCredit = parseFloat(data.pending_charges) + parseFloat(data.balance);
32 | $("progress_label").text = "Charges this month: $" + data.pending_charges;
33 | // console.log(balance);
34 | $("charges").value = balance;
35 | $("remaining_credit").text = "$" + Math.abs(remainCredit);
36 | $("progress_percent").text = percentBalance;
37 | }
38 | });
39 | }
40 |
41 | function getApiInfo(apiKey) {
42 | $http.request({
43 | method: "GET",
44 | url: "https://api.vultr.com/v1/auth/info",
45 | header: {
46 | "API-key": apiKey
47 | },
48 | body: {},
49 | handler: function (resp) {
50 | var data = resp.data;
51 | // console.log(data);
52 | $("email").text = "Email: " + data.email;
53 | $("name").text = "Name: " + data.name;
54 | }
55 | });
56 | }
57 |
58 | function getServerInfo(apiKey) {
59 | $http.request({
60 | method: "GET",
61 | url: "https://api.vultr.com/v1/server/list",
62 | header: {
63 | "API-key": apiKey
64 | },
65 | body: {},
66 | handler: function (resp) {
67 | var data = resp.data;
68 | console.log(data);
69 | // console.log(data[serverSubId]);
70 |
71 | var serverGeneralInfo = data[serverSubId];
72 |
73 | $("server_os").text = "# IP: " + serverGeneralInfo.main_ip + " " + serverGeneralInfo.os;
74 | if (serverGeneralInfo.vcpu_count == 1) {
75 | $("server_cpu_info").text = serverGeneralInfo.vcpu_count + " Core";
76 | } else {
77 | $("server_cpu_info").text = serverGeneralInfo.vcpu_count + " Cores";
78 | }
79 | $("server_ram_info").text = serverGeneralInfo.ram;
80 |
81 | var serverChargesPercent = parseFloat(serverGeneralInfo.pending_charges) / parseFloat(serverGeneralInfo.cost_per_month);
82 | $("server_charges_label").text = "Current charges: $" + serverGeneralInfo.pending_charges;
83 | $("server_charges_detail").text = parseFloat(serverChargesPercent * 100).toFixed(2) + "%";
84 | $("server_charges").value = parseFloat(serverChargesPercent);
85 |
86 | var serverUsagePercent = parseFloat(serverGeneralInfo.current_bandwidth_gb) / parseFloat(serverGeneralInfo.allowed_bandwidth_gb);
87 | // console.log(serverUsagePercent);
88 | $("server_usage_label").text = "Server usage: " + serverGeneralInfo.current_bandwidth_gb + " GB"
89 | $("server_usage_percent").text = parseFloat(serverUsagePercent * 100).toFixed(2) + "%";
90 | $("server_usage").value = parseFloat(serverUsagePercent);
91 |
92 | }
93 | })
94 | }
95 |
96 | function renderUI() {
97 | $ui.render({
98 | props: {
99 | title: "Vultr"
100 | },
101 | views: [{
102 | type: "label",
103 | props: {
104 | id: "email",
105 | align: $align.left,
106 | text: "Email: ",
107 | font: $font(12),
108 | textColor: $color("#888888")
109 | },
110 | layout: function (make, view) {
111 | make.top.equalTo(5)
112 | make.height.equalTo(20)
113 | make.left.right.equalTo(15)
114 | }
115 | }, {
116 | type: "label",
117 | props: {
118 | id: "name",
119 | align: $align.left,
120 | text: "Name: ",
121 | font: $font(12),
122 | textColor: $color("#888888")
123 | },
124 | layout: function (make, view) {
125 | make.top.equalTo($("email").bottom)
126 | make.height.equalTo(20)
127 | make.left.right.equalTo(15)
128 | }
129 | }, {
130 | type: "label",
131 | props: {
132 | id: "remaining_credit",
133 | font: $font(28),
134 | text: "$..",
135 | align: $align.right,
136 | textColor: $color("#7db249")
137 | },
138 | layout: function (make, view) {
139 | make.right.inset(15)
140 | make.top.equalTo(view.super).inset(5)
141 | }
142 | }, {
143 | type: "label",
144 | props: {
145 | id: "remaining_credit_label",
146 | font: $font(12),
147 | align: $align.right,
148 | textColor: $color("#888888"),
149 | text: "Remaining Credit"
150 | },
151 | layout: function (make, view) {
152 | make.right.inset(15)
153 | make.top.equalTo(view.super).equalTo($("remaining_credit").bottom)
154 | }
155 | }, {
156 | type: "label",
157 | props: {
158 | id: "progress_label",
159 | align: $align.left,
160 | font: $font("bold", 14),
161 | text: "Charges this month: ",
162 | textColor: $color("#2c2c2c")
163 | },
164 | layout: function (make, view) {
165 | make.top.equalTo($("name").bottom).offset(5)
166 | make.height.equalTo(30)
167 | make.left.right.equalTo(15)
168 | }
169 | }, {
170 | type: "label",
171 | props: {
172 | id: "progress_percent",
173 | align: $align.right,
174 | font: $font(14),
175 | text: "...%",
176 | textColor: $color("#2c2c2c")
177 | },
178 | layout: function (make, view) {
179 | make.top.equalTo($("name").bottom).offset(7)
180 | make.height.equalTo(30)
181 | make.left.right.inset(15)
182 | }
183 | }, {
184 | type: "progress",
185 | props: {
186 | id: "charges"
187 | },
188 | layout: function (make, view) {
189 | make.top.equalTo($("progress_label").bottom).offset(5)
190 | make.left.equalTo(15)
191 | make.right.inset(15)
192 | }
193 | }, {
194 | type: "label",
195 | props: {
196 | id: "server",
197 | align: $align.left,
198 | font: $font("bold", 14),
199 | text: "SERVER DETAILS",
200 | textColor: $color("#2c2c2c")
201 | },
202 | layout: function (make, view) {
203 | make.top.equalTo($("charges").bottom).offset(30)
204 | make.left.right.equalTo(15)
205 | }
206 | }, {
207 | type: "label",
208 | props: {
209 | id: "server_os",
210 | align: $align.left,
211 | font: $font("bold", 14),
212 | text: "# IP: ",
213 | textColor: $color("#2c2c2c")
214 | },
215 | layout: function (make, view) {
216 | make.top.equalTo($("server").bottom).offset(10)
217 | make.height.equalTo(20)
218 | make.left.right.equalTo(15)
219 | }
220 | }, {
221 | type: "label",
222 | props: {
223 | id: "server_cpu",
224 | align: $align.left,
225 | font: $font(14),
226 | text: "CPU",
227 | textColor: $color("#888888")
228 | },
229 | layout: function (make, view) {
230 | make.top.equalTo($("server_os").bottom).offset(5)
231 | make.height.equalTo(20)
232 | make.left.right.equalTo(15)
233 | }
234 | }, {
235 | type: "label",
236 | props: {
237 | id: "server_cpu_info",
238 | align: $align.right,
239 | font: $font(14),
240 | text: "... Core",
241 | textColor: $color("#888888")
242 | },
243 | layout: function (make, view) {
244 | make.top.equalTo($("server_os").bottom).offset(7)
245 | make.height.equalTo(20)
246 | make.left.right.inset(15)
247 | }
248 | }, {
249 | type: "label",
250 | props: {
251 | id: "server_ram",
252 | align: $align.left,
253 | font: $font(14),
254 | text: "RAM",
255 | textColor: $color("#888888")
256 | },
257 | layout: function (make, view) {
258 | make.top.equalTo($("server_cpu").bottom).offset(5)
259 | make.height.equalTo(20)
260 | make.left.right.equalTo(15)
261 | }
262 | }, {
263 | type: "label",
264 | props: {
265 | id: "server_ram_info",
266 | align: $align.right,
267 | font: $font(14),
268 | text: "... MB",
269 | textColor: $color("#888888")
270 | },
271 | layout: function (make, view) {
272 | make.top.equalTo($("server_cpu").bottom).offset(7)
273 | make.height.equalTo(20)
274 | make.left.right.inset(15)
275 | }
276 | }, {
277 | type: "label",
278 | props: {
279 | id: "server_charges_label",
280 | align: $align.left,
281 | font: $font(14),
282 | text: "Current charges: ",
283 | textColor: $color("#888888")
284 | },
285 | layout: function (make, view) {
286 | make.top.equalTo($("server_ram").bottom).offset(5)
287 | make.left.right.equalTo(15)
288 | }
289 | }, {
290 | type: "label",
291 | props: {
292 | id: "server_charges_detail",
293 | align: $align.right,
294 | font: $font(14),
295 | text: "$...",
296 | textColor: $color("#888888")
297 | },
298 | layout: function (make, view) {
299 | make.top.equalTo($("server_ram").bottom).offset(7)
300 | make.left.right.inset(15)
301 | }
302 | }, {
303 | type: "progress",
304 | props: {
305 | id: "server_charges"
306 | },
307 | layout: function (make, view) {
308 | make.top.equalTo($("server_charges_label").bottom).offset(5)
309 | make.left.equalTo(15)
310 | make.right.inset(15)
311 | }
312 | }, {
313 | type: "label",
314 | props: {
315 | id: "server_usage_label",
316 | align: $align.left,
317 | font: $font(14),
318 | text: "Server usage: ",
319 | textColor: $color("#888888")
320 | },
321 | layout: function (make, view) {
322 | make.top.equalTo($("server_charges").bottom).offset(5)
323 | make.left.right.equalTo(15)
324 | }
325 | }, {
326 | type: "label",
327 | props: {
328 | id: "server_usage_percent",
329 | align: $align.right,
330 | font: $font(14),
331 | text: "...%",
332 | textColor: $color("#888888")
333 | },
334 | layout: function (make, view) {
335 | make.top.equalTo($("server_charges").bottom).offset(7)
336 | make.left.right.inset(15)
337 | }
338 | }, {
339 | type: "progress",
340 | props: {
341 | id: "server_usage"
342 | },
343 | layout: function (make, view) {
344 | make.top.equalTo($("server_usage_percent").bottom).offset(5)
345 | make.left.equalTo(15)
346 | make.right.inset(15)
347 | }
348 | }]
349 | })
350 | }
351 |
352 | function main() {
353 | renderUI();
354 | $ui.toast("Refreshing...");
355 | getAccountInfo(apiKey);
356 | getApiInfo(apiKey);
357 | getServerInfo(apiKey);
358 | // console.log("Hello World");
359 | }
360 |
361 | main();
--------------------------------------------------------------------------------