This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
221 lines (196 loc) · 7.08 KB
/
index.html
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>PittMesh IP Address calculator</title>
<style>
#transcript {
border: 2px groove threedface;
font-family: monospace;
padding: 0.35em 0.75em 0.75em 0.625em;
margin: 8px 2px;
}
@media all and (min-width: 400px) {
.output {
display: inline-block;
white-space: nowrap;
}
.output label {
display: inline-block;
width: 4.5em;
}
}
@media all and (max-width: 479px) {
.input input {
width: calc(100% - 9.25em);
}
.input label {
display: block;
text-align: center;
}
.output input {
width: calc(100% - 12.5em);
}
h1 {
text-align: center;
}
}
@media all and (max-width: 399px) {
.output {
white-space: normal;
}
.output input {
width: calc(100% - 7.25em);
}
.output label {
display: block;
text-align: center;
}
}
</style>
</head>
<body>
<h1>PittMesh IP address calculator</h1>
<hr/>
<p>
This tool takes a router's MAC address and calculates the two IP addresses
that the router needs: mesh interface and wlan client interface.
</p>
<form id="calc" onSubmit="return calc();">
<fieldset form="calc">
<legend>Input</legend>
<div class="input">
<label for="mac">MAC address</label>
<input name="mac" id="mac" placeholder="de:ad:be:ef:00:00"/>
<button type="submit">Calculate</button>
</div>
</fieldset>
<fieldset form="calc">
<legend>Output</legend>
<div class="output">
<label for="mesh">Mesh IP</label>
<input name="mesh" id="mesh" disabled="disabled"/>
<button id="copy-mesh" type="button">copy</button>
</div>
<div class="output">
<label for="wlan">WLAN IP</label>
<input name="wlan" id="wlan" disabled="disabled"/>
<button id="copy-wlan" type="button">copy</button>
</div>
</fieldset>
</form>
<div id="transcript">
<p>Log messages will appear here.</p>
<p><a href="https://github.com/pittmesh/ip-calculator">Fork ip-calculator on Github</a>.</p>
</div>
<script>
"use strict";
//
// constants
//
var meshPrefix = [100];
var wlanPrefix = [10];
var wlanSuffix = [1];
var macEl = document.getElementById('mac');
var meshEl = document.getElementById('mesh');
var wlanEl = document.getElementById('wlan');
var transcriptEl = document.getElementById('transcript');
var cssError = "color:red; font-family:helvetica,sans-serif; font-weight:bold; text-shadow:0 1px 2px rgba(0,0,0,.5);";
var cssSuccess = "color:green; font-family:helvetica,sans-serif; font-weight:bold; text-shadow:0 1px 2px rgba(0,0,0,.5);";
var cssWarning = "background-color:gray; color:yellow; display: block; font-family:helvetica,sans-serif; font-weight:bold; text-shadow:0 1px 2px rgba(0,0,0,.5); width: 100%;";
function calc() {
var mac = macEl.value;
var macArray = mac.split(/[:-]/);
if (!mac.match(/^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$/)) {
displayError("The MAC address does not appear to be valid. Ensure that there are six octets, separated by colons (:) or hyphens (-) with valid hexadecimal values 00-FF");
return false;
}
var macArrayDecimals = convertHexToDecimals(macArray);
var meshIP = calcMeshIP(adjustOctets(lastThreeOctets(macArrayDecimals)));
var wlanIP = calcWlanIP(adjustOctets(lastTwoOctets(macArrayDecimals)));
meshEl.value = meshIP;
wlanEl.value = wlanIP;
displaySuccess("MAC Address "+macArray.join(":")+" calculates to Mesh IP "+meshIP+" WLAN IP "+wlanIP);
return false;
}
function calcMeshIP(lastThree) {
return makeIP(meshPrefix.concat(lastThree));
}
function calcWlanIP(lastTwo) {
return makeIP(wlanPrefix.concat(lastTwo).concat(wlanSuffix));
}
function makeIP(arr) { return arr.join("."); }
function pad(s, l, p) { return (Array(l||2).join(p||"0")+s).slice(-l||-2); }
function padcall(c, i, a) { return pad(c); }
function log(text, style) {
var date = new Date();
var timestamp = "["+[date.getHours(), date.getMinutes(), date.getSeconds()].map(padcall).join(":")+"]";
transcriptEl.appendFirst(createLogLineElement(timestamp, text, style));
console.log("[%.2d:%.2d:%.2d] %c"+text, date.getHours(), date.getMinutes(), date.getSeconds(), style);
}
function displayError(text) {
log(text, cssError);
}
function displaySuccess(text) {
log(text, cssSuccess);
}
function displayWarning(text) {
log(text, cssWarning);
}
function createLogLineElement(time, text, style) {
var line = document.createElement("p");
var lineTime = document.createElement("span");
var lineText = document.createElement("span");
lineTime.appendChild(document.createTextNode(time));
lineText.appendChild(document.createTextNode(text));
lineTime.classList.add("time");
lineText.classList.add("text");
lineText.style = style;
line.appendChild(lineTime);
line.appendChild(lineText);
return line;
}
function adjustOctets(arr) {
console.log(arr.length);
if (arr.length == 3) {
arr[0] %= 32 + 96;
arr[2] -= (arr[2] % 64 - arr[2] % 32);
} else if (arr.length == 2) {
arr[1] -= (arr[1] % 64 - arr[1] % 32);
}
return arr;
}
function convertHexToDecimals(arr) { return arr.map( function(h) { return parseInt(h, 16); } ); }
function firstThreeOctets(arr) { return arr.slice(0, 3); }
function lastThreeOctets(arr) { return arr.slice(3, 6); }
function lastTwoOctets(arr) { return arr.slice(4, 6); }
HTMLElement.prototype.appendFirst = function(childNode){
if(this.firstChild)
this.insertBefore(childNode, this.firstChild);
else
this.appendChild(childNode);
};
function doCopy(element) {
element.disabled = false;
element.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'Successfully' : 'Unsuccessfully';
displaySuccess(msg + " copied to clipboard.");
} catch (err) {
displayError("Oops, unable to copy to clipboard. You are on your own.");
} finally {
element.disabled = true;
}
}
function copyInput(element) {
return function(event) {
event.preventDefault();
doCopy(element);
}
}
document.getElementById("copy-mesh").addEventListener('click', copyInput(meshEl));
document.getElementById("copy-wlan").addEventListener('click', copyInput(wlanEl));
</script>
</body>
</html>