2022-03-01 18:17:42 +00:00
|
|
|
// Android Debug Bridge API
|
2024-07-10 02:37:58 +00:00
|
|
|
// Namhyeon Go (Catswords Research) <abuse@catswords.net>
|
|
|
|
// https://github.com/gnh1201/welsonjs
|
2022-03-01 18:15:24 +00:00
|
|
|
var SHELL = require("lib/shell");
|
|
|
|
var SYS = require("lib/system");
|
|
|
|
|
2024-04-07 06:30:30 +00:00
|
|
|
// A common Android devices
|
2022-03-01 18:15:24 +00:00
|
|
|
function ADBObject() {
|
2024-07-10 10:26:58 +00:00
|
|
|
this._interface = SHELL.create();
|
2022-03-01 18:15:24 +00:00
|
|
|
|
|
|
|
this.setBinPath = function(binPath) {
|
|
|
|
this.binPath = binPath;
|
2024-07-10 10:26:58 +00:00
|
|
|
this._interface.setPrefix(this.binPath);
|
2022-03-01 18:58:51 +00:00
|
|
|
return this;
|
2022-03-01 18:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getDevices = function() {
|
|
|
|
var devices = [];
|
2024-07-10 10:26:58 +00:00
|
|
|
var result = this._interface.exec(["devices"]);
|
2022-03-01 18:15:24 +00:00
|
|
|
|
|
|
|
splitLn(result).forEach(function(line) {
|
|
|
|
var row = line.split(/\s+/);
|
|
|
|
if(row.length == 2) {
|
|
|
|
devices.push({
|
|
|
|
id: row[0],
|
|
|
|
hostname: this.getHostname(row[0]),
|
|
|
|
type: row[1]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2022-03-01 20:58:39 +00:00
|
|
|
|
|
|
|
return devices;
|
2022-03-01 18:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getHostname = function(id) {
|
2022-03-02 14:51:07 +00:00
|
|
|
var result = this.getProperty(id, "net.hostname");
|
|
|
|
if (typeof result === "string") {
|
|
|
|
return result.trim();
|
|
|
|
} else {
|
|
|
|
return "";
|
|
|
|
}
|
2022-03-01 18:17:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.getProperty = function(id, name) {
|
2022-03-02 09:05:34 +00:00
|
|
|
return this.sendShell(id, ["getprop", name]);
|
2022-03-01 18:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.disableService = function(id, name) {
|
2022-03-02 09:05:34 +00:00
|
|
|
return this.sendShell(id, ["svc", name, "disable"]);
|
2022-03-01 18:15:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.enableService = function(id, name) {
|
2022-03-02 09:05:34 +00:00
|
|
|
return this.sendShell(id, ["svc", name, "enable"]);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.sendShell = function(id, args) {
|
|
|
|
try {
|
|
|
|
return SHELL.exec([this.binPath, "-s", id, "shell"].concat(args));
|
|
|
|
} catch (e) {
|
|
|
|
return "";
|
2022-03-02 09:06:11 +00:00
|
|
|
}
|
2022-03-01 18:15:24 +00:00
|
|
|
};
|
2022-03-02 15:17:00 +00:00
|
|
|
|
|
|
|
// download a file from target device
|
|
|
|
this.pull = function(id, path) {
|
2024-07-10 10:26:58 +00:00
|
|
|
return this._interface.exec(["-s", id, "pull", path, "data\\"]);
|
2022-03-02 15:17:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// upload a file to target device
|
2022-03-02 15:17:32 +00:00
|
|
|
this.push = function(id, filename, path) {
|
2024-07-10 10:26:58 +00:00
|
|
|
return this._interface.exec(["-s", id, "push", "data\\" + filename, path]);
|
2022-03-02 15:17:00 +00:00
|
|
|
};
|
2022-03-02 15:23:26 +00:00
|
|
|
|
|
|
|
// install APK file
|
2022-03-02 15:25:11 +00:00
|
|
|
this.install = function(id, filename) {
|
2024-07-10 10:26:58 +00:00
|
|
|
return this._interface.exec(["-s", id, "install", "data\\" + filename]);
|
2022-03-02 15:23:26 +00:00
|
|
|
};
|
2022-03-02 15:25:11 +00:00
|
|
|
|
|
|
|
// Uninstall the App
|
|
|
|
this.uninstall = function(id, appname) {
|
2024-07-10 10:26:58 +00:00
|
|
|
return this._interface.exec(["-s", id, "uninstall", appname]);
|
2022-03-02 15:25:11 +00:00
|
|
|
};
|
2022-03-02 15:23:26 +00:00
|
|
|
|
|
|
|
// reboot device
|
|
|
|
this.reboot = function(id) {
|
2024-07-10 10:26:58 +00:00
|
|
|
return this._interface.exec(["-s", id, "reboot"]);
|
2022-03-02 15:23:26 +00:00
|
|
|
};
|
2024-07-10 02:37:58 +00:00
|
|
|
|
|
|
|
// set the binary path
|
|
|
|
this.binPath = "bin\\platform-tools_r33.0.0-windows\\platform-tools\\adb.exe";
|
2024-07-10 10:26:58 +00:00
|
|
|
this._interface.setPrefix(this.binPath);
|
2022-03-01 18:15:24 +00:00
|
|
|
}
|
|
|
|
|
2024-04-07 06:30:30 +00:00
|
|
|
// An Android Emulator
|
|
|
|
function EmulatorObject(binPath) {
|
|
|
|
this.ADBI = (new ADBObject()).setBinPath(binPath);
|
|
|
|
|
|
|
|
this.getProcessID = function(id) {
|
|
|
|
var row = id.split(':');
|
|
|
|
//var addr = row[0];
|
|
|
|
var port = row[1];
|
|
|
|
var cmd = "netstat -ano | findstr :" + port + " | findstr :0";
|
|
|
|
var result = SHELL.exec(cmd);
|
|
|
|
return result.substring(result.lastIndexOf(' '));
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getList = function() {
|
|
|
|
var items = [];
|
|
|
|
var devices = this.ADBI.getDevices();
|
|
|
|
|
|
|
|
for(var i = 0; i < devices.length; i++) {
|
|
|
|
var hostname = devices[i].hostname;
|
|
|
|
var PID = parseInt(this.getProcessID(devices[i].id));
|
|
|
|
items.push({
|
|
|
|
hostname: hostname,
|
|
|
|
PID: PID
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-01 18:15:24 +00:00
|
|
|
exports.create = function() {
|
|
|
|
return new ADBObject();
|
|
|
|
};
|
2022-03-01 18:55:24 +00:00
|
|
|
|
2024-04-07 06:30:30 +00:00
|
|
|
exports.createEmulator = function(binPath) {
|
|
|
|
return new EmulatorObject(binPath);
|
|
|
|
};
|
|
|
|
|
2024-07-10 02:37:58 +00:00
|
|
|
exports.VERSIONINFO = "Android Debug Bridge Interface (adb.js) version 0.2.2";
|
2024-04-07 06:48:40 +00:00
|
|
|
exports.AUTHOR = "abuse@catswords.net";
|
2022-03-01 18:55:24 +00:00
|
|
|
exports.global = global;
|
|
|
|
exports.require = global.require;
|