-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathClientConnection.cs
More file actions
550 lines (504 loc) · 21.3 KB
/
ClientConnection.cs
File metadata and controls
550 lines (504 loc) · 21.3 KB
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using CoAP;
using System.Net;
using System.IO;
using CoAP.EndPoint;
using SuperWebSocket;
using System.Collections;
namespace Coap.Proxy
{
class ClientConnection
{
private Socket clientSocket;
private Request _currentRequest;
private Request _observingRequest;
private readonly String _tempImagePath = Path.GetTempFileName();
private WebSocketService coapWebSocektService;
private WebSocketSession observedSession=null;
private string portName;
private string _payload="";
public String Payload
{
set { _payload = value; }
get { return _payload; }
}
public ClientConnection(Socket client,WebSocketService wss,string port)
{
this.clientSocket = client;
coapWebSocektService = wss;
portName = port;
}
public void StartHandling()
{
Thread handler = new Thread(Handler);
handler.Priority = ThreadPriority.AboveNormal;
handler.Start();
}
//proxy Segregation
private void Handler()
{
bool recvRequest = true;
string EOL = "\r\n";
string requestPayload = "";
string requestTempLine = "";
List<string> requestLines = new List<string>();
byte[] requestBuffer = new byte[1];
byte[] responseBuffer = new byte[1];
requestLines.Clear();
try
{
//State 0: Handle Request from Client
while (recvRequest)
{
this.clientSocket.Receive(requestBuffer);
string fromByte = ASCIIEncoding.ASCII.GetString(requestBuffer);
requestPayload += fromByte;
requestTempLine += fromByte;
if (requestTempLine.EndsWith(EOL))
{
requestLines.Add(requestTempLine.Trim());
requestTempLine = "";
}
if (requestPayload.EndsWith(EOL + EOL))
{
recvRequest = false;
}
}
//received http request
Console.WriteLine("Raw Request Received...");
Console.WriteLine(requestPayload);
//State 1:Segregation
//Nomal coap request
if (requestLines[0].Contains(portName) & !requestLines[0].Contains("?observehost"))
{
//get the URL
string remoteHost = requestLines[0].Split(' ')[1].Replace("http://", "coap://").Split(' ')[0];
Uri uri = new Uri(remoteHost);
//creat coap request
Request.Method method;
string requestMethod = requestLines[0].Substring(0, requestLines[0].IndexOf(' ')).ToLower();
if (requestMethod.Trim() == "get")
method = Request.Method.GET;
else if (requestMethod.Trim() == "post")
{
method = Request.Method.POST;
requestPayload += requestLines[requestLines.Count-1];
}
else if (requestMethod.Trim() == "put")
{
method = Request.Method.PUT;
requestPayload += requestLines[requestLines.Count - 1];
}
else if (requestMethod.Trim() == "delete")
method = Request.Method.DELETE;
else
method = Request.Method.GET;
//get the payLoad
Payload = requestPayload;
//Resource discovery request
if (remoteHost.Contains("index"))
{
remoteHost = remoteHost.Replace("index", ".well-known/core");
Uri discoverUri = new Uri(remoteHost);
Request request = PerformCoAP(Request.Method.GET, discoverUri, false, false);
Action<Request> discoverHandler = new Action<Request>(HandleDiscover);
discoverHandler.BeginInvoke(request, null, null);
}
//nomal CoAP request
else
{
Request requestOnce = PerformCoAP(method, uri, true, false);
}
}
//observe request
else if (requestLines[0].Contains(portName) & requestLines[0].Contains("?observehost"))
{
//get URL
string remoteHost = requestLines[0].Split(' ')[1].Replace("http://", "coap://").Split(' ')[0];
remoteHost = remoteHost.Replace("?observehost", "");
Uri discoverUri = new Uri(remoteHost);
PerformObserve(Request.Method.GET, discoverUri);
}
//Http Conncet request
else if (requestLines[0].ToLower().Contains("connect"))
{
string remoteHost = requestLines[0].Split(' ')[1].Replace("http://", "").Split('/')[0];
int port = Int32.Parse(remoteHost.Split(':')[1]);
remoteHost = remoteHost.Split(':')[0];
Socket destServerSocket = null;
//Conncet to real Server
try
{
destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
destServerSocket.Connect(remoteHost, port);
}
catch (Exception e)
{
Console.WriteLine("Bad connect to real Server: " + e.Message);
}
// Conncet to real Server success
if (destServerSocket.Connected)
{
Console.WriteLine("Websocket connect success ");
//exchange message
ForwardTcpData(clientSocket, destServerSocket);
}
else
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
//nomal Http request
else
{
//State 1: Rebuilding Request Information and Create Connection to Destination Server
string remoteHost = requestLines[0].Split(' ')[1].Replace("http://", "").Split('/')[0];
string requestFile = requestLines[0].Replace("http://", "").Replace(remoteHost, "");
requestLines[0] = requestFile;
requestPayload = "";
foreach (string line in requestLines)
{
requestPayload += line;
requestPayload += EOL;
}
Socket destServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
destServerSocket.Connect(remoteHost, 80);
//State 2: Sending New Request Information to Destination Server and Relay Response to Client
destServerSocket.Send(ASCIIEncoding.ASCII.GetBytes(requestPayload));
//Console.WriteLine("Begin Receiving Response...");
while (destServerSocket.Receive(responseBuffer) != 0)
{
//Console.Write(ASCIIEncoding.ASCII.GetString(responseBuffer));
this.clientSocket.Send(responseBuffer);
}
destServerSocket.Disconnect(false);
destServerSocket.Dispose();
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
}
catch (Exception e)
{
Console.WriteLine("Error Occured: " + e.Message);
//Console.WriteLine(e.StackTrace);
}
}
/// <summary>
/// Exchange Connect message from client and server
/// </summary>
/// <param name="client">client socket</param>
/// <param name="server">server socket</param>
private void ForwardTcpData(Socket client, Socket server)
{
ArrayList ReadList = new ArrayList(2);
client.Send(Encoding.ASCII.GetBytes("HTTP/1.1 200 Connection established\r\n\r\n"));
while (true)
{
ReadList.Clear();
ReadList.Add(client);
ReadList.Add(server);
try
{
//Console.WriteLine("test socket");
Socket.Select(ReadList, null, null, 1 * 1000 * 1000);
}
catch (SocketException e)
{
Console.WriteLine("Select error: " + e.Message);
break;
}
// time out
if (ReadList.Count == 0)
{
//Console.WriteLine("Time out");
if (client.Connected && server.Connected)
continue;
else
break;
}
// client send message
if (ReadList.Contains(client))
{
byte[] Recv = new byte[1024];
int Length = 0;
try
{
Length = client.Receive(Recv, Recv.Length,0);
if (Length == 0)
{
Console.WriteLine("Client is disconnect.");
break;
}
Console.WriteLine(" Recv bytes from client" + Encoding.ASCII.GetString(Recv), Length);
}
catch (Exception e)
{
Console.WriteLine("Read from client error: " + e.Message);
break;
}
try
{
Length = server.Send(Recv, Length,0);
Console.WriteLine(" Write bytes to server{0}", Length);
}
catch (Exception e)
{
Console.WriteLine("Write data to server error: " + e.Message);
break;
}
}
// real server send messgae
if (ReadList.Contains(server))
{
byte[] Recv = new byte[1024];
int Length = 0;
try
{
Length = server.Receive(Recv, Recv.Length, 0);
if (Length == 0)
{
Console.WriteLine("Server is disconnect");
break;
}
Console.WriteLine("Recv bytes from server", Length);
}
catch (Exception e)
{
Console.WriteLine("Read from server error: " + e.Message);
break;
}
try
{
Length = client.Send(Recv, Length, 0);
Console.WriteLine(" Write bytes to client", Length);
}
catch (Exception e)
{
Console.WriteLine("Write data to client error: " + e.Message);
break;
}
}
}
try
{
client.Shutdown(SocketShutdown.Both);
server.Shutdown(SocketShutdown.Both);
client.Close();
server.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.WriteLine("connect close");
}
}
//Resource discovery Handle
private void HandleDiscover(Request request)
{
Response response = request.ReceiveResponse();
string responseCoap="";
if (null == response)
{
// timeout
responseCoap = "Timeout";
}
else
{
Resource root = RemoteResource.NewRoot(response.PayloadString);
responseCoap = "HTTP/1.1 200 OK\r\n";
responseCoap += "Content-Type: text/html; charset=UTF-8\r\n\r\n";
responseCoap += "<html><head><title>resource list</title></head><body>resource list:<ul>";
responseCoap+=PopulateResources(root, GetAbsolutePath(request.URI));
responseCoap += "</ul></body></html>";
}
byte[] bytes = Encoding.UTF8.GetBytes(responseCoap);
this.clientSocket.Send(bytes);
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
//get resouce path
private static String GetAbsolutePath(Uri uri)
{
if ("/".Equals(uri.AbsolutePath) && !uri.OriginalString.EndsWith("/"))
return uri.OriginalString;
else
return uri.OriginalString.Substring(0, uri.OriginalString.Length - uri.PathAndQuery.Length);
}
//make a html index
private string PopulateResources(Resource currentRoot, String baseUri)
{
Resource[] resources = currentRoot.GetSubResources();
string returnLi="";
foreach (Resource res in resources)
{
if (res.SubResourceCount > 0)
{
returnLi += PopulateResources(res, baseUri);
}
else
{
if (res.ResourceType != null && res.ResourceType.ToLower() == "observe")
{
returnLi += String.Format("<li><a href=\"{0}\">{1}</a><br/><span>*can be observed*</span><br/><span>*resource title:{2}</span></li>", baseUri.Replace("coap", "http") + res.Path, res.Path.Substring(1, res.Path.Length - 1), res.Title);
}
else
{
returnLi += String.Format("<li><a href=\"{0}\">{1}</a><br/><span>*resource title:{2}</span></li>", baseUri.Replace("coap", "http") + res.Path, res.Path.Substring(1, res.Path.Length - 1), res.Title);
}
}
}
return returnLi;
}
//send nomal coap request
private Request PerformCoAP(Request.Method method, Uri uri, Boolean messageVisible, Boolean observe)
{
if (null != _currentRequest)
_currentRequest.Cancel();
Request request = Request.Create(method);
_currentRequest = request;
request.URI = uri;
if (method == Request.Method.POST || method == Request.Method.PUT)
{
request.SetPayload(this.Payload, MediaType.TextPlain);
}
if (messageVisible)
{
request.Responding += new EventHandler<ResponseEventArgs>(request_Responding);
request.Responded += new EventHandler<ResponseEventArgs>(request_Responded);
}
request.ResponseQueueEnabled = true;
request.Execute();
return request;
}
//send observe request
private void PerformObserve(Request.Method method, Uri uri)
{
if (null != _currentRequest)
_currentRequest.Cancel();
Request request = Request.Create(method);
_currentRequest = request;
request.URI = uri;
request.AddOption(Option.Create(OptionType.Observe, 60));
request.Token = TokenManager.Instance.AcquireToken(false);
request.Responded += new EventHandler<ResponseEventArgs>(observe_Responded);
_observingRequest = request;
//try Websocket connect
try
{
//wait for websocket connect
Thread.Sleep(5000);
observedSession = coapWebSocektService.SessionList[coapWebSocektService.SessionList.Count - 1];
request.ResponseQueueEnabled = true;
request.Execute();
}
catch
{
_observingRequest.RemoveOptions(OptionType.Observe);
_observingRequest.Execute();
_observingRequest = null;
Console.WriteLine("Bad websocket connect");
this.clientSocket.Send(Encoding.UTF8.GetBytes("Bad websocket connect"));
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
}
//on receive observe response
void observe_Responded(Object sender, ResponseEventArgs e)
{
if (coapWebSocektService.SessionList.Contains(observedSession))
coapWebSocektService.SendToObserved(e.Response.PayloadString, observedSession);
//when the client is closed,cancel the observe
else
{
PerformCoAP(Request.Method.GET, _observingRequest.URI, false, false);
Console.WriteLine("Websocket链接关闭");
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
}
//on receiving nomal coap response
void request_Responding(Object sender, ResponseEventArgs e)
{
DisplayMessage(e.Response);
}
//on receive nomal coap response
void request_Responded(Object sender, ResponseEventArgs e)
{
DisplayMessage(e.Response);
}
//show messgae
private void DisplayMessage(Message msg)
{
try
{
IList<Option> options = msg.GetOptions();
Boolean ifObserve = false;
string responseCoap;
responseCoap = "HTTP/1.1 200 OK\r\n";
responseCoap += "Content-Type: text/html; charset=UTF-8\r\n\r\n";
//Judge if the resouce can be Observed
foreach (Option o in options)
{
if (o.Type.Equals(OptionType.MaxAge))
{
ifObserve = true;
break;
}
}
//if can be Observed
if (ifObserve)
{
responseCoap += "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"
+ "<html><head><title>Observe</title>"
+"<script type=\"text/javascript\"> var ws; function connectSocketServer() {"
+ "if (document.getElementById('btnSend').value == \"Observe\"){"
+ " document.getElementById('messageBoard').innerHTML = \"* Connecting to server ..<br/>\"; document.getElementById('btnSend').value =\"Cancel\";"
+ " ws = new WebSocket('ws://59.64.38.126:912');"
+ " var script_el=document.createElement(\"script\"); script_el.type=\"text/javascript\";"
+ " script_el.src=window.location.href + \"?observehost\";"
+ " document.getElementById('messageBoard').appendChild(script_el);"
+ " ws.onmessage = function (evt) { document.getElementById('messageBoard').innerHTML+=\"# \" + evt.data + \"<br />\"; };"
+ " ws.onopen = function () { document.getElementById('messageBoard').innerHTML +=\"* Connection open<br/>\";};"
+ " ws.onclose = function (){ document.getElementById('messageBoard').innerHTML += \"* Connection closed<br/>\"; };"
+ "} else{ window.location.reload(); }"
+ "}</script>"
+"</head><body><div id=\"messageBoard\">"
+ msg.PayloadString
+ "</div><br/><input type=\"button\" id=\"btnSend\" value=\"Observe\" onclick=\"connectSocketServer();\" /></body></html>";
}
else
{
responseCoap += msg.PayloadString;
}
Byte[] payloadBytes = msg.Payload;
if (MediaType.IsImage(msg.ContentType))
{
// show image
// doesn't work :(
//webBrowserPayload.DocumentStream = new MemoryStream(payloadBytes);
// save to a temp file
File.WriteAllBytes(_tempImagePath, payloadBytes);
String path = "file:///" + _tempImagePath.Replace('\\', '/');
responseCoap = String.Format("<html><head><title></title></head><body><img src=\"{0}\" alt=\"\"></body></html>", path);
}
byte[] bytes = Encoding.UTF8.GetBytes(responseCoap);
this.clientSocket.Send(bytes);
this.clientSocket.Disconnect(false);
this.clientSocket.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}