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
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
namespace IceInternal
{
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
using System.Text;
public interface NetworkProxy
{
//
// Write the connection request on the connection established
// with the network proxy server. This is called right after
// the connection establishment succeeds.
//
void beginWrite(EndPoint endpoint, Buffer buf);
int endWrite(Buffer buf);
//
// Once the connection request has been sent, this is called
// to prepare and read the response from the proxy server.
//
void beginRead(Buffer buf);
int endRead(Buffer buf);
//
// This is called when the response from the proxy has been
// read. The proxy should copy the extra read data (if any) in the
// given byte vector.
//
void finish(Buffer readBuffer, Buffer writeBuffer);
//
// If the proxy host needs to be resolved, this should return
// a new NetworkProxy containing the IP address of the proxy.
// This is called from the endpoint host resolver thread, so
// it's safe if this this method blocks.
//
NetworkProxy resolveHost(int protocolSupport);
//
// Returns the IP address of the network proxy. This method
// must not block. It's only called on a network proxy object
// returned by resolveHost().
//
EndPoint getAddress();
//
// Returns the name of the proxy, used for tracing purposes.
//
string getName();
//
// Returns the protocols supported by the proxy.
//
int getProtocolSupport();
}
public sealed class SOCKSNetworkProxy : NetworkProxy
{
public SOCKSNetworkProxy(string host, int port)
{
_host = host;
_port = port;
}
private SOCKSNetworkProxy(EndPoint address)
{
_address = address;
}
public void beginWrite(EndPoint endpoint, Buffer buf)
{
if(!(endpoint is IPEndPoint))
{
throw new Ice.FeatureNotSupportedException("SOCKS4 does not support domain names");
}
else if(endpoint.AddressFamily != AddressFamily.InterNetwork)
{
throw new Ice.FeatureNotSupportedException("SOCKS4 only supports IPv4 addresses");
}
//
// SOCKS connect request
//
IPEndPoint addr = (IPEndPoint)endpoint;
buf.resize(9, false);
ByteBuffer.ByteOrder order = buf.b.order();
buf.b.order(ByteBuffer.ByteOrder.BIG_ENDIAN); // Network byte order.
buf.b.position(0);
buf.b.put(0x04); // SOCKS version 4.
buf.b.put(0x01); // Command, establish a TCP/IP stream connection
buf.b.putShort((short)addr.Port); // Port
buf.b.put(addr.Address.GetAddressBytes()); // IPv4 address
buf.b.put(0x00); // User ID.
buf.b.position(0);
buf.b.limit(buf.size());
buf.b.order(order);
}
public int endWrite(Buffer buf)
{
// Once the request is sent, read the response
return buf.b.hasRemaining() ? SocketOperation.Write : SocketOperation.Read;
}
public void beginRead(Buffer buf)
{
//
// Read the SOCKS4 response whose size is 8 bytes.
//
buf.resize(8, true);
buf.b.position(0);
}
public int endRead(Buffer buf)
{
// We're done once we read the response
return buf.b.hasRemaining() ? SocketOperation.Read : SocketOperation.None;
}
public void finish(Buffer readBuffer, Buffer writeBuffer)
{
readBuffer.b.position(0);
byte b1 = readBuffer.b.get();
byte b2 = readBuffer.b.get();
if(b1 != 0x00 || b2 != 0x5a)
{
throw new Ice.ConnectFailedException();
}
}
public NetworkProxy resolveHost(int protocolSupport)
{
Debug.Assert(_host != null);
return new SOCKSNetworkProxy(Network.getAddresses(_host,
_port,
protocolSupport,
Ice.EndpointSelectionType.Random,
false,
true)[0]);
}
public EndPoint getAddress()
{
Debug.Assert(_address != null); // Host must be resolved.
return _address;
}
public string getName()
{
return "SOCKS";
}
public int getProtocolSupport()
{
return Network.EnableIPv4;
}
private readonly string _host;
private readonly int _port;
private readonly EndPoint _address;
}
public sealed class HTTPNetworkProxy : NetworkProxy
{
public HTTPNetworkProxy(string host, int port)
{
_host = host;
_port = port;
_protocolSupport = Network.EnableBoth;
}
private HTTPNetworkProxy(EndPoint address, int protocolSupport)
{
_address = address;
_protocolSupport = protocolSupport;
}
public void beginWrite(EndPoint endpoint, Buffer buf)
{
string addr = Network.addrToString(endpoint);
StringBuilder str = new StringBuilder();
str.Append("CONNECT ");
str.Append(addr);
str.Append(" HTTP/1.1\r\nHost: ");
str.Append(addr);
str.Append("\r\n\r\n");
byte[] b = System.Text.Encoding.ASCII.GetBytes(str.ToString());
//
// HTTP connect request
//
buf.resize(b.Length, false);
buf.b.position(0);
buf.b.put(b);
buf.b.position(0);
buf.b.limit(buf.size());
}
public int endWrite(Buffer buf)
{
// Once the request is sent, read the response
return buf.b.hasRemaining() ? SocketOperation.Write : SocketOperation.Read;
}
public void beginRead(Buffer buf)
{
//
// Read the HTTP response
//
buf.resize(7, true); // Enough space for reading at least HTTP1.1
buf.b.position(0);
}
public int endRead(Buffer buf)
{
//
// Check if we received the full HTTP response, if not, continue
// reading otherwise we're done.
//
int end = new HttpParser().isCompleteMessage(buf.b, 0, buf.b.position());
if(end < 0 && !buf.b.hasRemaining())
{
//
// Read one more byte, we can't easily read bytes in advance
// since the transport implenentation might be be able to read
// the data from the memory instead of the socket.
//
buf.resize(buf.size() + 1, true);
return SocketOperation.Read;
}
return SocketOperation.None;
}
public void finish(Buffer readBuffer, Buffer writeBuffer)
{
HttpParser parser = new HttpParser();
parser.parse(readBuffer.b, 0, readBuffer.b.position());
if(parser.status() != 200)
{
throw new Ice.ConnectFailedException();
}
}
public NetworkProxy resolveHost(int protocolSupport)
{
Debug.Assert(_host != null);
return new HTTPNetworkProxy(Network.getAddresses(_host,
_port,
protocolSupport,
Ice.EndpointSelectionType.Random,
false,
true)[0],
protocolSupport);
}
public EndPoint getAddress()
{
Debug.Assert(_address != null); // Host must be resolved.
return _address;
}
public string getName()
{
return "HTTP";
}
public int getProtocolSupport()
{
return _protocolSupport;
}
private readonly string _host;
private readonly int _port;
private readonly EndPoint _address;
private readonly int _protocolSupport;
}
}
|