Skip to content

Commit

Permalink
fix screencap error on smartision
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Jan 2, 2019
1 parent 2b78289 commit 624ee4c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
16 changes: 16 additions & 0 deletions adb/send2adb.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#

set -e


while read -p "@? " CMD
do
FULLCMD=$(printf "%04x%s" ${#CMD} "${CMD}")
echo -n "$FULLCMD"
done

exit 0
#echo "SEND: $FULLCMD"
echo -n "${PREFIX}$FULLCMD" | nc localhost 5037
echo ""
114 changes: 114 additions & 0 deletions adb/tcpusb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package adb

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
)

const (
SYNC = "SYNC"
CXNN = "CXNN"
OPEN = "OPEN"
OKAY = "OKAY"
CLSE = "CLSE"
WRTE = "WRTE"
AUTH = "AUTH"
)

func checksum(data []byte) byte {
sum := byte(0)
for _, c := range data {
sum += c
}
return sum
}

func xorBytes(a, b []byte) []byte {
if len(a) != len(b) {
panic(fmt.Sprintf("xorBytes a:%x b:%x have different size", a, b))
}
dst := make([]byte, len(a))
for i := 0; i < len(a); i++ {
dst[i] = a[i] ^ b[i]
}
return dst
}

type AdbServer struct {
version int
mayPayload int
authorized bool
syncToken int
remoteID int
services map[string]string
remoteAddress string
token string
signature string
}

type PacketReader struct {
reader io.Reader
err error
}

type errReader struct{}

func (e errReader) Read(p []byte) (int, error) {
return 0, errors.New("package already read error")
}

func (p *PacketReader) r() io.Reader {
if p.err != nil { // use p.err to short error checks
return errReader{}
}
return p.reader
}

// Receive packet example
// 00000000 43 4e 58 4e 01 00 00 01 00 00 10 00 23 00 00 00 |CNXN........#...|
// 00000010 3c 0d 00 00 bc b1 a7 b1 68 6f 73 74 3a 3a 66 65 |<.......host::fe|
// 00000020 61 74 75 72 65 73 3d 63 6d 64 2c 73 74 61 74 5f |atures=cmd,stat_|
// 00000030 76 32 2c 73 68 65 6c 6c 5f 76 32 |v2,shell_v2|
func (p *PacketReader) readPacket() {
var (
command = p.readStringN(4)
arg0 = p.readN(4)
arg1 = p.readN(4)
length = p.readInt32()
check = p.readInt32()
magic = p.readN(4)
)
if p.err != nil {
return
// log.Println("ERR:", p.err)
}
if !bytes.Equal(xorBytes([]byte(command), magic), []byte{0xff, 0xff, 0xff, 0xff}) {
p.err = errors.New("verify magic failed")
return
}
log.Printf("cmd:%s, arg0:%x, arg1:%x, len:%d, check:%d, magic:%x",
command, arg0, arg1, length, check, magic)
log.Printf("cmd:%x", []byte(command))
body := p.readStringN(int(length))
log.Println(body)
}

func (p *PacketReader) readN(n int) []byte {
buf := make([]byte, n)
_, p.err = io.ReadFull(p.r(), buf)
return buf
}

func (p *PacketReader) readStringN(n int) string {
return string(p.readN(n))
}

func (p *PacketReader) readInt32() int32 {
var i int32
p.err = binary.Read(p.r(), binary.LittleEndian, &i)
return i
}
21 changes: 21 additions & 0 deletions adb/tcpusb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package adb

import (
"net"
"testing"
)

func TestTcpUsb(t *testing.T) {
lis, err := net.Listen("tcp", ":9000")
if err != nil {
t.Fatal(err)
}
defer lis.Close()
conn, err := lis.Accept()
if err != nil {
t.Fatal(err)
}
t.Logf("remote: %s", conn.RemoteAddr())
prd := PacketReader{reader: conn}
prd.readPacket()
}
2 changes: 1 addition & 1 deletion screenshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func anyFuncs(funcs ...func() error) error {

func takeScreenshot(serial, output string) error {
execOut := func() error {
c := adbCommand(serial, "exec-out", "screencap", "-p")
c := adbCommand(serial, "exec-out", "screencap", "-p", "2>/dev/null")
imgfile, err := os.Create(output)
if err != nil {
return err
Expand Down

0 comments on commit 624ee4c

Please sign in to comment.