2017-12-07 17 views
1

私は最近、Titanium-Web-Proxyを使用してリバースプロキシを作成しました。Titanium-Web-Proxyを使用してリバースプロキシを作成する方法は?

私がテストした

+---------+ Request +---------------+     +------------+ 
|   +-------------> |    |  Request  |   | 
|   |    | Reverse Proxy +---------------> | Web Server | 
|   |    |    |     |   | 
| Browser |    | 127.0.0.1  |     | 127.0.0.1 | 
|   |    |    | Response  |   | 
|   | Response | 80   | <---------------+ 2366  | 
|   | <-------------+    |     |   | 
+---------+    +---------------+     +------------+ 

をブラウザにIP 127.0.0.1、ポート80を介してリバースプロキシにアクセスし、リバースプロキシは、IP 127.0.0.1でIISサーバーにブラウザの要求を転送し、ポート2366.

それは、リバースプロキシが期待通りに機能せず、ブラウザがHTTP 400エラーを返しました。

Bad Request - Invalid Hostname 

HTTP Error 400. The request hostname is invalid. 

Iはlocalhost:2366として127.0.0.1:2366を書き換えると再試験試み、エラーが同じでした。

後でmodifiedUrihttp://example.comに変更しようとしましたが、404 Not Foundエラーが発生しました。

var modifiedUri = new UriBuilder("http://example.com/"); 

これは私のコードですが、私の貧弱なコードレベルは気にしないでください。

public class Startup 
{ 
    private readonly ProxyServer proxyServer; 

    private readonly IDictionary<Guid, HeaderCollection> requestHeaderHistory = new ConcurrentDictionary<Guid, HeaderCollection>(); 

    private readonly IDictionary<Guid, HeaderCollection> responseHeaderHistory = new ConcurrentDictionary<Guid, HeaderCollection>(); 

    public Startup() 
    { 
     proxyServer = new ProxyServer() 
     { 
      TrustRootCertificate = true, 
      ForwardToUpstreamGateway = true, 
     }; 
    } 

    public void Start() 
    { 
     proxyServer.BeforeRequest += OnRequest; 
     proxyServer.BeforeResponse += OnResponse; 

     var transparentProxyEndPoint = new TransparentProxyEndPoint(IPAddress.Loopback, 80, false) 
     { 

     }; 

     proxyServer.AddEndPoint(transparentProxyEndPoint); 
     proxyServer.Start(); 
    } 

    public void Stop() 
    { 
     proxyServer.BeforeRequest -= OnRequest; 
     proxyServer.BeforeResponse -= OnResponse; 
     proxyServer.Stop(); 
    } 

    public async Task OnRequest(object sender, SessionEventArgs e) 
    { 
     var requestUri = e.WebSession.Request.RequestUri; 
     var modifiedUri = new UriBuilder("http", "127.0.0.1", 2366, requestUri.AbsolutePath); 

     e.WebSession.Request.RequestUri = modifiedUri.Uri; 

     requestHeaderHistory[e.Id] = e.WebSession.Request.RequestHeaders; 

     if (e.WebSession.Request.HasBody) 
     { 
      var bodyBytes = await e.GetRequestBody(); 
      await e.SetRequestBody(bodyBytes); 

      string bodyString = await e.GetRequestBodyAsString(); 
      await e.SetRequestBodyString(bodyString); 
     } 
    } 

    public async Task OnResponse(object sender, SessionEventArgs e) 
    { 
     responseHeaderHistory[e.Id] = e.WebSession.Response.ResponseHeaders; 

     if (e.WebSession.Request.Method == "GET" || e.WebSession.Request.Method == "POST") 
     { 
      if (e.WebSession.Response.ResponseStatusCode == (int)HttpStatusCode.OK) 
      { 
       if (e.WebSession.Response.ContentType != null && e.WebSession.Response.ContentType.Trim().ToLower().Contains("text/html")) 
       { 
        var bodyBytes = await e.GetResponseBody(); 
        await e.SetResponseBody(bodyBytes); 

        string body = await e.GetResponseBodyAsString(); 
        await e.SetResponseBodyString(body); 
       } 
      } 
     } 
    } 
} 

答えて

1

解決済み!

リクエストホスト名を書き換える必要があります。

チタン - ウェブプロキシの問題#344を参照してください。

関連する問題