-
Notifications
You must be signed in to change notification settings - Fork 1
/
remoting.go
217 lines (175 loc) · 4.98 KB
/
remoting.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package amf
import (
"errors"
"io"
"strings"
)
// function for WIP code:
func unused(a ...interface{}) {}
var STATUS_CODES = map[string]string{
"STATUS_OK": "/onResult",
"STATUS_ERROR": "/onStatus",
"STATUS_DEBUG": "/onDebugEvents",
}
type FlexRemotingMessage struct {
// AbstractMessage:
Body []interface{}
ClientId string
Destination string
Headers map[string]interface{}
MessageId string
Timestamp uint32
TimeToLive uint32
// RemotingMessage:
Operation string
Source string
}
type FlexErrorMessage struct {
// AbstractMessage:
Body []interface{}
ClientId string
Destination string
Headers map[string]interface{}
MessageId string
Timestamp uint32
TimeToLive uint32
// AcknowledgeMessage:
Flags byte
// ErrorMessage:
ExtendedData string
FaultCode int
FaultDetail int
FaultString string
RootCause string
}
type MessageBundle struct {
AmfVersion uint16
Headers []Header
Messages []AmfMessage
}
type Header struct {
Name string
MustUnderstand bool
Value interface{}
}
type AmfMessage struct {
TargetUri string
ResponseUri string
Body interface{}
}
func DecodeMessageBundle(stream io.Reader) (*MessageBundle, error) {
cxt := NewDecoder(stream, 0)
cxt.RegisterType("flex.messaging.messages.RemotingMessage", FlexRemotingMessage{})
amfVersion := cxt.ReadUint16()
result := MessageBundle{}
cxt.AmfVersion = amfVersion
result.AmfVersion = amfVersion
/*
From http://osflash.org/documentation/amf/envelopes/remoting:
The first two bytes of an AMF message are an unsigned short int. The result
indicates what type of Flash Player connected to the server.
0x00 for Flash Player 8 and below
0x01 for FlashCom/FMS
0x03 for Flash Player 9
Note that Flash Player 9 will always set the second byte to 0x03, regardless of
whether the message was sent in AMF0 or AMF3.
*/
if cxt.AmfVersion > 0x09 {
return nil, errors.New("Malformed stream (wrong amfVersion)")
}
headerCount := cxt.ReadUint16()
/*
From http://osflash.org/documentation/amf/envelopes/remoting:
Each header consists of the following:
UTF string (including length bytes) - name
Boolean - specifies if understanding the header is 'required'
Long - Length in bytes of header
Variable - Actual data (including a type code)
*/
// Read headers
result.Headers = make([]Header, headerCount)
for i := 0; i < int(headerCount); i++ {
_, name := cxt.ReadString()
mustUnderstand := cxt.ReadUint8() != 0
data_len := cxt.ReadUint32()
unused(data_len)
value := cxt.ReadValue()
header := Header{name, mustUnderstand, value}
result.Headers[i] = header
//fmt.Printf("Read header, name = %s\n", name)
}
/*
From http://osflash.org/documentation/amf/envelopes/remoting:
Between the headers and the start of the bodies is a int specifying the number of
bodies. Each body consists of the following:
UTF String - Target
UTF String - Response
Long - Body length in bytes
Variable - Actual data (including a type code)
*/
// Read message bodies
messageCount := cxt.ReadUint16()
result.Messages = make([]AmfMessage, messageCount)
for i := 0; i < int(messageCount); i++ {
// TODO: Should reset object tables here
cxt.Clear()
message := &result.Messages[i]
_, message.TargetUri = cxt.ReadString()
_, message.ResponseUri = cxt.ReadString()
status := "STATUS_OK"
is_request := true
for code, s := range STATUS_CODES {
if !strings.HasSuffix(message.TargetUri, s) {
continue
}
is_request = false
status = code
message.TargetUri = message.TargetUri[len(message.TargetUri)-len(s)+1 : len(message.TargetUri)]
_ = status
}
//fmt.Println(message)
messageLength := cxt.ReadUint32()
// TODO: Check targetUri to see if this isn't an array?
if is_request {
// Read an array, however this array is strange because it doesn't use
// the reference bit.
typeCode := cxt.ReadUint8()
if typeCode != 9 {
return nil, errors.New("Expected Array type code in message body")
}
ref := cxt.ReadUint32()
itemCount := int(ref)
args := make([]interface{}, itemCount)
for i := 0; i < itemCount; i++ {
args[i] = cxt.ReadValue()
}
message.Body = args
} else {
message.Body = cxt.ReadValue()
}
unused(messageLength)
}
return &result, nil
}
// Encode message for http request
func EncodeMessageBundle(cxt *Encoder, bundle *MessageBundle) error {
cxt.WriteUint16(bundle.AmfVersion)
// Write headers
cxt.WriteUint16(uint16(len(bundle.Headers)))
for _, header := range bundle.Headers {
cxt.WriteString(header.Name)
cxt.WriteBool(header.MustUnderstand)
}
// Write messages
cxt.WriteUint16(uint16(len(bundle.Messages)))
for _, message := range bundle.Messages {
cxt.WriteString(message.TargetUri)
cxt.WriteString(message.ResponseUri)
cxt.WriteUint32(0)
cxt.WriteUint8(0x0a)
cxt.WriteUint32(0x01) // message body length
cxt.WriteUint8(0x11) // type amf3
cxt.WriteValueAmf3(message.Body)
}
return nil
}