C# 如何使用Tianium Web proxy编写反向代理?

C# 如何使用Tianium Web proxy编写反向代理?,c#,.net,proxy,titanium-web-proxy,C#,.net,Proxy,Titanium Web Proxy,我最近使用编写了一个反向代理 浏览器通过IP 127.0.0.1端口80访问反向代理,反向代理将浏览器的请求转发到IP 127.0.0.1端口2366的IIS服务器 +---------+ Request +---------------+ +------------+ | +-------------> | | Request | | | |

我最近使用编写了一个反向代理

浏览器通过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       |
|         | <-------------+               |                 |            |
+---------+               +---------------+                 +------------+
我试着将
127.0.0.1:2366
改写为
localhost:2366
并重新测试,错误是一样的

后来我尝试将
modifiedUri
更改为,但出现404未找到错误

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);
                }
            }
        }
    }
}
公共类启动
{
专用只读代理服务器代理服务器;
私有只读IDictionary requestHeaderHistory=新的ConcurrentDictionary();
私有只读IDictionary responseHeaderHistory=new ConcurrentDictionary();
公共启动()
{
proxyServer=新的proxyServer()
{
TrustRootCertificate=true,
ForwardToUpstreamGateway=true,
};
}
公开作废开始()
{
proxyServer.BeforeRequest+=OnRequest;
proxyServer.BeforeResponse+=OnResponse;
var transparentproxy端点=新的transparentproxy端点(IPAddress.Loopback,80,false)
{
};
proxyServer.AddEndPoint(transparentProxyEndPoint);
proxyServer.Start();
}
公共停车场()
{
proxyServer.BeforeRequest-=OnRequest;
proxyServer.BeforeResponse-=OnResponse;
proxyServer.Stop();
}
公共异步任务OnRequest(对象发送方,SessionEventArgs e)
{
var requestUri=e.WebSession.Request.requestUri;
var modifiedUri=newUriBuilder(“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=wait e.GetRequestBody();
等待e.SetRequestBody(bodyBytes);
string-bodyString=Wait e.GetRequestBodyAsString();
等待e.SetRequestBodyString(bodyString);
}
}
公共异步任务OnResponse(对象发送方,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)
{
如果(e.WebSession.Response.ContentType!=null&&e.WebSession.Response.ContentType.Trim().ToLower().Contains(“text/html”))
{
var bodyBytes=wait e.GetResponseBody();
等待e.SetResponseBody(bodyBytes);
字符串体=等待e.GetResponseBodyAsString();
等待e.setresponsebroing(body);
}
}
}
}
}
已解决

我需要重写请求主机名


请参阅Tianium Web代理问题。

对于其他使用此功能的用户,请小心阅读OnRequest中的请求正文和OnResponse中的响应正文。Titanium将自行发送数据,没有理由获取和设置,并且在某些情况下会导致问题,例如100个继续请求。
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);
                }
            }
        }
    }
}