From cf819ede2b4f12a4ed4b866d11b90ddc970da8c2 Mon Sep 17 00:00:00 2001 From: Mingyuan Xia Date: Tue, 22 Dec 2020 13:11:58 +0800 Subject: [PATCH] add kill-forward command. on-behalf-of: @appetizerio Signed-off-by: Mingyuan Xia --- README.md | 16 ++++++++++++ lib/adb/client.ts | 9 +++++++ lib/adb/command/host-serial/killforward.ts | 30 ++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 lib/adb/command/host-serial/killforward.ts diff --git a/README.md b/README.md index 961a9619..a011dd87 100644 --- a/README.md +++ b/README.md @@ -334,6 +334,22 @@ Forwards socket connections from the ADB server host (local) to the device (remo * Returns: `Promise` * Resolves with: `true` +#### client.removeForward(serial, local[, callback]) + +Remove the port forward at ADB server host (local). This is analogous to `adb forward --remove `. It's important to note that if you are connected to a remote ADB server, the forward on that host will be removed. + +* **serial** The serial number of the device. Corresponds to the device ID in `client.listDevices()`. +* **local** A string representing the local endpoint on the ADB host. At time of writing, can be one of: + - `tcp:` + - `localabstract:` + - `localreserved:` + - `localfilesystem:` + - `dev:` +* **callback(err)** Optional. Use this or the returned `Promise`. + - **err** `null` when successful, `Error` otherwise. +* Returns: `Promise` +* Resolves with: `true` + #### client.framebuffer(serial[, format][, callback]) Fetches the current **raw** framebuffer (i.e. what is visible on the screen) from the device, and optionally converts it into something more usable by using [GraphicsMagick][graphicsmagick]'s `gm` command, which must be available in `$PATH` if conversion is desired. Note that we don't bother supporting really old framebuffer formats such as RGB_565. If for some mysterious reason you happen to run into a `>=2.3` device that uses RGB_565, let us know. diff --git a/lib/adb/client.ts b/lib/adb/client.ts index 4635cdfb..d1b91a56 100644 --- a/lib/adb/client.ts +++ b/lib/adb/client.ts @@ -21,6 +21,7 @@ import GetPropertiesCommand from './command/host-transport/getproperties'; import InstallCommand from './command/host-transport/install'; import IsInstalledCommand from './command/host-transport/isinstalled'; import ListReversesCommand from './command/host-transport/listreverses'; +import KillForwardCommand from './command/host-serial/killforward'; import LocalCommand from './command/host-transport/local'; import LogcatCommand from './command/host-transport/logcat'; import LogCommand from './command/host-transport/log'; @@ -277,6 +278,14 @@ class Client extends EventEmitter { .nodeify(callback); } + public removeForward(serial: string, local: string, callback?: Callback): Bluebird { + return this.connection() + .then(function (conn) { + return new KillForwardCommand(conn).execute(serial, local); + }) + .nodeify(callback); + } + public listForwards(serial: string, callback?: Callback): Bluebird { return this.connection() .then(function (conn) { diff --git a/lib/adb/command/host-serial/killforward.ts b/lib/adb/command/host-serial/killforward.ts new file mode 100644 index 00000000..8095bd4a --- /dev/null +++ b/lib/adb/command/host-serial/killforward.ts @@ -0,0 +1,30 @@ +import Command from '../../command'; +import Protocol from '../../protocol'; +import Bluebird from 'bluebird'; + +class KillForwardCommand extends Command { + execute(serial: string, local: string): Bluebird { + this._send(`host-serial:${serial}:killforward:${local}`); + return this.parser.readAscii(4).then((reply) => { + switch (reply) { + case Protocol.OKAY: + return this.parser.readAscii(4).then((reply) => { + switch (reply) { + case Protocol.OKAY: + return true; + case Protocol.FAIL: + return this.parser.readError(); + default: + return this.parser.unexpected(reply, 'OKAY or FAIL'); + } + }); + case Protocol.FAIL: + return this.parser.readError(); + default: + return this.parser.unexpected(reply, 'OKAY or FAIL'); + } + }); + } +} + +export = KillForwardCommand;