forked from natefox/aws-ssh-rdp-links
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpage.js
198 lines (172 loc) · 5.55 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
var default_options = {
ssh_user: "ec2-user",
always_override_user: false,
rdp_user: "Administrator",
rdp_style: "MS",
}
var options = {}
function get_options() {
chrome.storage.sync.get(default_options, function(items) {
options = items
})
}
get_options()
chrome.storage.onChanged.addListener(function() {
get_options()
// remove all links
var links = document.querySelectorAll(".awssshrdplink")
for (var i=0; i < links.length; i++) {
links[i].parentNode.removeChild(links[i])
}
// add new links
window.setTimeout(go, 10)
})
// poll at a high frequency until the "detailsPublicDNS" element has loaded
// the instance details have finished loaded when this element appears
// after this, rely on the click and keyup events below to add new SSH links when the selection changes
var load_timer = setInterval(function() {
if (document.getElementById("detailsPublicDNS")) {
clearInterval(load_timer)
window.setTimeout(function() {
go()
}, 1000)
}
}, 100)
document.addEventListener("click", function() {
setTimeout(go, 500)
})
document.addEventListener("keyup", function() {
setTimeout(go, 1000)
})
function get_selector(row, col) {
// nth-child is 1-indexed!
return document.querySelector(`.gwt-TabLayoutPanelContent table > tbody tr:nth-child(${row}) > td:nth-child(${col}) div:nth-child(2)`)
}
function go() {
if (!window.location.hash.startsWith("#Instances:")) return
var active_tab = document.querySelector(".gwt-TabLayoutPanelContent > div:not([aria-hidden])")
if (!active_tab || active_tab.children.length == 0 || active_tab.children[0].tagName == "UL") {
// multiple instances are selected
return
}
var elastic_ips = get_selector(5, 1)
if (elastic_ips) {
var list_items = elastic_ips.getElementsByTagName("li")
for (var i=0; i < list_items.length; i++) {
add_to_element(list_items[i])
}
}
add_to_element(get_selector(2, 1)) // Instance ID
add_to_element(get_selector(2, 2)) // Public DNS (IPv4)
add_to_element(get_selector(3, 2)) // IPv4 Public IP
add_to_element(get_selector(4, 2)) // IPv6 IPs
add_to_element(get_selector(5, 2)) // Private DNS
add_to_element(get_selector(6, 2)) // Private IPs
add_to_element(get_selector(7, 2)) // Secondary private IPs
// Top bar "Public DNS" / "Private IP" / "Elastic IP"
var instance = document.querySelector("span[style^='padding-left: 5px;']")
if (instance) {
add_to_element(instance.parentNode.lastChild)
}
}
function add_to_element(el) {
if (!el || el.querySelector(".awssshrdplink")) {
// do not add multiple times
return
}
var text = el.textContent.trim()
if (text.endsWith(" IPs")) {
// instances with multiple IPv6 addresses have a link that brings up a popup with the list of addresses ("2 IPs")
return
}
if (text.startsWith("Private IP: ")) {
text = text.substring("Private IP: ".length)
}
else if (text.startsWith("Public DNS: ")) {
text = text.substring("Public DNS: ".length)
}
else if (text.startsWith("Elastic IP: ")) {
text = text.substring("Elastic IP: ".length)
}
else if (text.endsWith("*")) {
// remove * from EIP
text = text.substr(0, text.length-1)
}
else if (text.includes(",")) {
// multiple Secondary private IPs; only use the first one
text = text.substr(0, text.indexOf(","))
}
// put IPv6 inside []
if (text.includes(":")) {
text = `[${text}]`
}
if (text[0] == "-" || text.trim() == "") {
return
}
var link = document.createElement("a")
link.className = "awssshrdplink"
var platform = get_selector(10, 1)
platform = (platform ? platform.textContent : "")
if (platform == "windows") {
link.setAttribute("data-link-text", "RDP")
var user = options["rdp_user"]
if (options["rdp_style"] == "MS") {
var query_string_opts = []
if (user != "") {
query_string_opts.push("username=s:"+user)
}
query_string_opts.push("full%20address=s:"+text+":3389")
var query_string = query_string_opts.join("&")
link.href = "rdp://"+query_string
}
else if (options["rdp_style"] == "CoRD") {
link.href = "rdp://"+(user?`${user}@`:"")+text
}
}
else {
var instance_id = get_selector(2, 1)
instance_id = (instance_id ? instance_id.textContent : "")
var avail_zone = get_selector(6, 2);
avail_zone = (avail_zone ? avail_zone.textContent : "")
aws_region = avail_zone.substring(0, avail_zone.length - 1)
if ( get_selector(2, 1).textContent == text ) {
link.setAttribute("data-link-text", "SSM")
link.setAttribute('target','_blank')
link.href = "https://console.aws.amazon.com/systems-manager/session-manager/"+text+"?region="+aws_region
}
else {
link.setAttribute("data-link-text", "SSH")
var user = get_ssh_user()
link.href = "ssh://"+(user?`${user}@`:"")+text
}
}
el.classList.add("awssshrdp-element")
if (el.children.length > 1) {
// add link before the copy to clipboard button
el.insertBefore(link, el.lastChild)
}
else {
el.appendChild(link)
}
}
function get_ssh_user() {
if (options["always_override_user"])
return options["ssh_user"]
var ami = get_selector(9, 1)
if (!ami)
return options["ssh_user"]
ami = ami.textContent
if (ami.includes("ubuntu"))
return "ubuntu"
else if (ami.includes("amzn"))
return "ec2-user"
else if (ami.includes("RHEL"))
return "ec2-user"
else if (ami.includes("suse-sles"))
return "ec2-user"
else if (ami.includes("CoreOS"))
return "core"
else if (ami.includes("VyOS"))
return "vyos"
return options["ssh_user"]
}