// ReferenceError: navigator is not defined
global.navigator={ userAgent: 'node.js', };
import {RSA} from "./crypto/rsa.js"


// ReferenceError: fetch is not defined
//  failed, reason: unable to verify the first certificate
import fetch from 'node-fetch'
function get_publickey_mod(usr)
{
  return fetch(`https://api.steampowered.com/IAuthenticationService/GetPasswordRSAPublicKey/v1?origin=https:%2F%2Fstore.steampowered.com&input_json=%7B%22account_name%22:%22${usr}%22%7D`, {
    "headers": {
      "accept": "application/json, text/plain, */*",
      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
      "sec-ch-ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Windows\"",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site"
    },
    "referrer": "https://store.steampowered.com/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": null,
    "method": "GET",
    "mode": "cors",
    "credentials": "omit"
  }).then(resp => {
    return resp.json()
  })
  .then(j => {
    console.log('get_publickey_mod', j)
    return j.response
  })
  // .catch(err => {
  
  // })
}


function myLogin(usr, timestamp, encPwd) {
  // let _ = {
  //   "account_name": usr,
  //   "encrypted_password": encPwd, // "dviiPhtg5dYHb9Pv4LSdg/5JTvoEdII+Wcwi8KS6DR3NZHBs4oqxej3NY64icVPDZz3nLqJJ1IWCDUsQgqG0KDa3lT6L9Q9BdgX+Rml63rzYa8lBtwS1dzDngfkDqPjVNY+CM8y7NE6SVJFloY5obiCBeIVqC1ldLTl31h01a6yMY5ylWc18mE6zlO3nrqA75f/YiiDgehp/06kEa6N/PTtF/QhvB01iLrGPGJya1JwnPpfvaX/nWTiy0z6XszuNojsw+oIHKml8VPvc1QPNXf1zeWGbfWURIFDTb63YLQ+w9GKJvEoQ2N3CHxTTaBWS+UrUGtJcA2gsBEuJURwHDw==",
  //   "encryption_timestamp": timestamp,
  //   "remember_login": true,
  //   "persistence": 1,
  //   "website_id": "Store",
  //   "device_details": {
  //       "device_friendly_name": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50",
  //       "platform_type": 2
  //   },
  //   "language": 6,
  //   "qos_level": 2
  // }

  
  fetch("https://api.steampowered.com/IAuthenticationService/BeginAuthSessionViaCredentials/v1", {
    "headers": {
      "accept": "application/json, text/plain, */*",
      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
      "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryqdEn9VkmYdIUEwRi",
      "sec-ch-ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Windows\"",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site"
    },
    "referrer": "https://store.steampowered.com/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": `------WebKitFormBoundaryqdEn9VkmYdIUEwRi\r\nContent-Disposition: form-data; name=\"input_json\"\r\n\r\n{\"account_name\":\"${usr}\",\"encrypted_password\":\"${encPwd}\",\"encryption_timestamp\":\"${timestamp}\",\"remember_login\":true,\"persistence\":1,\"website_id\":\"Store\",\"device_details\":{\"device_friendly_name\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50\",\"platform_type\":2},\"language\":6,\"qos_level\":2}\r\n------WebKitFormBoundaryqdEn9VkmYdIUEwRi--\r\n`,
    "method": "POST",
    "mode": "cors",
    "credentials": "omit"
  }).then(resp => {
    return resp.json()
  })
  .then(j => {
    console.log('myLogin', j)
    return j
  })
}

function main(usr, pwd, results) {
  pwd = pwd.replace( /[^\x00-\x7F]/g, '' ); // remove non-standard-ASCII characters
  usr = usr.replace( /[^\x00-\x7F]/g, '' ); // remove non-standard-ASCII characters

  get_publickey_mod(usr)
  .then(results => {
    // let data =new FormData();
    // data.append('file',$("#realFile").files[0]);
    // data.append('name','denzel'),
    // data.append('flag','test')
    var pubKey = RSA.getPublicKey(results.publickey_mod, results.publickey_exp);
    var encryptedPassword = RSA.encrypt(pwd, pubKey);
    myLogin(usr, results.timestamp, encryptedPassword)
  
    // var rgParameters = {
    //   password: encryptedPassword,
    //   username: username,
    //   // twofactorcode: authCode,
    //   // emailauth: form.elements['emailauth'] ? form.elements['emailauth'].value : '',
    //   // loginfriendlyname: form.elements['loginfriendlyname'] ? form.elements['loginfriendlyname'].value : '',
    //   // captchagid: this.m_gidCaptcha,
    //   captcha_text: '',
    //   // emailsteamid: this.m_steamidEmailAuth,
    //   rsatimestamp: results.timestamp,
    //   remember_login: 'true'
    // };
    // console.log(pubKey, rgParameters)
  })
}

// let results = {
//   publickey_mod: '123aee',
//   publickey_exp: '010001',
//   timestamp: '58350'
// }
// main('111', '222')
main('fgsc177824', '====')
console.log("leave")

/*
{
    "interval": 5,
    "extended_error_message": ""
}
*/
function t() {
  fetch("https://api.steampowered.com/IAuthenticationService/BeginAuthSessionViaCredentials/v1", {
    "headers": {
      "accept": "application/json, text/plain, */*",
      "accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
      "content-type": "multipart/form-data; boundary=----WebKitFormBoundaryqdEn9VkmYdIUEwRi",
      "sec-ch-ua": "\"Microsoft Edge\";v=\"113\", \"Chromium\";v=\"113\", \"Not-A.Brand\";v=\"24\"",
      "sec-ch-ua-mobile": "?0",
      "sec-ch-ua-platform": "\"Windows\"",
      "sec-fetch-dest": "empty",
      "sec-fetch-mode": "cors",
      "sec-fetch-site": "same-site"
    },
    "referrer": "https://store.steampowered.com/",
    "referrerPolicy": "strict-origin-when-cross-origin",
    "body": "------WebKitFormBoundaryqdEn9VkmYdIUEwRi\r\nContent-Disposition: form-data; name=\"input_json\"\r\n\r\n{\"account_name\":\"111\",\"encrypted_password\":\"dviiPhtg5dYHb9Pv4LSdg/5JTvoEdII+Wcwi8KS6DR3NZHBs4oqxej3NY64icVPDZz3nLqJJ1IWCDUsQgqG0KDa3lT6L9Q9BdgX+Rml63rzYa8lBtwS1dzDngfkDqPjVNY+CM8y7NE6SVJFloY5obiCBeIVqC1ldLTl31h01a6yMY5ylWc18mE6zlO3nrqA75f/YiiDgehp/06kEa6N/PTtF/QhvB01iLrGPGJya1JwnPpfvaX/nWTiy0z6XszuNojsw+oIHKml8VPvc1QPNXf1zeWGbfWURIFDTb63YLQ+w9GKJvEoQ2N3CHxTTaBWS+UrUGtJcA2gsBEuJURwHDw==\",\"encryption_timestamp\":\"64904150000\",\"remember_login\":true,\"persistence\":1,\"website_id\":\"Store\",\"device_details\":{\"device_friendly_name\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.50\",\"platform_type\":2},\"language\":6,\"qos_level\":2}\r\n------WebKitFormBoundaryqdEn9VkmYdIUEwRi--\r\n",
    "method": "POST",
    "mode": "cors",
    "credentials": "omit"
  });

}