ladnene / Check login page

// ==UserScript==
// @name Check login page
// @license MIT
// @namespace Script Runner Pro
// @match *://192.168.8.14/
// @grant none
// ==/UserScript==
var markup = document.documentElement.innerHTML;
var page1=`<head>
    <meta charset="UTF-8">
    <title>Se connecter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="sha.js"></script>
    <script src="my_functions.js"></script>
    <script type="text/javascript">
      var hashSalt2="notValidHash"; 
      var userIndex2="notValidIndex"; 
      function showPasswordFunction() {var x = document.getElementById("password");if (x.type === "password") {x.type = "text";} else {x.type = "password";}}
      function getMyIP(){var e,t=get_xhr();return t.onreadystatechange=function(){4==t.readyState&&(e=200==t.status?t.responseText:"notValidIP")},t.open("GET","/get_my_ip",!1),t.send(null),e}
      function getUserIndexByHash(hash){var xhr=get_xhr();var result;xhr.onreadystatechange  = function() { if(xhr.readyState  == 4){if(xhr.status  == 200) result=xhr.responseText;else result="notValidIndex";}};xhr.open("GET", "/get_user_index_by_hash?hash="+hash,false);xhr.send(null);return result; }
  
      if(window.sessionStorage.getItem('login')!=null && window.sessionStorage.getItem('password')!=null && window.sessionStorage.getItem('myIP')!=null)
      {
        hashSalt2=getHashSalt();
        if(hashSalt2!="notValidHash" && hashSalt2!=undefined)
        {
          var str = window.sessionStorage.getItem('login')+";"+window.sessionStorage.getItem('password')+";"+ window.sessionStorage.getItem('myIP') +";"+ hashSalt2;
          console.log("load2="+str);
          var sha1_2 = new jsSHA('SHA-1', 'TEXT');
          sha1_2.update(str);
          var hash2 = sha1_2.getHash("HEX");
    
          userIndex2=getUserIndexByHash(hash2);
          if(userIndex2!="checkcredentials" && userIndex2!="notValidIndex" && userIndex2!=undefined && userIndex2!="userblocked")
          { 
            window.sessionStorage.setItem('userIndex', userIndex2);
            let urlParams = new URLSearchParams(window.location.search);
            redirect=true;
            if(urlParams.has('redirecturl'))
              window.location.href = "http://"+window.location.host+urlParams.get('redirecturl')+"?userIndex="+userIndex2+"&hash="+hash2
            else
              window.location.href = "http://"+window.location.host+"/controle?userIndex="+userIndex2+"&hash="+hash2
          }
          else
          {
            window.sessionStorage.removeItem('login');
            window.sessionStorage.removeItem('password');
            window.sessionStorage.removeItem('myIP');     
          }  
        }
      }
    </script>
    <style>
    body{font-family:Arial,Helvetica,sans-serif}form{border:3px solid #f1f1f1}input[type=text],input[type=password]{width:100%;padding:12px 20px;margin:8px 0;display:inline-block;border:1px solid #ccc;box-sizing:border-box}button{background-color:#4CAF50;color:#fff;padding:14px 20px;margin:8px 0;border:none;cursor:pointer;width:100%}button:hover{opacity:.8}.cancelbtn{width:auto;padding:10px 18px;background-color:#f44336}.imgcontainer{text-align:center;margin:24px 0 12px}img.avatar{width:40%;border-radius:50%}.container{padding:16px}span.psw{float:right;padding-top:16px}@media screen and (max-width: 300px){span.psw{display:block;float:none}.cancelbtn{width:100%}}
    </style>
    <link rel="icon" href="data:,">
  </head>
<body>
  <h2>Se connecter</h2>
  <form onsubmit="return submitFunction()" method="post">
    <div class="container">
      <label for="username"><b>Nom d'utilisateur</b></label>
      <input type="text" name="username" id="username" placeholder="Entrez votre nom d'utilisateur" required="">
      <label for="password"><b>Mot de passe</b></label>
      <input type="password" name="password" id="password" placeholder="Entrez votre mot de passe" required="">
      <input type="checkbox" onclick="showPasswordFunction()">Afficher le mot de passe
      <button type="submit">Se connecter</button>
    </div>
    <div id="error_msg" style="text-align:center"></div>
    <div class="container" style="background-color:#f1f1f1">
      <span class="psw"><a href="#">Mot de passe oublié ?</a></span>
    </div>
  </form>
  <input id="load" type="text" name="load" style="display: none;">
  <script type="text/javascript">
    var myIP="notValidIP"; 
    var hashSalt="notValidHash"; 
    var userIndex="notValidIndex"; 
    var hash_input=document.getElementById("hash");
    function submitFunction()
    {

      myIP=getMyIP();
      hashSalt=getHashSalt();
      if(myIP=="notValidIP"||myIP==undefined)
      { 
        confirm('Adresse IP non recu, verifier votre connexion et reconnecter vous');
        return false; // don't submit the form
      }
      if(hashSalt=="notValidHash"||hashSalt==undefined)
      { 
        confirm('Hash salt non recu, verifier votre connexion et reconnecter vous');
        return false; // don't submit the form
      }
      window.sessionStorage.setItem('login', document.getElementById("username").value);
      window.sessionStorage.setItem('password', document.getElementById("password").value);
      window.sessionStorage.setItem('myIP', myIP);

      var str = window.sessionStorage.getItem('login')+";"+window.sessionStorage.getItem('password')+";"+ window.sessionStorage.getItem('myIP') +";"+ hashSalt;
      console.log("load="+str);
      var sha1 = new jsSHA('SHA-1', 'TEXT');
      sha1.update(str);
      var hash = sha1.getHash("HEX");

      userIndex=getUserIndexByHash(hash);
      if(userIndex=="notValidIndex"||userIndex==undefined)
      { 
        confirm('Verifier votre connexion et reconnecter vous.');
        return false; // don't submit the form
      }
      else if(userIndex=="userblocked")
      {
        document.getElementById("error_msg").innerHTML="Ce compte est bloqué." 
        return false;
      }
      else if(userIndex=="checkcredentials")
      {
        document.getElementById("error_msg").innerHTML="Veuillez vérifier votre nom d'utilisateur et votre mot de passe." 
        return false;      
      }       
      window.sessionStorage.setItem('userIndex', userIndex);
      
      document.getElementById("username").value="";
      document.getElementById("password").value="";
      redirect=true;
      let urlParams = new URLSearchParams(window.location.search);
      if(urlParams.has('redirecturl')) 
        window.location.href = "http://"+window.location.host+urlParams.get('redirecturl')+"?userIndex="+userIndex+"&hash="+hash;
      else
        window.location.href = "http://"+window.location.host+"/controle?userIndex="+userIndex+"&hash="+hash;
        
      return false; // do not submit the form
    }
  </script>


</body>`;
var page2=`<head>
    <meta charset="UTF-8">
    <title>Se connecter</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="sha.js"></script>
    <script src="my_functions.js"></script>
    <script type="text/javascript">
      var hashSalt2="notValidHash"; 
      var userIndex2="notValidIndex"; 
      function showPasswordFunction() {var x = document.getElementById("password");if (x.type === "password") {x.type = "text";} else {x.type = "password";}}
      function getMyIP(){var e,t=get_xhr();return t.onreadystatechange=function(){4==t.readyState&&(e=200==t.status?t.responseText:"notValidIP")},t.open("GET","/get_my_ip",!1),t.send(null),e}
      function getUserIndexByHash(hash){var xhr=get_xhr();var result;xhr.onreadystatechange  = function() { if(xhr.readyState  == 4){if(xhr.status  == 200) result=xhr.responseText;else result="notValidIndex";}};xhr.open("GET", "/get_user_index_by_hash?hash="+hash,false);xhr.send(null);return result; }
  
      if(window.sessionStorage.getItem('login')!=null && window.sessionStorage.getItem('password')!=null && window.sessionStorage.getItem('myIP')!=null)
      {
        hashSalt2=getHashSalt();
        if(hashSalt2!="notValidHash" && hashSalt2!=undefined)
        {
          var str = window.sessionStorage.getItem('login')+";"+window.sessionStorage.getItem('password')+";"+ window.sessionStorage.getItem('myIP') +";"+ hashSalt2;
          console.log("load2="+str);
          var sha1_2 = new jsSHA('SHA-1', 'TEXT');
          sha1_2.update(str);
          var hash2 = sha1_2.getHash("HEX");
    
          userIndex2=getUserIndexByHash(hash2);
          if(userIndex2!="checkcredentials" && userIndex2!="notValidIndex" && userIndex2!=undefined && userIndex2!="userblocked")
          { 
            window.sessionStorage.setItem('userIndex', userIndex2);
            let urlParams = new URLSearchParams(window.location.search);
            redirect=true;
            if(urlParams.has('redirecturl'))
              window.location.href = "http://"+window.location.host+urlParams.get('redirecturl')+"?userIndex="+userIndex2+"&hash="+hash2
            else
              window.location.href = "http://"+window.location.host+"/controle?userIndex="+userIndex2+"&hash="+hash2
          }
          else
          {
            window.sessionStorage.removeItem('login');
            window.sessionStorage.removeItem('password');
            window.sessionStorage.removeItem('myIP');     
          }  
        }
      }
    </script>
    <style>
    body{font-family:Arial,Helvetica,sans-serif}form{border:3px solid #f1f1f1}input[type=text],input[type=password]{width:100%;padding:12px 20px;margin:8px 0;display:inline-block;border:1px solid #ccc;box-sizing:border-box}button{background-color:#4CAF50;color:#fff;padding:14px 20px;margin:8px 0;border:none;cursor:pointer;width:100%}button:hover{opacity:.8}.cancelbtn{width:auto;padding:10px 18px;background-color:#f44336}.imgcontainer{text-align:center;margin:24px 0 12px}img.avatar{width:40%;border-radius:50%}.container{padding:16px}span.psw{float:right;padding-top:16px}@media screen and (max-width: 300px){span.psw{display:block;float:none}.cancelbtn{width:100%}}
    </style>
    <link rel="icon" href="data:,">
  </head>
<body>
  <h2>Se connecter</h2>
  <form onsubmit="return submitFunction()" method="post">
    <div class="container">
      <label for="username"><b>Nom d'utilisateur</b></label>
      <input type="text" name="username" id="username" placeholder="Entrez votre nom d'utilisateur" required="">
      <label for="password"><b>Mot de passe</b></label>
      <input type="password" name="password" id="password" placeholder="Entrez votre mot de passe" required="">
      <input type="checkbox" onclick="showPasswordFunction()">Afficher le mot de passe
      <button type="submit">Se connecter</button>
    </div>
    <div id="error_msg" style="text-align:center"></div>
    <div class="container" style="background-color:#f1f1f1">
      <span class="psw"><a href="#">Mot de passe oublié ?</a></span>
    </div>
  </form>
  <input id="load" type="text" name="load" style="display: none;">
  <script type="text/javascript">
    var myIP="notValidIP"; 
    var hashSalt="notValidHash"; 
    var userIndex="notValidIndex"; 
    var hash_input=document.getElementById("hash");
    function submitFunction()
    {

      myIP=getMyIP();
      hashSalt=getHashSalt();
      if(myIP=="notValidIP"||myIP==undefined)
      { 
        confirm('Adresse IP non recu, verifier votre connexion et reconnecter vous');
        return false; // don't submit the form
      }
      if(hashSalt=="notValidHash"||hashSalt==undefined)
      { 
        confirm('Hash salt non recu, verifier votre connexion et reconnecter vous');
        return false; // don't submit the form
      }
      window.sessionStorage.setItem('login', document.getElementById("username").value);
      window.sessionStorage.setItem('password', document.getElementById("password").value);
      window.sessionStorage.setItem('myIP', myIP);

      var str = window.sessionStorage.getItem('login')+";"+window.sessionStorage.getItem('password')+";"+ window.sessionStorage.getItem('myIP') +";"+ hashSalt;
      console.log("load="+str);
      var sha1 = new jsSHA('SHA-1', 'TEXT');
      sha1.update(str);
      var hash = sha1.getHash("HEX");

      userIndex=getUserIndexByHash(hash);
      if(userIndex=="notValidIndex"||userIndex==undefined)
      { 
        confirm('Verifier votre connexion et reconnecter vous.');
        return false; // don't submit the form
      }
      else if(userIndex=="userblocked")
      {
        document.getElementById("error_msg").innerHTML="Ce compte est bloqué." 
        return false;
      }
      else if(userIndex=="checkcredentials")
      {
        document.getElementById("error_msg").innerHTML="Veuillez vérifier votre nom d'utilisateur et votre mot de passe." 
        return false;      
      }       
      window.sessionStorage.setItem('userIndex', userIndex);
      
      document.getElementById("username").value="";
      document.getElementById("password").value="";
      redirect=true;
      let urlParams = new URLSearchParams(window.location.search);
      if(urlParams.has('redirecturl')) 
        window.location.href = "http://"+window.location.host+urlParams.get('redirecturl')+"?userIndex="+userIndex+"&hash="+hash;
      else
        window.location.href = "http://"+window.location.host+"/controle?userIndex="+userIndex+"&hash="+hash;
        
      return false; // do not submit the form
    }
  </script>


<style>.qc-cmp-showing { visibility: hidden !important; } body.didomi-popup-open { overflow: auto !important; } #didomi-host { visibility: hidden !important; }</style></body>`;
var newWindow = window.open();
window.alert = newWindow.alert;
newWindow.close();
if(markup==page1||markup==page2)
alert("Page sécurisée, vous pouvez vous connecter");
else
alert("Page non sécurisée! n'entrez pas vos données !!!");