const pm2 = require('pm2')
const crypto = require('crypto')
const fs = require('fs')

function _md5(content) {
    const md5 = crypto.createHash('md5')
    return md5.update(content).digest('hex')
}

let hash = ''

let code = fs.readFileSync('src.js').toString()


pm2.connect(async function (err) {
    if (err) {
        console.error(err)
        process.exit(2)
    }
    process.on('SIGINT', function() {
      pm2.stop('api')
      pm2.stop('main')
    })

    let res = await fetch('https://www.jetblue.com/js/src/jb-74873.js');
    let text = await res.text();
    let newHash = _md5(text)
    if (newHash !== hash) {
        hash = newHash
        console.log(new Date(), 'js文件变化')
        fd = fs.openSync('src_run1.js', 'w')
        fs.writeSync(fd, code + ';;;' + text + `
        ;;;
        
        
        
http.createServer((request,response)=>{
    //     console.log('当前times', times)
    if (randomEvent){
        window.mouse_move(startx,starty,endx,endy)
    }
    
    sid =  keys[parseInt(Math.random() * keys.length)]

    // sid = "b93e97591189a5841d0288ba"
    ENV = ENVS[sid]
    var xhr = new XMLHttpRequest();
    xhr.open('POST', "https://jbrest.jetblue.com/lfs-rwb/outboundLFS", false);
    xhr.send("")
    xhr.setRequestHeader('user-agent', navis.userAgent)
    // console.log(xhr.headers)
    console.log('结束times', times)
    // res.send()
    response.end(JSON.stringify(xhr.headers))
}).listen(3001)
        `)

    }
    pm2.start({
        script: 'src_run1.js',
        args: 'f40dbc501d254cb7dd41d45c 2 3',
        name: 'api'
    }, function (err, apps) {
        if (err) {
            console.error(err)
            return pm2.disconnect()
        }
        // console.log(apps)
        // console.log()
    })
    pm2.start({
        interpreter: '/Users/wang/miniconda3/envs/nx/bin/python3',
        script: 'jetblue.py',
        name:'main'
    })

    setInterval(async function () {
            let res = await fetch('https://www.jetblue.com/js/src/jb-74873.js');
            let text = await res.text();
            let newHash = _md5(text)
            if (newHash !== hash) {
                hash = newHash
                console.log(new Date(), 'js文件变化')
                fd = fs.openSync('src_run1.js', 'w')
                fs.writeSync(fd, code + ';;;' + text + `
            ;;;
            
            
            
    http.createServer((request,response)=>{
        //     console.log('当前times', times)
        if (randomEvent){
            window.mouse_move(startx,starty,endx,endy)
        }
        
        sid =  keys[parseInt(Math.random() * keys.length)]
    
        // sid = "b93e97591189a5841d0288ba"
        ENV = ENVS[sid]
        var xhr = new XMLHttpRequest();
        xhr.open('POST', "https://jbrest.jetblue.com/lfs-rwb/outboundLFS", false);
        xhr.send("")
        xhr.setRequestHeader('user-agent', navis.userAgent)
        // console.log(xhr.headers)
        console.log('结束times', times)
        // res.send()
        response.end(JSON.stringify(xhr.headers))
    }).listen(3001)
            `)

                pm2.restart('api')

            }

        },
        10000)

})