通过Azure Functions(c#)通过代理连接到REST API

问题描述 投票:1回答:1

我创建了一个Azure Function(C#,ASP.net core 3.1)(A)以通过Squid代理服务器访问API(Azure Function(B))以将源端口更改为Function(B)。信息如下所示。)

我可以通过代理访问Azure Function(B),但源端口不会更改。

当我两次访问Function(B)时,鱿鱼代理服务器仅记录了第一次访问。但是Function(B)记录了它每次都是通过代理访问的。(记录的代理ip地址。)

[请告诉我,函数(A)与代理之间的连接会发生什么情况,以及如何更改Function(A)或squid配置。

┌────────────────────┐
│ Azure Function (A) │
└────────────────────┘
         ↓
         ↓
 ┌──────────────┐
 │ Proxy Server │
 └──────────────┘
         ↓
         ↓
┌────────────────────┐
│ Azure Function (B) │
└────────────────────┘

功能(A)的代码在这里。

// startup.cs
// Setting DI and using Proxy
[...]
public override void Configure(IFunctionsHostBuilder builder)
{
    [...]
    builder.Services.AddHttpClient<IApiCallLogic, ApiCallLogic>(c => { })
                    .ConfigurePrimaryHttpMessageHandler(() =>
                    {
                        var handler = new HttpClientHandler { UseProxy = true };
                        handler.Proxy = new WebProxy($"http://xxx.xxx.xxx.xxx:xxx"); // proxy's IP & port
                        return handler;
                    });
}
// call REST API Logic with DI
class ApiCallLogic : IApiCallLogic
{
    private HttpClient httpClient;

    public ApiCallLogic(HttpClient httpClient)
    {
        this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
    }

    public async Task<HttpResponseMessage> CallApi(string url, ILogger log)
    {
        var reqMessage = new HttpRequestMessage(HttpMethod.Get, url);
        var response = await httpClient.SendAsync(reqMessage);
        return response;
    }
}

而squid.conf就是这样。

# allow Function IP
acl function_net src xxx.xxx.xxx.xxx
http_access allow function_net

# deny others
http_access deny all

# no cache
cache deny all

# set port
http_port xxxx

forwarded_for off

# persistent connection setting
server_persistent_connections off
client_persistent_connections on

  • asp.net核心3.1.2
  • 乌贼v3.5.27
  • 鱿鱼服务器操作系统:ubuntu 18.04
    • squid服务器是Azure VM
c# azure asp.net-core proxy squid
1个回答
0
投票

我已经自己解决了这个问题。当我访问外部API而不是Function(B)时,不同请求上的源端口不同。我认为源端口相同的原因是Azure函数返回值keep-alive的连接头。

© www.soinside.com 2019 - 2024. All rights reserved.