welsonjs/app/index.js

303 lines
9.7 KiB
JavaScript
Raw Normal View History

2020-11-05 05:09:20 +00:00
////////////////////////////////////////////////////////////////////////
2020-11-10 09:13:41 +00:00
// index.js
2020-11-05 05:09:20 +00:00
////////////////////////////////////////////////////////////////////////
var CONFIG = require("lib/config");
var FILE = require("lib/file");
var OldBrowser = require("lib/oldbrowser");
var HTTP = require("lib/http");
2020-11-05 06:38:51 +00:00
var SYS = require("lib/system");
2020-11-05 08:51:09 +00:00
var SHELL = require("lib/shell");
2020-11-06 07:17:20 +00:00
var LDPlayer = require("lib/ldplayer");
var NoxPlayer = require("lib/noxplayer");
2020-08-04 09:29:42 +00:00
2020-11-05 05:32:27 +00:00
var token, userId;
2020-11-10 09:13:41 +00:00
var apiUrl = CONFIG.readConfig("/ApiUrl").first().getText();
2020-11-05 05:32:27 +00:00
2020-11-05 06:38:51 +00:00
var servers = [];
2020-11-06 07:17:20 +00:00
var applications = [];
2020-11-18 06:46:34 +00:00
var localApplications = [];
2020-11-05 06:38:51 +00:00
2020-11-12 04:19:59 +00:00
var assign = function() {
2020-11-18 07:58:01 +00:00
SHELL.runVisibleWindow("cscript app.js shadow");
2020-11-12 04:19:59 +00:00
};
2020-11-12 03:16:31 +00:00
var pingtest = function() {
for (var i = 0; i < servers.length; i++) {
servers[i].entry.find("span.ping").text(SYS.ping(servers[i].data.ipaddress) + " ms");
}
};
var getLocalApplications = function() {
2020-11-12 04:19:59 +00:00
// LDPlayer
var LDPList = LDPlayer.getList();
for (var i = 0; i < LDPList.length; i++) {
localApplications.push({
name: "LDPlayer",
uniqueId: LDPList[i].title
});
}
// NoxPlayer
var NoxPList = NoxPlayer.getList();
for (var i = 0; i < NoxPList.length; i++) {
localApplications.push({
name: "NoxPlayer",
uniqueId: NoxPList[i].hostname
});
}
// Chrome
2020-11-18 06:46:34 +00:00
/*
2020-11-12 04:19:59 +00:00
localApplications.push({
name: "Chrome",
uniqueId: "John"
});
localApplications.push({
name: "Chrome",
uniqueId: "James"
});
localApplications.push({
name: "Chrome",
uniqueId: "Jasmine"
});
2020-11-18 06:46:34 +00:00
*/
2020-11-12 04:19:59 +00:00
var template = $("#listview_applications .template");
for (var i = 0; i < servers.length; i++) {
template.find("select").append($("<option/>").attr({
value: servers[i].data.id
}).text(servers[i].data.ipaddress));
}
for (var i = 0; i < localApplications.length; i++) {
var serverId = "";
var entry = template.clone();
entry.find("a.title").text(localApplications[i].uniqueId + " (" + localApplications[i].name + ")");
entry.find("select").data("application-name", localApplications[i].name);
entry.find("select").data("application-unique-id", localApplications[i].uniqueId);
for (var k = 0; k < applications.length; k++) {
if (applications[k].name == localApplications[i].name
&& applications[k].uniqueId == localApplications[i].uniqueId
&& applications[k].createdBy == userId)
{
entry.find("select").data("application-id", applications[i].id);
serverId = applications[k].server;
break;
}
}
entry.find("select").change(function() {
if ($(this).val() != "") {
var data = {
"status": "published",
"name": $(this).data("application-name"),
"unique_id": $(this).data("application-unique-id"),
"created_by": userId,
"server": $(this).val()
};
var applicationId = $(this).data("application-id");
var req, res;
var onSuccess = function(res) {
if ("error" in res) {
console.error(res.error.message);
} else {
console.log("반영되었습니다.");
}
};
2020-11-12 04:36:16 +00:00
2020-11-12 04:19:59 +00:00
if (applicationId) {
2020-11-12 04:36:16 +00:00
HTTP.create()
.setContentType("application/json-patch+json")
2020-11-12 04:49:25 +00:00
.setBearerAuth(token)
2020-11-12 04:36:16 +00:00
.setRequestBody(JSON.stringify(data))
.patch(apiUrl + "/netsolid/items/applications/" + applicationId, onSuccess)
;
2020-11-12 04:19:59 +00:00
} else {
HTTP.create()
.setContentType("application/json")
2020-11-12 04:36:16 +00:00
.setBearerAuth(token)
2020-11-12 04:19:59 +00:00
.setRequestBody(data)
.post(apiUrl + "/netsolid/items/applications", onSuccess)
;
}
}
}).val(serverId);
entry.appendTo("#listview_applications");
}
template.css("display", "none");
2020-11-12 03:16:31 +00:00
};
var getMyApplications = function() {
var onSuccess = function(res) {
2020-11-12 04:19:59 +00:00
var xmlStrings = [];
2020-11-18 04:13:38 +00:00
2020-11-12 03:16:31 +00:00
xmlStrings.push('<?xml version="1.0" encoding="UTF-8"?>');
xmlStrings.push("<StaticIP>");
for (var i = 0; i < res.data.length; i++) {
xmlStrings.push("<Item>");
xmlStrings.push("<Name>" + res.data[i].name + "</Name>");
xmlStrings.push("<UniqueID>" + res.data[i].unique_id + "</UniqueID>");
for (var k = 0; k < servers.length; k++) {
if (servers[k].data.id == res.data[i].server) {
xmlStrings.push("<IPAddress>" + servers[k].data.ipaddress + "</IPAddress>");
applications.push({
id: res.data[i].id,
name: res.data[i].name,
uniqueId: res.data[i].unique_id,
server: res.data[i].server,
ipAddress: servers[k].data.ipaddress,
createdBy: userId
});
}
}
xmlStrings.push("</Item>");
2020-11-18 06:46:34 +00:00
// for Chrome
if (res.data[i].name == "Chrome") {
localApplications.push({
name: "Chrome",
uniqueId: res.data[i].unique_id
});
}
2020-11-12 03:16:31 +00:00
}
xmlStrings.push("</StaticIP>");
FILE.writeFile("staticip.xml", xmlStrings.join("\r\n"), "utf-8");
getLocalApplications();
};
HTTP.create()
.setContentType("application/x-www-form-urlencoded")
.setBearerAuth(token)
.setUseCache(false)
.get(apiUrl + "/netsolid/items/applications", onSuccess)
;
};
var getMyServers = function(assignedServers) {
var onSuccess = function(res) {
var template = $("#listview_servers .template");
for (var i = 0; i < res.data.length; i++) {
if (assignedServers.indexOf(res.data[i].id) > -1) {
var entry = template.clone();
entry.find("a.title").text(res.data[i].ipaddress);
entry.find("div.description").text(res.data[i].name);
entry.appendTo("#listview_servers");
servers.push({
"data": res.data[i],
"entry": entry
});
}
}
template.css("display", "none");
}
HTTP.create()
.setContentType("application/x-www-form-urlencoded")
.setBearerAuth(token)
.setUseCache(false)
.get(apiUrl + "/netsolid/items/servers", onSuccess)
;
pingtest();
setInterval(pingtest, 5000);
2020-11-12 06:39:50 +00:00
//document.getElementById("btn_pingtest").onclick = pingtest;
2020-11-12 03:16:31 +00:00
getMyApplications();
};
2020-11-10 09:13:41 +00:00
var getAssignedServers = function() {
var onSuccess = function(res) {
2020-11-12 03:16:31 +00:00
var assignedServers = [];
2020-11-10 09:13:41 +00:00
for (var i = 0; i < res.data.length; i++) {
if (res.data[i].assigned_to == userId) {
assignedServers.push(res.data[i].server);
2020-11-06 07:17:20 +00:00
}
}
2020-11-12 03:16:31 +00:00
getMyServers(assignedServers);
2020-11-10 09:13:41 +00:00
};
2020-11-06 07:17:20 +00:00
2020-11-10 09:13:41 +00:00
HTTP.create()
2020-11-12 03:16:31 +00:00
.setContentType("application/x-www-form-urlencoded")
.setBearerAuth(token)
.setUseCache(false)
.get(apiUrl + "/netsolid/items/assignedservers", onSuccess)
2020-11-10 09:13:41 +00:00
;
2020-11-06 07:17:20 +00:00
};
2020-11-05 05:09:20 +00:00
if (FILE.fileExists("token.txt")) {
token = FILE.readFile("token.txt", "utf-8");
}
2020-11-05 05:32:27 +00:00
if (FILE.fileExists("userid.txt")) {
userId = FILE.readFile("userid.txt", "utf-8");
}
if (typeof(token) !== "undefined") {
2020-11-10 09:13:41 +00:00
OldBrowser.setContent(FILE.readFile("app\\servers.html", "utf-8"));
2020-11-12 03:16:31 +00:00
getAssignedServers();
2020-11-12 04:54:55 +00:00
document.getElementById("btn_logout").onclick = function() {
if (FILE.fileExists("token.txt")) {
token = FILE.deleteFile("token.txt")
}
if (FILE.fileExists("userid.txt")) {
userId = FILE.deleteFile("userid.txt");
}
exit(0);
};
2020-11-18 05:34:23 +00:00
document.getElementById("btn_refresh").onclick = function() {
OldBrowser.reload();
};
2020-11-12 06:39:50 +00:00
//document.getElementById("btn_assign").onclick = assign;
assign();
2020-11-05 05:32:27 +00:00
} else {
2020-11-05 05:09:20 +00:00
OldBrowser.setContent(FILE.readFile("app\\login.html", "utf-8"));
document.getElementById("loginform").onsubmit = function(ev) {
ev.preventDefault();
};
2020-11-18 04:13:38 +00:00
if (FILE.fileExists("credential.json")) {
2020-11-18 06:46:34 +00:00
var credential = JSON.parse(FILE.readFile("credential.json", "utf-8"));
2020-11-18 04:13:38 +00:00
document.getElementById("txt_email").value = credential.email;
document.getElementById("txt_password").value = credential.password;
}
2020-11-05 05:09:20 +00:00
document.getElementById("btn_submit").onclick = function() {
2020-11-06 07:17:20 +00:00
var credential = {
2020-11-05 05:09:20 +00:00
"email": document.getElementById("txt_email").value,
"password": document.getElementById("txt_password").value
2020-11-06 07:17:20 +00:00
};
2020-11-05 05:09:20 +00:00
2020-11-10 09:13:41 +00:00
HTTP.create()
.setContentType("application/json")
.setRequestBody(credential)
.post(apiUrl + "/netsolid/auth/authenticate", function(res) {
if ("error" in res) {
console.error(res.error.message);
} else if ("data" in res) {
console.log("ok");
2020-11-18 04:13:38 +00:00
FILE.writeFile("credential.json", JSON.stringify(credential), "utf-8");
2020-11-10 09:13:41 +00:00
FILE.writeFile("token.txt", res.data.token, "utf-8");
FILE.writeFile("userid.txt", res.data.user.id, "utf-8");
2020-11-12 04:49:25 +00:00
OldBrowser.reload();
2020-11-10 09:13:41 +00:00
}
})
;
2020-11-05 05:09:20 +00:00
};
}