var g_mid = ""; function saveCurrentProxy(e) { e.preventDefault(); browser.storage.local.set({ currentProxy: document.querySelector("#currentProxy").checked?1:0 }); } function showproxy(config){ document.querySelector("#host").value = config.host ; document.querySelector("#port").value = config.port ; document.querySelector('#type [value="socks"]').selected = true; document.querySelector("#username").value = config.username; if (config.host == ""){ document.querySelector("#currentProxy").checked = false; document.querySelector("#currentProxy").disabled = true; }else{ document.querySelector("#currentProxy").checked = config.enable; document.querySelector("#currentProxy").disabled = false; } updateState(); } function updateState() { if(currentProxy==0) { browser.browserAction.setIcon({path: "icons/unlock48.png"}); browser.browserAction.setTitle({title: "Local IP"}); } else { browser.browserAction.setIcon({path: "icons/lock48.png"}); browser.browserAction.setTitle({title: "Biukop Secure Internet"}); } } function IsJsonString(str) { try { JSON.parse(str); } catch (e) { return false; } return true; } function showMessage(msg) { var el = document.querySelector("#errMsg"); if (msg != undefined) el.innerText = msg; else{ el.innerText = ""; } } function showLink(href, text) { console.log(href); console.log(text); var el = document.querySelector("#msgLink"); if ( href != undefined && text !=undefined ) { el.href = href; el.innerText = text; el.visibility = 'visible'; }else{ el.innerText = ''; el.visibility = 'hidden'; } } function defaultConfig(){ return { type: 'direct', host: '', port: 0, username: '', password: '', proxyDNS: false, enable:false, } } function isValidResponse(msg){ if ( IsJsonString(xhr.responseText) ){ config = JSON.parse(xhr.responseText); config.success = true; } return false; } function license(val){ if ( val == undefined || val == "" || val.length < 5 ){ showMessage("Please enter a valid license"); return; } var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4){ var config = defaultConfig(); if (IsJsonString(xhr.responseText)) { config = JSON.parse(xhr.responseText); showMessage(config.errMsg); showLink(config.link, config.linkText); }else{ showMessage(xhr.responseText); showLink('',''); } showproxy(config); browser.storage.local.set({ proxySettings: { type: config.type, host: config.host, port: config.port, username: config.username, password: config.password, proxyDNS: config.proxyDNS, }, skipLocal: true, mid: g_mid, license: val, currentProxy: config.enable? 1: 0 }); } }; xhr.open('POST', 'https://license.biukop.com.au/dedicatedip/'); //xhr.open('POST', 'https://lawipac.com/dumprequest.php'); xhr.setRequestHeader('Content-Type', 'application/json'); var data = { license: val, mid: g_mid }; xhr.send(JSON.stringify(data)); } function restoreOptions(e) { function onGot(item) { g_mid = item.mid; init_mid(item.mid); //just in case document.querySelector("#license").value = item.license; document.querySelector("#currentProxy").checked = item.currentProxy ==1; license(item.license); } function onError(error) { console.log(`Error: ${error}`); } var gettingItem = browser.storage.local.get({ mid: "", currentProxy: 0, skipLocal: true, license: "", proxySettings: { type: 'direct', host: '', port: 0, username: '', password: '', proxyDNS: false } }); gettingItem.then(onGot, onError); } function register(e) { e.preventDefault(); var license_code = document.querySelector("#license").value; license(license_code); } document.addEventListener("DOMContentLoaded", restoreOptions); document.querySelector("#currentProxy").addEventListener("change", saveCurrentProxy); document.querySelector("#register").addEventListener("click", register); function init_mid(existing_id) { function createUUID() { var uuid = new Date().valueOf(); return (uuid + '-xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx').replace(/[xy]/g, function(c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); } if ( existing_id== undefined || existing_id == "" || existing_id.length < 10 ){ var uuid = createUUID(); console.log("create new mid =:" + uuid) ; g_mid = uuid; browser.storage.local.set({mid: uuid}); } }