|
| 1 | +package adb |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | +) |
| 11 | + |
| 12 | +const ( |
| 13 | + SYNC = "SYNC" |
| 14 | + CXNN = "CXNN" |
| 15 | + OPEN = "OPEN" |
| 16 | + OKAY = "OKAY" |
| 17 | + CLSE = "CLSE" |
| 18 | + WRTE = "WRTE" |
| 19 | + AUTH = "AUTH" |
| 20 | +) |
| 21 | + |
| 22 | +func checksum(data []byte) byte { |
| 23 | + sum := byte(0) |
| 24 | + for _, c := range data { |
| 25 | + sum += c |
| 26 | + } |
| 27 | + return sum |
| 28 | +} |
| 29 | + |
| 30 | +func xorBytes(a, b []byte) []byte { |
| 31 | + if len(a) != len(b) { |
| 32 | + panic(fmt.Sprintf("xorBytes a:%x b:%x have different size", a, b)) |
| 33 | + } |
| 34 | + dst := make([]byte, len(a)) |
| 35 | + for i := 0; i < len(a); i++ { |
| 36 | + dst[i] = a[i] ^ b[i] |
| 37 | + } |
| 38 | + return dst |
| 39 | +} |
| 40 | + |
| 41 | +type AdbServer struct { |
| 42 | + version int |
| 43 | + mayPayload int |
| 44 | + authorized bool |
| 45 | + syncToken int |
| 46 | + remoteID int |
| 47 | + services map[string]string |
| 48 | + remoteAddress string |
| 49 | + token string |
| 50 | + signature string |
| 51 | +} |
| 52 | + |
| 53 | +type PacketReader struct { |
| 54 | + reader io.Reader |
| 55 | + err error |
| 56 | +} |
| 57 | + |
| 58 | +type errReader struct{} |
| 59 | + |
| 60 | +func (e errReader) Read(p []byte) (int, error) { |
| 61 | + return 0, errors.New("package already read error") |
| 62 | +} |
| 63 | + |
| 64 | +func (p *PacketReader) r() io.Reader { |
| 65 | + if p.err != nil { // use p.err to short error checks |
| 66 | + return errReader{} |
| 67 | + } |
| 68 | + return p.reader |
| 69 | +} |
| 70 | + |
| 71 | +// Receive packet example |
| 72 | +// 00000000 43 4e 58 4e 01 00 00 01 00 00 10 00 23 00 00 00 |CNXN........#...| |
| 73 | +// 00000010 3c 0d 00 00 bc b1 a7 b1 68 6f 73 74 3a 3a 66 65 |<.......host::fe| |
| 74 | +// 00000020 61 74 75 72 65 73 3d 63 6d 64 2c 73 74 61 74 5f |atures=cmd,stat_| |
| 75 | +// 00000030 76 32 2c 73 68 65 6c 6c 5f 76 32 |v2,shell_v2| |
| 76 | +func (p *PacketReader) readPacket() { |
| 77 | + var ( |
| 78 | + command = p.readStringN(4) |
| 79 | + arg0 = p.readN(4) |
| 80 | + arg1 = p.readN(4) |
| 81 | + length = p.readInt32() |
| 82 | + check = p.readInt32() |
| 83 | + magic = p.readN(4) |
| 84 | + ) |
| 85 | + if p.err != nil { |
| 86 | + return |
| 87 | + // log.Println("ERR:", p.err) |
| 88 | + } |
| 89 | + if !bytes.Equal(xorBytes([]byte(command), magic), []byte{0xff, 0xff, 0xff, 0xff}) { |
| 90 | + p.err = errors.New("verify magic failed") |
| 91 | + return |
| 92 | + } |
| 93 | + log.Printf("cmd:%s, arg0:%x, arg1:%x, len:%d, check:%d, magic:%x", |
| 94 | + command, arg0, arg1, length, check, magic) |
| 95 | + log.Printf("cmd:%x", []byte(command)) |
| 96 | + body := p.readStringN(int(length)) |
| 97 | + log.Println(body) |
| 98 | +} |
| 99 | + |
| 100 | +func (p *PacketReader) readN(n int) []byte { |
| 101 | + buf := make([]byte, n) |
| 102 | + _, p.err = io.ReadFull(p.r(), buf) |
| 103 | + return buf |
| 104 | +} |
| 105 | + |
| 106 | +func (p *PacketReader) readStringN(n int) string { |
| 107 | + return string(p.readN(n)) |
| 108 | +} |
| 109 | + |
| 110 | +func (p *PacketReader) readInt32() int32 { |
| 111 | + var i int32 |
| 112 | + p.err = binary.Read(p.r(), binary.LittleEndian, &i) |
| 113 | + return i |
| 114 | +} |
0 commit comments