lmc/linodes/remote_access/remote_access.js

269 lines
9.3 KiB
JavaScript

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.ipFailover = "ip-failover";
elements.ipRemove = "ip-remove";
elements.ipSwap = "ip-swap";
elements.ipv4Addrs = "ipv4-addrs";
elements.ipv4Gateways = "ipv4-gateways";
elements.linkLocal = "link-local";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.lishLink = "lish-link";
elements.privateIPGuide = "private-ips-guide";
elements.privateIPs = "private-ips";
elements.resolvers = "resolvers";
elements.slaacAddr = "slaac-addr";
elements.slaacGateway = "slaac-gateway";
elements.slaacPrefix = "slaac-prefix";
elements.slaacRdns = "slaac-rdns";
elements.sshLink = "ssh-link";
// Data recieved from API calls
var data = {};
data.linode = {};
data.netInfo = {};
// LISH uses different DC names than the API uses, so we need to translate them
data.lishDCs = {
"us-central": "dallas",
"us-west": "fremont",
"us-southeast": "atlanta",
"us-east": "newark",
"us-east-1b": "cjj2",
"eu-west": "london",
"ap-south": "singapore",
"eu-central": "frankfurt",
"ap-northeast": "shg1",
"ap-northeast-1a": "shg1",
"ca-central": "tor1",
"ap-west": "mum1",
"ap-southeast": "syd1",
"philadelphia": "phl1"
};
// Static references to UI elements
var ui = {};
ui.ipFailover = {};
ui.ipRemove = {};
ui.ipSwap = {};
ui.ipv4Addrs = {};
ui.ipv4Gateways = {};
ui.linkLocal = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.lishLink = {};
ui.privateIPGuide = {};
ui.privateIPs = {};
ui.resolvers = {};
ui.slaacAddr = {};
ui.slaacGateway = {};
ui.slaacPrefix = {};
ui.slaacRdns = {};
ui.sshLink = {};
// Callback for linode details API call
var displayDetails = function(response)
{
data.linode = response;
// Set page title and header stuff
document.title += " // " + data.linode.label;
ui.linodeLabel.innerHTML = data.linode.label;
if (data.linode.tags.length == 1) {
ui.linodeTagLink.href = "/linodes?tag=" + data.linode.tags[0];
ui.linodeTagLink.innerHTML = "(" + data.linode.tags[0] + ")";
ui.linodeTag.style.display = "inline";
}
// Replace user in SSH link if we're running CoreOS Container Linux
if (data.linode.image == "linode/containerlinux") {
ui.sshLink.href = ui.sshLink.href.replace("root", "core");
ui.sshLink.innerHTML = ui.sshLink.innerHTML.replace("root", "core");
}
// Get list of other instances in the same region
var filters = {
"region": data.linode.region
};
apiGet("/linode/instances", displayIPLinks, filters);
// Get region info to display resolvers
apiGet("/regions/" + data.linode.region, displayResolvers, null);
// Update the LISH link
ui.lishLink.href = ui.lishLink.href.replace("lish-", "lish-" + data.lishDCs[data.linode.region]);
ui.lishLink.innerHTML = ui.lishLink.innerHTML.replace("lish-", "lish-" + data.lishDCs[data.linode.region]);
ui.lishLink.innerHTML += " " + data.linode.label;
};
// Callback for regional instances API call
var displayIPLinks = function(response)
{
// Only enable IP failover and swap links if there are 2 or more instances in this location
if (response.data.length < 2)
return;
var ipFailover = document.createElement("a");
ipFailover.href = "/linodes/ip_failover?lid=" + data.params.lid;
ipFailover.innerHTML = "IP Failover";
ui.ipFailover.replaceWith(ipFailover);
var ipSwap = document.createElement("a");
ipSwap.href = "/linodes/ip_swap?lid=" + data.params.lid;
ipSwap.innerHTML = "IP Swap";
ui.ipSwap.replaceWith(ipSwap);
};
// Callback for user info API call
var displayLishUser = function(response)
{
// Insert username into LISH link
ui.lishLink.href = ui.lishLink.href.replace("@", response.username + "@");
ui.lishLink.innerHTML = ui.lishLink.innerHTML.replace("@", response.username + "@");
};
// Callback for network info API call
var displayNetInfo = function(response)
{
data.netInfo = response;
// Set SSH link
// CoreOS Container Linux uses core instead of root
var user = "root";
if (data.linode && data.linode.image == "linode/containerlinux")
user = "core";
if (data.netInfo.ipv4["public"].length > 0) {
ui.sshLink.href = "ssh://" + user + "@" + data.netInfo.ipv4["public"][0].address;
ui.sshLink.innerHTML = "ssh " + user + "@" + data.netInfo.ipv4["public"][0].address;
}
// Show public IPv4 info
for (var i = 0; i < data.netInfo.ipv4["public"].length; i++) {
var ipinfo = document.createElement("span");
ipinfo.innerHTML = data.netInfo.ipv4["public"][i].address + " / " + data.netInfo.ipv4["public"][i].prefix + " ( " + data.netInfo.ipv4["public"][i].rdns + " )";
var gateway = document.createElement("span");
gateway.innerHTML = data.netInfo.ipv4["public"][i].gateway;
var br = document.createElement("br");
ui.ipv4Addrs.appendChild(ipinfo);
ui.ipv4Addrs.appendChild(br);
ui.ipv4Gateways.appendChild(gateway);
ui.ipv4Gateways.appendChild(br.cloneNode(false));
}
// Show public IPv6 info
ui.slaacAddr.innerHTML = data.netInfo.ipv6.slaac.address;
ui.slaacGateway.innerHTML = data.netInfo.ipv6.slaac.gateway;
ui.slaacPrefix.innerHTML = data.netInfo.ipv6.slaac.prefix;
ui.slaacRdns.innerHTML = data.netInfo.ipv6.slaac.rdns;
// Enable IP remove link if multiple IPv4s
if (data.netInfo.ipv4["public"].length > 1) {
var ipRemove = document.createElement("a");
ipRemove.href = "/linodes/ip_remove?lid=" + data.params.lid;
ipRemove.innerHTML = "IP Remove";
ui.ipRemove.replaceWith(ipRemove);
}
// Display private IP info
if (data.netInfo.ipv4["private"].length > 0) {
ui.privateIPs.innerHTML = "";
ui.privateIPGuide.style.visibility = "visible";
}
for (var i = 0; i < data.netInfo.ipv4["private"].length; i++) {
var ip = document.createElement("span");
ip.innerHTML = data.netInfo.ipv4["private"][i].address + " / " + data.netInfo.ipv4["private"][i].prefix;
var br = document.createElement("br");
ui.privateIPs.appendChild(ip);
ui.privateIPs.appendChild(br);
}
// Display link-local IPv6 address
ui.linkLocal.innerHTML = data.netInfo.ipv6.link_local.address + " / " + data.netInfo.ipv6.link_local.prefix;
};
// Callback for region info API call
var displayResolvers = function(response)
{
var resolvers = response.resolvers.ipv4.split(",").concat(response.resolvers.ipv6.split(","));
for (var i = 0; i < resolvers.length; i++) {
var resolver = document.createElement("span");
resolver.innerHTML = resolvers[i];
var br = document.createElement("br");
ui.resolvers.appendChild(resolver);
ui.resolvers.appendChild(br);
}
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a Linode ID, so die if we don't have it
if (!data.params.lid) {
alert("No Linode ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper Linode ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
anchors[i].href = anchors[i].href.replace("lid=00", data.params.lid);
anchors[i].href = anchors[i].href.replace("lid=0", "lid=" + data.params.lid);
}
// Get element references
ui.ipFailover = document.getElementById(elements.ipFailover);
ui.ipRemove = document.getElementById(elements.ipRemove);
ui.ipSwap = document.getElementById(elements.ipSwap);
ui.ipv4Addrs = document.getElementById(elements.ipv4Addrs);
ui.ipv4Gateways = document.getElementById(elements.ipv4Gateways);
ui.linkLocal = document.getElementById(elements.linkLocal);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.lishLink = document.getElementById(elements.lishLink);
ui.privateIPGuide = document.getElementById(elements.privateIPGuide);
ui.privateIPs = document.getElementById(elements.privateIPs);
ui.resolvers = document.getElementById(elements.resolvers);
ui.slaacAddr = document.getElementById(elements.slaacAddr);
ui.slaacGateway = document.getElementById(elements.slaacGateway);
ui.slaacPrefix = document.getElementById(elements.slaacPrefix);
ui.slaacRdns = document.getElementById(elements.slaacRdns);
ui.sshLink = document.getElementById(elements.sshLink);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
apiGet("/linode/instances/" + data.params.lid + "/ips", displayNetInfo, null);
apiGet("/profile", displayLishUser, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();