zrg
2025-04-02 235ddcabc45bab8a8434c92e537f5c6183b7efb7
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
    (The MIT License)
 
    Copyright (C) 2012 wsky (wskyhx at gmail.com) and other contributors
 
    Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 
    The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
 
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
 
namespace RemotingProtocolParser.TCP
{
    /// <summary>.Net Remoting Protocol (via TCP) Parser
    /// </summary>
    public class TcpProtocolHandle : ProtocolStreamHandle
    {
        private static readonly byte[] PREAMBLE = Encoding.ASCII.GetBytes(".NET");
        public TcpProtocolHandle() : this(new MemoryStream()) { }
        public TcpProtocolHandle(Stream source) : base(source) { }
 
        /// <summary>remoting protocol premable, expected value is ".NET".
        /// </summary>
        /// <returns></returns>
        public string ReadPreamble()
        {
            return Encoding.ASCII.GetString(new Byte[] { 
                (byte)this.ReadByte(), 
                (byte)this.ReadByte(), 
                (byte)this.ReadByte(), 
                (byte)this.ReadByte() });
        }
        public void WritePreamble()
        {
            this.WriteBytes(PREAMBLE);
        }
 
        /// <summary>remoting protocol majorVersion, expected value is "1".
        /// </summary>
        /// <returns></returns>
        public int ReadMajorVersion()
        {
            return this.ReadByte();
        }
        public void WriteMajorVersion()
        {
            this.WriteByte((byte)1);
        }
 
        /// <summary>remoting protocol minorVersion, expected value is "0".
        /// </summary>
        /// <returns></returns>
        public int ReadMinorVersion()
        {
            return this.ReadByte();
        }
        public void WriteMinorVersion()
        {
            this.WriteByte((byte)0);
        }
        
        /// <summary>remoting operation code, eg Request/OneWayRequest/Reply
        /// </summary>
        /// <returns></returns>
        public ushort ReadOperation()
        {
            return this.ReadUInt16();
        }
        /// <summary>write remoting operation code
        /// </summary>
        /// <param name="value">Request/OneWayRequest/Reply</param>
        public void WriteOperation(ushort value)
        {
            this.WriteUInt16(value);
        }
 
        /// <summary>Chunked or Fixed ContentLength. Only http channel support currently.
        /// </summary>
        /// <returns></returns>
        public ushort ReadContentDelimiter()
        {
            return this.ReadUInt16();
        }
        /// <summary>ContentLength=0, Chunked=1
        /// </summary>
        /// <param name="value"></param>
        public void WriteContentDelimiter(ushort value)
        {
            this.WriteUInt16(value);
        }
 
        /// <summary>get message content length
        /// </summary>
        /// <returns></returns>
        public int ReadContentLength()
        {
            return this._contentLength = this.ReadInt32();
        }
        public void WriteContentLength(int value)
        {
            this.WriteInt32(this._contentLength = value);
        }
 
        public IDictionary<string, object> ReadTransportHeaders()
        {
            var dict = new Dictionary<string, object>();
            ushort headerType = this.ReadUInt16();
 
            while (headerType != TcpHeaders.EndOfHeaders)
            {
                if (headerType == TcpHeaders.Custom)
                {
                    dict.Add(this.ReadCountedString(), this.ReadCountedString());
                }
                else if (headerType == TcpHeaders.RequestUri)
                {
                    this.ReadByte();//RequestUri-Format
                    dict.Add(TcpTransportHeader.RequestUri, this.ReadCountedString());
                }
                else if (headerType == TcpHeaders.StatusCode)
                {
                    this.ReadByte();//StatusCode-Format
                    dict.Add(TcpTransportHeader.StatusCode, this.ReadUInt16());
                    //HACK:error curse when StatusCode!=0
                    //if (code != 0) error = true;
                }
                else if (headerType == TcpHeaders.StatusPhrase)
                {
                    this.ReadByte();//StatusPhrase-Format
                    dict.Add(TcpTransportHeader.StatusPhrase, this.ReadCountedString());
                }
                else if (headerType == TcpHeaders.ContentType)
                {
                    this.ReadByte();//ContentType-Format
                    dict.Add(TcpTransportHeader.ContentType, this.ReadCountedString());
                }
                else if (this.ReadExtendedHeader(headerType, dict))
                {
                }
                else
                {
                    var headerFormat = (byte)ReadByte();
 
                    switch (headerFormat)
                    {
                        case TcpHeaderFormat.Void: break;
                        case TcpHeaderFormat.CountedString: this.ReadCountedString(); break;
                        case TcpHeaderFormat.Byte: this.ReadByte(); break;
                        case TcpHeaderFormat.UInt16: this.ReadUInt16(); break;
                        case TcpHeaderFormat.Int32: this.ReadInt32(); break;
                        default: throw new NotSupportedException();
                    }
                }
 
                headerType = this.ReadUInt16();
            }
            return dict;
        }
        /// <summary>write transport header. PS: "RequestUri" must be transport while request call
        /// </summary>
        /// <param name="headers"></param>
        public void WriteTransportHeaders(IDictionary<string, object> headers)
        {
            if (headers != null)
                foreach (var i in headers)
                {
                    if (i.Key.Equals(TcpTransportHeader.StatusCode, StringComparison.OrdinalIgnoreCase))
                        this.WriteStatusCodeHeader((ushort)i.Value);
                    else if (i.Key.Equals(TcpTransportHeader.StatusPhrase, StringComparison.OrdinalIgnoreCase))
                        this.WriteStatusPhraseHeader(i.Value.ToString());
                    else if (i.Key.Equals(TcpTransportHeader.ContentType, StringComparison.OrdinalIgnoreCase))
                        this.WriteContentTypeHeader(i.Value.ToString());
                    else if (i.Key.Equals(TcpTransportHeader.RequestUri, StringComparison.OrdinalIgnoreCase))
                        //Request-Uri must be transport while request call
                        this.WriteRequestUriHeader(i.Value.ToString());
                    else if (this.WriteExtendedHeader(i)) { }
                    else
                        this.WriteCustomHeader(i.Key, i.Value.ToString());
                }
            this.WriteUInt16(TcpHeaders.EndOfHeaders);
        }
 
