Skip to content

Instantly share code, notes, and snippets.

@yuanliwei
Created April 22, 2019 03:38
Show Gist options
  • Save yuanliwei/fad792686577021c105a6e2e1d79ecb8 to your computer and use it in GitHub Desktop.
Save yuanliwei/fad792686577021c105a6e2e1d79ecb8 to your computer and use it in GitHub Desktop.
adb shell monkey --port 1080
/*
adb shell monkey --port 1080
adb forward tcp:1080 tcp:1080
telnet 127.0.0.1 1080

sleep 300
quit
done
type string
press keycode
tap x y
wake
flip [open|close]
trackball dx dy
touch [down|up] keycode

*/

let cmds = `// menu
touch down 209 2310
sleep 32
touch move 377 1781
sleep 10
touch up 377 1781
sleep 369
// press wifi
touch down 552 1903
sleep 750
touch up 552 1903
sleep 343
// setting
tap 1022 514
sleep 345
// 手动代理
tap 997 1531
sleep 500
// 服务器主机名
tap 687 1668
sleep 400
type 192.168.3.217
sleep 300
// 确定
tap 833 1250
sleep 333
// 端口
tap 881 1810
sleep 595
type 8888
sleep 200
// 确定
tap 856 1255
sleep 356
// 返回
touch down 968 2306
sleep 9
touch move 977 2056
sleep 11
touch up 977 2056
sleep 350
// 返回
touch down 987 2315
sleep 9
touch move 977 2056
sleep 11
touch up 977 2056
`

function runCommands(){
    cmds.split('\n').map((item) => {
      return item.trim()
    }).filter((item) => {
      return item.length > 0 && !item.trim().startsWith('//')
    }).forEach((item) => {
      adbexec(item)
    })
}

const net = require('net');
const exec = require('child_process').exec;

let HOST = '127.0.0.1'
let PORT = 1080
let TIMER = 0
let TRY_COUNT = 300
let client = null

exec(`adb shell monkey --port ${PORT}\n`)
exec(`adb forward tcp:${PORT} tcp:${PORT}\n`, () => {
    connect(() => {
        runCommands()
        adbexec('quit')
        setTimeout(() => {
            process.exit(0)
        }, 1000);
    })
})

function adbexec(command) {
    client.write(`${command}\n`)
}

function connect(callback) {
    client = new net.Socket()
    client.connect(PORT, HOST, () => {
        console.log('connected to : ' + HOST + ' ' + PORT);
        callback()
    })
    client.on('data', (data) => {
        // console.log('DATA:'+data);
    })
    client.on('close', () => {
        console.log('connection closed!');
        console.log('TRY_COUNT : ' + TRY_COUNT);
        adbexec('try')
    })
    client.on('error', (data) => {
        console.log('error:' + data);
        console.log('TRY_COUNT : ' + TRY_COUNT);
        if (TRY_COUNT-- < 0) { return }
        clearTimeout(TIMER)
        TIMER = setTimeout(() => {
            connect(callback)
        }, 300)
    })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment