Skip to content

Commit d03827e

Browse files
committed
clean adb code
1 parent cb55d61 commit d03827e

File tree

6 files changed

+81
-138
lines changed

6 files changed

+81
-138
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@
1414
go.sum
1515

1616
# User defined
17-
ya
17+
fa
1818
*.apk
19+
.DS_Store
20+
dist/
21+
*.png

README.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ Articles
109109
- Article reverse ssh tunnling <https://www.howtoforge.com/reverse-ssh-tunneling>
110110
- [openstf/adbkit](https://github.com/openstf/adbkit)
111111
- [ADB Source Code](https://github.com/aosp-mirror/platform_system_core/blob/master/adb)
112-
113-
- [OVERVIEW.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/OVERVIEW.TXT)
114-
- [SERVICES.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SERVICES.TXT)
115-
- [SYNC.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SYNC.TXT)
112+
- ADB Protocols [OVERVIEW.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/OVERVIEW.TXT) [SERVICES.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SERVICES.TXT) [SYNC.TXT](https://github.com/aosp-mirror/platform_system_core/blob/master/adb/SYNC.TXT)
116113
117114
Libs might be useful
118115

adb.go

Lines changed: 30 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import (
1111
"strconv"
1212
"strings"
1313
"syscall"
14+
15+
shellquote "github.com/kballard/go-shellquote"
1416
)
1517

1618
const (
@@ -65,11 +67,13 @@ type AdbConnection struct {
6567
net.Conn
6668
}
6769

68-
// SendPacket data is like "000chost:version"
69-
func (conn *AdbConnection) SendPacket(data string) error {
70+
func (conn *AdbConnection) WritePacket(data string) error {
7071
pktData := fmt.Sprintf("%04x%s", len(data), data)
7172
_, err := conn.Write([]byte(pktData))
72-
return err
73+
if err != nil {
74+
return err
75+
}
76+
return conn.respCheck()
7377
}
7478

7579
func (conn *AdbConnection) readN(n int) (v string, err error) {
@@ -84,6 +88,9 @@ func (conn *AdbConnection) readN(n int) (v string, err error) {
8488
// respCheck check OKAY, or FAIL
8589
func (conn *AdbConnection) respCheck() error {
8690
status, err := conn.readN(4)
91+
if err != nil {
92+
return err
93+
}
8794
switch status {
8895
case _OKAY:
8996
return nil
@@ -94,7 +101,7 @@ func (conn *AdbConnection) respCheck() error {
94101
}
95102
return errors.New(data)
96103
default:
97-
return fmt.Errorf("Unknown status: %s, should be OKAY or FAIL", strconv.Quote(stat))
104+
return fmt.Errorf("Unexpected response: %s, should be OKAY or FAIL", strconv.Quote(status))
98105
}
99106
}
100107

@@ -111,27 +118,6 @@ func (conn *AdbConnection) readString() (string, error) {
111118
return conn.readN(length)
112119
}
113120

114-
// RecvPacket receive data like "OKAY00040028"
115-
func (conn *AdbConnection) RecvPacket() (data string, err error) {
116-
stat, err := conn.readN(4)
117-
if err != nil {
118-
return "", err
119-
}
120-
switch stat {
121-
case _OKAY:
122-
return conn.readString()
123-
case _FAIL:
124-
data, err = conn.readString()
125-
if err != nil {
126-
return
127-
}
128-
err = errors.New(data)
129-
return
130-
default:
131-
return "", fmt.Errorf("Unknown stat: %s", strconv.Quote(stat))
132-
}
133-
}
134-
135121
type AdbClient struct {
136122
Addr string
137123
}
@@ -154,51 +140,7 @@ func (c *AdbClient) newConnection() (conn *AdbConnection, err error) {
154140
return &AdbConnection{netConn}, nil
155141
}
156142

157-
func (c *AdbClient) sendTwoWay(data string) (string, error) {
158-
if _, err := c.Version(); err != nil {
159-
return "", err
160-
}
161-
conn, err := c.newConnection()
162-
if err != nil {
163-
return "", err
164-
}
165-
defer conn.Close()
166-
if err := conn.SendPacket(data); err != nil {
167-
return "", err
168-
}
169-
return conn.RecvPacket()
170-
}
171-
172-
func (c *AdbClient) sendOneWay(data string) error {
173-
if _, err := c.Version(); err != nil {
174-
return err
175-
}
176-
conn, err := c.newConnection()
177-
if err != nil {
178-
return err
179-
}
180-
defer conn.Close()
181-
if err := conn.SendPacket(data); err != nil {
182-
return err
183-
}
184-
status, err := conn.readN(4)
185-
if err != nil {
186-
return err
187-
}
188-
switch status {
189-
case _OKAY:
190-
return nil
191-
case _FAIL:
192-
message, err := conn.readString()
193-
if err != nil {
194-
return err
195-
}
196-
return errors.New(message)
197-
default:
198-
return errors.New("Invalid status: " + status)
199-
}
200-
}
201-
143+
// Version return 4 size string
202144
func (c *AdbClient) Version() (string, error) {
203145
ver, err := c.rawVersion()
204146
if err == nil {
@@ -215,10 +157,10 @@ func (c *AdbClient) rawVersion() (string, error) {
215157
return "", err
216158
}
217159
defer conn.Close()
218-
if err := conn.SendPacket("host:version"); err != nil {
160+
if err := conn.WritePacket("host:version"); err != nil {
219161
return "", err
220162
}
221-
return conn.RecvPacket()
163+
return conn.readString()
222164
}
223165

224166
type AdbDevice struct {
@@ -233,60 +175,29 @@ func (c *AdbClient) DeviceWithSerial(serial string) *AdbDevice {
233175
}
234176
}
235177

236-
func (d *AdbDevice) deviceSelector() string {
237-
if d.Serial == "" {
238-
return "host:transport-any"
239-
}
240-
return "host-serial:" + d.Serial
241-
}
242-
243-
func (d *AdbDevice) transportSelector() string {
244-
if d.Serial == "" {
245-
return "host:transport-any"
246-
}
247-
return "host:transport:" + d.Serial
248-
}
249-
250-
func (d *AdbDevice) check() error {
251-
_, err := d.sendTwoWay(d.deviceSelector() + ":features")
252-
return err
253-
}
254-
255-
type TCPCmd struct {
256-
Cmd string
257-
NeedResponse bool
258-
}
259-
260-
func (c *AdbClient) Shell(args ...string) (output string, exitCode int, err error) {
178+
func (c *AdbDevice) openCommand(cmd string) (reader io.ReadCloser, err error) {
261179
conn, err := c.newConnection()
262180
if err != nil {
263181
return
264182
}
265-
defer conn.Close()
266-
// if err = conn.SendPacket("host:features"); err != nil {
267-
// return
268-
// }
269-
// _, err = conn.RecvPacket()
270-
// if err != nil {
271-
// return
272-
// }
273-
if err = conn.SendPacket("host:transport-any"); err != nil {
183+
err = conn.WritePacket("host:transport:" + c.Serial)
184+
if err != nil {
274185
return
275186
}
276-
ok, _ := conn.Okay()
277-
if !ok {
278-
err = fmt.Errorf("shell connection broken")
279-
}
280-
err = conn.SendPacket("shell:" + shellquote.Join(args...) + " ; echo :$?")
187+
err = conn.WritePacket("shell:" + cmd) //shellquote.Join(args...)) // + " ; echo :$?")
281188
if err != nil {
282189
return
283190
}
284-
ok, _ = conn.Okay()
285-
if !ok {
286-
err = fmt.Errorf("shell connection broken")
287-
}
288-
buf := bytes.NewBuffer(nil)
289-
io.Copy(buf, conn)
290-
output = string(buf.Bytes())
291-
return
191+
return conn, nil
192+
}
193+
194+
func (c *AdbDevice) OpenShell(args ...string) (reader io.ReadCloser, err error) {
195+
return c.openCommand(shellquote.Join(args...))
292196
}
197+
198+
// func (c *AdbDevice) RunShell(args ...string) (exitCode int, err error) {
199+
// reader, err := c.OpenShell(args...)
200+
// if err != nil {
201+
// return
202+
// }
203+
// }

adb_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestAdbVersion(t *testing.T) {
10+
version, err := DefaultAdbClient.Version()
11+
if err != nil {
12+
panic(err)
13+
}
14+
t.Logf("version: %s", version)
15+
}
16+
17+
func TestAdbShell(t *testing.T) {
18+
t.Log("Shell Test")
19+
d := DefaultAdbClient.DeviceWithSerial("0123456789ABCDEF")
20+
rd, err := d.OpenShell("pwd")
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
io.Copy(os.Stdout, rd)
25+
// output, exitCode, err := DefaultAdbClient.Shell("pwd")
26+
// if err != nil {
27+
// t.Log(err)
28+
// }
29+
// t.Log(output)
30+
// t.Log(exitCode)
31+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/codeskyblue/fa
22

33
require (
44
github.com/cavaliercoder/grab v2.0.0+incompatible
5+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
56
github.com/kr/pty v1.1.3 // indirect
67
github.com/manifoldco/promptui v0.3.2
78
github.com/mattn/go-runewidth v0.0.3 // indirect

main.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -279,20 +279,20 @@ func main() {
279279
},
280280
Action: actInstall,
281281
},
282-
{
283-
Name: "shell",
284-
Usage: "run shell command",
285-
SkipFlagParsing: true,
286-
Action: func(ctx *cli.Context) error {
287-
output, exitCode, err := DefaultAdbClient.Shell(ctx.Args()...)
288-
if err != nil {
289-
return err
290-
}
291-
fmt.Print(output)
292-
os.Exit(exitCode)
293-
return nil
294-
},
295-
},
282+
// {
283+
// Name: "shell",
284+
// Usage: "run shell command",
285+
// SkipFlagParsing: true,
286+
// Action: func(ctx *cli.Context) error {
287+
// output, exitCode, err := DefaultAdbClient.Shell(ctx.Args()...)
288+
// if err != nil {
289+
// return err
290+
// }
291+
// fmt.Print(output)
292+
// os.Exit(exitCode)
293+
// return nil
294+
// },
295+
// },
296296
{
297297
Name: "healthcheck",
298298
Usage: "check device health status",

0 commit comments

Comments
 (0)