        protected virtual bool ReadExtendedHeader(ushort headerType, IDictionary<String, Object> dict)
        {
            return false;
        }
        protected virtual bool WriteExtendedHeader(KeyValuePair<string, object> item)
        {
            return false;
        }
 
        protected ushort ReadUInt16()
        {
            return (ushort)(this.ReadByte() & 0xFF | this.ReadByte() << 8);
        }
        protected void WriteUInt16(ushort value)
        {
            this.WriteByte((byte)value);
            this.WriteByte((byte)(value >> 8));
        }
 
        protected int ReadInt32()
        {
            return (int)((this.ReadByte() & 0xFF)
                | this.ReadByte() << 8
                | this.ReadByte() << 16
                | this.ReadByte() << 24);
        }
        protected void WriteInt32(int value)
        {
            this.WriteByte((byte)value);
            this.WriteByte((byte)(value >> 8));
            this.WriteByte((byte)(value >> 16));
            this.WriteByte((byte)(value >> 24));
        }
 
        protected string ReadCountedString()
        {
            var format = (byte)this.ReadByte();
            int size = ReadInt32();
 
            if (size > 0)
            {
                byte[] data = this.ReadBytes(size);
 
                switch (format)
                {
                    case TcpStringFormat.Unicode:
                        return Encoding.Unicode.GetString(data);
 
                    case TcpStringFormat.UTF8:
                        return Encoding.UTF8.GetString(data);
 
                    default:
                        throw new NotSupportedException();
                }
            }
            else
            {
                return null;
            }
        }
        protected void WriteCountedString(string value)
        {
            int strLength = 0;
            if (value != null)
                strLength = value.Length;
 
            if (strLength > 0)
            {
                byte[] strBytes = Encoding.UTF8.GetBytes(value);
                this.WriteByte(TcpStringFormat.UTF8);
                this.WriteInt32(strBytes.Length);
                this.WriteBytes(strBytes);
            }
            else
            {
                //just call it Unicode (doesn't matter since there is no data)
                this.WriteByte(TcpStringFormat.Unicode);
                this.WriteInt32(0);
            }
        }
 
        private void WriteRequestUriHeader(string value)
        {
            //value maybe "application/octet-stream"
            this.WriteUInt16(TcpHeaders.RequestUri);
            this.WriteByte(TcpHeaderFormat.CountedString);
            this.WriteCountedString(value);
        }
        private void WriteContentTypeHeader(string value)
        {
            this.WriteUInt16(TcpHeaders.ContentType);
            this.WriteByte(TcpHeaderFormat.CountedString);
            this.WriteCountedString(value);
        }
        private void WriteStatusCodeHeader(ushort value)
        {
            this.WriteUInt16(TcpHeaders.StatusCode);
            this.WriteByte(TcpHeaderFormat.UInt16);
            this.WriteUInt16(value);
        }
        private void WriteStatusPhraseHeader(string value)
        {
            this.WriteUInt16(TcpHeaders.StatusPhrase);
            this.WriteByte(TcpHeaderFormat.CountedString);
            this.WriteCountedString(value);
        }
        private void WriteCustomHeader(string name, string value)
        {
            this.WriteUInt16(TcpHeaders.Custom);
            this.WriteCountedString(name);
            this.WriteCountedString(value);
        }
    }
}