检查 UUID 是否包含在 pastebin 中

问题描述

我想检查一下我的 UUID 是否包含在我的 pastebin 中。

知道如何在 JavaScript 中检查吗?

实际获取 UUID 的代码是这样的:

// GET UUID

const execSync = require("child_process").execSync;
const { type } = require("os");
const { SSL_OP_EPHEMERAL_RSA } = require("constants");
let response = execSync("wmic csproduct get uuid");
let serial = String(response).split("\n")[1];
console.log(serial);

async function fetchText() {
  let response = await fetch("https://pastebin.com/raw/4hxgLxyd");
  let data = await response.text();

  console.log(data.indexOf(serial));

  if (data.indexOf(serial) !== -1) {
    console.log("included");
  } else {
    console.log("not included");
  }
}
fetchText();

我是 JS 新手 - 在 Python 中我知道如何使用请求命令检查它。

有人知道如何在 JS 中处理这个吗?


按照我的要求,我的 Python 代码

def init(): # check HWID
    try:
        HWID = subprocess.check_output('wmic csproduct get uuid').decode().split('\n')[1].strip()
    except:
        cmd = "system_profiler SPHardwareDataType | grep 'Serial Number' | awk '{print $4}'"
        result = subprocess.run(cmd,stdout=subprocess.PIPE,shell=True,check=True)
        HWID = result.stdout.decode().strip()
        
    print('Checking license...') 

# -------------------------------------------
# Below this - I need the code for JavaScript
# -------------------------------------------

    r = requests.get('https://pastebin.com/xxx')

    try:
        if HWID in r.text:
            pass
        else:
            print('[ERROR] HWID not registered!')
            print(f'HWID: {HWID}')
            time.sleep(5)
            sys.exit(0)
    except:
        print('[ERROR] Failed to initiate')
        time.sleep(5)
        sys.exit(0)

    print(f'HWID: {HWID}')
    print('--- License is valid ---')

解决方法

在 javascript 中,您可以使用 indexOf 来搜索字符串的出现。如果不存在,函数将返回-1,否则索引第一次出现。

此外,请确保您考虑区分大小写以及破折号和空格的位置。

var str = "Hello world,welcome to the universe.";
var n = str.indexOf("welcome");

请参阅 Node.Js documentation 以执行 http 请求。


// GET UUID

const execSync = require("child_process").execSync;
const response = execSync("wmic csproduct get uuid");
const serial = String(response).split("\n")[1].replace("-","").trim().toLowerCase();

const https = require('https')
const options = {
  hostname: 'pastebin.com',port: 443,path: '/xxx',method: 'GET'
}

const req = https.request(options,res => {
  console.log(`statusCode: ${res.statusCode}`)

  res.on('data',d => {
    var dnorm= d.replace("-","").trim().toLowerCase();
    process.stdout.write(dnorm.indexOf(serial))
  })
})

req.on('error',error => {
  console.error(error)
})

req.end()

由于出现空格或编码问题,请手动比较字符串的内容:

function debugComp(a,b)
    {
      a= a.toString().trim();
      b= a.toString().trim();
      console.log("a: '" + a + "' - Length: " + a.length);
      console.log("b: '" + b + "' - Length: " + b.length);
      if(a.indexOf(b)>-1)
      {
        console.log("Found at index " + a.indexOf(b)); 
      }
      else if(a.length==b.length)
      {
         for(var i=0; i< a.length; i++)
         {
             console.log("a[" + i + "]= '" + a[i] + "' - b[" + i + "] = '" + b[i]);
             console.log("a[" + i + "] == b[" + i+ "] = " + (a[i]==b[i]));          
         }
      }
      else {
        console.log("Strings have different lengths");
      }
    }

    debugComp("D340D9AE-A43F-DF47-AFED-A93222AB3646","D340D9AE-A43F-DF47-AFED-A93222AB3646");