|
| 1 | +// from option43.org |
| 2 | + |
| 3 | +function IPToHexDigit(ip_addr: string) { // Ruckus |
| 4 | + const arr1 = []; |
| 5 | + // ip_addr = ip_addr.split('.').join(''); |
| 6 | + for (let n = 0, l = ip_addr.length; n < l; n++) { |
| 7 | + const hex = Number(ip_addr.charCodeAt(n)).toString(16); |
| 8 | + arr1.push(hex); |
| 9 | + } |
| 10 | + return arr1.join(''); |
| 11 | +} |
| 12 | + |
| 13 | +function IPToHexNumber(ip_addr: string) { |
| 14 | + const arr1 = []; |
| 15 | + const ips = ip_addr.split('.'); |
| 16 | + for (let n = 0, l = ips.length; n < l; n++) { |
| 17 | + const hex = (`0${Number(ips[n]).toString(16)}`).slice(-2); |
| 18 | + arr1.push(hex); |
| 19 | + } |
| 20 | + return arr1.join(''); |
| 21 | +} |
| 22 | + |
| 23 | +function IPCharCounter(ip_addr: string, ret: string) { |
| 24 | + if (ret === 'hex') { |
| 25 | + return (`0${ip_addr.length.toString(16)}`).slice(-2); |
| 26 | + } |
| 27 | + else { |
| 28 | + return ip_addr.length; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function splitIPs(ip_addr: string) { |
| 33 | + const validIPs = []; |
| 34 | + const rows = ip_addr.split('\n'); |
| 35 | + for (let n = 0, l = rows.length; n < l; n++) { |
| 36 | + if (/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/.test(rows[n])) { |
| 37 | + validIPs.push(rows[n]); |
| 38 | + } |
| 39 | + } |
| 40 | + return validIPs; |
| 41 | +} |
| 42 | + |
| 43 | +function IPNumberCounter(ip_addr: string[]) { |
| 44 | + return ip_addr.length; |
| 45 | +} |
| 46 | + |
| 47 | +function renderSettings( |
| 48 | + dhcp_vendor: string, |
| 49 | + option43_type: string, |
| 50 | + option43_subtype: string, |
| 51 | + option43_value: string, |
| 52 | + option60_type: string | undefined, |
| 53 | + option60_value: string | undefined, |
| 54 | + vendor_link: string | undefined, |
| 55 | + diff_number: number | undefined) { |
| 56 | + let output; |
| 57 | + switch (dhcp_vendor) { |
| 58 | + case 'cisco_01': |
| 59 | + // option code [instance number] {ascii string | hex string | ip-address} // no quotation marks in option 43! |
| 60 | + output = '<p>Cisco CLI commands:</p><div class="cli"><p>option '; |
| 61 | + if (typeof diff_number !== 'undefined') { |
| 62 | + output = output + diff_number; |
| 63 | + } |
| 64 | + else { output = `${output}43`; } |
| 65 | + output = `${output} ${option43_type}`; |
| 66 | + output = `${output} ${option43_subtype}${option43_value}`; |
| 67 | + if (typeof option60_value !== 'undefined') { |
| 68 | + output = `${output}</p><p>option 60 ascii "${option60_value}"`; |
| 69 | + } |
| 70 | + output = `${output}</p></div><!--<p>More information: ${vendor_link}</p>-->`; |
| 71 | + return output; |
| 72 | + case 'juniper_01': |
| 73 | + // https://www.juniper.net/documentation/en_US/junos/topics/topic-map/dhcp-serever-options.html#id-configure-user-defined-dhcp-options |
| 74 | + |
| 75 | + output = '<p>Juniper EX CLI commands:</p><div class="cli"><p>set system services dhcp option '; |
| 76 | + if (typeof diff_number !== 'undefined') { |
| 77 | + output = output + diff_number; |
| 78 | + } |
| 79 | + else { output = `${output}43`; } |
| 80 | + if (option43_type === 'ascii') { |
| 81 | + option43_type = 'string'; |
| 82 | + } |
| 83 | + if (option60_type === 'ascii') { |
| 84 | + option60_type = 'string'; |
| 85 | + } |
| 86 | + output = `${output} ${option43_type}`; |
| 87 | + output = `${output} ${option43_subtype}${option43_value}`; |
| 88 | + if (typeof option60_value !== 'undefined') { |
| 89 | + output = `${output}</p><p>set system services dhcp option 60 ascii "${option60_value}"`; |
| 90 | + } |
| 91 | + output = `${output}</p></div>`; |
| 92 | + return output; |
| 93 | + case 'juniper_02': |
| 94 | + // https://www.juniper.net/documentation/en_US/junos/topics/topic-map/dhcp-serever-options.html#id-configure-user-defined-dhcp-options |
| 95 | + |
| 96 | + output = '<p>Juniper SRX CLI commands:</p><div class="cli"><p>set access address-assignment pool <i>AP_DHCP_POOL</i> family inet dhcp-attributes option '; |
| 97 | + if (typeof diff_number !== 'undefined') { |
| 98 | + output = output + diff_number; |
| 99 | + } |
| 100 | + else { output = `${output}43`; } |
| 101 | + if (option43_type === 'ascii') { |
| 102 | + option43_type = 'string'; |
| 103 | + } |
| 104 | + if (option43_type === 'hex') { |
| 105 | + option43_type = 'byte-stream'; |
| 106 | + } |
| 107 | + output = `${output} ${option43_type}`; |
| 108 | + if (option43_type === 'byte-stream') { |
| 109 | + output = `${output} "0x${(option43_subtype + option43_value).match(/.{1,2}/g)?.join(' 0x')}"`; |
| 110 | + } |
| 111 | + else { |
| 112 | + output = `${output} ${option43_subtype}${option43_value}`; |
| 113 | + } |
| 114 | + if (option60_type === 'ascii') { |
| 115 | + option60_type = 'string'; |
| 116 | + } |
| 117 | + if (typeof option60_value !== 'undefined') { |
| 118 | + output = `${output}</p><p>set access address-assignment pool <i>AP_DHCP_POOL</i> family inet dhcp-attributes option 60 ascii "${option60_value}"`; |
| 119 | + } |
| 120 | + output = `${output}</p></div>`; |
| 121 | + return output; |
| 122 | + default: |
| 123 | + output = '<p>Raw values:</p><p><b>Option '; |
| 124 | + if (typeof diff_number !== 'undefined') { |
| 125 | + output = output + diff_number; |
| 126 | + } |
| 127 | + else { |
| 128 | + output = `${output}43`; |
| 129 | + } |
| 130 | + output = `${output}</b><br />Type: "`; |
| 131 | + if (option43_type === 'ascii') { |
| 132 | + option43_type = 'String/ASCII'; |
| 133 | + } |
| 134 | + if (option43_type === 'hex') { |
| 135 | + option43_type = 'Hexadecimal'; |
| 136 | + } |
| 137 | + if (option43_type === 'ip') { |
| 138 | + option43_type = 'IP-Address'; |
| 139 | + } |
| 140 | + if (option60_type === 'ascii') { |
| 141 | + option60_type = 'String/ASCII'; |
| 142 | + } |
| 143 | + output = output + option43_type; |
| 144 | + output = `${output}"<br />Value: ${option43_subtype}${option43_value}`; |
| 145 | + if (typeof option60_value !== 'undefined') { |
| 146 | + output = `${output}</p><p><b>Option 60</b><br /> Type: "${option60_type}"<br />Value: "${option60_value}"`; |
| 147 | + } |
| 148 | + output = `${output}</p></div>`; |
| 149 | + return output; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +export function getOption43Infos(ip_addr: string, wifi_vendor: string, dhcp_vendor: string) { |
| 154 | + const ip_addr_array = splitIPs(ip_addr); |
| 155 | + const ip_addr_hexDigit = IPToHexDigit (ip_addr_array.join(',')); // Ruckus, comma separation |
| 156 | + const ip_addr_hexNumber = IPToHexNumber (ip_addr_array.join('.')); // valid for Cisco, no separation |
| 157 | + const ip_addr_hexNumber_first = IPToHexNumber (ip_addr_array[0]); |
| 158 | + |
| 159 | + const num_ips = IPNumberCounter (ip_addr_array); |
| 160 | + const num_ip_bytes = (`0${(num_ips * 4).toString(16)}`).slice(-2); |
| 161 | + |
| 162 | + let option43_type = ''; // hex, ascii, ip |
| 163 | + let option43_subtype = ''; // vendor prefix |
| 164 | + let option43_value; |
| 165 | + let diff_number; |
| 166 | + let option60_type; // hex, ascii, ip |
| 167 | + let option60_value; |
| 168 | + let vendor_link; |
| 169 | + |
| 170 | + switch (wifi_vendor) { |
| 171 | + case 'cisco_01': |
| 172 | + option43_type = 'hex'; |
| 173 | + option43_subtype = 'f1'; |
| 174 | + option43_value = num_ip_bytes + ip_addr_hexNumber; |
| 175 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 176 | + case 'ruckus_01': |
| 177 | + option43_type = 'hex'; |
| 178 | + option43_subtype = '06'; |
| 179 | + option43_value = IPCharCounter (ip_addr_array.join(','), 'hex') + ip_addr_hexDigit; |
| 180 | + option60_type = 'ascii'; |
| 181 | + option60_value = 'Ruckus CPE'; |
| 182 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 183 | + case 'ruckus_02': |
| 184 | + option43_type = 'hex'; |
| 185 | + option43_subtype = '03'; |
| 186 | + option43_value = IPCharCounter (ip_addr_array.join(','), 'hex') + ip_addr_hexDigit; |
| 187 | + option60_type = 'ascii'; |
| 188 | + option60_value = 'Ruckus CPE'; |
| 189 | + vendor_link = 'https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html'; |
| 190 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 191 | + case 'aruba_01': |
| 192 | + option43_type = 'ip'; |
| 193 | + option43_value = ip_addr; |
| 194 | + option60_type = 'ascii'; |
| 195 | + option60_value = 'ArubaAP'; |
| 196 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 197 | + case 'fortinet_01': |
| 198 | + option43_type = 'ip'; |
| 199 | + option43_value = ip_addr_array.join(' '); |
| 200 | + // diff_number = 138; |
| 201 | + // option60_type = "ascii"; |
| 202 | + // option60_value = "ArubaAP"; |
| 203 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 204 | + case 'fortinet_02': |
| 205 | + option43_type = 'ip'; |
| 206 | + option43_value = ip_addr_array.join(' '); |
| 207 | + diff_number = 138; |
| 208 | + // option60_type = "ascii"; |
| 209 | + // option60_value = "ArubaAP"; |
| 210 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 211 | + case 'ubiquiti_01': |
| 212 | + option43_type = 'hex'; |
| 213 | + option43_subtype = '01'; |
| 214 | + option43_value = `04${ip_addr_hexNumber_first}`; |
| 215 | + // option60_type = "ascii"; |
| 216 | + // option60_value = "Ruckus CPE"; |
| 217 | + // vendor_link = "https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html"; |
| 218 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 219 | + case 'cambium_01': |
| 220 | + option43_type = 'ascii'; |
| 221 | + // option43_subtype = "01"; |
| 222 | + option43_value = `"https://${ip_addr[0]}"`; |
| 223 | + // option60_type = "ascii"; |
| 224 | + // option60_value = "Ruckus CPE"; |
| 225 | + // vendor_link = "https://docs.commscope.com/bundle/zd-10.2-userguide/page/GUID-D5CF7FE0-D73F-4B4B-95C8-08CAB5B235D5.html"; |
| 226 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 227 | + case 'linux_01': |
| 228 | + return `Linux:<br />Option 43 (IP-address): ${ip_addr}<br /> You also <b>need</b>:<br />Option 60 (String): "ArubaAP".`; |
| 229 | + case 'netgear_01': |
| 230 | + option43_type = 'hex'; |
| 231 | + option43_subtype = '0204'; |
| 232 | + option43_value = ip_addr_hexNumber_first; |
| 233 | + return renderSettings(dhcp_vendor, option43_type, option43_subtype, option43_value, option60_type, option60_value, vendor_link, diff_number); |
| 234 | + default: |
| 235 | + return 'Not implemented'; |
| 236 | + } |
| 237 | +}; |
0 commit comments