Skip to content

Commit

Permalink
support instrument
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymous5l committed Aug 2, 2020
1 parent 8573e7d commit c1f03f6
Show file tree
Hide file tree
Showing 11 changed files with 1,133 additions and 31 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module iconsole
go 1.13

require (
github.com/olekukonko/tablewriter v0.0.4
github.com/satori/go.uuid v1.2.0
github.com/urfave/cli v1.22.2
howett.net/plist v0.0.0-20181124034731-591f970eefbb
Expand Down
195 changes: 195 additions & 0 deletions instrument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package main

import (
"errors"
"fmt"
"iconsole/services"
"os"
"strconv"
"strings"

"github.com/olekukonko/tablewriter"

"github.com/urfave/cli"
)

func actionProcessList(ctx *cli.Context) error {
udid := ctx.String("UDID")

device, err := getDevice(udid)
if err != nil {
return err
}

s, err := services.NewInstrumentService(device)
if err != nil {
return err
}

if err := s.Handshake(); err != nil {
return err
}

p, err := s.ProcessList()
if err != nil {
return err
}

writer := tablewriter.NewWriter(os.Stdout)
writer.SetHeader([]string{"PID", "ProcessName", "StartDate"})
for _, v := range p {
writer.Append([]string{
strconv.Itoa(v.Pid),
v.Name,
v.StartDate.Format("2006-01-02 15:04:05"),
})
}
writer.Render()

return nil
}

func actionAppList(ctx *cli.Context) error {
udid := ctx.String("UDID")

device, err := getDevice(udid)
if err != nil {
return err
}

s, err := services.NewInstrumentService(device)
if err != nil {
return err
}

if err := s.Handshake(); err != nil {
return err
}

p, err := s.AppList()
if err != nil {
return err
}

writer := tablewriter.NewWriter(os.Stdout)
writer.SetHeader([]string{"Name", "BundleID"})
for _, a := range p {
writer.Append([]string{
a.DisplayName,
a.CFBundleIdentifier,
})
}
writer.Render()

return nil
}

func actionKill(ctx *cli.Context) error {
udid := ctx.String("UDID")
pid := ctx.Int("pid")

if pid <= 0 {
return errors.New("argument error")
}

device, err := getDevice(udid)
if err != nil {
return err
}

s, err := services.NewInstrumentService(device)
if err != nil {
return err
}

if err := s.Handshake(); err != nil {
return err
}

if err := s.Kill(pid); err != nil {
return err
}

return nil
}

func actionLaunch(ctx *cli.Context) error {
udid := ctx.String("UDID")
bundleId := ctx.String("bundleid")

if strings.Trim(bundleId, "") == "" {
return errors.New("argument error")
}

device, err := getDevice(udid)
if err != nil {
return err
}

s, err := services.NewInstrumentService(device)
if err != nil {
return err
}

if err := s.Handshake(); err != nil {
return err
}

pid, err := s.Launch(bundleId)
if err != nil {
return err
}

fmt.Printf("Launch successful pid: %d\n", pid)

return nil
}

func initProcessCommond() cli.Command {
return cli.Command{
Name: "instrument",
Usage: "Instrument tools",
UsageText: "iconsole instrument [-u serial_number|UDID] <Command>",
Flags: globalFlags,
Subcommands: []cli.Command{
{
Name: "proclist",
ShortName: "pl",
Usage: "List all process",
Action: actionProcessList,
Flags: globalFlags,
},
{
Name: "applist",
ShortName: "al",
Usage: "List all installed app",
Action: actionAppList,
Flags: globalFlags,
},
{
Name: "kill",
ShortName: "k",
Usage: "Kill application",
Action: actionKill,
Flags: append(globalFlags, cli.IntFlag{
Name: "pid, p",
Usage: "Process id",
EnvVar: "PROCESS_PID",
Required: true,
}),
},
{
Name: "launch",
ShortName: "s",
Usage: "Launch application",
Action: actionLaunch,
Flags: append(globalFlags, cli.StringFlag{
Name: "bundleid, i",
Usage: "Application bundle id",
EnvVar: "BUNDLE_ID",
Required: true,
}),
},
},
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func main() {
initMountCommand(),
initAFCCommand(),
initArrest(),
initProcessCommond(),
}

if err := app.Run(os.Args); err != nil {
Expand Down
138 changes: 138 additions & 0 deletions ns/dtxmessage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package ns

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

type DTXMessage struct {
msgbuf *bytes.Buffer
}

func NewDTXMessage() *DTXMessage {
return &DTXMessage{msgbuf: &bytes.Buffer{}}
}

func (this *DTXMessage) AppendObject(obj interface{}) error {
archiver := NewNSKeyedArchiver()

b, err := archiver.Marshal(obj)
if err != nil {
return err
}

this.AppendUInt32(10)
this.AppendUInt32(2)
this.AppendUInt32(uint32(len(b)))
this.msgbuf.Write(b)

return nil
}

func (this *DTXMessage) AppendInt64(v int64) {
this.AppendUInt32(10)
this.AppendUInt32(4)
this.AppendUInt64(uint64(v))
}

func (this *DTXMessage) AppendInt32(v int32) {
this.AppendUInt32(10)
this.AppendUInt32(3)
this.AppendUInt32(uint32(v))
}

func (this *DTXMessage) AppendUInt32(v uint32) {
_ = binary.Write(this.msgbuf, binary.LittleEndian, v)
}

func (this *DTXMessage) AppendUInt64(v uint64) {
_ = binary.Write(this.msgbuf, binary.LittleEndian, v)
}

func (this *DTXMessage) AppendBytes(b []byte) {
this.msgbuf.Write(b)
}

func (this *DTXMessage) Len() int {
return this.msgbuf.Len()
}

func (this *DTXMessage) ToBytes() []byte {
dup := this.msgbuf.Bytes()
b := make([]byte, 16)
binary.LittleEndian.PutUint64(b, 0x1f0)
binary.LittleEndian.PutUint64(b[8:], uint64(this.Len()))
return append(b, dup...)
}

func UnmarshalDTXMessage(b []byte) ([]interface{}, error) {
r := bytes.NewReader(b)
var magic uint64
var pkgLen uint64
if err := binary.Read(r, binary.LittleEndian, &magic); err != nil {
return nil, err
} else if err := binary.Read(r, binary.LittleEndian, &pkgLen); err != nil {
return nil, err
}

if magic != 0x1df0 {
return nil, errors.New("magic not equal 0x1df0")
}

if pkgLen > uint64(len(b)-16) {
return nil, errors.New("package length not enough")
}

var ret []interface{}

for r.Len() > 0 {
var flag uint32
var typ uint32
if err := binary.Read(r, binary.LittleEndian, &flag); err != nil {
return nil, err
} else if err := binary.Read(r, binary.LittleEndian, &typ); err != nil {
return nil, err
}
switch typ {
case 2:
var l uint32
if err := binary.Read(r, binary.LittleEndian, &l); err != nil {
return nil, err
}
plistBuf := make([]byte, l)
if _, err := r.Read(plistBuf); err != nil {
return nil, err
}
archiver := NewNSKeyedArchiver()
d, err := archiver.Unmarshal(plistBuf)
if err != nil {
return nil, err
}
ret = append(ret, d)
case 3, 5:
var i int32
if err := binary.Read(r, binary.LittleEndian, &i); err != nil {
return nil, err
}
ret = append(ret, i)
case 4, 6:
var i int64
if err := binary.Read(r, binary.LittleEndian, &i); err != nil {
return nil, err
}
ret = append(ret, i)
case 10:
// debug
fmt.Println("Dictionary key!")
continue
default:
// debug
fmt.Printf("Unknow type %d\n", typ)
break
}
}

return ret, nil
}
Loading

0 comments on commit c1f03f6

Please sign in to comment.