如何使用Titanium-Web-Proxy编写反向代理?

时间:2017-12-07 05:09:41

标签: c# .net proxy titanium-web-proxy

我最近使用Titanium-Web-Proxy编写了反向代理。

浏览器通过IP 127.0.0.1,端口80访问反向代理,反向代理将浏览器的请求转发到IP服务器127.0.0.1,端口2366的IIS服务器。

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

当我测试它时,反向代理没有按预期工作,浏览器返回HTTP 400错误。

Bad Request - Invalid Hostname

HTTP Error 400. The request hostname is invalid.

我尝试将127.0.0.1:2366重写为localhost:2366并重新测试,错误是相同的。

稍后我尝试将modifiedUri更改为http://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 个答案:

答案 0 :(得分:1)

解决!

我需要重写请求主机名。

请参阅Titanium-Web-Proxy问题#344