welsonjs/app/index.js

287 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-11-05 05:09:20 +00:00
////////////////////////////////////////////////////////////////////////
// WebPage
////////////////////////////////////////////////////////////////////////
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-09 10:06:34 +00:00
var apiUrl = CONFIG.readConfig("/ApiUrl").first().getText();
2020-11-05 05:32:27 +00:00
var token, userId;
2020-11-05 06:38:51 +00:00
var servers = [];
2020-11-06 07:17:20 +00:00
var applications = [];
2020-11-05 06:38:51 +00:00
2020-11-05 08:51:09 +00:00
var assignStaticIP = function() {
SHELL.runWindow("cscript app.js shadow");
};
2020-11-06 07:17:20 +00:00
var showLocalApplications = function() {
var localApplications = [];
// 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
});
}
2020-11-09 19:54:01 +00:00
// Chrome
2020-11-06 08:25:17 +00:00
localApplications.push({
2020-11-09 19:54:01 +00:00
name: "Chrome",
uniqueId: "John"
2020-11-06 08:25:17 +00:00
});
localApplications.push({
2020-11-09 19:54:01 +00:00
name: "Chrome",
uniqueId: "James"
2020-11-06 08:25:17 +00:00
});
localApplications.push({
2020-11-09 19:54:01 +00:00
name: "Chrome",
uniqueId: "Jasmine"
2020-11-06 08:25:17 +00:00
});
2020-11-06 07:17:20 +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("반영되었습니다.");
}
};
if (applicationId) {
req = $.ajax({
type: "PATCH",
url: apiUrl + "/netsolid/items/applications/" + applicationId,
data: JSON.stringify(data),
contentType: 'application/json-patch+json',
success: onSuccess
});
} else {
req = HTTP.post(apiUrl + "/netsolid/items/applications", JSON.stringify(data), {
"Content-Type": "application/json"
});
res = JSON.parse(req.responseText);
onSuccess(res);
}
2020-11-09 20:02:11 +00:00
showApplications();
2020-11-06 07:17:20 +00:00
}
}).val(serverId);
entry.appendTo("#listview_applications");
}
template.css("display", "none");
};
var showApplications = function() {
var xmlStrings = [];
2020-11-05 07:50:05 +00:00
var req = HTTP.get(apiUrl + "/netsolid/items/applications", "", {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "bearer " + token,
//"Pragma": "no-cache",
//"Cache-Control": "no-cache",
"If-Modified-Since": "Sat, 1 Jan 2000 00:00:00 GMT"
});
var res = JSON.parse(req.responseText);
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>");
2020-11-06 07:17:20 +00:00
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
});
2020-11-05 07:50:05 +00:00
}
}
xmlStrings.push("</Item>");
}
xmlStrings.push("</StaticIP>");
FILE.writeFile("staticip.xml", xmlStrings.join("\r\n"), "utf-8");
};
2020-11-05 06:17:01 +00:00
var getAssignedServers = function() {
var assignedServers = [];
var req = HTTP.get(apiUrl + "/netsolid/items/assignedservers", "", {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "bearer " + token,
//"Pragma": "no-cache",
//"Cache-Control": "no-cache",
"If-Modified-Since": "Sat, 1 Jan 2000 00:00:00 GMT"
});
var res = JSON.parse(req.responseText);
for (var i = 0; i < res.data.length; i++) {
if (res.data[i].assigned_to == userId) {
assignedServers.push(res.data[i].server);
}
}
return assignedServers;
};
2020-11-05 05:32:27 +00:00
var showServers = function() {
OldBrowser.setContent(FILE.readFile("app\\servers.html", "utf-8"));
2020-11-05 06:17:01 +00:00
var assignedServers = getAssignedServers();
var req = HTTP.get(apiUrl + "/netsolid/items/servers", "", {
2020-11-05 05:32:27 +00:00
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "bearer " + token
});
var res = JSON.parse(req.responseText);
2020-11-06 07:17:20 +00:00
var template = $("#listview_servers .template");
2020-11-05 05:32:27 +00:00
2020-11-05 06:17:01 +00:00
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);
2020-11-06 07:17:20 +00:00
entry.appendTo("#listview_servers");
2020-11-05 06:38:51 +00:00
servers.push({
"data": res.data[i],
"entry": entry
});
2020-11-05 06:17:01 +00:00
}
2020-11-05 05:32:27 +00:00
}
2020-11-05 06:17:01 +00:00
template.css("display", "none");
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-05 06:38:51 +00:00
var pingTest = function() {
for (var i = 0; i < servers.length; i++) {
var responseTime = SYS.pingTest(servers[i].data.ipaddress);
2020-11-05 07:50:05 +00:00
servers[i].entry.find("span.ping").text(responseTime + " ms");
2020-11-05 06:38:51 +00:00
}
};
document.getElementById("btn_pingtest").onclick = pingTest;
2020-11-05 06:43:59 +00:00
setInterval(pingTest, 5000);
2020-11-05 06:38:51 +00:00
pingTest();
2020-11-05 07:50:05 +00:00
2020-11-06 07:17:20 +00:00
showApplications();
2020-11-05 05:32:27 +00:00
2020-11-06 07:17:20 +00:00
document.getElementById("btn_assign").onclick = function() {
showApplications();
assignStaticIP();
};
};
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") {
showServers();
2020-11-06 07:17:20 +00:00
showLocalApplications();
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();
};
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-06 07:17:20 +00:00
var req = HTTP.post(apiUrl + "/netsolid/auth/authenticate", JSON.stringify(credential), {
2020-11-05 05:09:20 +00:00
"Content-Type": "application/json"
});
var res = JSON.parse(req.responseText);
if ("error" in res) {
console.error(res.error.message);
} else if ("data" in res) {
console.log("ok");
FILE.writeFile("token.txt", res.data.token, "utf-8");
2020-11-05 05:32:27 +00:00
FILE.writeFile("userid.txt", res.data.user.id, "utf-8");
2020-11-05 06:17:01 +00:00
window.location.reload();
2020-11-05 05:09:20 +00:00
}
};
}