forked from beaugunderson/ip-address
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathv6decode.js
More file actions
189 lines (144 loc) · 4.54 KB
/
v6decode.js
File metadata and controls
189 lines (144 loc) · 4.54 KB
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
// This is a re-implementation of the example given in diff_match_patch;
// I needed a way to specify a class name for the wrapped text.
diff_match_patch.prototype.diff_ourHtml = function(diffs) {
var html = [];
var i = 0;
var pattern_amp = /&/g;
var pattern_lt = /</g;
var pattern_gt = />/g;
var pattern_para = /\n/g;
for (var x = 0; x < diffs.length; x++) {
var op = diffs[x][0]; // Operation (insert, delete, equal)
var data = diffs[x][1]; // Text of change.
var text = data.replace(pattern_amp, '&').replace(pattern_lt, '<')
.replace(pattern_gt, '>').replace(pattern_para, '¶<br>');
switch (op) {
case DIFF_INSERT:
html[x] = '<ins class="diff">' + text + '</ins>';
break;
case DIFF_DELETE:
html[x] = '<del class="diff">' + text + '</del>';
break;
case DIFF_EQUAL:
html[x] = '<span class="diff">' + text + '</span>';
break;
}
if (op !== DIFF_DELETE) {
i += data.length;
}
}
return html.join('');
};
// Runs whenever the address changes
function update_address() {
var o = $('#address').val();
var a = new v6.Address(o);
$.bbq.pushState({ address: o });
o = o.replace(/%.*/, '');
if (a.isValid()) {
$('.error').hide();
$('#address-wrapper').addClass('blue');
$('#address-wrapper').removeClass('red');
$('#valid').text('true');
$('#correct').text(a.isCorrect());
$('#canonical').text(a.isCanonical());
$('#original').text(o);
$('#subnet-string').text(a.subnet_string);
$('#percent-string').text(a.percent_string);
var p = a.parsed_address.join(':');
var p2 = diff(o, p);
$('#parsed').html(p2);
var co = a.correct_form();
var co2 = diff(o, co);
$('#correct-form').html(co2);
var ca = a.canonical_form();
var ca2 = diff(o, ca);
$('#canonical-form').html(ca2);
$('#ipv4-form').text(a.v4_form());
$('#decimal-groups').text(a.decimal());
$('#base-16').text(a.bigInteger().toString(16));
$('#base-10').text(a.bigInteger().toString());
var z = [a.zeroPad().slice(0, 64), a.zeroPad().slice(64,128)];
$('#base-2').html(z.join('<br />'));
if (a.isTeredo()) {
$('#teredo').text(JSON.stringify(a.teredo(), '', 3));
} else {
$('#teredo').text('Not a Teredo address.');
}
$.getJSON('/arin/ip/' + a.correct_form(), function(data) {
$('#arin').text(JSON.stringify(data, '', 3));
}).error(function(result) {
if (result.status == 404) {
$('#arin').text('No ARIN record found for that address.');
}
});
} else {
$('#address-wrapper').addClass('red');
$('#address-wrapper').removeClass('blue');
$('.output').text('');
$('.error').show();
$('#error').text(a.error);
$('#valid').text('false');
}
update_diff();
update_arin();
}
// A convenience function to diff two addresses (or other text)
function diff(a, b) {
var dmp = new diff_match_patch();
var d = dmp.diff_main(a, b);
return dmp.diff_ourHtml(d);
}
function set_address(address) {
$('#address').val(address);
$('#address').change();
return false;
}
function update_arin() {
if ($('#show-arin').is(':checked')) {
$('.arin').show()
} else {
$('.arin').hide()
}
}
function update_diff() {
if ($('#show-diff').is(':checked')) {
$('ins.diff').addClass('visible');
$('del.diff').show();
} else {
$('ins.diff').removeClass('visible');
$('del.diff').hide();
}
}
function update_from_hash() {
var a = $.bbq.getState('address');
if (a) {
$('#address').val(a);
update_address();
}
}
$(function() {
update_from_hash();
update_arin();
// Setup the event handlers for the 'Show ARIN' and 'Show diff' checkboxes
$('#show-arin').click(function() {
update_arin();
});
$('#show-diff').click(function() {
update_diff();
});
// Setup the event handlers for the example addresses
$('#correct-example').click(function() {
return set_address('2608::3:5');
});
$('#canonical-example').click(function() {
return set_address('000a:000b:000c:000d:0000:0000:0000:0000');
});
$('#teredo-example').click(function() {
return set_address('2001::ce49:7601:e866:efff:62c3:fffe');
});
// Make the input textbox update on change and keyup
$('#address').bind('change keyup', function() {
update_address();
});
});