-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
decoders.go
172 lines (150 loc) · 4.07 KB
/
decoders.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package debugproxy
import (
"bytes"
"encoding/json"
"fmt"
"io"
"strings"
"os"
"time"
dtx "github.com/danielpaulus/go-ios/ios/dtx_codec"
log "github.com/sirupsen/logrus"
)
type decoder interface {
decode([]byte)
}
type dtxDecoder struct {
jsonFilePath string
binFilePath string
buffer bytes.Buffer
isBroken bool
log *log.Entry
}
type MessageWithMetaInfo struct {
DtxMessage interface{}
MessageType string
TimeReceived time.Time
OffsetInDump int64
Length int
}
func NewDtxDecoder(jsonFilePath string, binFilePath string, log *log.Entry) decoder {
return &dtxDecoder{jsonFilePath: jsonFilePath, binFilePath: binFilePath, buffer: bytes.Buffer{}, isBroken: false, log: log}
}
func (f *dtxDecoder) decode(data []byte) {
file, err := os.OpenFile(f.binFilePath+".raw",
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
file.Write(data)
file.Close()
if f.isBroken {
//when an error happens while decoding, this flag prevents from flooding the logs with errors
//while still dumping binary to debug later
return
}
f.buffer.Write(data)
slice := f.buffer.Next(f.buffer.Len())
written := 0
for {
msg, remainingbytes, err := dtx.DecodeNonBlocking(slice)
if dtx.IsIncomplete(err) {
f.buffer.Reset()
f.buffer.Write(slice)
break
}
if err != nil {
f.log.Errorf("Failed decoding DTX:%s, continuing bindumping", err)
f.log.Info(fmt.Sprintf("%x", slice))
f.isBroken = true
}
slice = remainingbytes
file, err := os.OpenFile(f.binFilePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
s, _ := file.Stat()
offset := s.Size()
file.Write(msg.RawBytes)
file.Close()
file, err = os.OpenFile(f.jsonFilePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
type Alias dtx.Message
auxi := ""
if msg.HasAuxiliary() {
auxi = msg.Auxiliary.String()
}
aux := &struct {
AuxiliaryContents string
*Alias
}{
AuxiliaryContents: auxi,
Alias: (*Alias)(&msg),
}
aux.RawBytes = nil
jsonMetaInfo := MessageWithMetaInfo{aux, "dtx", time.Now(), offset, len(msg.RawBytes)}
mylog := f.log
if strings.Contains(f.binFilePath, "from-device") {
mylog = f.log.WithFields(log.Fields{"d": "in"})
}
if strings.Contains(f.binFilePath, "to-device") {
mylog = f.log.WithFields(log.Fields{"d": "out"})
}
logDtxMessageNice(mylog, msg)
jsonmsg, err := json.Marshal(jsonMetaInfo)
file.Write(jsonmsg)
io.WriteString(file, "\n")
file.Close()
written += len(msg.RawBytes)
}
}
func logDtxMessageNice(log *log.Entry, msg dtx.Message) {
if msg.PayloadHeader.MessageType == dtx.Methodinvocation {
expectsReply := ""
if msg.ExpectsReply {
expectsReply = "e"
}
log.Infof("%d.%d%s c%d %s %s", msg.Identifier, msg.ConversationIndex, expectsReply, msg.ChannelCode, msg.Payload[0], msg.Auxiliary)
return
}
if msg.PayloadHeader.MessageType == dtx.Ack {
log.Infof("%d.%d c%d Ack", msg.Identifier, msg.ConversationIndex, msg.ChannelCode)
return
}
if msg.PayloadHeader.MessageType == dtx.UnknownTypeOne {
log.Infof("type1: %x", msg.Payload[0])
return
}
if msg.PayloadHeader.MessageType == dtx.ResponseWithReturnValueInPayload {
log.Infof("%d.%d c%d response: %s", msg.Identifier, msg.ConversationIndex, msg.ChannelCode, msg.Payload[0])
return
}
if msg.PayloadHeader.MessageType == dtx.DtxTypeError {
log.Infof("%d.%d c%d error: %s", msg.Identifier, msg.ConversationIndex, msg.ChannelCode, msg.Payload[0])
return
}
log.Infof("%+v", msg)
}
type binaryOnlyDumper struct {
path string
}
//NewNoOpDecoder does nothing
func NewBinDumpOnly(jsonFilePath string, dumpFilePath string, log *log.Entry) decoder {
return binaryOnlyDumper{dumpFilePath}
}
func (n binaryOnlyDumper) decode(bytes []byte) {
writeBytes(n.path, bytes)
}
func writeBytes(filePath string, data []byte) {
file, err := os.OpenFile(filePath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
panic(fmt.Sprintf("Could not write to file error: %v path:'%s'", err, filePath))
}
file.Write(data)
file.Close()
}