summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorHaishan <[email protected]>2018-10-20 20:32:02 +0800
committerHaishan <[email protected]>2018-10-20 20:32:02 +0800
commit133b29c9dac2209a3c88c3289f84ff709d404392 (patch)
treee023785cc82db732c64328a5d99938992eceecfd /src/api
first commit
Diffstat (limited to 'src/api')
-rw-r--r--src/api/configs.js22
-rw-r--r--src/api/logs.js64
-rw-r--r--src/api/proxies.js50
-rw-r--r--src/api/traffic.js67
4 files changed, 203 insertions, 0 deletions
diff --git a/src/api/configs.js b/src/api/configs.js
new file mode 100644
index 0000000..a9ae0e3
--- /dev/null
+++ b/src/api/configs.js
@@ -0,0 +1,22 @@
+'use strict';
+
+const { getAPIURL } = require('../config');
+
+const headers = {
+ 'Content-Type': 'application/json'
+};
+
+export async function fetchConfigs() {
+ const apiURL = getAPIURL();
+ return await fetch(apiURL.configs);
+}
+
+export async function updateConfigs(o) {
+ const apiURL = getAPIURL();
+ return await fetch(apiURL.configs, {
+ method: 'PUT',
+ // mode: 'cors',
+ headers,
+ body: JSON.stringify(o)
+ });
+}
diff --git a/src/api/logs.js b/src/api/logs.js
new file mode 100644
index 0000000..9bcab58
--- /dev/null
+++ b/src/api/logs.js
@@ -0,0 +1,64 @@
+'use strict';
+
+const { getAPIURL } = require('../config');
+const textDecoder = new TextDecoder('utf-8');
+
+const Size = 300;
+
+const store = {
+ logs: [],
+ size: Size,
+ updateCallback: null,
+ appendData(o) {
+ const now = new Date();
+ const time = now.toLocaleString('zh-Hans');
+ // mutate input param in place intentionally
+ o.time = time;
+ o.id = now - 0;
+ this.logs.unshift(o);
+ if (this.logs.length > this.size) this.logs.pop();
+ // TODO consider throttle this
+ if (this.updateCallback) this.updateCallback();
+ }
+};
+
+function pump(reader) {
+ return reader.read().then(({ done, value }) => {
+ if (done) {
+ console.log('done');
+ return;
+ }
+ const t = textDecoder.decode(value);
+ // console.log(t);
+ const l = t[t.length - 1];
+ let o;
+ try {
+ o = JSON.parse(t);
+ } catch (err) {
+ console.log(
+ 'lastchar',
+ t.length,
+ ' is r',
+ l === '\r',
+ ' is n',
+ l === '\n'
+ );
+ }
+ store.appendData(o);
+ return pump(reader);
+ });
+}
+
+let fetched = false;
+function fetchLogs() {
+ if (fetched) return store;
+ const apiURL = getAPIURL();
+ fetch(apiURL.logs).then(response => {
+ fetched = true;
+ const reader = response.body.getReader();
+ pump(reader);
+ });
+ return store;
+}
+
+export { fetchLogs };
diff --git a/src/api/proxies.js b/src/api/proxies.js
new file mode 100644
index 0000000..1789f67
--- /dev/null
+++ b/src/api/proxies.js
@@ -0,0 +1,50 @@
+'use strict';
+
+const { getAPIURL } = require('../config');
+
+const headers = {
+ 'Content-Type': 'application/json'
+};
+
+/*
+$ curl "http://127.0.0.1:8080/proxies/Proxy" -XPUT -d '{ "name": "ss3" }' -i
+HTTP/1.1 400 Bad Request
+Vary: Origin
+Date: Tue, 16 Oct 2018 16:38:20 GMT
+Content-Length: 56
+Content-Type: text/plain; charset=utf-8
+
+{"error":"Selector update error: Proxy does not exist"}
+
+~
+$ curl "http://127.0.0.1:8080/proxies/GLOBAL" -XPUT -d '{ "name": "Proxy" }' -i
+HTTP/1.1 204 No Content
+Vary: Origin
+Date: Tue, 16 Oct 2018 16:38:33 GMT
+*/
+
+async function fetchProxies() {
+ const apiURL = getAPIURL();
+ const res = await fetch(apiURL.proxies);
+ return await res.json();
+}
+
+async function requestToSwitchProxy(name1, name2) {
+ const body = { name: name2 };
+ const apiURL = getAPIURL();
+ const url = `${apiURL.proxies}/${name1}`;
+ return await fetch(url, {
+ method: 'PUT',
+ headers,
+ body: JSON.stringify(body)
+ });
+}
+
+async function requestDelayForProxy(name) {
+ const apiURL = getAPIURL();
+ const qs = `timeout=5000&url=http://www.google.com/generate_204`;
+ const url = `${apiURL.proxies}/${name}/delay?${qs}`;
+ return await fetch(url);
+}
+
+export { fetchProxies, requestToSwitchProxy, requestDelayForProxy };
diff --git a/src/api/traffic.js b/src/api/traffic.js
new file mode 100644
index 0000000..53330a5
--- /dev/null
+++ b/src/api/traffic.js
@@ -0,0 +1,67 @@
+'use strict';
+
+const { getAPIURL } = require('../config');
+const textDecoder = new TextDecoder('utf-8');
+
+const Size = 150;
+
+const traffic = {
+ labels: Array(Size),
+ // labels: [],
+ up: Array(Size),
+ down: Array(Size),
+
+ size: Size,
+ subscribers: [],
+ appendData(o) {
+ this.up.push(o.up);
+ this.down.push(o.down);
+ const t = new Date();
+ const l = '' + t.getMinutes() + t.getSeconds();
+ this.labels.push(l);
+ if (this.up.length > this.size) this.up.shift();
+ if (this.down.length > this.size) this.down.shift();
+ if (this.labels.length > this.size) this.labels.shift();
+
+ this.subscribers.forEach(f => f(o));
+ },
+
+ subscribe(listener) {
+ const me = this;
+ this.subscribers.push(listener);
+ return function unsubscribe() {
+ const idx = me.subscribers.indexOf(listener);
+ me.subscribers.splice(idx, 1);
+ };
+ }
+};
+
+function pump(reader) {
+ return reader.read().then(({ done, value }) => {
+ if (done) {
+ console.log('done');
+ return;
+ }
+ const t = textDecoder.decode(value);
+ // console.log('response', t);
+ const o = JSON.parse(t);
+
+ traffic.appendData(o);
+
+ return pump(reader);
+ });
+}
+
+let fetched = false;
+function fetchData() {
+ if (fetched) return traffic;
+ const apiURL = getAPIURL();
+ fetch(apiURL.traffic).then(response => {
+ fetched = true;
+ const reader = response.body.getReader();
+ pump(reader);
+ });
+ return traffic;
+}
+
+export { fetchData };