summaryrefslogtreecommitdiff
path: root/maj/main.js
blob: 2f31585e85c480fc21ec294fbc41f9db566ec82f (plain)
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
/**
 *  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="Téléchager 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="Télécharger 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);
}