diff options
author | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2016-03-12 15:33:23 +0100 |
---|---|---|
committer | Ludovic Pouzenc <ludovic@pouzenc.fr> | 2016-03-12 15:33:23 +0100 |
commit | ff242e6e618ee53ebcd7251f9c26ff721c7a03b4 (patch) | |
tree | 9faf48236ebc78a8e0d0ff1646bc4f10ac54ce80 /maj/main.js | |
parent | edeaebe6e41d383c45553f3aca8328141e258fb9 (diff) | |
download | chd_openwrt-ff242e6e618ee53ebcd7251f9c26ff721c7a03b4.zip chd_openwrt-ff242e6e618ee53ebcd7251f9c26ff721c7a03b4.tar.gz chd_openwrt-ff242e6e618ee53ebcd7251f9c26ff721c7a03b4.tar.bz2 |
Initial import.
Diffstat (limited to 'maj/main.js')
-rw-r--r-- | maj/main.js | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/maj/main.js b/maj/main.js new file mode 100644 index 0000000..a880b92 --- /dev/null +++ b/maj/main.js @@ -0,0 +1,200 @@ +/** + * Copyright 2016 Ludovic Pouzenc <ludovic@pouzenc.fr> + * + * CHD OpenWRT 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. + * + * CHD OpenWRT 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 CHD OpenWRT. If not, see <http://www.gnu.org/licenses/>. +**/ + +/* +document.onreadystatechange = function () { + console.log("document.onreadystatechange"); +}; + +document.onunload = function () { + console.log("document.onunload"); + for(var i=0; i<1000000000; i++) {} +};*/ + +document.addEventListener("DOMContentLoaded", function() { + console.log("DOMContentLoaded"); + + // HTML5 form validation + var supports_input_validity = function() { var i = document.createElement("input"); return "setCustomValidity" in i; } + + if(supports_input_validity()) { + var pwd1Input = document.getElementById("field_pwd1"); + var pwd2Input = document.getElementById("field_pwd2"); + var key1Input = document.getElementById("field_key1"); + var key2Input = document.getElementById("field_key2"); + + pwd2Input.addEventListener("keyup", function() { + this.setCustomValidity( + (this.value!=pwd1Input.value)?pwd2Input.title:"" + )}, false); + + key2Input.addEventListener("keyup", function() { + this.setCustomValidity( + (this.value!=key1Input.value)?key2Input.title:"" + )}, false); + } + + /* Range slider value update + var txpower = document.getElementById("field_txpower"); + var txpowerText = document.getElementById("field_txpower_text"); + txpower.addEventListener("change", function(e) { + txpowerText.value=txpower.value + " dB"; + }, true); + */ + var btn_gen = document.getElementById("btn_gen"); + btn_gen.value="Générer le fichier de mise à jour"; + + // Additionnal form validation (on submit) + var checkForm = function(e) { + + if (this.pwd1.value != this.pwd2.value) { + this.pwd1.focus(); + e.preventDefault(); + return; + } + if (this.key1.value != this.key2.value) { + this.key1.focus(); + e.preventDefault(); + return; + } + // Safari needs this one too + if (!e.target.checkValidity()) { + e.preventDefault(); + return; + } + // Form will pass, disable the form button to prevent multiple clicks + var btn_gen = document.getElementById("btn_gen"); + btn_gen.value="Patientez puis sauvez le fichier"; + btn_gen.disabled=true; + btn_gen.style.color="gray"; + }; + var form_main = document.getElementById("form_main"); + form_main.addEventListener("submit", checkForm, true); + + // Guess of current LAN gateway + var field_ip4lan = document.getElementById("field_ip4lan"); + getIPs(function(ip){ + console.log(ip); + var res = ip.match(/^192\.168\.\d+/); + if ( res ) { + // Dumb guess of gateway IP + field_ip4lan.value = res + ".1"; + } + }); + +}, false); + + +window.addEventListener('unload', function(event) { + console.log('unload'); + + // Revert submit button to inital state if history.go(-1) was used + var btn_gen = document.getElementById("btn_gen"); + btn_gen.value="Générer le fichier de mise à jour"; + btn_gen.disabled=false; + btn_gen.style.color="black"; + +}); + + +// LAN IPs detection +function getIPs(callback){ + var ip_dups = {}; + + //compatibility for firefox and chrome + var RTCPeerConnection = window.RTCPeerConnection + || window.mozRTCPeerConnection + || window.webkitRTCPeerConnection; + var useWebKit = !!window.webkitRTCPeerConnection; + + if(!RTCPeerConnection){ + return; // The following code pops JS errors on Safari + } + + //bypass naive webrtc blocking using an iframe + if(!RTCPeerConnection){ + //NOTE: you need to have an iframe in the page right above the script tag + // + //<iframe id="iframe" sandbox="allow-same-origin" style="display: none"></iframe> + //<script>...getIPs called in here... + // + var win = iframe.contentWindow; + RTCPeerConnection = win.RTCPeerConnection + || win.mozRTCPeerConnection + || win.webkitRTCPeerConnection; + useWebKit = !!win.webkitRTCPeerConnection; + } + + //minimal requirements for data connection + var mediaConstraints = { +optional: [{RtpDataChannels: true}] + }; + + //firefox already has a default stun server in about:config + // media.peerconnection.default_iceservers = + // [{"url": "stun:stun.services.mozilla.com"}] + var servers = undefined; + + //add same stun server for chrome + if(useWebKit) + servers = {iceServers: [{urls: "stun:stun.services.mozilla.com"}]}; + + //construct a new RTCPeerConnection + var pc = new RTCPeerConnection(servers, mediaConstraints); + + function handleCandidate(candidate){ + //match just the IP address + var ip_regex = /([0-9]{1,3}(\.[0-9]{1,3}){3})/ + var ip_addr = ip_regex.exec(candidate)[1]; + + //remove duplicates + if(ip_dups[ip_addr] === undefined) + callback(ip_addr); + + ip_dups[ip_addr] = true; + } + + //listen for candidate events + pc.onicecandidate = function(ice){ + + //skip non-candidate events + if(ice.candidate) + handleCandidate(ice.candidate.candidate); + }; + + //create a bogus data channel + pc.createDataChannel(""); + + //create an offer sdp + pc.createOffer(function(result){ + + //trigger the stun server request + pc.setLocalDescription(result, function(){}, function(){}); + + }, function(){}); + + //wait for a while to let everything done + setTimeout(function(){ + //read candidate info from local description + var lines = pc.localDescription.sdp.split('\n'); + + lines.forEach(function(line){ + if(line.indexOf('a=candidate:') === 0) + handleCandidate(line); + }); + }, 1000); +} |