Skip to content

Commit

Permalink
add more code to fa share
Browse files Browse the repository at this point in the history
  • Loading branch information
hzsunshx committed Jan 10, 2019
1 parent 36bcfbf commit 094a2f6
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 33 deletions.
28 changes: 19 additions & 9 deletions adb/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"os/exec"
Expand All @@ -27,7 +26,6 @@ func NewClient(addr string) *Client {

func (c *Client) dial() (conn *ADBConn, err error) {
nc, err := net.DialTimeout("tcp", c.Addr, 2*time.Second)
log.Println("dial")
if err != nil {
if err = c.StartServer(); err != nil {
err = errors.Wrap(err, "adb start-server")
Expand Down Expand Up @@ -55,7 +53,7 @@ func (c *Client) roundTripSingleResponse(data string) (string, error) {
return "", err
}
defer conn.Close()
if err := conn.Check(); err != nil {
if err := conn.CheckOKAY(); err != nil {
return "", err
}
return conn.DecodeString()
Expand Down Expand Up @@ -113,7 +111,7 @@ func (c *Client) KillServer() error {
return err
}
defer conn.Close()
return conn.Check()
return conn.CheckOKAY()
}

func (c *Client) Device(descriptor DeviceDescriptor) *Device {
Expand Down Expand Up @@ -142,8 +140,20 @@ func (d *Device) Serial() (serial string, err error) {
return
}

// OpenTransport is a low level function
// Connect to adbd.exe and send <host-prefix>:transport and check OKAY
// conn should be Close after using
func (d *Device) OpenTransport() (conn *ADBConn, err error) {
return
req := "host:" + d.descriptor.getTransportDescriptor()
conn, err = d.client.roundTrip(req)
if err != nil {
return
}
conn.CheckOKAY()
if conn.Err() != nil {
conn.Close()
}
return conn, conn.Err()
}

func (d *Device) OpenShell(cmd string) (rwc io.ReadWriteCloser, err error) {
Expand All @@ -152,9 +162,9 @@ func (d *Device) OpenShell(cmd string) (rwc io.ReadWriteCloser, err error) {
if err != nil {
return
}
conn.Check()
conn.CheckOKAY()
conn.EncodeString("shell:" + cmd)
conn.Check()
conn.CheckOKAY()
if conn.Err() != nil {
conn.Close()
}
Expand Down Expand Up @@ -211,11 +221,11 @@ func (d *Device) Stat(path string) (info os.FileInfo, err error) {
return
}
defer conn.Close()
if err = conn.Check(); err != nil {
if err = conn.CheckOKAY(); err != nil {
return
}
conn.EncodeString("sync:")
conn.Check()
conn.CheckOKAY()
conn.WriteObjects("STAT", uint32(len(path)), path)

id, err := conn.ReadNString(4)
Expand Down
52 changes: 31 additions & 21 deletions adb/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,45 @@ import (
)

type ADBConn struct {
io.ReadWriter
rw io.ReadWriter
io.Closer
err error
}

func NewADBConn(conn net.Conn) *ADBConn {
prw := DebugProxyConn{
proxyRW := debugProxyConn{
R: bufio.NewReader(conn),
W: conn,
Debug: true}

return &ADBConn{
ReadWriter: prw,
Closer: conn,
rw: proxyRW,
Closer: conn,
}
}

func (conn *ADBConn) Err() error {
return conn.err
}

func (conn *ADBConn) Read(p []byte) (n int, err error) {
if conn.err != nil {
return 0, conn.err
}
n, err = conn.rw.Read(p)
conn.err = err
return
}

func (conn *ADBConn) Write(p []byte) (n int, err error) {
if conn.err != nil {
return 0, conn.err
}
n, err = conn.rw.Write(p)
conn.err = err
return
}

func (conn *ADBConn) Encode(v []byte) error {
val := string(v)
return conn.EncodeString(val)
Expand Down Expand Up @@ -100,20 +122,8 @@ func (conn *ADBConn) DecodeString() (string, error) {
return conn.ReadNString(length)
}

func (conn *ADBConn) Err() error {
return conn.err
}

func (conn *ADBConn) Check() error {
if conn.err != nil {
return conn.err
}
conn.err = conn.respCheck()
return conn.err
}

// respCheck check OKAY, or FAIL
func (conn *ADBConn) respCheck() error {
// CheckOKAY check OKAY, or FAIL
func (conn *ADBConn) CheckOKAY() error {
status, _ := conn.ReadNString(4)
switch status {
case _OKAY:
Expand All @@ -129,13 +139,13 @@ func (conn *ADBConn) respCheck() error {
}
}

type DebugProxyConn struct {
type debugProxyConn struct {
R io.Reader
W io.Writer
Debug bool
}

func (px DebugProxyConn) Write(data []byte) (int, error) {
func (px debugProxyConn) Write(data []byte) (int, error) {
if px.Debug {
m := regexp.MustCompile(`^[-:/0-9a-zA-Z ]+$`)
if m.Match(data) {
Expand All @@ -155,7 +165,7 @@ func reverseBytes(b []byte) []byte {
return out
}

func (px DebugProxyConn) Read(data []byte) (int, error) {
func (px debugProxyConn) Read(data []byte) (int, error) {
n, err := px.R.Read(data)
if px.Debug {
m := regexp.MustCompile(`^[-:/0-9a-zA-Z ]+$`)
Expand Down
4 changes: 2 additions & 2 deletions adb/tcpusb.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *Session) writePacket(cmd string, arg0, arg1 uint32, body []byte) error
return err
}

func (s *Session) handle() {
func (s *Session) Serve() {
defer s.conn.Close()
pr := NewPacketReader(s.conn)

Expand Down Expand Up @@ -155,6 +155,6 @@ func Serve(l net.Listener) error {
return err
}
sess := NewSession(conn)
go sess.handle()
go sess.Serve()
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/qiniu/log v0.0.0-20140728010919-a304a74568d6
github.com/shogo82148/androidbinary v0.0.0-20180627093851-01c4bfa8b3b5
github.com/stretchr/testify v1.2.2
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16
golang.org/x/sys v0.0.0-20181213200352-4d1cda033e06 // indirect
golang.org/x/tools v0.0.0-20181214171254-3c39ce7b6105 // indirect
gopkg.in/cheggaaa/pb.v1 v1.0.27
Expand Down
37 changes: 36 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ import (
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"syscall"
"time"

shellquote "github.com/kballard/go-shellquote"
tty "github.com/mattn/go-tty"

"github.com/codeskyblue/fa/adb"
"github.com/codeskyblue/fa/tunnel"
"github.com/manifoldco/promptui"
cli "gopkg.in/urfave/cli.v1"
)
Expand Down Expand Up @@ -448,7 +453,37 @@ func main() {
Name: "share",
Usage: "TODO: share device as address for adb connect",
Action: func(ctx *cli.Context) error {
log.Println("NotImplemented")
// log.Println("NotImplemented")
// return nil

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world!")
})

c := &tunnel.Configuration{
Host: "labstack.me:22",
RemoteHost: "0.0.0.0",
RemotePort: 8000,
Channel: make(chan int),
InBoundConnectionHook: func(in net.Conn) error {
log.Println("Accept new connection", in.RemoteAddr().String())
// http.Serve()
// http.Serve
adb.NewSession(in).Serve()
io.WriteString(in, "Nice to meed you")
in.Close()
return nil
},
}

CREATE:
go tunnel.Create(c)
event := <-c.Channel
if event == tunnel.EventReconnect {
log.Println("trying to reconnect")
time.Sleep(1 * time.Second)
goto CREATE
}
return nil
// serial, err := chooseOne()
// if err != nil {
Expand Down
File renamed without changes.
Loading

0 comments on commit 094a2f6

Please sign in to comment.