-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTTProxy.cs
693 lines (614 loc) · 23.6 KB
/
HTTProxy.cs
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Net;
using System.IO;
using System.Threading;
namespace KCB2
{
public class HTTProxy : IDisposable
{
#region プロキシ設定
[DllImport("wininet.dll", SetLastError = true)]
private static extern bool InternetSetOption(IntPtr hInternet,
int dwOption, IntPtr lpBuffer, int lpdwBufferLength);
struct INTERNET_PROXY_INFO
{
public int dwAccessType;
public IntPtr proxy;
public IntPtr proxyBypass;
}
/// <summary>
/// WinInetプロセスプロキシ設定
/// </summary>
/// <param name="strProxy">プロキシ設定文字列 http=http-proxy:8080 https=https-proxy:8081</param>
/// <param name="strProxyBypass">プロキシ不使用設定文字列</param>
public static void SetProcessProxy(string strProxy,string strProxyBypass)
{
/*
* Listing proxy servers.
* https://msdn.microsoft.com/en-us/library/windows/desktop/aa383996(v=vs.85).aspx
*
*/
const int INTERNET_OPTION_PROXY = 38;
const int INTERNET_OPEN_TYPE_PROXY = 3;
INTERNET_PROXY_INFO proxyInfo;
// Filling in structure
proxyInfo.dwAccessType = INTERNET_OPEN_TYPE_PROXY;
proxyInfo.proxy = Marshal.StringToHGlobalAnsi(strProxy);
proxyInfo.proxyBypass = Marshal.StringToHGlobalAnsi(strProxyBypass);
// Allocating memory
IntPtr pProxyInfo = Marshal.AllocCoTaskMem(Marshal.SizeOf(proxyInfo));
DebugOut("Update Proxy Settings proxy:[{0}] proxyBypass:[{1}]", strProxy, strProxyBypass);
// Converting structure to IntPtr
Marshal.StructureToPtr(proxyInfo, pProxyInfo, true);
InternetSetOption(IntPtr.Zero, INTERNET_OPTION_PROXY,pProxyInfo, Marshal.SizeOf(proxyInfo));
Marshal.FreeCoTaskMem(pProxyInfo);
}
#endregion
/// <summary>
/// セッション情報
/// </summary>
public class SessionInfo
{
public SessionInfo()
{
Ignore = false;
BypassServerRequest = false;
Response = new HTTPResponse();
Request = new HTTPRequest();
}
/// <summary>
/// リクエストURI
/// </summary>
public Uri Uri { get; set; }
public HTTPRequest Request { get; set; }
/// <summary>
/// レスポンス情報
/// </summary>
public HTTPResponse Response { get; set; }
/// <summary>
/// 当該レスポンスを処理しない(AfterSessionCompletedが呼ばれない)
/// </summary>
public bool Ignore { get; set; }
/// <summary>
/// サーバへリクエストを投げず、現在設定されているHTTPResponseをクライアントへ返す
/// </summary>
public bool BypassServerRequest { get; set; }
/// <summary>
/// HTTPレスポンス情報
/// </summary>
public class HTTPResponse
{
public HTTPResponse()
{
StatusCode = 200;
ProtocolVersion = HttpVersion.Version10;
ContentType = "text/plain;charset=utf-8";
ContentBody = null;
_statusDescription = null;
}
/// <summary>
/// 現在のHTTPレスポンスをHttpListenerResponseへ設定
/// </summary>
/// <param name="res"></param>
public void UpdateResponse(HttpListenerResponse res)
{
ContentType = res.ContentType;
StatusCode = res.StatusCode;
StatusDescription = res.StatusDescription;
ProtocolVersion = res.ProtocolVersion;
}
/// <summary>
/// HTTPステータスコード
/// </summary>
public int StatusCode { get;set;}
/// <summary>
/// Content-Type
/// </summary>
public string ContentType { get;set;}
/// <summary>
/// HTTPプロトコルバージョン
/// </summary>
public Version ProtocolVersion;
string _statusDescription;
/// <summary>
/// HTTPステータス情報、nullを設定するとステータスコードから引っ張った文字になる
/// </summary>
public string StatusDescription
{
get
{
if (_statusDescription != null)
return _statusDescription;
switch (StatusCode)
{
case 200:
return "OK";
case 502:
return "Bad Gateway";
default:
return string.Format("Unknown({0})",StatusCode);
}
}
set { _statusDescription = value; }
}
/// <summary>
/// レスポンスボディ
/// </summary>
public byte[] ContentBody { get; set; }
/// <summary>
/// レスポンスボディをUTF-8文字列と見なして処理
/// </summary>
public string ContentString
{
get
{
if (ContentBody == null)
return "";
return Encoding.UTF8.GetString(ContentBody);
}
set { ContentBody = Encoding.UTF8.GetBytes(value); }
}
}
/// <summary>
/// HTTPリクエスト情報
/// </summary>
public class HTTPRequest
{
public HTTPRequest()
{
Body = null;
}
public byte[] Body { get; set; }
public string String
{
get
{
if (Body == null)
return "";
return Uri.UnescapeDataString(Encoding.UTF8.GetString(Body));
}
set
{
Body = Encoding.UTF8.GetBytes(Uri.EscapeDataString(value));
}
}
}
}
public delegate void HTTProxyRequestProcessingCallbackHandler(SessionInfo info);
/// <summary>
/// セッションが終了した際に呼ばれる
/// </summary>
public event HTTProxyRequestProcessingCallbackHandler AfterSessionCompleted;
/// <summary>
/// プロキシがリクエストを受信し、サーバへ中継する前に呼ばれる
/// </summary>
public event HTTProxyRequestProcessingCallbackHandler BeforeRequest;
public class RequestFailedContext
{
/// <summary>
/// リクエストURI
/// </summary>
public Uri Uri { get; private set; }
/// <summary>
/// エラーメッセージ
/// </summary>
public string Message { get; private set; }
/// <summary>
/// リクエストを再試行する場合はtrue
/// </summary>
public bool Retry = false;
public RequestFailedContext(string uri,string message)
{
Uri = new Uri(uri);
Message = message;
}
}
public delegate void HTTProxyRequestFailedCallbackHandler(RequestFailedContext ctx);
public event HTTProxyRequestFailedCallbackHandler RequestFailed;
private IWebProxy _proxy = new WebProxy();
/// <summary>
/// サーバへリクエストを中継する際に使うプロキシ。nullでプロキシなし
/// </summary>
public IWebProxy UpstreamProxy
{
get
{
return _proxy;
}
set
{
if (value == null)
_proxy = new WebProxy();
else
_proxy = value;
}
}
/*
* http://d.hatena.ne.jp/wwwcfe/20081228/1230470881
* HttpListenerを使った簡単プロクシ
*
*/
/*
* Windows7等ではnetshを使ってprefixを設定しないといけない
* http://ivis-mynikki.blogspot.jp/2011/02/nethttp.html
* http://www.moonmile.net/blog/archives/6406
* http://stackoverflow.com/questions/4019466/httplistener-access-denied
*
* #netsh http add urlacl url=http://127.0.0.1:8881/ user=everyone
*/
private HttpListener _listener = null;
/// <summary>
/// リッスンするプレフィクス
/// </summary>
public string Prefix { get; private set; }
/// <summary>
/// プロキシをスタートする
/// </summary>
/// <param name="port">listenするポート</param>
/// <returns>失敗すると例外を投げます</returns>
public void Start(int port)
{
if (!HttpListener.IsSupported)
{
DebugOut("Not supported platform");
throw new PlatformNotSupportedException("HttpListener未対応プラットフォームです");
}
if (_listener != null)
{
DebugOut("Already started.");
throw new InvalidOperationException("既に有効なHttpListenerが起動しています");
}
Prefix = string.Format("http://127.0.0.1:{0}/", port);
_listener = new HttpListener();
try
{
_listener.Prefixes.Add(Prefix);
DebugOut("Add HttpListener prefix:{0}", Prefix);
_listener.Start();
}
catch(HttpListenerException ex)
{
DebugOut("Exception at starting HttpListener\n{0}", ex.ToString());
_listener = null;
throw;
}
//リクエストの到着を待つ
_listener.BeginGetContext(OnHTTPRequest, _listener);
}
/// <summary>
/// 停止する
/// </summary>
/// <returns></returns>
public bool Stop()
{
if (_listener == null)
return false;
Dispose();
return true;
}
/// <summary>
/// オブジェクトを破棄
/// </summary>
public void Dispose()
{
if (_listener != null)
{
if(_listener.IsListening)
_listener.Stop();
_listener.Close();
_listener = null;
}
}
/// <summary>
/// リクエストが到着するたびに別スレッドで呼ばれる
/// </summary>
/// <param name="ar">非同期情報</param>
private void OnHTTPRequest(IAsyncResult ar)
{
HttpListener listener = ar.AsyncState as HttpListener;
if(listener == null)
{
DebugOut("invalid state(IAsyncResult.AsyncState is not HttpListener)");
return;
}
if (!listener.IsListening)
{
DebugOut("not listening.");
return;
}
//次のリクエストに備える
try
{
listener.BeginGetContext(OnHTTPRequest, listener);
}
catch (ObjectDisposedException ex)
{
//コネクションが閉じた
DebugOut("ObjectDisposedException\n{0}", ex.ToString());
return;
}
catch (HttpListenerException ex)
{
//コネクションが閉じた
DebugOut("HttpListenerException\n{0}", ex.ToString());
return;
}
HttpListenerContext ctx = null;
try
{
//リクエストを処理
ctx = _listener.EndGetContext(ar);
if (ctx != null)
{
HandleHTTPRequest(ctx);
ctx.Response.Close();
}
}
catch (Exception ex)
{
DebugOut("Exception {0}\n{1}",ctx != null ? ctx.Request.RawUrl : "[ctx=null]", ex.ToString());
if (ctx != null)
ctx.Response.Abort();
}
}
/// <summary>
/// HTTPリクエストを処理する
/// </summary>
/// <param name="ctx"></param>
private void HandleHTTPRequest(HttpListenerContext ctx)
{
HttpListenerRequest req = ctx.Request;
HttpListenerResponse res = ctx.Response;
var info = new SessionInfo();
info.Uri = new Uri(req.RawUrl);
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
string exMsg = "";
// ボディあったら読んでおく
if (req.HasEntityBody)
{
using (var ms = new MemoryStream())
{
req.InputStream.CopyTo(ms);
info.Request.Body = ms.ToArray();
}
}
// リトライ用のエンドポイント
beginRequest:
webRequest = CreateHttpWebRequest(req);
if (info.Request.Body != null)
{
var reqStream = webRequest.GetRequestStream();
reqStream.Write(info.Request.Body, 0, info.Request.Body.Length);
reqStream.Close();
}
if (BeforeRequest != null)
BeforeRequest(info);
//サーバへのリクエストを投げずクライアントへレスポンスを返すよう設定された。
if (info.BypassServerRequest)
{
DebugOut("marked as bypassing server request");
SendResponse(ctx.Response, info.Response);
// AfterSessionCompleted は呼ばない
return;
}
// レスポンス取得
try
{
webResponse = webRequest.GetResponse() as HttpWebResponse;
}
catch (WebException e)
{
webResponse = e.Response as HttpWebResponse;
DebugOut("WebException {0} / GetResponse \n{1}",req.RawUrl, e.ToString());
exMsg = e.Message;
}
// 失敗したら502を返す
if (webResponse == null)
{
if(RequestFailed != null)
{
var eventCtx = new RequestFailedContext(req.RawUrl, exMsg);
RequestFailed(eventCtx);
if(eventCtx.Retry)
{
goto beginRequest;
}
}
SendResponse(ctx.Response, new SessionInfo.HTTPResponse()
{
StatusCode = 502,
ContentString = string.Format("Cannot connect {0} \n {1}",req.RawUrl,exMsg),
});
return;
}
SetHttpListenerResponse(ctx, webResponse);
var resStream = webResponse.GetResponseStream();
if (AfterSessionCompleted != null && !info.Ignore)
{
using (MemoryStream ms = new MemoryStream())
{
try
{
resStream.CopyTo(ms);
ms.Seek(0, SeekOrigin.Begin);
ms.CopyTo(res.OutputStream);
info.Response.UpdateResponse(res);
info.Response.ContentBody = ms.ToArray();
}
catch
{
DebugOut("Exception Tee: {0}", req.RawUrl);
throw;
}
}
AfterSessionCompleted(info);
}
else
{
try
{
resStream.CopyTo(res.OutputStream);
}
catch
{
DebugOut("Exception Relay: " + req.RawUrl);
throw;
}
}
resStream.Close();
webResponse.Close();
}
/// <summary>
/// HttpListenerRequestからWebRequestを生成
/// </summary>
/// <param name="req"></param>
/// <returns></returns>
private HttpWebRequest CreateHttpWebRequest(HttpListenerRequest req)
{
HttpWebRequest webRequest = WebRequest.Create(req.RawUrl) as HttpWebRequest;
//リクエスト情報を設定
webRequest.Method = req.HttpMethod;
webRequest.ProtocolVersion = req.ProtocolVersion;
webRequest.Proxy = _proxy;
// webRequest.Timeout = 5000;
webRequest.ContentType = req.ContentType;
webRequest.UserAgent = req.UserAgent;
if (req.UrlReferrer != null)
webRequest.Referer = req.UrlReferrer.OriginalString;
if(req.ContentLength64 > 0)
webRequest.ContentLength = req.ContentLength64;
//リダイレクトをブラウザで処理させる
webRequest.AllowAutoRedirect = false;
//gzip/deflateをプロキシ内で伸張する
webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
// HttpWebRequest の制限がきついのでヘッダごとに対応
foreach(var name in req.Headers.AllKeys)
{
string value = req.Headers[name];
switch (name.ToLower())
{
case "content-length":
case "content-type":
case "referer":
case "user-agent":
case "host":
//すでに設定済み
break;
case "accept":
webRequest.Accept = value;
break;
case "connection":
case "proxy-connection":
webRequest.KeepAlive = req.KeepAlive; // TODO: keepalive の取得はここで行う。
break;
case "if-modified-since":
webRequest.IfModifiedSince = DateTime.Parse(value);
break;
default:
try
{
//DebugOut(" ReqHeader[{0}]:[{1}]", name, value);
webRequest.Headers.Add(name, value);
}
catch(Exception ex)
{
DebugOut("Exception [{0}] [{1}]->[{2}]\n{3}",
req.RawUrl,name,value,ex.ToString());
}
break;
}
}
return webRequest;
}
/// <summary>
/// WebResponseをHttpListenerResponseへ設定する
/// </summary>
/// <param name="ctx"></param>
/// <param name="webResponse"></param>
void SetHttpListenerResponse(HttpListenerContext ctx, HttpWebResponse webResponse)
{
HttpListenerResponse res = ctx.Response;
res.ProtocolVersion = webResponse.ProtocolVersion;
res.StatusCode = (int)webResponse.StatusCode;
res.StatusDescription = webResponse.StatusDescription;
if (webResponse.ContentLength > 0)
res.ContentLength64 = webResponse.ContentLength;
if (webResponse.ContentType != null && webResponse.ContentType != "")
res.ContentType = webResponse.ContentType;
foreach (var name in webResponse.Headers.AllKeys)
{
string value = webResponse.Headers[name];
switch (name.ToLower())
{
case "content-length":
case "content-type":
case "keep-alive":
break;
case "transfer-encoding":
res.SendChunked = value.ToLower().IndexOf("chunked") >= 0 ? true : false;
break;
case "location":
res.RedirectLocation = value;
break;
case "connection":
if (value.ToLower() == "keep-alive")
res.KeepAlive = true;
break;
default:
try
{
//DebugOut(" ResHeader[{0}]:{1}", name, value);
res.Headers.Add(name, value);
}
catch (Exception ex)
{
DebugOut("Exception[{0}] [{1}]->[{2}]\n{3}",
ctx.Request.RawUrl, name, value, ex.ToString());
}
break;
}
}
}
/// <summary>
/// HttpListenerResponeへレスポンスを書き出す
/// </summary>
/// <param name="ctx"></param>
/// <param name="response"></param>
private void SendResponse(HttpListenerResponse ctx, SessionInfo.HTTPResponse response)
{
ctx.StatusCode = response.StatusCode;
ctx.StatusDescription = response.StatusDescription;
ctx.ProtocolVersion = response.ProtocolVersion;
ctx.ContentType = response.ContentType;
ctx.KeepAlive = false;
if (response.ContentBody != null)
{
ctx.ContentLength64 = response.ContentBody.Length;
ctx.OutputStream.Write(response.ContentBody, 0, response.ContentBody.Length);
ctx.OutputStream.Close();
}
else
{
ctx.ContentLength64 = 0;
}
DebugOut("SendResponse");
}
/// <summary>
/// デバッグログ出力関数
/// </summary>
/// <param name="format">フォーマット</param>
/// <param name="args">引数</param>
[System.Diagnostics.Conditional("DEBUG")]
private static void DebugOut(string format, params object[] args)
{
var st = new System.Diagnostics.StackTrace(false);
string name = st.GetFrame(1).GetMethod().Name;
System.Diagnostics.Debug.WriteLine(string.Format("HTTProxy::{0} {1}", name, string.Format(format, args)));
}
}
}